Module: ActiveForm::Mixins::ElementMethods

Included in:
Definition, Element::Base, Element::Builder, Element::Section
Defined in:
lib/active_form/mixins/element_methods.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



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

def self.included(base)
  base.send(:include, ActiveForm::Mixins::AttributeMethods)  
  base.send(:extend, ActiveForm::Mixins::ElementMethods::ClassMethods)  
  base.send(:include, ActiveForm::Mixins::Casting)  
  base.send(:attr_reader, :container)
  base.send(:attr_accessor, :name)
end

Instance Method Details

#accept_block(&block) ⇒ Object



32
33
34
# File 'lib/active_form/mixins/element_methods.rb', line 32

def accept_block(&block)
  self.instance_eval(&block)
end

#contained?Boolean

Returns:

  • (Boolean)


195
196
197
198
# File 'lib/active_form/mixins/element_methods.rb', line 195

def contained?
  @container ||= nil
  (@container && @container.container?) || false
end

#default_valueObject



73
74
75
# File 'lib/active_form/mixins/element_methods.rb', line 73

def default_value
  nil
end

#define_formatting_filter(prc = nil, &block) ⇒ Object Also known as: formatting_filter=



251
252
253
# File 'lib/active_form/mixins/element_methods.rb', line 251

def define_formatting_filter(prc = nil, &block)
  define_singleton_method(:formatting_filter, &(block_given? ? block : prc))
end

#define_freeze_filter(prc = nil, &block) ⇒ Object Also known as: freeze_filter=



270
271
272
# File 'lib/active_form/mixins/element_methods.rb', line 270

def define_freeze_filter(prc = nil, &block)
  define_singleton_method(:freeze_filter, &(block_given? ? block : prc))
end

#descriptionObject



60
61
62
63
# File 'lib/active_form/mixins/element_methods.rb', line 60

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

#disabled?Boolean

Returns:

  • (Boolean)


212
213
214
# File 'lib/active_form/mixins/element_methods.rb', line 212

def disabled?
  (option_flags[:disabled] == true) || (contained? && container.disabled?)
end

#element_attributesObject



144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/active_form/mixins/element_methods.rb', line 144

def element_attributes
  attrs = attribute_names.inject(default_attributes) do |hash, attribute|
    value = send(attribute) rescue nil
    hash[attribute] = value unless value.blank?
    hash
  end
  final = attrs.merge(attributes).merge(html_flag_attributes).merge(option_flag_attributes).stringify_keys.delete_blanks
  if skip_css_class? && !(vcss = validation_css_class.to_s).blank?
    final['class'] = vcss
  end
  final.delete('style') if skip_css_style?
  final
end

#element_bindingObject



65
66
67
# File 'lib/active_form/mixins/element_methods.rb', line 65

def element_binding
  element_value
end

#element_binding_keyObject



69
70
71
# File 'lib/active_form/mixins/element_methods.rb', line 69

def element_binding_key
  self.name
end

#element_nameObject



40
41
42
43
44
# File 'lib/active_form/mixins/element_methods.rb', line 40

def element_name
  elem_name = self.contained? ? "#{container.element_name}[#{self.group || self.name}]" : "#{self.group || self.name}"
  elem_name << '[]' unless self.group.blank?
  elem_name
end

#element_typeObject



46
47
48
# File 'lib/active_form/mixins/element_methods.rb', line 46

def element_type
  self.class.element_type
end

#element_value(export = false) ⇒ Object Also known as: values, value



103
104
105
106
107
108
109
110
111
# File 'lib/active_form/mixins/element_methods.rb', line 103

def element_value(export = false)
  return export_value if export
  if self.contained?
    revert_value unless container.bound_value?(element_binding_key)
    container.get_bound_value(element_binding_key)
  else
    @element_value ||= default_value
  end   
end

#element_value=(value, force = false) ⇒ Object Also known as: update_from_params, params=, initial_value=, update_value, values=, value=, binding=, bind_to



