Class: Formidable::Elements::ElementList
- Inherits:
-
BasicElement
- Object
- BasicElement
- Formidable::Elements::ElementList
- Includes:
- GroupValidations
- Defined in:
- lib/formidable/elements.rb
Overview
pro form, group, fieldset
Constant Summary
Constants included from Rendering
Rendering::RendererNotAssigned
Instance Attribute Summary
Attributes inherited from BasicElement
Attributes included from Rendering
Class Method Summary collapse
-
.register(klass, method_name) ⇒ Object
We had a few beers and we decided that this is pretty cool :) This will define DSL method for creating email_field.
Instance Method Summary collapse
- #cleaned_data ⇒ Object
- #content ⇒ Object
- #elements ⇒ Object
-
#initialize(tag, name, attributes = Hash.new, raw_data = nil, &block) ⇒ ElementList
constructor
This is pretty much just a copy of Element#initialize, it’s because if we’d use super, then the Element#raw_data=(raw_data) would be called, but we actually need to trigger the ElementList#raw_data=(raw_data).
- #raw_data ⇒ Object
- #raw_data=(raw_data) ⇒ Object
-
#set_prefix(prefix) ⇒ Object
TODO: this should be done dynamically, something like: def name self.parent.name + @name end.
Methods included from GroupValidations
#before_validate, #errors, register_validation, #valid?, #validate, #validations
Methods included from Rendering
Constructor Details
#initialize(tag, name, attributes = Hash.new, raw_data = nil, &block) ⇒ ElementList
This is pretty much just a copy of Element#initialize, it’s because if we’d use super, then the Element#raw_data=(raw_data) would be called, but we actually need to trigger the ElementList#raw_data=(raw_data).
59 60 61 62 63 64 |
# File 'lib/formidable/elements.rb', line 59 def initialize(tag, name, attributes = Hash.new, raw_data = nil, &block) @tag, @name, @attributes = tag, name, attributes @attributes.merge!(name: name) if name self.raw_data = raw_data if raw_data self.instance_eval(&block) if block end |
Class Method Details
.register(klass, method_name) ⇒ Object
We had a few beers and we decided that this is pretty cool :) This will define DSL method for creating email_field
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/formidable/elements.rb', line 38 def self.register(klass, method_name) define_method(method_name) do |*args, &block| element = klass.new(*args, &block) elements << element unless element.name.nil? if self.class.method_defined?(element.name) warn "Overriding method #{element.name}" end self.class.send(:define_method, element.name) do element end end element end end |
Instance Method Details
#cleaned_data ⇒ Object
93 94 95 96 97 98 99 100 |
# File 'lib/formidable/elements.rb', line 93 def cleaned_data self.elements.inject(Hash.new) do |result, element| if element.name && element.raw_data result[element.name] = element.cleaned_data end result end end |
#content ⇒ Object
70 71 72 73 74 |
# File 'lib/formidable/elements.rb', line 70 def content self.elements.inject(String.new) do |buffer, element| buffer += "\n" + element.render end end |
#elements ⇒ Object
66 67 68 |
# File 'lib/formidable/elements.rb', line 66 def elements @elements ||= Array.new end |
#raw_data ⇒ Object
76 77 78 79 80 81 82 83 |
# File 'lib/formidable/elements.rb', line 76 def raw_data self.elements.inject(Hash.new) do |result, element| if element.name && element.raw_data result[element.name] = element.raw_data end result end end |
#raw_data=(raw_data) ⇒ Object
85 86 87 88 89 90 91 |
# File 'lib/formidable/elements.rb', line 85 def raw_data=(raw_data) return if raw_data.nil? raw_data.each do |key, value| element = self.send(key) element.raw_data = value end end |
#set_prefix(prefix) ⇒ Object
TODO: this should be done dynamically, something like: def name
self.parent.name + @name
end
106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/formidable/elements.rb', line 106 def set_prefix(prefix) self.elements.each do |element| if element.respond_to?(:elements) && element.name element.set_prefix("#{prefix}[#{element.name}]") end if element.attributes[:name] element.attributes[:name] = begin "#{prefix}[#{element.attributes[:name]}]" end end end end |