Class: Niles::FormBuilder
- Inherits:
-
Object
- Object
- Niles::FormBuilder
- Defined in:
- lib/niles/form_builder.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.
7 8 9 10 11 12 |
# File 'lib/niles/form_builder.rb', line 7 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.
5 6 7 |
# File 'lib/niles/form_builder.rb', line 5 def @form_options end |
#helpers ⇒ Object (readonly)
Returns the value of attribute helpers.
5 6 7 |
# File 'lib/niles/form_builder.rb', line 5 def helpers @helpers end |
#resource ⇒ Object (readonly)
Returns the value of attribute resource.
5 6 7 |
# File 'lib/niles/form_builder.rb', line 5 def resource @resource end |
Instance Method Details
#input(name, options = {}) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/niles/form_builder.rb', line 14 def input(name, = {}) name = name.to_s # Set default options = { label: "%s:" % label_text(name), value: resource.send(name) }.merge() # FIXME [:as] ||= case [:value] when Date then :date when DateTime, Time then :datetime 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 label s << helpers.html_tag(:label) { [:label] } # Add actual input field case [:as].to_sym when :textarea s << helpers.html_tag(:textarea, ) { helpers.preserve(helpers.escape_html([:value])) } when :datetime s << helpers.date_time_select(.delete(:name), value: [:value]) else 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
68 69 70 |
# File 'lib/niles/form_builder.rb', line 68 def label_text(name) helpers.translate("forms.#{@resource_name}.labels.#{name}", :default => name.humanize) end |
#placeholder(name) ⇒ Object
72 73 74 |
# File 'lib/niles/form_builder.rb', line 72 def placeholder(name) helpers.translate("forms.#{@resource_name}.placeholders.#{name}", :default => "") end |
#submit(label = nil) ⇒ Object
76 77 78 79 |
# File 'lib/niles/form_builder.rb', line 76 def submit(label = nil) label ||= (resource.new_record? ? 'Create' : 'Update') helpers.html_tag(:input, type: 'submit', value: label) end |