115
116
117
118
119
120
121
122
# File 'lib/active_form/mixins/element_methods.rb', line 115

def element_value=(value, force = false)
  value = cast_value(value)
  if self.contained?
    container.set_bound_value(element_binding_key, value)
  else
    @element_value = value
  end    
end

#export_valueObject



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

def export_value
  blank? ? cast_value(fallback_value || default_value) : element_value
end

#fallback_valueObject



77
78
79
# File 'lib/active_form/mixins/element_methods.rb', line 77

def fallback_value
  @fallback_value || default_value
end

#fallback_value=(value) ⇒ Object Also known as: default=



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

def fallback_value=(value)
  @fallback_value = value
end

#format_value(value) ⇒ Object



241
242
243
244
245
246
247
248
249
# File 'lib/active_form/mixins/element_methods.rb', line 241

def format_value(value)
  if self.respond_to?(:formatting_filter)
    self.formatting_filter(element_value).to_s rescue element_value
  elsif self.class.respond_to?(:formatting_filter)
    self.class.formatting_filter(element_value).to_s rescue element_value
  else
    element_value.to_s
  end
end

#formatted_valueObject



98
99
100
101
# File 'lib/active_form/mixins/element_methods.rb', line 98

def formatted_value
  return frozen_value if frozen? && !frozen_value.blank? 
  format_value(element_value)
end

#freeze_elementObject Also known as: freeze!



175
176
177
# File 'lib/active_form/mixins/element_methods.rb', line 175

def freeze_element
  self.frozen = true
end

#freeze_value(value) ⇒ Object



260
261
262
263
264
265
266
267
268
# File 'lib/active_form/mixins/element_methods.rb', line 260

def freeze_value(value)
  if self.respond_to?(:freeze_filter)
    self.freeze_filter(element_value).to_s rescue element_value
  elsif self.class.respond_to?(:freeze_filter)
    self.class.freeze_filter(element_value).to_s rescue element_value
  else
    element_value.to_s
  end
end

#frozen?Boolean

Returns:

  • (Boolean)


208
209
210
# File 'lib/active_form/mixins/element_methods.rb', line 208

def frozen?
  (option_flags[:frozen] == true) || (contained? && container.frozen?)
end

#frozen_valueObject



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

def frozen_value
  @frozen_value.blank? ? freeze_value(element_value) : @frozen_value
end

#frozen_value=(value) ⇒ Object



94
95
96
# File 'lib/active_form/mixins/element_methods.rb', line 94

def frozen_value=(value)
  @frozen_value = value
end

#hidden?Boolean

Returns:

  • (Boolean)


204
205
206
# File 'lib/active_form/mixins/element_methods.rb', line 204

def hidden?
  (option_flags[:hidden] == true)
end

#hide_elementObject Also known as: hide!



180
181
182
# File 'lib/active_form/mixins/element_methods.rb', line 180

def hide_element
  self.hidden = true
end

#identifierObject



36
37
38
# File 'lib/active_form/mixins/element_methods.rb', line 36

def identifier
  self.contained? ? "#{container.identifier}_#{self.name}" : "#{self.name}"
end

#initialize_element(*args) {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

Raises:

  • (ArgumentError)


11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/active_form/mixins/element_methods.rb', line 11

def initialize_element(*args)
  attributes = args.last.is_a?(Hash) ? args.pop : {} 
  container = args.shift if is_container?(args.first)
  args.unshift(element_type) unless args.first.kind_of?(String) || args.first.kind_of?(Symbol)
  raise ArgumentError if args.empty?
  self.name = ActiveForm::symbolize_name(args.first)
  self.label = self.name.to_s.titleize      
  register_container(container) if is_container?(container)     
  initialize_properties   
  update_options_and_attributes(attributes)
  yield self if block_given?
end

#labelObject



50
51
52
53
# File 'lib/active_form/mixins/element_methods.rb', line 50

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

