Class: Locomotive::API::Forms::ContentEntryForm

Inherits:
BaseForm
  • Object
show all
Defined in:
app/api/locomotive/api/forms/content_entry_form.rb

Instance Attribute Summary collapse

Attributes inherited from BaseForm

#_persisted, #_policy

Instance Method Summary collapse

Methods inherited from BaseForm

attributes, attrs, define_attribute, #persisted?, #set_attribute

Constructor Details

#initialize(content_type, attributes) ⇒ ContentEntryForm

Returns a new instance of ContentEntryForm.



11
12
13
14
15
# File 'app/api/locomotive/api/forms/content_entry_form.rb', line 11

def initialize(content_type, attributes)
  self.content_type = content_type
  self.dynamic_attributes = {}
  super(attributes)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'app/api/locomotive/api/forms/content_entry_form.rb', line 39

def method_missing(name, *args, &block)
  if field = find_field(name)
    if respond_to?(:"set_#{field.type}")
      public_send(:"set_#{field.type}", field, args.first)
    else
      dynamic_attributes[field.name.to_sym] = args.first
    end
  else
    super
  end
end

Instance Attribute Details

#content_typeObject

Returns the value of attribute content_type.



7
8
9
# File 'app/api/locomotive/api/forms/content_entry_form.rb', line 7

def content_type
  @content_type
end

#dynamic_attributesObject

Returns the value of attribute dynamic_attributes.



7
8
9
# File 'app/api/locomotive/api/forms/content_entry_form.rb', line 7

def dynamic_attributes
  @dynamic_attributes
end

Instance Method Details

#serializable_hashObject



51
52
53
# File 'app/api/locomotive/api/forms/content_entry_form.rb', line 51

def serializable_hash
  super.merge(self.dynamic_attributes)
end

#set_belongs_to(field, value) ⇒ Object



23
24
25
# File 'app/api/locomotive/api/forms/content_entry_form.rb', line 23

def set_belongs_to(field, value)
  dynamic_attributes[field.name.to_sym] = fetch_entry_id(field.class_name, value)
end

#set_date_time(field, value) ⇒ Object Also known as: set_date



17
18
19
# File 'app/api/locomotive/api/forms/content_entry_form.rb', line 17

def set_date_time(field, value)
  dynamic_attributes[:"formatted_#{field.name}"] = value
end

#set_many_to_many(field, value) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'app/api/locomotive/api/forms/content_entry_form.rb', line 27

def set_many_to_many(field, value)
  if value.blank?
    dynamic_attributes[:"#{field.name.singularize}_ids"] = [] # reset it
  else
    dynamic_attributes[:"#{field.name.singularize}_ids"] = fetch_entry_ids(field.class_name, value).sort do |a, b|
      # keep the original order
      (value.index(a.first.to_s) || value.index(a.last)) <=>
      (value.index(b.first.to_s) || value.index(b.last))
    end.map(&:first)
  end
end