Class: Niles::FormBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/niles/form_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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, 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.



5
6
7
# File 'lib/niles/form_builder.rb', line 5

def form_options
  @form_options
end

#helpersObject (readonly)

Returns the value of attribute helpers.



5
6
7
# File 'lib/niles/form_builder.rb', line 5

def helpers
  @helpers
end

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

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

  # FIXME
  options[:as] ||= case options[:value]
    when Date           then :date
    when DateTime, Time then :datetime
    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 label
      s << helpers.html_tag(:label) { options[:label] }

      # Add actual input field
      case options[:as].to_sym
      when :textarea
        s << helpers.html_tag(:textarea, field_options) { helpers.preserve(helpers.escape_html(options[:value])) }
      when :datetime
        s << helpers.date_time_select(field_options.delete(:name), value: options[:value])
      else
        s << helpers.html_tag(:input, field_options.merge(type: 'text', value: options[: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