#labelled?Boolean

Returns:

  • (Boolean)


200
201
202
# File 'lib/active_form/mixins/element_methods.rb', line 200

def labelled?
  true
end

#localize(*args) ⇒ Object



237
238
239
# File 'lib/active_form/mixins/element_methods.rb', line 237

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

#localized?Boolean

Returns:

  • (Boolean)


233
234
235
# File 'lib/active_form/mixins/element_methods.rb', line 233

def localized?
  contained? && container.localized?
end

#readonly?Boolean

Returns:

  • (Boolean)


216
217
218
# File 'lib/active_form/mixins/element_methods.rb', line 216

def readonly?
  (option_flags[:readonly] == true) || (contained? && container.readonly?)
end

#register_container(container) ⇒ Object Also known as: container=



24
25
26
27
28
29
# File 'lib/active_form/mixins/element_methods.rb', line 24

def register_container(container) 
  return ArgumentError unless is_container?(container)
  @container = container
  revert_value unless container.bound_value?(self.name)
  define_localizer(container.localizer) if self.container? && container.localized?
end

#render_element(builder = create_builder) ⇒ Object



191
192
193
# File 'lib/active_form/mixins/element_methods.rb', line 191

def render_element(builder = create_builder)
  raise ActiveForm::StubException
end

#render_frozen(builder = create_builder) ⇒ Object

stub implementations:



187
188
189
# File 'lib/active_form/mixins/element_methods.rb', line 187

def render_frozen(builder = create_builder)
  raise ActiveForm::StubException
end

#required=(value) ⇒ Object



220
221
222
223
224
225
226
227
# File 'lib/active_form/mixins/element_methods.rb', line 220

def required=(value)
  self.option_flags[:required] = value.to_s.to_boolean
  if self.option_flags[:required]
    validates_as_required
  else
    validators.delete_if { |v| v.code == 'required' }
  end
end

#required?Boolean

Returns:

  • (Boolean)


229
230
231
# File 'lib/active_form/mixins/element_methods.rb', line 229

def required?
  (option_flags[:required] == true) || (contained? && container.required?)
end

#reset_formatting_filterObject



256
257
258
# File 'lib/active_form/mixins/element_methods.rb', line 256

def reset_formatting_filter
  define_singleton_method(:formatting_filter) rescue nil
end

#reset_freeze_filterObject



275
276
277
# File 'lib/active_form/mixins/element_methods.rb', line 275

def reset_freeze_filter
  define_singleton_method(:freeze_filter) rescue nil
end

#revert_valueObject



136
137
138
139
140
141
142
# File 'lib/active_form/mixins/element_methods.rb', line 136

def revert_value
  if self.contained?
    container.set_bound_value(element_binding_key, default_value)
  else
    @element_value = default_value   
  end 
end

#titleObject



55
56
57
58
# File 'lib/active_form/mixins/element_methods.rb', line 55

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

#update_options_and_attributes(hash) ⇒ Object Also known as: update



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/active_form/mixins/element_methods.rb', line 158

def update_options_and_attributes(hash)
  last_pass_options = ['options', 'option', 'multiple', 'binding', 'initial_value', 'fallback_value', 'checked']
  ordered_options = [*self.class.element_html_flag_names] + [*self.class.element_attribute_names]
  ordered_options.map!(&:to_s)
  hash.stringify_keys!
  self.type_cast = hash.delete('type_cast') if hash.key?('type_cast')
  hash['fallback_value'] = hash.delete('default') if hash.key?('default')
  hash['initial_value'] = hash.delete('values') if hash.key?('values')
  hash['initial_value'] = hash.delete('value') if hash.key?('value')   
  (((hash.keys & ordered_options) - last_pass_options) + (last_pass_options & hash.keys)).each do |option|
    send("#{option}=", hash[option]) rescue nil
    hash.delete(option)
  end
  hash.each { |option, value| send("#{option}=", value) rescue nil } 
end