Module: Searchgasm::Helpers::ControlTypes::Link

Defined in:
lib/searchgasm/helpers/control_types/link.rb

Overview

Link Control Types

These helpers make ordering and paginating your data a breeze in your view. They only produce links.

Instance Method Summary collapse

Instance Method Details

Creates a link for ascending or descending data, pretty self e

Example uses

order_as_link("asc")
order_as_link("desc")
order_as_link("asc", :text => "Ascending", :html => {:class => "ascending"})

Options

  • :text – default: column_name.to_s.humanize, text for the link

  • :html – html arrtributes for the <a> tag.

Advanced Options

  • :params_scope – default: :search, this is the scope in which your search params will be preserved (params). If you don’t want a scope and want your options to be at base leve in params such as params, params, etc, then set this to nil.

  • :search_obj – default: @#params_scope, this is your search object, everything revolves around this. It will try to infer the name from your params_scope. If your params_scope is :search it will try to get @search, etc. If it can not be inferred by this, you need to pass the object itself.

  • :params – default: nil, Additional params to add to the url, must be a hash

  • :exclude_params – default: nil, params you want to exclude. This is nifty because it does a “deep delete”. So you can pass => {:param2 => :param3} and it will make sure param3 does not get include. param1 and param2 will not be touched. This also accepts an array or just a symbol or string.

  • :search_params – default: nil, Additional search params to add to the url, must be a hash. Adds the options into the :params_scope.

  • :exclude_search_params – default: nil, Same as :exclude_params but for the :search_params.



120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/searchgasm/helpers/control_types/link.rb', line 120

def order_as_link(order_as, options = {})
  add_order_as_link_defaults!(order_as, options)
  html = searchgasm_state_for(:order_as, options)
  
  if !options[:is_remote]
    html += link_to(options[:text], options[:url], options[:html])
  else
    html += link_to_remote(options[:text], options[:remote].merge(:url => options[:url]), options[:html])
  end
  
  html
end

Creates a link for ordering data by a column or columns

Example uses for a User class that has many orders

order_by_link(:first_name)
order_by_link([:first_name, :last_name])
order_by_link({:orders => :total})
order_by_link([{:orders => :total}, :first_name])
order_by_link(:id, :text => "Order Number", :html => {:class => "order_number"})

What’s nifty about this is that the value gets “serialized”, if it is not a string or a symbol, so that it can be passed via a param in the url. Searchgasm will automatically try to “unserializes” this value and use it. This allows you to pass complex objects besides strings and symbols, such as arrays and hashes. All of the hard work is done for you.

Another thing to keep in mind is that this will alternate between “asc” and “desc” each time it is clicked.

Options

  • :text – default: column_name.to_s.humanize, text for the link

  • :desc_indicator – default: &nbsp;&#9660;, the indicator that this column is descending

  • :asc_indicator – default: &nbsp;&#9650;, the indicator that this column is ascending

  • :html – html arrtributes for the <a> tag.

Advanced Options

  • :params_scope – default: :search, this is the scope in which your search params will be preserved (params). If you don’t want a scope and want your options to be at base leve in params such as params, params, etc, then set this to nil.

  • :search_obj – default: @#params_scope, this is your search object, everything revolves around this. It will try to infer the name from your params_scope. If your params_scope is :search it will try to get @search, etc. If it can not be inferred by this, you need to pass the object itself.

  • :params – default: nil, Additional params to add to the url, must be a hash

  • :exclude_params – default: nil, params you want to exclude. This is nifty because it does a “deep delete”. So you can pass => {:param2 => :param3} and it will make sure param3 does not get include. param1 and param2 will not be touched. This also accepts an array or just a symbol or string.

  • :search_params – default: nil, Additional search params to add to the url, must be a hash. Adds the options into the :params_scope.

  • :exclude_search_params – default: nil, Same as :exclude_params but for the :search_params.



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/searchgasm/helpers/control_types/link.rb', line 87

def order_by_link(order_by, options = {})
  order_by = deep_stringify(order_by)
  add_order_by_link_defaults!(order_by, options)
  html = searchgasm_state_for(:order_by, options) + searchgasm_state_for(:order_as, options)
  
  if !options[:is_remote]
    html += link_to(options[:text], options[:url], options[:html])
  else
    html += link_to_remote(options[:text], options[:remote].merge(:url => options[:url]), options[:html])
  end
  
  html
end

Creates a link for changing to a sepcific page of your data

Example uses

page_link(2)
page_link(1)
page_link(5, :text => "Fifth page", :html => {:class => "fifth_page"})

Options

  • :text – default: column_name.to_s.humanize, text for the link

  • :html – html arrtributes for the <a> tag.

Advanced Options

  • :params_scope – default: :search, this is the scope in which your search params will be preserved (params). If you don’t want a scope and want your options to be at base leve in params such as params, params, etc, then set this to nil.

  • :search_obj – default: @#params_scope, this is your search object, everything revolves around this. It will try to infer the name from your params_scope. If your params_scope is :search it will try to get @search, etc. If it can not be inferred by this, you need to pass the object itself.

  • :params – default: nil, Additional params to add to the url, must be a hash

  • :exclude_params – default: nil, params you want to exclude. This is nifty because it does a “deep delete”. So you can pass => {:param2 => :param3} and it will make sure param3 does not get include. param1 and param2 will not be touched. This also accepts an array or just a symbol or string.

  • :search_params – default: nil, Additional search params to add to the url, must be a hash. Adds the options into the :params_scope.

  • :exclude_search_params – default: nil, Same as :exclude_params but for the :search_params.



