Class: YouthTree::Helpers::FormBuilder
- Inherits:
-
Formtastic::SemanticFormBuilder
- Object
- Formtastic::SemanticFormBuilder
- YouthTree::Helpers::FormBuilder
- Defined in:
- lib/youth_tree/helpers/form_builder.rb
Class Method Summary collapse
Instance Method Summary collapse
- #boolean_input(method, options) ⇒ Object
- #commit_button_with_cancel(*args) ⇒ Object
- #date_picker_input(method, options = {}) ⇒ Object
- #datetime_picker_input(method, options = {}) ⇒ Object
- #dob_input(*args) ⇒ Object
-
#error_sentence(errors, options = {}) ⇒ Object
Returns errors converted to a sentence, adding a full stop.
-
#label(*args) ⇒ Object
Generates a label, using a safe buffer and a few other things along those lines.
- #submit(value = "Save changes", options = {}) ⇒ Object
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, ) 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 (*args) = args. text = .delete(:label) || args.shift = .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) = .delete(:button_html) || {} .merge!(:class => [[:class], key].compact.join(' ')) element_class = ['commit', .delete(:class)].compact.join(' ') # TODO: Add class reflecting on form action. accesskey = (.delete(:accesskey) || self.class.) unless .has_key?(:accesskey) = .merge(:accesskey => accesskey) if accesskey inner = Formtastic::Util.html_safe(self.submit(text, )) # Start custom code if .present? inner << template.content_tag(:span, "or", :class => "or") inner << @template.link_to(.delete(:text), .delete(:url), ) end # End custom code template.content_tag(: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, = {}) format = ([:format] || Time::DATE_FORMATS[:default] || '%d %B %Y') string_input(method, (format, object.send(method)).merge()) 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, = {}) format = ([:format] || Time::DATE_FORMATS[:default] || '%d %B %Y %I:%M %p') string_input(method, (format, object.send(method)).merge()) 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) = args. .merge!(:start_year => (Time.now.year - 100), :end_year => Time.now.year, :selected => nil) args << 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, = {}) error_class = [: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.content_tag(: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", = {}) @template.content_tag(:button, value, .reverse_merge(:type => "submit", :id => "#{object_name}_submit")) end |