Module: Admin::BaseHelper
- Defined in:
- app/helpers/admin/base_helper.rb
Instance Method Summary collapse
- #additional_field_for(controller, field) ⇒ Object
- #class_for_error(model, method) ⇒ Object
- #error_message_on(object, method, options = {}) ⇒ Object
- #field_container(model, method, options = {}, &block) ⇒ Object
-
#generate_html(form_builder, method, options = {}) ⇒ Object
This method demonstrates the use of the :child_index option to render a form partial for, for instance, client side addition of new nested records.
- #generate_template(form_builder, method, options = {}) ⇒ Object
-
#get_additional_field_value(controller, field) ⇒ Object
You can add additional_fields to the product and variant models.
-
#link_to_add_fields(name, append_to_selector, f, association) ⇒ Object
renders set of hidden fields and button to add new record using nested_attributes.
-
#link_to_remove_fields(name, f) ⇒ Object
renders hidden field and link to remove record using nested_attributes.
- #preference_field(form, field, options) ⇒ Object
- #preference_fields(object, form) ⇒ Object
- #product_picker_field(name, value) ⇒ Object
- #remove_nested(fields) ⇒ Object
- #spree_date_picker(object, method, options = {}, html_options = {}) ⇒ Object
Instance Method Details
#additional_field_for(controller, field) ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'app/helpers/admin/base_helper.rb', line 145 def additional_field_for(controller, field) field[:use] ||= 'text_field' = field[:options] || {} object_name, method = controller.controller_name.singularize, attribute_name_for(field[:name]) case field[:use] when 'check_box' check_box(object_name, method, , field[:checked_value] || 1, field[:unchecked_value] || 0) when 'radio_button' html = '' field[:value].call(controller, field).each do |value| html << (object_name, method, value, ) html << " #{value.to_s} " end html when 'select' select(object_name, method, field[:value].call(controller, field), , field[:html_options] || {}) else value = field[:value] ? field[:value].call(controller, field) : get_additional_field_value(controller, field) __send__(field[:use], object_name, method, .merge(:value => value)) end # case end |
#class_for_error(model, method) ⇒ Object
27 28 29 30 |
# File 'app/helpers/admin/base_helper.rb', line 27 def class_for_error(model, method) if :product, :name end end |
#error_message_on(object, method, options = {}) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 |
# File 'app/helpers/admin/base_helper.rb', line 10 def (object, method, = {}) object = convert_to_model(object) obj = object.respond_to?(:errors) ? object : instance_variable_get("@#{object}") if obj && obj.errors[method].present? errors = obj.errors[method].map{|err| h(err)}.join('<br/>').html_safe content_tag(:span, errors, :class => 'formError') else '' end end |
#field_container(model, method, options = {}, &block) ⇒ Object
3 4 5 6 7 8 |
# File 'app/helpers/admin/base_helper.rb', line 3 def field_container(model, method, = {}, &block) unless (model, method).blank? css_class = 'withError' end content_tag('p', capture(&block), :class => css_class) end |
#generate_html(form_builder, method, options = {}) ⇒ Object
This method demonstrates the use of the :child_index option to render a form partial for, for instance, client side addition of new nested records.
This specific example creates a link which uses javascript to add a new form partial to the DOM.
<%= form_for @project do |project_form| %>
<div id="tasks">
<%= project_form.fields_for :tasks do |task_form| %>
<%= render :partial => 'task', :locals => { :f => task_form } %>
<% end %>
</div>
<% end %>
65 66 67 68 69 70 71 72 73 74 |
# File 'app/helpers/admin/base_helper.rb', line 65 def generate_html(form_builder, method, = {}) [:object] ||= form_builder.object.class.reflect_on_association(method).klass.new [:partial] ||= method.to_s.singularize [:form_builder_local] ||= :f form_builder.fields_for(method, [:object], :child_index => 'NEW_RECORD') do |f| render(:partial => [:partial], :locals => { [:form_builder_local] => f }) end end |
#generate_template(form_builder, method, options = {}) ⇒ Object
76 77 78 |
# File 'app/helpers/admin/base_helper.rb', line 76 def generate_template(form_builder, method, = {}) escape_javascript generate_html(form_builder, method, ) end |
#get_additional_field_value(controller, field) ⇒ Object
You can add additional_fields to the product and variant models. See section 4.2 here: spreecommerce.com/documentation/extensions.html If you do choose to add additional_fields, you can utilize the :use parameter to set the input type for any such fields. For example, :use => ‘check_box’ In the event that you add this functionality, the following method takes care of rendering the proper input type and logic for the supported input-types, which are text_field, check_box, radio_button, and select.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'app/helpers/admin/base_helper.rb', line 35 def get_additional_field_value(controller, field) attribute = attribute_name_for(field[:name]) value = eval("@" + controller.controller_name.singularize + "." + attribute) if value.nil? && controller.controller_name == "variants" value = @variant.product.has_attribute?(attribute) ? @variant.product[attribute] : nil end if value.nil? return value else return field.key?(:format) ? sprintf(field[:format], value) : value end end |
#link_to_add_fields(name, append_to_selector, f, association) ⇒ Object
renders set of hidden fields and button to add new record using nested_attributes
176 177 178 179 180 181 182 |
# File 'app/helpers/admin/base_helper.rb', line 176 def link_to_add_fields(name, append_to_selector, f, association) new_object = f.object.class.reflect_on_association(association).klass.new fields = f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder| render(association.to_s.singularize + "_fields", :f => builder) end link_to_function(name, raw("add_fields(\"#{append_to_selector}\", \"#{association}\", \"#{escape_javascript(fields)}\")"), :class => 'add_fields') end |
#link_to_remove_fields(name, f) ⇒ Object
renders hidden field and link to remove record using nested_attributes
185 186 187 |
# File 'app/helpers/admin/base_helper.rb', line 185 def link_to_remove_fields(name, f) f.hidden_field(:_destroy) + link_to_with_icon(:delete, name, '#', :class => 'remove_fields') end |
#preference_field(form, field, options) ⇒ Object
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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'app/helpers/admin/base_helper.rb', line 87 def preference_field(form, field, ) case [:type] when :integer form.text_field(field, { :size => 10, :class => 'input_integer', :readonly => [:readonly], :disabled => [:disabled] } ) when :boolean form.check_box(field, {:readonly => [:readonly], :disabled => [:disabled]}) when :string form.text_field(field, { :size => 10, :class => 'input_string', :readonly => [:readonly], :disabled => [:disabled] } ) when :password form.password_field(field, { :size => 10, :class => 'password_string', :readonly => [:readonly], :disabled => [:disabled] } ) when :text form.text_area(field, {:rows => 15, :cols => 85, :readonly => [:readonly], :disabled => [:disabled]} ) else form.text_field(field, { :size => 10, :class => 'input_string', :readonly => [:readonly], :disabled => [:disabled] } ) end end |
#preference_fields(object, form) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'app/helpers/admin/base_helper.rb', line 132 def preference_fields(object, form) return unless object.respond_to?(:preferences) object.preferences.keys.map{ |key| next unless object.class.preference_definitions.has_key? key definition = object.class.preference_definitions[key] type = definition.instance_eval{@type}.to_sym form.label("preferred_#{key}", t(key)+": ") + preference_field(form, "preferred_#{key}", :type => type) }.join("<br />").html_safe end |
#product_picker_field(name, value) ⇒ Object
169 170 171 172 173 |
# File 'app/helpers/admin/base_helper.rb', line 169 def product_picker_field(name, value) products = Product.with_ids(value) product_names_hash = products.inject({}){|memo,item| memo[item.id] = item.name; memo} %(<input type="text" name="#{name}" value="#{value}" class="tokeninput products" data-names='#{product_names_hash.to_json}' />).html_safe end |
#remove_nested(fields) ⇒ Object
80 81 82 83 84 85 |
# File 'app/helpers/admin/base_helper.rb', line 80 def remove_nested(fields) out = '' out << fields.hidden_field(:_destroy) unless fields.object.new_record? out << (link_to icon("delete"), "#", :class => "remove") out.html_safe end |
#spree_date_picker(object, method, options = {}, html_options = {}) ⇒ Object
22 23 24 25 |
# File 'app/helpers/admin/base_helper.rb', line 22 def spree_date_picker(object, method, = {}, = {}) .merge!(:format => "y-m-d") unobtrusive_date_text_picker(object, method, , ) end |