Class: MuckCustomFormBuilder
- Inherits:
-
ActionView::Helpers::FormBuilder
- Object
- ActionView::Helpers::FormBuilder
- MuckCustomFormBuilder
- Defined in:
- app/helpers/muck_custom_form_builder.rb
Instance Method Summary collapse
-
#country_select(method, options = {}, html_options = {}, additional_country = nil) ⇒ Object
creates a select control with countries.
- #get_instance_object_value(field_name) ⇒ Object
-
#language_select(method, options = {}, html_options = {}, additional_language = nil) ⇒ Object
creates a select control with languages.
- #menu_select(method, label, collection, options = {}, html_options = {}) ⇒ Object
- #muck_select(method, value_method, name_method, label, collection, options = {}, html_options = {}) ⇒ Object
-
#render_field_template(name, field, options) ⇒ Object
Options to pass to render_field_template through the ‘options’ argument.
-
#state_select(method, options = {}, html_options = {}, additional_state = nil) ⇒ Object
creates a select control with states.
-
#us_state_select(method, options = {}, html_options = {}, additional_state = nil) ⇒ Object
Call ‘<%= country_scripts %>’ to render javascript that will change the state control based on the current country creates a select control with us states.
Instance Method Details
#country_select(method, options = {}, html_options = {}, additional_country = nil) ⇒ Object
creates a select control with countries. Default id is ‘countries’. If ‘retain’ is passed for the class value the value of this control will be written into a cookie with the key ‘countries’.
136 137 138 139 |
# File 'app/helpers/muck_custom_form_builder.rb', line 136 def country_select(method, = {}, = {}, additional_country = nil) @countries ||= (additional_country ? [additional_country] : []) + Country.find(:all, :order => 'sort, name asc') self.(method, I18n.t('muck.engine.choose_country'), @countries, .merge(:prompt => I18n.t('muck.engine.select_country_prompt'), :wrapper_id => 'countries-container'), .merge(:id => 'countries')) end |
#get_instance_object_value(field_name) ⇒ Object
148 149 150 151 |
# File 'app/helpers/muck_custom_form_builder.rb', line 148 def get_instance_object_value(field_name) obj = object || @template.instance_variable_get("@#{@object_name}") obj.send(field_name) rescue nil end |
#language_select(method, options = {}, html_options = {}, additional_language = nil) ⇒ Object
creates a select control with languages. Default id is ‘languages’. If ‘retain’ is passed for the class value the value of this control will be written into a cookie with the key ‘languages’.
143 144 145 146 |
# File 'app/helpers/muck_custom_form_builder.rb', line 143 def language_select(method, = {}, = {}, additional_language = nil) @languages ||= (additional_language ? [additional_language] : []) + Language.find(:all, :order => 'name asc') self.(method, I18n.t('muck.engine.choose_language'), @languages, .merge(:prompt => I18n.t('muck.engine.select_language_prompt'), :wrapper_id => 'languages-container'), .merge(:id => 'languages')) end |
#menu_select(method, label, collection, options = {}, html_options = {}) ⇒ Object
153 154 155 |
# File 'app/helpers/muck_custom_form_builder.rb', line 153 def (method, label, collection, = {}, = {}) muck_select(method, :id, :name, label, collection, , ) end |
#muck_select(method, value_method, name_method, label, collection, options = {}, html_options = {}) ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'app/helpers/muck_custom_form_builder.rb', line 157 def muck_select(method, value_method, name_method, label, collection, = {}, = {}) # this code will look for a cookie that matches the control and set the value to the cookie value # if html_options.has_key?(:class) # if html_options[:class].include?('retain') # id = html_options[:id] # @object.send("#{method}=", @template.cookies[id]) if @template.cookies.has_key? id # end # end if ![:tip].nil? [:class] ||= '' [:class] << add_space_to_css() + 'tip-field' end self.collection_select(method, collection, value_method, name_method, .merge(:object => @object, :label => label, :type => :choose_menu, :label_class => 'desc', :field_id => [:id]), ) end |
#render_field_template(name, field, options) ⇒ Object
Options to pass to render_field_template through the ‘options’ argument. pre_html html content to insert at the beginning of the container extra_html html content to insert at the end of the container tip Text for a popup text tip. tip_title Title for popup text tip tip_key The id of the div that contains the tip text. tip_position Position for tip text. Valid values are ‘top’, ‘right’, ‘bottom’, ‘left’. Default is ‘right’ wrapper_id Alternate id for the container. Each field is typically wrapper in a div. This is the id of that div. hide_required Do not show ‘required’ label even though field is required hide_control_error Hide errors that show up next to the field
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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'app/helpers/muck_custom_form_builder.rb', line 28 def render_field_template(name, field, ) tippable = ![:tip].nil? # used by country, state and other selects to specify a different id field_id = [:field_id] = { :extra_html => .delete(:extra_html), :tip => .delete(:tip), :tip_title => .delete(:tip_title), :tip_key => .delete(:tip_key), :tip_position => .delete(:tip_position) || 'right', :wrapper_id => .delete(:wrapper_id), :pre_html => .delete(:pre_html), :hide_required => .delete(:hide_required), :hide_control_error => .delete(:hide_control_error), :css_class => .delete(:css_class) } # TODO css_class does not appear to be used. Can just use the standard :class in html options to set the class type = .delete(:type) type ||= :tippable if tippable if ![:label_class].nil? = { :class => .delete(:label_class) } else = { } end if [:hide_required] required = false label_text = [:label] else required = required_field?(field) label_text = ([:label] || field.to_s.camelize) label_text = label_text + required_mark(field) if required end label_name = .delete(:label) is_checkbox = false is_checkbox = true if %w(check_box).include?(name) [:class] ||= '' [:class] << add_space_to_css() + 'tip-field' if tippable [:class] << add_space_to_css() + "required-value" if required if type == :choose_menu [:label_class] = 'desc' end locals = { :field_element => yield, :field_name => field_id || field_name(field), :label_name => .delete(:required_label) || label_name || '', :label_element => label(field, label_text, ), :is_checkbox => is_checkbox, :required => required }.merge() if object.nil? locals[:value] = '' locals[:id] = '' else locals[:value] = object.send(field) locals[:id] = object.id end if has_errors_on?(field) locals.merge!(:error => (field, )) end @template.capture do if type == :tippable @template.render :partial => 'forms/field_with_tips', :locals => locals elsif type == :choose_menu @template.render :partial => 'forms/menu_field', :locals => locals elsif type == :color_picker @template.render :partial => 'forms/color_picker_field', :locals => locals elsif type == :default @template.render :partial => 'forms/default', :locals => locals else @template.render :partial => 'forms/field', :locals => locals end end end |
#state_select(method, options = {}, html_options = {}, additional_state = nil) ⇒ Object
creates a select control with states. Default id is ‘states’. If ‘retain’ is passed for the class value the value of this control will be written into a cookie with the key ‘states’.
127 128 129 130 131 132 |
# File 'app/helpers/muck_custom_form_builder.rb', line 127 def state_select(method, = {}, = {}, additional_state = nil) country_id_field_name = .delete(:country_id) || 'country_id' country_id = get_instance_object_value(country_id_field_name) @states = country_id.nil? ? [] : (additional_state ? [additional_state] : []) + State.find(:all, :conditions => ["country_id = ?", country_id], :order => "name asc") self.(method, I18n.t('muck.engine.choose_state'), @states, .merge(:prompt => I18n.t('muck.engine.select_state_prompt'), :wrapper_id => 'states-container'), .merge(:id => 'states')) end |
#us_state_select(method, options = {}, html_options = {}, additional_state = nil) ⇒ Object
Call ‘<%= country_scripts %>’ to render javascript that will change the state control based on the current country creates a select control with us states. Default id is ‘us_states’. If ‘retain’ is passed for the class value the value of this control will be written into a cookie with the key ‘us_states’.
119 120 121 122 123 |
# File 'app/helpers/muck_custom_form_builder.rb', line 119 def us_state_select(method, = {}, = {}, additional_state = nil) @@country_id ||= Country.find_by_abbreviation('US') @states = (additional_state ? [additional_state] : []) + State.find(:all, :conditions => ["country_id = ?", @@country_id], :order => "name asc") self.(method, I18n.t('muck.engine.choose_state'), @states, .merge(:prompt => I18n.t('muck.engine.select_state_prompt'), :wrapper_id => 'us-states-container'), .merge(:id => 'us-states')) end |