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

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

Constant Summary collapse

NON_CUSTOM_FIELD_ATTRIBUTES =
%w{_auth_reset_token _auth_reset_sent_at}

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.


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

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


41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/api/locomotive/api/forms/content_entry_form.rb', line 41

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[getter_name(name).to_sym] = args.first
    end
  elsif NON_CUSTOM_FIELD_ATTRIBUTES.include?(getter_name(name))
    dynamic_attributes[getter_name(name).to_sym] = args.first
  else
    super
  end
end

Instance Attribute Details

#content_typeObject

Returns the value of attribute content_type


9
10
11
# File 'app/api/locomotive/api/forms/content_entry_form.rb', line 9

def content_type
  @content_type
end

#dynamic_attributesObject

Returns the value of attribute dynamic_attributes


9
10
11
# File 'app/api/locomotive/api/forms/content_entry_form.rb', line 9

def dynamic_attributes
  @dynamic_attributes
end

Instance Method Details

#respond_to?(name, include_all = false) ⇒ Boolean

Returns:


55
56
57
# File 'app/api/locomotive/api/forms/content_entry_form.rb', line 55

def respond_to?(name, include_all = false)
  find_field(name) || NON_CUSTOM_FIELD_ATTRIBUTES.include?(getter_name(name)) || super
end

#serializable_hashObject


59
60
61
# File 'app/api/locomotive/api/forms/content_entry_form.rb', line 59

def serializable_hash
  super.merge(self.dynamic_attributes)
end

#set_belongs_to(field, value) ⇒ Object


25
26
27
# File 'app/api/locomotive/api/forms/content_entry_form.rb', line 25

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


19
20
21
# File 'app/api/locomotive/api/forms/content_entry_form.rb', line 19

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

#set_many_to_many(field, value) ⇒ Object


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

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