Module: Spree::Admin::BaseHelper

Defined in:
app/helpers/spree/admin/base_helper.rb

Instance Method Summary collapse

Instance Method Details

#additional_field_for(controller, field) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'app/helpers/spree/admin/base_helper.rb', line 156

def additional_field_for(controller, field)
  field[:use] ||= 'text_field'
  options = 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, options, field[:checked_value] || 1, field[:unchecked_value] || 0)
  when 'radio_button'
    html = ''
    field[:value].call(controller, field).each do |value|
      html << radio_button(object_name, method, value, options)
      html << " #{value.to_s} "
    end
    html
  when 'select'
    select(object_name, method, field[:value].call(controller, field), options, field[:html_options] || {})
  else
    value = field[:value] ? field[:value].call(controller, field) : get_additional_field_value(controller, field)
    __send__(field[:use], object_name, method, options.merge(:value => value))
  end # case
end

#class_for_error(model, method) ⇒ Object



24
25
26
27
# File 'app/helpers/spree/admin/base_helper.rb', line 24

def class_for_error(model, method)
  if error_message_on :product, :name
  end
end

#error_message_on(object, method, options = {}) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
# File 'app/helpers/spree/admin/base_helper.rb', line 12

def error_message_on(object, method, options = {})
  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
    (:span, errors, :class => 'formError')
  else
    ''
  end
end

#field_container(model, method, options = {}, &block) ⇒ Object



4
5
6
7
8
9
10
# File 'app/helpers/spree/admin/base_helper.rb', line 4

def field_container(model, method, options = {}, &block)
  css_classes = options[:class].to_a
  if error_message_on(model, method).present?
    css_classes << 'withError'
  end
  ('p', capture(&block), :class => css_classes.join(' '), :id => "#{model}_#{method}_field")
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 %>


62
63
64
65
66
67
68
69
70
71
# File 'app/helpers/spree/admin/base_helper.rb', line 62

def generate_html(form_builder, method, options = {})
  options[:object] ||= form_builder.object.class.reflect_on_association(method).klass.new
  options[:partial] ||= method.to_s.singularize
  options[:form_builder_local] ||= :f

  form_builder.fields_for(method, options[:object], :child_index => 'NEW_RECORD') do |f|
    render(:partial => options[:partial], :locals => { options[:form_builder_local] => f })
  end

end

#generate_template(form_builder, method, options = {}) ⇒ Object



73
74
75
# File 'app/helpers/spree/admin/base_helper.rb', line 73

def generate_template(form_builder, method, options = {})
  escape_javascript generate_html(form_builder, method, options)
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.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/helpers/spree/admin/base_helper.rb', line 32

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


187
188
189
# File 'app/helpers/spree/admin/base_helper.rb', line 187

def link_to_add_fields(name, target)
  link_to icon('add') + name, 'javascript:', :data => { :target => target }, :class => "add_fields"
end

renders hidden field and link to remove record using nested_attributes



192
193
194
# File 'app/helpers/spree/admin/base_helper.rb', line 192

def link_to_remove_fields(name, f)
  f.hidden_field(:_destroy) + link_to_with_icon(:delete, name, '#', :class => 'remove_fields')
end

#preference_field_for(form, field, options) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'app/helpers/spree/admin/base_helper.rb', line 102

def preference_field_for(form, field, options)
  case options[:type]
  when :integer
    form.text_field(field, preference_field_options(options))
  when :boolean
    form.check_box(field, preference_field_options(options))
  when :string
    form.text_field(field, preference_field_options(options))
  when :password
    form.password_field(field, preference_field_options(options))
  when :text
    form.text_area(field, preference_field_options(options))
  else
    form.text_field(field, preference_field_options(options))
  end
end

#preference_field_options(options) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'app/helpers/spree/admin/base_helper.rb', line 119

def preference_field_options(options)
  field_options = case options[:type]
  when :integer
    { :size => 10,
      :class => 'input_integer' }
  when :boolean
    {}
  when :string
    { :size => 10,
      :class => 'input_string' }
  when :password
    { :size => 10,
      :class => 'password_string' }
  when :text
    { :rows => 15,
      :cols => 85 }
  else
    { :size => 10,
      :class => 'input_string' }
  end

  field_options.merge!({
    :readonly => options[:readonly],
    :disabled => options[:disabled]
  })
end

#preference_field_tag(name, value, options) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'app/helpers/spree/admin/base_helper.rb', line 84

def preference_field_tag(name, value, options)
  case options[:type]
  when :integer
    text_field_tag(name, value, preference_field_options(options))
  when :boolean
    hidden_field_tag(name, 0) +
    check_box_tag(name, 1, value, preference_field_options(options))
  when :string
    text_field_tag(name, value, preference_field_options(options))
  when :password
    password_field_tag(name, value, preference_field_options(options))
  when :text
    text_area_tag(name, value, preference_field_options(options))
  else
    text_field_tag(name, value, preference_field_options(options))
  end
end

#preference_fields(object, form) ⇒ Object



146
147
148
149
150
151
152
153
154
# File 'app/helpers/spree/admin/base_helper.rb', line 146

def preference_fields(object, form)
  return unless object.respond_to?(:preferences)
  object.preferences.keys.map{ |key|

    form.label("preferred_#{key}", t(key) + ": ") +
      preference_field_for(form, "preferred_#{key}", :type => object.preference_type(key))

  }.join("<br />").html_safe
end

#product_picker_field(name, value) ⇒ Object



180
181
182
183
184
185
# File 'app/helpers/spree/admin/base_helper.rb', line 180

def product_picker_field(name, value)
  products = Product.with_ids(value.split(','))
  product_names = products.inject({}){|memo,item| memo[item.id] = item.name; memo}
  product_rules = products.collect{ |p| { :id => p.id, :name => p.name } }
  %(<input type="text" name="#{name}" value="#{value}" class="tokeninput products" data-names='#{product_names.to_json}' data-pre='#{product_rules.to_json}'/>).html_safe
end

#remove_nested(fields) ⇒ Object



77
78
79
80
81
82
# File 'app/helpers/spree/admin/base_helper.rb', line 77

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_dom_id(record) ⇒ Object



196
197
198
# File 'app/helpers/spree/admin/base_helper.rb', line 196

def spree_dom_id(record)
  dom_id(record, 'spree')
end