Module: Admin::BaseHelper

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

Instance Method Summary collapse

Instance Method Details

#button(text, icon = nil, button_type = 'submit') ⇒ Object



23
24
25
# File 'app/helpers/admin/base_helper.rb', line 23

def button(text, icon = nil, button_type = 'submit')
  ('button', ('span', text), :type => button_type)
end


27
28
29
# File 'app/helpers/admin/base_helper.rb', line 27

def button_link_to(text, url, html_options = {})
  link_to(text_for_button_link(text, html_options), url, html_options_for_button_link(html_options))
end


31
32
33
# File 'app/helpers/admin/base_helper.rb', line 31

def button_link_to_function(text, function, html_options = {})
  link_to_function(text_for_button_link(text, html_options), function, html_options_for_button_link(html_options))
end


35
36
37
# File 'app/helpers/admin/base_helper.rb', line 35

def button_link_to_remote(text, options, html_options = {})
  link_to_remote(text_for_button_link(text, html_options), options, html_options_for_button_link(html_options))
end

#class_for_error(model, method) ⇒ Object



94
95
96
97
# File 'app/helpers/admin/base_helper.rb', line 94

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

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



86
87
88
89
90
91
92
# File 'app/helpers/admin/base_helper.rb', line 86

def field_container(model, method, options = {}, &block)
  unless error_message_on(model, method).blank?
    css_class = 'withError' 
  end
  html = ('p', capture(&block), :class => css_class)
  concat(html)
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 -%>


129
130
131
132
133
134
135
136
137
138
# File 'app/helpers/admin/base_helper.rb', line 129

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



140
141
142
# File 'app/helpers/admin/base_helper.rb', line 140

def generate_template(form_builder, method, options = {})
  escape_javascript generate_html(form_builder, method, options)
end

#get_additional_field_value(controller, field) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'app/helpers/admin/base_helper.rb', line 99

def get_additional_field_value(controller, field)  
  attribute = field[:name].gsub(" ", "_").downcase

  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


48
49
50
# File 'app/helpers/admin/base_helper.rb', line 48

def html_options_for_button_link(html_options)
  options = {:class => 'button'}.update(html_options)
end

#icon(icon_name) ⇒ Object



19
20
21
# File 'app/helpers/admin/base_helper.rb', line 19

def icon(icon_name)
  image_tag("/images/admin/icons/#{icon_name}.png")
end


11
12
13
# File 'app/helpers/admin/base_helper.rb', line 11

def link_to_delete(resource)
  link_to_with_icon('delete', t("delete"), object_url(resource), :confirm => t('are_you_sure'), :method => :delete )
end


7
8
9
# File 'app/helpers/admin/base_helper.rb', line 7

def link_to_edit(resource)
  link_to_with_icon('edit', t("edit"), edit_object_url(resource))
end


3
4
5
# File 'app/helpers/admin/base_helper.rb', line 3

def link_to_new(resource)
  link_to_with_icon('add', t("new"), edit_object_url(resource))
end


15
16
17
# File 'app/helpers/admin/base_helper.rb', line 15

def link_to_with_icon(icon_name, text, url, options = {})
  link_to(icon(icon_name) + ' ' + text, url, options.update(:class => 'iconlink'))
end

#remove_nested(fields) ⇒ Object



144
145
146
147
148
149
# File 'app/helpers/admin/base_helper.rb', line 144

def remove_nested(fields)
  out = ''
  out << fields.hidden_field(:_delete) unless fields.object.new_record?
  out << (link_to icon("delete"), "#", :class => "remove")
  out
end

#tab(*args) ⇒ Object

Make an admin tab that coveres one or more resources supplied by symbols Option hash may follow. Valid options are

* :label to override link text, otherwise based on the first resource name (translated)
* :route to override automatically determining the default route
* :match_path as an alternative way to control when the tab is active, /products would match /admin/products, /admin/products/5/variants etc.


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
# File 'app/helpers/admin/base_helper.rb', line 59

def tab(*args)
  options = {:label => args.first.to_s}
  if args.last.is_a?(Hash)
    options = options.merge(args.pop)
  end
  options[:route] ||=  "admin_#{args.first}"
  
  ## if more than one form, it'll capitalize all words
  label_with_first_letters_capitalized = t(options[:label]).gsub(/\b\w/){$&.upcase}
  link = link_to(label_with_first_letters_capitalized, send("#{options[:route]}_path"))
  
  css_classes = []

  selected = if options[:match_path]
    request.request_uri.starts_with?("/admin#{options[:match_path]}")
  else
    args.include?(controller.controller_name.to_sym)
  end
  css_classes << 'selected' if selected

  if options[:css_class]
    css_classes << options[:css_class]
  end
  ('li', link, :class => css_classes.join(' '))
end


39
40
41
42
43
44
45
46
# File 'app/helpers/admin/base_helper.rb', line 39

def text_for_button_link(text, html_options)
  s = ''
  if html_options[:icon]
    s << icon(html_options.delete(:icon)) + ' &nbsp; '
  end
  s << text
  ('span', s)
end