Module: Forma::Utils

Included in:
Field, Form
Defined in:
lib/forma/utils.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extract_value(val, name) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/forma/utils.rb', line 11

def extract_value(val, name)
  def simple_value(model, name)
    if model.respond_to?(name); model.send(name)
    elsif model.respond_to?('[]'); model[name] || model[name.to_sym]
    end
  end
  name.to_s.split('.').each { |n| val = simple_value(val, n) if val }
  val
end

.singular_name(model) ⇒ Object



4
5
6
7
8
9
# File 'lib/forma/utils.rb', line 4

def singular_name(model)
  if model.respond_to?(:model_name); model.model_name.singular_route_key # Mongoid
  elsif model.class.respond_to?(:table_name); model.class.table_name.singularize # ActiveModel
  else; '' # Others
  end
end

Instance Method Details

#simple_value(model, name) ⇒ Object



12
13
14
15
16
# File 'lib/forma/utils.rb', line 12

def simple_value(model, name)
  if model.respond_to?(name); model.send(name)
  elsif model.respond_to?('[]'); model[name] || model[name.to_sym]
  end
end