Module: MotionModel::Formotion

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

Constant Summary collapse

FORMOTION_MAP =
{
  :string   => :string,
  :date     => :date,
  :time     => :date,
  :int      => :number,
  :integer  => :number,
  :float    => :number,
  :double   => :number,
  :bool     => :check,
  :boolean  => :check,
  :text     => :text
}

Instance Method Summary collapse

Instance Method Details

#combine_options(column, hash) ⇒ Object

nodoc



46
47
48
49
# File 'lib/motion_model/model/formotion.rb', line 46

def combine_options(column, hash) #nodoc
  options = column_named(column).options[:formotion]
  options ? hash.merge(options) : hash
end

#default_hash_for(column, value) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/motion_model/model/formotion.rb', line 26

def default_hash_for(column, value)
  {:key         => column.to_sym,
   :title       => column.to_s.humanize,
   :type        => FORMOTION_MAP[type(column)],
   :placeholder => column.to_s.humanize,
   :value       => value
   }
end

#from_formotion!(data) ⇒ Object

from_formotion takes the information rendered from a Formotion form and stuffs it back into a MotionModel. This data is not saved until you say so, offering you the opportunity to validate your form data.



79
80
81
82
83
84
85
86
# File 'lib/motion_model/model/formotion.rb', line 79

def from_formotion!(data)
  self.returnable_columns.each{|column|
    if data[column] && type(column) == :date || type(column) == :time
      data[column] = Time.at(data[column]) unless data[column].nil?
    end
    value = self.send("#{column}=", data[column])
  }
end

#is_date_time?(column) ⇒ Boolean

Returns:

  • (Boolean)


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

def is_date_time?(column)
 column_type = type(column)
 [:date, :time].include?(column_type)
end

#returnable_columnsObject

nodoc



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

def returnable_columns #nodoc
  columns.select{|column| should_return(column)}
end

#should_return(column) ⇒ Object

nodoc



16
17
18
19
20
# File 'lib/motion_model/model/formotion.rb', line 16

def should_return(column) #nodoc
  skippable = [:id]
  skippable += [:created_at, :updated_at] unless @expose_auto_date_fields
  !skippable.include?(column) && !relation_column?(column)
end

#to_formotion(section_title = nil, expose_auto_date_fields = false) ⇒ Object

to_formotion maps a MotionModel into a hash suitable for creating a Formotion form. By default, the auto date fields, created_at and updated_at are suppressed. If you want these shown in your Formotion form, set expose_auto_date_fields to true

If you want a title for your Formotion form, set the section_title argument to a string that will become that title.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/motion_model/model/formotion.rb', line 58

def to_formotion(section_title = nil, expose_auto_date_fields = false)
  @expose_auto_date_fields = expose_auto_date_fields
  form = {
    sections: [{}]
  }

  section = form[:sections].first
  section[:title] ||= section_title
  section[:rows] = []

  returnable_columns.each do |column|
    value = value_for(column)
    h = default_hash_for(column, value)
    section[:rows].push(combine_options(column, h))
  end
  form
end

#value_for(column) ⇒ Object

nodoc



40
41
42
43
44
# File 'lib/motion_model/model/formotion.rb', line 40

def value_for(column) #nodoc
  value = self.send(column)
  value = value.to_f if value && is_date_time?(column)
  value
end