Class: YouthTree::Helpers::FormBuilder

Inherits:
Formtastic::SemanticFormBuilder
  • Object
show all
Defined in:
lib/youth_tree/helpers/form_builder.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.install!Object



5
6
7
8
9
10
11
# File 'lib/youth_tree/helpers/form_builder.rb', line 5

def self.install!
  Formtastic::SemanticFormHelper.builder   = self
  self.include_blank_for_select_by_default = true
  self.i18n_lookups_by_default             = true
  self.required_string                     = proc { ::Formtastic::I18n.t(:required).html_safe }
  self.optional_string                     = proc { ::Formtastic::I18n.t(:optional).html_safe }
end

Instance Method Details

#boolean_input(method, options) ⇒ Object



22
23
24
# File 'lib/youth_tree/helpers/form_builder.rb', line 22

def boolean_input(method, options)
  super.gsub(":</label>", "</label>").gsub(": <abbr", " <abbr")
end

#commit_button_with_cancel(*args) ⇒ Object



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
67
68
69
70
71
72
73
74
# File 'lib/youth_tree/helpers/form_builder.rb', line 33

def commit_button_with_cancel(*args)
  options = args.extract_options!
  text = options.delete(:label) || args.shift
  cancel_options = options.delete(:cancel)

  if @object && @object.respond_to?(:new_record?)
    key = @object.new_record? ? :create : :update

    # Deal with some complications with ActiveRecord::Base.human_name and two name models (eg UserPost)
    # ActiveRecord::Base.human_name falls back to ActiveRecord::Base.name.humanize ("Userpost")
    # if there's no i18n, which is pretty crappy.  In this circumstance we want to detect this
    # fall back (human_name == name.humanize) and do our own thing name.underscore.humanize ("User Post")
    if @object.class.model_name.respond_to?(:human)
      object_name = @object.class.model_name.human
    else
      object_human_name = @object.class.human_name                # default is UserPost => "Userpost", but i18n may do better ("User post")
      crappy_human_name = @object.class.name.humanize             # UserPost => "Userpost"
      decent_human_name = @object.class.name.underscore.humanize  # UserPost => "User post"
      object_name = (object_human_name == crappy_human_name) ? decent_human_name : object_human_name
    end
  else
    key = :submit
    object_name = @object_name.to_s.send(self.class.label_str_method)
  end

  text = (self.localized_string(key, text, :action, :model => object_name) ||
          ::Formtastic::I18n.t(key, :model => object_name)) unless text.is_a?(::String)

  button_html = options.delete(:button_html) || {}
  button_html.merge!(:class => [button_html[:class], key].compact.join(' '))
  element_class = ['commit', options.delete(:class)].compact.join(' ') # TODO: Add class reflecting on form action.
  accesskey = (options.delete(:accesskey) || self.class.default_commit_button_accesskey) unless button_html.has_key?(:accesskey)
  button_html = button_html.merge(:accesskey => accesskey) if accesskey
  inner = Formtastic::Util.html_safe(self.submit(text, button_html))
  # Start custom code
  if cancel_options.present?
    inner << template.(:span, "or", :class => "or")
    inner << @template.link_to(cancel_options.delete(:text), cancel_options.delete(:url), cancel_options)
  end
  # End custom code
  template.(:li, inner, :class => element_class)
end

#date_picker_input(method, options = {}) ⇒ Object



89
90
91
92
# File 'lib/youth_tree/helpers/form_builder.rb', line 89

def date_picker_input(method, options = {})
  format = (options[:format] || Time::DATE_FORMATS[:default] || '%d %B %Y')
  string_input(method, date_picker_options(format, object.send(method)).merge(options))
end

#datetime_picker_input(method, options = {}) ⇒ Object



84
85
86
87
# File 'lib/youth_tree/helpers/form_builder.rb', line 84

def datetime_picker_input(method, options = {})
  format = (options[:format] || Time::DATE_FORMATS[:default] || '%d %B %Y %I:%M %p')
  string_input(method, datetime_picker_options(format, object.send(method)).merge(options))
end

#dob_input(*args) ⇒ Object



26
27
28
29
30
31
# File 'lib/youth_tree/helpers/form_builder.rb', line 26

def dob_input(*args)
  options = args.extract_options!
  options.merge!(:start_year => (Time.now.year - 100), :end_year => Time.now.year, :selected => nil)
  args << options
  date_input(*args)
end

#error_sentence(errors, options = {}) ⇒ Object

Returns errors converted to a sentence, adding a full stop.



77
78
79
80
81
82
# File 'lib/youth_tree/helpers/form_builder.rb', line 77

def error_sentence(errors, options = {})
  error_class = options[:error_class] || self.class.default_inline_error_class
  error_text = errors.to_sentence.untaint.strip
  error_text << "." unless %w(? ! . :).include?(error_text[-1, 1])
  template.(:p, Formtastic::Util.html_safe(error_text), :class => error_class)
end

#label(*args) ⇒ Object

Generates a label, using a safe buffer and a few other things along those lines.



18
19
20
# File 'lib/youth_tree/helpers/form_builder.rb', line 18

def label(*args)
  super(*args).gsub(/\?\s*\:\<\/label\>/, "?</label>").gsub(/\?\s*\:\s*\<abbr/, "? <abbr")
end

#submit(value = "Save changes", options = {}) ⇒ Object



13
14
15
# File 'lib/youth_tree/helpers/form_builder.rb', line 13

def submit(value = "Save changes", options = {})
  @template.(:button, value, options.reverse_merge(:type => "submit", :id => "#{object_name}_submit"))
end