Module: ActiveForm::Mixins::ContainerMethods

Included in:
Definition, Element::Section
Defined in:
lib/active_form/mixins/container_methods.rb

Defined Under Namespace

Modules: ClassMethods, ElementMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



3
4
5
6
7
8
# File 'lib/active_form/mixins/container_methods.rb', line 3

def self.included(base)
  base.send(:include, ActiveForm::Mixins::LoaderMethods)    
  base.send(:include, Enumerable)
  base.send(:include, ElementMethods)
  base.send(:extend,  ClassMethods) 
end

Instance Method Details

#bound_value?(name) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/active_form/mixins/container_methods.rb', line 27

def bound_value?(name)
  self.element_value.bound_value?(name)
end

#define_localizer(prc = nil, &block) ⇒ Object Also known as: localizer=



95
96
97
# File 'lib/active_form/mixins/container_methods.rb', line 95

def define_localizer(prc = nil, &block)
  @localizer = (block_given? ? block : prc)
end

#descriptionObject



90
91
92
93
# File 'lib/active_form/mixins/container_methods.rb', line 90

def description
  return (localize(nil, 'description') || @description) if localized?
  @description
end

#export_value(values = ActiveForm::Values.new) ⇒ Object Also known as: export_values



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/active_form/mixins/container_methods.rb', line 31

def export_value(values = ActiveForm::Values.new)
  elements.inject(values) do |vals, elem|
    if elem.group.blank?
      vals[elem.name] = elem.export_value
    else
      if elem.element_type == :radio
        vals[elem.group] ||= nil
        vals[elem.group] = elem.export_value unless elem.blank?
      else
        vals[elem.group] ||= []
        vals[elem.group] << elem.export_value unless elem.blank?
      end
    end
    vals
  end
end

#get_bound_value(name) ⇒ Object



19
20
21
# File 'lib/active_form/mixins/container_methods.rb', line 19

def get_bound_value(name)
  self.element_value.bound_value(name) rescue nil
end

#labelObject



80
81
82
83
# File 'lib/active_form/mixins/container_methods.rb', line 80

def label
  return (localize(nil, 'label') || @label) if localized?
  @label
end

#localize(*args) ⇒ Object



100
101
102
# File 'lib/active_form/mixins/container_methods.rb', line 100

def localize(*args)
  @localizer.call(identifier, *args) if localized?  
end

#localized?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/active_form/mixins/container_methods.rb', line 104

def localized?
  (@localizer ||= nil).kind_of?(Proc)
end

#localizerObject



108
109
110
# File 'lib/active_form/mixins/container_methods.rb', line 108

def localizer
  localized? ? @localizer : nil
end

#set_bound_value(name, value) ⇒ Object



23
24
25
# File 'lib/active_form/mixins/container_methods.rb', line 23

def set_bound_value(name, value)
  self.element_value.bound_value(name, value) rescue nil
end

#submitted?Boolean Also known as: sent?

Returns:

  • (Boolean)


14
15
16
# File 'lib/active_form/mixins/container_methods.rb', line 14

def 
  not get_elements_of_type(:submit).all?(&:blank?)
end

#titleObject



85
86
87
88
# File 'lib/active_form/mixins/container_methods.rb', line 85

def title
  return (localize(nil, 'title') || attributes[:title]) if localized?
  attributes[:title]
end

#update_from_params(params, force = false) ⇒ Object Also known as: update_values_from_params, params=



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/active_form/mixins/container_methods.rb', line 65

def update_from_params(params, force = false)
  if params.respond_to?(:key?) && !params.empty?
    value = ActiveForm::Values.new(params) rescue default_value
    value = (value[self.name] || value[self.name.to_s] || default_value) unless self.contained?
    elements.each do |e|
      e.update_from_params(value[e.name] || e.default_value) if force || value.key?(e.name) || e.respond_to?(:checked?)
    end   
  else
    self.element_value = default_value
  end
  self.element_value   
end

#update_value(value, force = false) ⇒ Object Also known as: update_values



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/active_form/mixins/container_methods.rb', line 49

def update_value(value, force = false)   
  if self.contained?
    self.element_value = value
  else    
    if value.respond_to?(:key?) && !value.empty?
      elements.each do |e|
        e.update_value(value[e.name] || e.default_value) if force || value.key?(e.name) || e.respond_to?(:checked?)
      end
    else
      self.element_value = default_value
    end   
    self.element_value
  end
end

#validated?Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/active_form/mixins/container_methods.rb', line 10

def validated?
   && validate
end