Class: CaringForm::Field::Base
- Inherits:
-
Struct
- Object
- Struct
- CaringForm::Field::Base
- Defined in:
- lib/caring_form/field/base.rb
Direct Known Subclasses
Collection, Dummy, Email, FullName, Metadata, String, Submit, Tel, Text, UsZipCode
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
Class Method Summary collapse
Instance Method Summary collapse
- #apply_to_model(klass) ⇒ Object
- #default_label ⇒ Object
-
#initialize(*args) ⇒ Base
constructor
A new instance of Base.
- #input_properties(form, options = {}) ⇒ Object
- #render(form, builder, options = {}) ⇒ Object
- #simple_type ⇒ Object
Constructor Details
#initialize(*args) ⇒ Base
Returns a new instance of Base.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/caring_form/field/base.rb', line 28 def initialize(*args) super self.type ||= :string self.optional ||= false self.input_tag ||= 'input' self.wrapper_tag ||= 'li' self.wrapper_html ||= {} self.input_html ||= {} self.label_html ||= {} self.ancillary ||= false self. ||= "Enter the #{name.to_s.humanize.downcase}" self.label = default_label if self.label.nil? @referee = Tools::Referee.new(self) end |
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name
23 24 25 |
# File 'lib/caring_form/field/base.rb', line 23 def name @name end |
Class Method Details
.register(type) ⇒ Object
24 25 26 |
# File 'lib/caring_form/field/base.rb', line 24 def self.register(type) CaringForm::FieldContainer.register_field(self, type) end |
Instance Method Details
#apply_to_model(klass) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/caring_form/field/base.rb', line 46 def apply_to_model(klass) klass.send(:attr_accessor, name) = {:message => } if @referee.is_rule?(:optional) referee = @referee [:if] = lambda do |form| !referee.decide(:optional, form) end klass.send(:validates_presence_of, name, ) elsif !optional [:if] = self.if if self.if.present? klass.send(:validates_presence_of, name, ) end end |
#default_label ⇒ Object
99 100 101 |
# File 'lib/caring_form/field/base.rb', line 99 def default_label self.name.to_s.humanize.titleize end |
#input_properties(form, options = {}) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/caring_form/field/base.rb', line 61 def input_properties(form, = {}) = (form, ) req = { :as => .fetch(:as, simple_type), :collection => .fetch(:collection, collection), :prompt => .fetch(:prompt, prompt), :label => .fetch(:label, label), :wrapper => .fetch(:wrapper, wrapper), :wrapper_tag => .fetch(:wrapper_tag, wrapper_tag), :wrapper_html => .fetch(:wrapper_html, wrapper_html).merge(:class => 'field'), :input_html => .fetch(:input_html, input_html), :label_html => .fetch(:label_html, label_html) } req[:input_html][:autofocus] = 'autofocus' if form.autofocus?(name) req[:input_html][:title] ||= if .present? if form.errors[self.name].present? req[:wrapper_html][:class] = "field-with-errors #{req[:wrapper_html][:class]}".rstrip end if form.index_on.present? req[:label_html][:index] = req[:input_html][:index] = form.index_on end if @referee.decide(:optional, form) req[:required] = false else req[:input_html][:required] = 'required' end req[:input_html].merge!(@referee.rule_as_data_attribute(:optional, form)) req[:input_html][:name] = name if ancillary set_hint!(req) set_remote_validator!(req) req.deep_merge!() end |
#render(form, builder, options = {}) ⇒ Object
103 104 105 106 107 108 109 110 111 |
# File 'lib/caring_form/field/base.rb', line 103 def render(form, builder, = {}) properties = input_properties(form, ) if properties[:generate] == false '' else properties[:required] = true unless optional builder.input(name, properties) end end |
#simple_type ⇒ Object
95 96 97 |
# File 'lib/caring_form/field/base.rb', line 95 def simple_type raise "override me!" end |