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, options = {}) ⇒ Object
- #link_to_edit(resource) ⇒ Object
- #link_to_new(resource) ⇒ Object
- #link_to_with_icon(icon_name, text, url, options = {}) ⇒ Object
- #preference_field(form, field, options) ⇒ Object
- #preference_fields(object, form) ⇒ 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
38 39 40 |
# File 'app/helpers/admin/base_helper.rb', line 38 def (text, icon = nil, = 'submit') content_tag('button', content_tag('span', text), :type => ) end |
#button_link_to(text, url, html_options = {}) ⇒ Object
42 43 44 |
# File 'app/helpers/admin/base_helper.rb', line 42 def (text, url, = {}) link_to((text, ), url, ()) end |
#button_link_to_function(text, function, html_options = {}) ⇒ Object
46 47 48 |
# File 'app/helpers/admin/base_helper.rb', line 46 def (text, function, = {}) link_to_function((text, ), function, ()) end |
#button_link_to_remote(text, options, html_options = {}) ⇒ Object
50 51 52 |
# File 'app/helpers/admin/base_helper.rb', line 50 def (text, , = {}) link_to_remote((text, ), , ()) end |
#class_for_error(model, method) ⇒ Object
113 114 115 116 |
# File 'app/helpers/admin/base_helper.rb', line 113 def class_for_error(model, method) if :product, :name end end |
#field_container(model, method, options = {}, &block) ⇒ Object
105 106 107 108 109 110 111 |
# File 'app/helpers/admin/base_helper.rb', line 105 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 -%>
148 149 150 151 152 153 154 155 156 157 |
# File 'app/helpers/admin/base_helper.rb', line 148 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
159 160 161 |
# File 'app/helpers/admin/base_helper.rb', line 159 def generate_template(form_builder, method, = {}) escape_javascript generate_html(form_builder, method, ) end |
#get_additional_field_value(controller, field) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'app/helpers/admin/base_helper.rb', line 118 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
63 64 65 |
# File 'app/helpers/admin/base_helper.rb', line 63 def () = {:class => 'button'}.update() end |
#icon(icon_name) ⇒ Object
34 35 36 |
# File 'app/helpers/admin/base_helper.rb', line 34 def icon(icon_name) image_tag("/images/admin/icons/#{icon_name}.png") end |
#link_to_delete(resource, options = {}) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'app/helpers/admin/base_helper.rb', line 10 def link_to_delete(resource, = {}) .assert_valid_keys(:url, :caption, :title) .reverse_merge! :url => object_url(resource) unless .key? :url .reverse_merge! :caption => t('are_you_sure') .reverse_merge! :title => t('confirm_delete') #link_to_with_icon('delete', t("delete"), object_url(resource), :confirm => t('are_you_sure'), :method => :delete ) link_to_function icon("delete") + ' ' + t("delete"), "jConfirm('#{[:caption]}', '#{[:title]}', function(r) { if(r){ jQuery.ajax({ type: 'POST', url: '#{[:url]}', data: ({_method: 'delete', authenticity_token: AUTH_TOKEN}), success: function(r){ jQuery('##{dom_id resource}').fadeOut('hide'); } }); } });" end |
#link_to_edit(resource) ⇒ Object
6 7 8 |
# File 'app/helpers/admin/base_helper.rb', line 6 def link_to_edit(resource) link_to_with_icon('edit', t("edit"), edit_object_url(resource)) end |
#link_to_new(resource) ⇒ Object
2 3 4 |
# File 'app/helpers/admin/base_helper.rb', line 2 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
30 31 32 |
# File 'app/helpers/admin/base_helper.rb', line 30 def link_to_with_icon(icon_name, text, url, = {}) link_to(icon(icon_name) + ' ' + text, url, .update(:class => 'iconlink')) end |
#preference_field(form, field, options) ⇒ Object
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'app/helpers/admin/base_helper.rb', line 170 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 :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
207 208 209 210 211 212 213 214 215 216 |
# File 'app/helpers/admin/base_helper.rb', line 207 def preference_fields(object, form) return unless object.respond_to?(:preferences) object.preferences.keys.map{ |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 />") end |
#remove_nested(fields) ⇒ Object
163 164 165 166 167 168 |
# File 'app/helpers/admin/base_helper.rb', line 163 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.
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 |
# File 'app/helpers/admin/base_helper.rb', line 74 def tab(*args) = {:label => args.first.to_s} if args.last.is_a?(Hash) = .merge(args.pop) end [:route] ||= "admin_#{args.first}" destination_url = send("#{[:route]}_path") return("") unless (ActionController::Routing::Routes.recognize_path(destination_url)) ## 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, destination_url) 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
54 55 56 57 58 59 60 61 |
# File 'app/helpers/admin/base_helper.rb', line 54 def (text, ) s = '' if [:icon] s << icon(.delete(:icon)) + ' ' end s << text content_tag('span', s) end |