Module: Formotion::Formable::ClassMethods

Defined in:
lib/formotion/model/formable.rb

Constant Summary collapse

INHERITABLE_ATTRIBUTES =
[:form_properties, :form_title].each do |prop|
  attr_accessor prop
end

Instance Method Summary collapse

Instance Method Details

#form_propertiesObject



21
22
23
24
# File 'lib/formotion/model/formable.rb', line 21

def form_properties
  @form_properties ||= self.superclass.form_properties if is_kvo_subclass?
  @form_properties ||= []
end

#form_property(property, row_type, options = {}) ⇒ Object

Relates a property to a RowType. EX form_property :my_title => :string form_property :my_date => :date, :transform => lambda { |value| some_function(date) }

Parameters:

  • property

    is the name of the attribute to KVO

  • row_type

    is the Formotion::RowType to use for that attribute

  • options (defaults to: {})

    are the extra options for this model. Keys can include any usual Formotion::Row keys to override, plus :transform, which is a single-argument lambda for transforming the row’s string value before it’s synced to your model.



36
37
38
# File 'lib/formotion/model/formable.rb', line 36

def form_property(property, row_type, options = {})
  self.form_properties << { property: property, row_type: row_type}.merge(options)
end

#form_title(title = -1)) ⇒ Object

Sets the top bar title for this model EX form_title “Some Settings”



43
44
45
46
47
# File 'lib/formotion/model/formable.rb', line 43

def form_title(title = -1)
  @form_title ||= self.superclass.form_title if is_kvo_subclass?
  @form_title = title if title != -1
  @form_title
end

#inherited(subclass) ⇒ Object

Does NOT get called when KVO occurs. (KVO uses isa swizzling and not proper subclassing)



14
15
16
17
18
19
# File 'lib/formotion/model/formable.rb', line 14

def inherited(subclass)
  INHERITABLE_ATTRIBUTES.each do |inheritable_attribute|
    instance_var = "@#{inheritable_attribute}"
    subclass.instance_variable_set(instance_var, instance_variable_get(instance_var))
  end
end