Class: Locomotive::ContentEntryPresenter

Inherits:
BasePresenter show all
Defined in:
app/presenters/locomotive/content_entry_presenter.rb

Instance Attribute Summary collapse

Attributes inherited from BasePresenter

#ability, #depth

Instance Method Summary collapse

Methods inherited from BasePresenter

#ability?, #after_initialize, getters_to_hash, #id, setters_to_hash, #site

Methods included from Presentable

#after_initialize, #attributes=, #getters, #initialize, #property_options, #setters

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object (protected)

Delegate custom field setters to source



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'app/presenters/locomotive/content_entry_presenter.rb', line 187

def method_missing(meth, *args, &block)
  # If the setter is missing but it's in the available_custom_field_names, delegate it to the source
  field_name = get_custom_field_name_for_method(meth)
  if self.available_custom_field_names.include?(field_name)
    # If it's a "many" or "belong_to" field, pass in the right objects
    if is_many_custom_field?(field_name)
      new_args = args.collect { |list| get_many_field_objects(field_name, list) }

      # Get the objects which we need to save
      self.objects_to_save ||= []
      self.objects_to_save += new_args.flatten

      # If it's a has_many field, set the positions
      if is_has_many_custom_field?(field_name)
        self.set_positions_for_has_many(new_args.flatten, field_name)
      end

      self.source.send(meth, *new_args, &block)
    elsif is_belongs_to_custom_field?(field_name)
      new_args = args.collect { |slug| get_belongs_field_object(meth, slug) }
      self.source.send(meth, *new_args, &block)
    else
      self.source.send(meth, *args, &block)
    end
  else
    super
  end
end

Instance Attribute Details

#objects_to_saveObject

Returns the value of attribute objects_to_save.



10
11
12
# File 'app/presenters/locomotive/content_entry_presenter.rb', line 10

def objects_to_save
  @objects_to_save
end

Instance Method Details

#additional_custom_fields_methodsObject



31
32
33
34
35
36
37
38
39
# File 'app/presenters/locomotive/content_entry_presenter.rb', line 31

def additional_custom_fields_methods
  [].tap do |methods|
    self.source.custom_fields_recipe['rules'].each do |rule|
      if rule['type'] == 'belongs_to'
        methods << "position_in_#{rule['name'].underscore}"
      end
    end
  end
end

#as_json(methods = nil) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'app/presenters/locomotive/content_entry_presenter.rb', line 69

def as_json(methods = nil)
  methods ||= self.included_methods
  {}.tap do |hash|
    methods.each do |meth|
      hash[meth]= (if self.source.custom_fields_methods.include?(meth.to_s) \
                   || self.additional_custom_fields_methods.include?(meth.to_s)
        if self.source.is_a_custom_field_many_relationship?(meth.to_s)
          # go deeper
          self.source.send(meth).ordered.map { |entry| entry.to_presenter(:depth => self.depth + 1) }
        else
          self.source.send(meth) rescue nil
        end
      else
        self.send(meth.to_sym) rescue nil
      end)
    end
  end
end

#content_type_slugObject



55
56
57
# File 'app/presenters/locomotive/content_entry_presenter.rb', line 55

def content_type_slug
  self.source.content_type.slug
end

#errorsObject



51
52
53
# File 'app/presenters/locomotive/content_entry_presenter.rb', line 51

def errors
  self.source.errors.to_hash.stringify_keys
end

#filtered_custom_fields_methodsObject



20
21
22
23
24
25
26
27
28
29
# File 'app/presenters/locomotive/content_entry_presenter.rb', line 20

def filtered_custom_fields_methods
  self.source.custom_fields_methods do |rule|
    if self.source.is_a_custom_field_many_relationship?(rule['name'])
      # avoid circular dependencies, it should accept only one deep level
      self.depth == 0
    else
      true
    end
  end
end

#formatted_created_at=(created_at) ⇒ Object



41
42
43
44
# File 'app/presenters/locomotive/content_entry_presenter.rb', line 41

def formatted_created_at=(created_at)
  self.source.timeless
  self.source.created_at = formatted_time(created_at)
end

#formatted_updated_at=(updated_at) ⇒ Object



46
47
48
49
# File 'app/presenters/locomotive/content_entry_presenter.rb', line 46

def formatted_updated_at=(updated_at)
  self.source.timeless
  self.source.updated_at = formatted_time(updated_at)
end

#included_methodsObject



59
60
61
62
63
# File 'app/presenters/locomotive/content_entry_presenter.rb', line 59

def included_methods
  default_list = %w(_label _slug _position seo_title meta_keywords meta_description content_type_slug select_custom_fields file_custom_fields has_many_custom_fields many_to_many_custom_fields translated_in safe_attributes)
  default_list << 'errors' if !!self.options[:include_errors]
  super + self.filtered_custom_fields_methods + self.additional_custom_fields_methods + default_list
end

#included_settersObject



65
66
67
# File 'app/presenters/locomotive/content_entry_presenter.rb', line 65

def included_setters
  super + %w(_slug _position seo_title meta_keywords meta_description formatted_created_at formatted_updated_at) + self.available_custom_field_names
end

#safe_attributesObject

Lists of all the attributes editable thru the html form for instance



16
17
18
# File 'app/presenters/locomotive/content_entry_presenter.rb', line 16

def safe_attributes
  self.source.custom_fields_safe_attributes + %w(_slug seo_title meta_keywords meta_description _destroy)
end