Class: Glass::TimelineItem

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/glass/timeline_item.rb

Defined Under Namespace

Classes: GoogleAccountNotSpecifiedError, MenuItemHandlerIsNotDefinedError, TimelineInsertionError, UnserializedTemplateError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#clientObject



43
44
45
46
# File 'lib/glass/timeline_item.rb', line 43

def client
  raise UnserializedTemplateError unless @client
  @client
end

#mirror_contentObject

a couple custom attr_readers which raise a helpful error message if the value is nil; i.e. error flow handling.



39
40
41
42
# File 'lib/glass/timeline_item.rb', line 39

def mirror_content
  raise UnserializedTemplateError unless @mirror_content
  @mirror_content
end

#template_nameObject

this is not intended to be a part of the public api



264
265
266
# File 'lib/glass/timeline_item.rb', line 264

def template_name
  @template_name = self.class.default_template
end

#template_typeObject

Returns the value of attribute template_type.



28
29
30
# File 'lib/glass/timeline_item.rb', line 28

def template_type
  @template_type
end

#to_jsonObject

Returns the value of attribute to_json.



28
29
30
# File 'lib/glass/timeline_item.rb', line 28

def to_json
  @to_json
end

Class Method Details

.defaults_template(opts = {}) ⇒ Object

end



59
60
61
62
# 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

.defaults_template_with(name_of_template) ⇒ Object

end



75
76
77
78
79
80
81
# 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

.defines_callback_methods(action, opts) ⇒ Object

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.



139
140
141
142
143
144
145
146
147
# 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

.has_menu_item(action_sym, opts = {}) ⇒ Object

def custom_handler_methodname

      # this gets executed when this
      # action occurs.
    end
end


119
120
121
122
123
124
125
126
127
128
# File 'lib/glass/timeline_item.rb', line 119

def self.has_menu_item(action_sym, opts={})
  self.actions ||= []
  self.menu_items ||= []
  unless self.actions.include?(action_sym)
    self.actions += [action_sym]
    defines_callback_methods(action_sym, opts)
    menu_item = ::Glass::MenuItem.create(action_sym, opts)
    self.menu_items += [menu_item]
  end
end

.manages_templates(opts = {}) ⇒ Object

end



95
96
97
# File 'lib/glass/timeline_item.rb', line 95

def self.manages_templates(opts={})
  self.template_manager = opts[:with] if opts[:with]
end

convert the menu items into hash form not a part of the public api.



153
154
155
# File 'lib/glass/timeline_item.rb', line 153

def self.menu_items_hash
  {menuItems: self.menu_items.map(&:serialize) }
end

Instance Method Details

#actionsObject

i’d use cattr_accessor, but unfortunately ruby has some very crappy class variables, which are nonsensically over-written across sub-classes. so use the rails class_attribute helpers instead.



24
# File 'lib/glass/timeline_item.rb', line 24

class_attribute :actions

#has_default_template?Boolean

this is not intended to be a part of the public api

Returns:

  • (Boolean)


275
276
277
# File 'lib/glass/timeline_item.rb', line 275

def has_default_template?
  self.class.default_template
end

#insert(opts = {}) ⇒ Object



210
211
212
213
# 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

convert class to instance method. not meant to be a part of the public api.



159
160
161
# File 'lib/glass/timeline_item.rb', line 159

def menu_items_hash
  self.class.menu_items_hash
end

#mirror_deleteObject



238
239
240
241
242
# 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

#mirror_get(opts = {}) ⇒ Object



221
222
223
224
225
# 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

#mirror_insert(opts = {}) ⇒ Object



216
217
218
219
220
# 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

#mirror_patch(opts = {}) ⇒ Object



226
227
228
229
230
231
# 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

#mirror_update(timeline_item, opts = {}) ⇒ Object



232
233
234
235
236
237
# 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

#save_data(result) ⇒ Object



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# 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

#serialize(opts = {}) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
# File 'lib/glass/timeline_item.rb', line 194

def serialize(opts={})
  raise GoogleAccountNotSpecifiedError unless self..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

#setup_template(variables = {}) ⇒ Object

this is not intended to be a part of the public api



269
270
271
272
# 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