module Formotion::Formable

Public Class Methods

included(base) click to toggle source
# File lib/formotion/model/formable.rb, line 3
def self.included(base)
  base.extend(ClassMethods)
end

Public Instance Methods

on_submit() click to toggle source

what happens when the form is submitted?

# File lib/formotion/model/formable.rb, line 97
def on_submit
  p "need to implement on_submit in your Formable model #{self.class.to_s}"
end
to_form() click to toggle source
# File lib/formotion/model/formable.rb, line 74
def to_form
  form = Formotion::Form.new(to_form_hash)
  form.on_submit do
    self.on_submit
  end

  # Use the :transform lambdas passed in form_property
  form.sections.first.rows.each_with_index { |row, index|
    row.instance_variable_set("@formable_options", self.class.form_properties[index])
    if self.class.form_properties[index][:transform]
      row.class.send(:alias_method, :old_value_setter, :value=)
      row.instance_eval do
        def value=(value)
          old_value_setter(@formable_options[:transform].call(value))
        end
      end
    end
  }

  form
end
to_form_hash() click to toggle source

Creates a Formotion::Form out of the model

# File lib/formotion/model/formable.rb, line 57
def to_form_hash
  rows = self.class.form_properties.collect { |options|
    {
      title: options[:property].capitalize,
      key: options[:property],
      type: options[:row_type],
      value: self.send(options[:property])
    }.merge(options)
  }
  return {
    title: self.class.form_title || self.class.to_s.capitalize,
    sections: [{
        rows: rows
      }]
  }
end
value=(value) click to toggle source
# File lib/formotion/model/formable.rb, line 86
def value=(value)
  old_value_setter(@formable_options[:transform].call(value))
end