class Glass::TimelineItem
Attributes
only a writer for these two because I want to hook an error message to each of these if they haven’t had there values set yet.
only a writer for these two because I want to hook an error message to each of these if they haven’t had there values set yet.
only a writer for these two because I want to hook an error message to each of these if they haven’t had there values set yet.
Public Class Methods
end
# File lib/glass/timeline_item.rb, line 59 def self.defaults_template(opts={}) puts "DEPRECATION WARNING: defaults_template is now deprecated, please use defaults_template_with instead" self.default_template = opts[:with] end
end
# File lib/glass/timeline_item.rb, line 75 def self.defaults_template_with(name_of_template) if name_of_template.is_a? Symbol self.default_template = name_of_template.to_s else raise InvalidArgumentError, 'Template name is not a symbol' end end
this is really just a little meta-programming trick which basically forces a call to the method specified by with parameter in the has_menu_item
method.
it allows you to put the callback logic right there in the model.
# File lib/glass/timeline_item.rb, line 139 def self.defines_callback_methods(action, opts) self.send(:define_method, "handles_#{action.to_s.underscore}") do |json| if self.respond_to?(opts[:handles_with]) self.method(opts[:handles_with]).arity > 0 ? self.send(opts[:handles_with], json) : self.send(opts[:handles_with]) else raise MenuItemHandlerIsNotDefinedError end end end
manages_templates :template_manager_name
this will set your template manager for this class. this will override defaults_template
path if it is defined.
end
# File lib/glass/timeline_item.rb, line 95 def self.manages_templates(opts={}) self.template_manager = opts[:with] if opts[:with] end
Public Instance Methods
# File lib/glass/timeline_item.rb, line 43 def client raise UnserializedTemplateError unless @client @client end
this is not intended to be a part of the public api
# File lib/glass/timeline_item.rb, line 275 def has_default_template? self.class.default_template end
# File lib/glass/timeline_item.rb, line 210 def insert(opts={}) puts "DEPRECATION WARNING: insert is now deprecated, please use mirror_insert instead" mirror_insert(opts) end
a couple custom attr_readers which raise a helpful error message if the value is nil; i.e. error flow handling.
# File lib/glass/timeline_item.rb, line 39 def mirror_content raise UnserializedTemplateError unless @mirror_content @mirror_content end
# File lib/glass/timeline_item.rb, line 238 def mirror_delete self.client = Glass::Client.create(self) client.delete id: self.glass_item_id self.update_attributes(is_deleted: true) end
# File lib/glass/timeline_item.rb, line 221 def mirror_get(opts={}) self.client = Glass::Client.create(self) result = client.get(self.glass_item_id) save_data(result) end
# File lib/glass/timeline_item.rb, line 216 def mirror_insert(opts={}) result = client.insert(opts) raise TimelineInsertionError if result.error? save_data(result) end
# File lib/glass/timeline_item.rb, line 226 def mirror_patch(opts={}) opts.merge! glass_item_id: self.glass_item_id self.client = Glass::Client.create(self) result = client.patch(opts) save_data(result) end
# File lib/glass/timeline_item.rb, line 232 def mirror_update(timeline_item, opts={}) opts.merge! glass_item_id: self.glass_item_id self.client = Glass::Client.create(self) result = client.update(timeline_item, opts) save_data(result) end
# File lib/glass/timeline_item.rb, line 244 def save_data(result) data = result.data result_data_type = :html #default [:html, :text].each do |result_type| result_data_type = result_type if data.send(result_type).present? end self.update_attributes(glass_item_id: data.id, glass_etag: data.etag, glass_self_link: data.self_link, glass_kind: data.kind, glass_created_at: data.created, glass_updated_at: data.updated, glass_content_type: result_data_type, glass_content: data.send(result_data_type)) to_indifferent_json_hash(result) end
# File lib/glass/timeline_item.rb, line 194 def serialize(opts={}) raise GoogleAccountNotSpecifiedError unless self.google_account.present? type = self.template_type || :html json_hash = {} json_hash[type] = self.setup_template(opts.delete(:template_variables).merge({template_name: opts.delete(:template_name) })) json_hash = json_hash.merge(self.menu_items_hash) json_hash.merge(opts) self.to_json = json_hash self.client = Glass::Client.create(self) return self end
this is not intended to be a part of the public api
# File lib/glass/timeline_item.rb, line 269 def setup_template(variables={}) variables[:template_name] ||= self.template_name Glass::Template.new(variables).render_self end
this is not intended to be a part of the public api
# File lib/glass/timeline_item.rb, line 264 def template_name @template_name = self.class.default_template end
Private Instance Methods
# File lib/glass/timeline_item.rb, line 279 def to_indifferent_json_hash(obj) JSON.parse(obj.body).with_indifferent_access end