Module: ScaffoldLogic::Helper
- Defined in:
- lib/scaffold_logic/helper.rb
Instance Method Summary collapse
-
#action?(expression) ⇒ Boolean
Returns true if the current request’s action matches the specified string or regular expression.
-
#admin_crud_links(model, actions) ⇒ Object
Display admin links for standard CRUD actions.
-
#controller?(expression) ⇒ Boolean
Returns true if the current request’s controller matches the specified string or regular expression.
-
#crud_links(model, actions) ⇒ Object
Display links for standard CRUD actions.
-
#crud_links_for_actions_and_paths(actions_and_paths) ⇒ Object
Display links for the specified hash of CRUD actions and paths.
- #meta_description(content = nil) ⇒ Object
- #meta_keywords(content = nil) ⇒ Object
-
#nav_link(url, link_text, id, options = {}) ⇒ Object
Returns a navigation link.
- #nav_link_with_dropdowns(url, link_text, id, options = {}, &proc) ⇒ Object
-
#sort_link(model, field, params, html_options = {}) ⇒ Object
Returns a link_to tag with sorting parameters that can be used with ActiveRecord.order_by.
-
#tab_for(link, link_title, tab_name, options = {}) ⇒ Object
Returns a navigation link.
Instance Method Details
#action?(expression) ⇒ Boolean
Returns true if the current request’s action matches the specified string or regular expression.
4 5 6 |
# File 'lib/scaffold_logic/helper.rb', line 4 def action?( expression ) !! ( expression.class == Regexp ? controller.action_name =~ expression : controller.action_name == expression ) end |
#admin_crud_links(model, actions) ⇒ Object
Display admin links for standard CRUD actions. Action symbols follow the convention in routes.rb. The controller must be namespaced as Admin::MyController.
Use in index views like this:
<%- @models.each do |model| -%>
<td class="crud_links"><%= admin_crud_links(model, [:destroy, :edit]) -%></td>
<%- end -%>
29 30 31 |
# File 'lib/scaffold_logic/helper.rb', line 29 def admin_crud_links(model, actions) crud_links_for_actions_and_paths actions.map{ |a| {a => eval((a == :destroy || a == :show) ? "admin_#{model.class.name.underscore}_path(model)" : "#{a.to_s}_admin_#{model.class.name.underscore}_path(model)") } }.inject({}) {|a, b| a.merge(b)} end |
#controller?(expression) ⇒ Boolean
Returns true if the current request’s controller matches the specified string or regular expression.
9 10 11 |
# File 'lib/scaffold_logic/helper.rb', line 9 def controller?( expression ) !! ( expression.class == Regexp ? controller.controller_name =~ expression : controller.controller_name == expression ) end |
#crud_links(model, actions) ⇒ Object
Display links for standard CRUD actions. Action symbols follow the convention in routes.rb.
Use in index views like this:
<%- @models.each do |model| -%>
<td class="crud_links"><%= crud_links(model, [:edit, :show]) -%></td>
<%- end -%>
40 41 42 |
# File 'lib/scaffold_logic/helper.rb', line 40 def crud_links(model, actions) crud_links_for_actions_and_paths actions.map{ |a| {a => eval((a == :destroy || a == :show) ? "#{model.class.name.underscore}_path(model)" : "#{a.to_s}_#{model.class.name.underscore}_path(model)") } }.inject({}) {|a, b| a.merge(b)} end |
#crud_links_for_actions_and_paths(actions_and_paths) ⇒ Object
Display links for the specified hash of CRUD actions and paths. Action symbols follow the convention in routes.rb.
Use in index views like this:
<td class=“crud_links”><%= crud_links_for_actions_and_paths(:show => model_path) -%></td>
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/scaffold_logic/helper.rb', line 50 def crud_links_for_actions_and_paths(actions_and_paths) _html = '' [:destroy, :edit, :show].each do |action| next unless actions_and_paths.key?(action) if action == :show _html << link_to(image_tag('icons/view.png', :class => 'crud_icon', :width => 14, :height => 14), actions_and_paths[action], :title => 'View') elsif action == :edit _html << link_to(image_tag('icons/edit.png', :class => 'crud_icon', :width => 14, :height => 14), actions_and_paths[action], :title => 'Edit') elsif action == :destroy _html << ('', actions_and_paths[action], :class => 'delete_crud_icon', :confirm => 'Are you sure? This action cannot be undone.', :method => :delete) end end _html.html_safe end |
#meta_description(content = nil) ⇒ Object
13 14 15 |
# File 'lib/scaffold_logic/helper.rb', line 13 def (content=nil) content_for(:meta_description) { content } unless content.blank? end |
#meta_keywords(content = nil) ⇒ Object
17 18 19 |
# File 'lib/scaffold_logic/helper.rb', line 17 def (content=nil) content_for(:meta_keywords) { content } unless content.blank? end |
#nav_link(url, link_text, id, options = {}) ⇒ Object
Returns a navigation link.
Usage:
<ul>
<%= nav_link( root_path, 'Home', 'home' ) -%>
</ul>
74 75 76 77 78 79 80 |
# File 'lib/scaffold_logic/helper.rb', line 74 def nav_link(url, link_text, id, = {}) %{ <li id="#{id}" class="#{@active_tab == id || [:active] ? 'here' : ''}" onclick="javascript:window.location='#{url}';"> <a href="#{url}" title="#{link_text}">#{link_text}</a> </li> }.html_safe end |
#nav_link_with_dropdowns(url, link_text, id, options = {}, &proc) ⇒ Object
82 83 84 85 86 87 |
# File 'lib/scaffold_logic/helper.rb', line 82 def nav_link_with_dropdowns(url, link_text, id, = {}, &proc) concat %{<li id="#{id}" class="#{@active_tab == id || [:active] ? 'here' : ''}" onclick="javascript:window.location='#{url}';"><a href="#{url}" title="#{link_text}">#{link_text}</a>}.html_safe yield concat %{</li>}.html_safe return '' end |
#sort_link(model, field, params, html_options = {}) ⇒ Object
Returns a link_to tag with sorting parameters that can be used with ActiveRecord.order_by.
To use standard resources, specify the resources as a plural symbol:
sort_link(:users, 'email', params)
To use resources aliased with :as (in routes.rb), specify the aliased route as a string.
sort_link('users_admin', 'email', params)
You can override the link’s label by adding a labels hash to your params in the controller:
params[:labels] = {'user_id' => 'User'}
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 |
# File 'lib/scaffold_logic/helper.rb', line 99 def sort_link(model, field, params, ={}) if (field.to_sym == params[:by] || field == params[:by]) && params[:dir] == "asc" classname = "arrow-asc" dir = "desc" elsif (field.to_sym == params[:by] || field == params[:by]) classname = "arrow-desc" dir = "asc" else dir = "asc" end = { :anchor => [:anchor], :by => field, :dir => dir, :search => params[:search], :category => params[:category], :show => params[:show] } [:show] = params[:show] unless params[:show].blank? || params[:show] == 'all' [:class] = [classname, [:class]].compact * ' ' if classname field_name = params[:labels] && params[:labels][field] ? params[:labels][field] : field.titleize [:title] ||= "Sort by #{field_name}" _link = model.is_a?(Symbol) ? eval("#{model}_url(options)") : "/#{model}?#{.to_params}" link_to(field_name, _link, ) end |
#tab_for(link, link_title, tab_name, options = {}) ⇒ Object
Returns a navigation link. DEPRECATED: Please use nav_link
instead.
131 132 133 |
# File 'lib/scaffold_logic/helper.rb', line 131 def tab_for(link, link_title, tab_name, = {}) nav_link link, link_title, tab_name, end |