Module: Admin::BaseHelper
- Defined in:
- app/helpers/admin/base_helper.rb
Instance Method Summary collapse
- #button(text, icon = nil, button_type = 'submit') ⇒ Object
- #button_link_to(text, url, html_options = {}) ⇒ Object
- #button_link_to_function(text, function, html_options = {}) ⇒ Object
- #button_link_to_remote(text, options, html_options = {}) ⇒ Object
- #class_for_error(model, method) ⇒ 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
- #html_options_for_button_link(html_options) ⇒ Object
- #icon(icon_name) ⇒ Object
- #link_to_delete(resource) ⇒ Object
- #link_to_edit(resource) ⇒ Object
- #link_to_new(resource) ⇒ Object
- #link_to_with_icon(icon_name, text, url, options = {}) ⇒ Object
- #remove_nested(fields) ⇒ Object
-
#tab(*args) ⇒ Object
Make an admin tab that coveres one or more resources supplied by symbols Option hash may follow.
- #text_for_button_link(text, html_options) ⇒ Object
Instance Method Details
#button(text, icon = nil, button_type = 'submit') ⇒ Object
23 24 25 |
# File 'app/helpers/admin/base_helper.rb', line 23 def (text, icon = nil, = 'submit') content_tag('button', content_tag('span', text), :type => ) end |
#button_link_to(text, url, html_options = {}) ⇒ Object
27 28 29 |
# File 'app/helpers/admin/base_helper.rb', line 27 def (text, url, = {}) link_to((text, ), url, ()) end |
#button_link_to_function(text, function, html_options = {}) ⇒ Object
31 32 33 |
# File 'app/helpers/admin/base_helper.rb', line 31 def (text, function, = {}) link_to_function((text, ), function, ()) end |
#button_link_to_remote(text, options, html_options = {}) ⇒ Object
35 36 37 |
# File 'app/helpers/admin/base_helper.rb', line 35 def (text, , = {}) link_to_remote((text, ), , ()) 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 :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, = {}, &block) unless (model, method).blank? css_class = 'withError' end html = content_tag('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, = {}) [: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
140 141 142 |
# File 'app/helpers/admin/base_helper.rb', line 140 def generate_template(form_builder, method, = {}) escape_javascript generate_html(form_builder, method, ) 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 |
#html_options_for_button_link(html_options) ⇒ Object
48 49 50 |
# File 'app/helpers/admin/base_helper.rb', line 48 def () = {:class => 'button'}.update() 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 |
#link_to_delete(resource) ⇒ Object
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 |
#link_to_edit(resource) ⇒ Object
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 |
#link_to_new(resource) ⇒ Object
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 |
#link_to_with_icon(icon_name, text, url, options = {}) ⇒ Object
15 16 17 |
# File 'app/helpers/admin/base_helper.rb', line 15 def link_to_with_icon(icon_name, text, url, = {}) link_to(icon(icon_name) + ' ' + text, url, .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) = {:label => args.first.to_s} if args.last.is_a?(Hash) = .merge(args.pop) end [:route] ||= "admin_#{args.first}" ## if more than one form, it'll capitalize all words label_with_first_letters_capitalized = t([:label]).gsub(/\b\w/){$&.upcase} link = link_to(label_with_first_letters_capitalized, send("#{[:route]}_path")) css_classes = [] selected = if [:match_path] request.request_uri.starts_with?("/admin#{[:match_path]}") else args.include?(controller.controller_name.to_sym) end css_classes << 'selected' if selected if [:css_class] css_classes << [:css_class] end content_tag('li', link, :class => css_classes.join(' ')) end |
#text_for_button_link(text, html_options) ⇒ Object
39 40 41 42 43 44 45 46 |
# File 'app/helpers/admin/base_helper.rb', line 39 def (text, ) s = '' if [:icon] s << icon(.delete(:icon)) + ' ' end s << text content_tag('span', s) end |