Class: HappyHelpers::Helpers::Forms::FormBuilder
- Inherits:
-
Object
- Object
- HappyHelpers::Helpers::Forms::FormBuilder
- Defined in:
- lib/happy-helpers/helpers/forms.rb
Instance Attribute Summary collapse
-
#form_options ⇒ Object
readonly
Returns the value of attribute form_options.
-
#helpers ⇒ Object
readonly
Returns the value of attribute helpers.
-
#resource ⇒ Object
readonly
Returns the value of attribute resource.
Instance Method Summary collapse
-
#initialize(helpers, resource, form_options = {}) ⇒ FormBuilder
constructor
A new instance of FormBuilder.
- #input(name, options = {}) ⇒ Object
- #label_text(name) ⇒ Object
- #placeholder(name) ⇒ Object
- #submit(label = nil) ⇒ Object
Constructor Details
#initialize(helpers, resource, form_options = {}) ⇒ FormBuilder
Returns a new instance of FormBuilder.
89 90 91 92 93 94 |
# File 'lib/happy-helpers/helpers/forms.rb', line 89 def initialize(helpers, resource, = {}) @helpers = helpers @resource = resource @resource_name = resource.class.to_s.singularize.underscore @form_options = end |
Instance Attribute Details
#form_options ⇒ Object (readonly)
Returns the value of attribute form_options.
87 88 89 |
# File 'lib/happy-helpers/helpers/forms.rb', line 87 def @form_options end |
#helpers ⇒ Object (readonly)
Returns the value of attribute helpers.
87 88 89 |
# File 'lib/happy-helpers/helpers/forms.rb', line 87 def helpers @helpers end |
#resource ⇒ Object (readonly)
Returns the value of attribute resource.
87 88 89 |
# File 'lib/happy-helpers/helpers/forms.rb', line 87 def resource @resource end |
Instance Method Details
#input(name, options = {}) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/happy-helpers/helpers/forms.rb', line 96 def input(name, = {}) name = name.to_s value = resource.send(name) # Set default options = { label: "%s:" % label_text(name) }.merge() [:as] ||= case [:value] when Date then :date when DateTime, Time then :datetime when Boolean, TrueClass, FalseClass then :checkbox else :text end # Set default options for the input field = { name: "%s[%s]" % [@resource_name, name] } unless (placeholder = .delete(:placeholder) || placeholder(name)).blank? [:placeholder] = placeholder end # Set default options for the wrapper element = { class: "input #{[:as]}" } [:class] << " with_error" if resource.errors[name].any? # Output wrapper tag with contents helpers.html_tag :div, do String.new.tap do |s| # Add actual input field case .delete(:as).to_sym when :textarea s << helpers.html_tag(:label, class: 'for-field') { [:label] } s << helpers.html_tag(:textarea, ) { helpers.preserve(helpers.escape_html(value)) } when :datetime s << helpers.html_tag(:label, class: 'for-field') { [:label] } s << helpers.date_time_select_tag(.delete(:name), value, ) when :radio s << helpers.(.delete(:name), options: (.delete(:options) || ['Y', 'N'])) when :checkbox s << helpers.checkbox_tag(.delete(:name), value, .merge(:text => [:text] || label_text(name))) else s << helpers.html_tag(:label, class: 'for-field') { [:label] } s << helpers.html_tag(:input, .merge(type: 'text', value: value)) end # Add error message, if necessary if @resource.errors[name].any? s << helpers.html_tag(:div, :class => "error") { @resource.errors[name].first } end end end end |
#label_text(name) ⇒ Object
154 155 156 |
# File 'lib/happy-helpers/helpers/forms.rb', line 154 def label_text(name) helpers.translate("forms.#{@resource_name}.labels.#{name}", :default => name.humanize) end |
#placeholder(name) ⇒ Object
158 159 160 |
# File 'lib/happy-helpers/helpers/forms.rb', line 158 def placeholder(name) helpers.translate("forms.#{@resource_name}.placeholders.#{name}", :default => "") end |
#submit(label = nil) ⇒ Object
162 163 164 165 |
# File 'lib/happy-helpers/helpers/forms.rb', line 162 def submit(label = nil) label ||= (resource.new_record? ? 'Create' : 'Update') helpers.html_tag(:input, type: 'submit', value: label) end |