228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/searchgasm/helpers/control_types/link.rb', line 228

def page_link(page, options = {})
  add_page_link_defaults!(page, options)
  html = searchgasm_state_for(:page, options)
  
  if !options[:is_remote]
    html += link_to(options[:text], options[:url], options[:html])
  else
    html += link_to_remote(options[:text], options[:remote].merge(:url => options[:url]), options[:html])
  end
  
  html
end

Creates a link for limiting how many items are on each page

Example uses

per_page_link(200)
per_page_link(nil) # => Show all
per_page_link(nil, :text => "All", :html => {:class => "show_all"})

As you can see above, passing nil means “show all” and the text will automatically revert to “show all”

Options

  • :text – default: column_name.to_s.humanize, text for the link

  • :html – html arrtributes for the <a> tag.

Advanced Options

  • :params_scope – default: :search, this is the scope in which your search params will be preserved (params). If you don’t want a scope and want your options to be at base leve in params such as params, params, etc, then set this to nil.

  • :search_obj – default: @#params_scope, this is your search object, everything revolves around this. It will try to infer the name from your params_scope. If your params_scope is :search it will try to get @search, etc. If it can not be inferred by this, you need to pass the object itself.

  • :params – default: nil, Additional params to add to the url, must be a hash

  • :exclude_params – default: nil, params you want to exclude. This is nifty because it does a “deep delete”. So you can pass => {:param2 => :param3} and it will make sure param3 does not get include. param1 and param2 will not be touched. This also accepts an array or just a symbol or string.

  • :search_params – default: nil, Additional search params to add to the url, must be a hash. Adds the options into the :params_scope.

  • :exclude_search_params – default: nil, Same as :exclude_params but for the :search_params.



196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/searchgasm/helpers/control_types/link.rb', line 196

def per_page_link(per_page, options = {})
  add_per_page_link_defaults!(per_page, options)
  html = searchgasm_state_for(:per_page, options)
  
  if !options[:is_remote]
    html += link_to(options[:text], options[:url], options[:html])
  else
    html += link_to_remote(options[:text], options[:remote].merge(:url => options[:url]), options[:html])
  end
  
  html
end

This is similar to order_by_link but with a small difference. The best way to explain priority ordering is with an example. Let’s say you wanted to list products on a page. You have “featured” products that you want to show up first, no matter what. This is what this is all about. It makes ordering by featured products a priority, then searching by price, quantity, etc. is the same as it has always been.

The difference between order_by_link and priority_order_by_link is that priority_order_by_link it just a switch. Turn it on or turn it off. You don’t neccessarily want to flip between ASC and DESC. If you do then you should just incorporate this into your regular order_by, like: order_by_link [:featured, :price]

Example uses for a User class that has many orders

priority_order_by_link(:featured, "DESC")
order_by_link([:featured, :created_at], "ASC")
order_by_link({:orders => :featured}, "ASC")
order_by_link([{:orders => :featured}, :featured], "ASC")
order_by_link(:featured, "ASC", :text => "Featured", :html => {:class => "featured_link"})

Options

  • :activate_text – default: “Show #Searchgasm::Helpers::ControlTypes::Link.column_namecolumn_name.to_scolumn_name.to_s.humanize first”

  • :deactivate_text – default: “Don’t show #Searchgasm::Helpers::ControlTypes::Link.column_namecolumn_name.to_scolumn_name.to_s.humanize first”, text for the link, text for the link

  • :column_name – default: column_name.to_s.humanize, automatically inferred by what you are ordering by and is added into the active_text and deactive_text strings.

  • :text – default: :activate_text or :deactivate_text depending on if its active or not, Overwriting this will make this text stay the same, no matter way. A good alternative would be “Toggle featured first”

  • :html – html arrtributes for the <a> tag.

Advanced Options

  • :params_scope – default: :search, this is the scope in which your search params will be preserved (params). If you don’t want a scope and want your options to be at base leve in params such as params, params, etc, then set this to nil.

  • :search_obj – default: @#params_scope, this is your search object, everything revolves around this. It will try to infer the name from your params_scope. If your params_scope is :search it will try to get @search, etc. If it can not be inferred by this, you need to pass the object itself.

  • :params – default: nil, Additional params to add to the url, must be a hash

  • :exclude_params – default: nil, params you want to exclude. This is nifty because it does a “deep delete”. So you can pass => {:param2 => :param3} and it will make sure param3 does not get include. param1 and param2 will not be touched. This also accepts an array or just a symbol or string.

  • :search_params – default: nil, Additional search params to add to the url, must be a hash. Adds the options into the :params_scope.

  • :exclude_search_params – default: nil, Same as :exclude_params but for the :search_params.



161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/searchgasm/helpers/control_types/link.rb', line 161

def priority_order_by_link(priority_order_by, priority_order_as, options = {})
  priority_order_by = deep_stringify(priority_order_by)
  add_priority_order_by_link_defaults!(priority_order_by, priority_order_as, options)
  html = searchgasm_state_for(:priority_order_by, options) + searchgasm_state_for(:priority_order_as, options)
  
  if !options[:is_remote]
    html += link_to(options[:text], options[:url], options[:html])
  else
    html += link_to_remote(options[:text], options[:remote].merge(:url => options[:url]), options[:html])
  end
  
  html
end