Class: HappyHelpers::Helpers::Forms::FormBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/happy-helpers/helpers/forms.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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, form_options = {})
  @helpers = helpers
  @resource = resource
  @resource_name = resource.class.to_s.singularize.underscore
  @form_options = form_options
end

Instance Attribute Details

#form_optionsObject (readonly)

Returns the value of attribute form_options.



87
88
89
# File 'lib/happy-helpers/helpers/forms.rb', line 87

def form_options
  @form_options
end

#helpersObject (readonly)

Returns the value of attribute helpers.



87
88
89
# File 'lib/happy-helpers/helpers/forms.rb', line 87

def helpers
  @helpers
end

#resourceObject (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, options = {})
  name = name.to_s
  value = resource.send(name)

  # Set default options
  options = {
    label: "%s:" % label_text(name)
  }.merge(options)

  options[:as] ||= case options[: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
  field_options = {
    name: "%s[%s]" % [@resource_name, name]
  }
  unless (placeholder = options.delete(:placeholder) || placeholder(name)).blank?
    field_options[:placeholder] = placeholder
  end

  # Set default options for the wrapper element
  wrapper_options = {
    class: "input #{options[:as]}"
  }
  wrapper_options[:class] << " with_error" if resource.errors[name].any?

  # Output wrapper tag with contents
  helpers.html_tag :div, wrapper_options do
    String.new.tap do |s|
      # Add actual input field
      case options.delete(:as).to_sym
      when :textarea
        s << helpers.html_tag(:label, class: 'for-field') { options[:label] }
        s << helpers.html_tag(:textarea, field_options) { helpers.preserve(helpers.escape_html(value)) }
      when :datetime
        s << helpers.html_tag(:label, class: 'for-field') { options[:label] }
        s << helpers.date_time_select_tag(field_options.delete(:name), value, options)
      when :radio
        s << helpers.radio_buttons(field_options.delete(:name), options: (options.delete(:options) || ['Y', 'N']))
      when :checkbox
        s << helpers.checkbox_tag(field_options.delete(:name), value, options.merge(:text => options[:text] || label_text(name)))
      else
        s << helpers.html_tag(:label, class: 'for-field') { options[:label] }
        s << helpers.html_tag(:input, field_options.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