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
-
#order_as_link(order_as, options = {}) ⇒ Object
Creates a link for ascending or descending data, pretty self e.
-
#order_by_link(order_by, options = {}) ⇒ Object
Creates a link for ordering data by a column or columns.
-
#page_link(page, options = {}) ⇒ Object
Creates a link for changing to a sepcific page of your data.
-
#per_page_link(per_page, options = {}) ⇒ Object
Creates a link for limiting how many items are on each page.
Instance Method Details
#order_as_link(order_as, options = {}) ⇒ Object
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.
61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/searchgasm/helpers/control_types/link.rb', line 61 def order_as_link(order_as, = {}) add_order_as_link_defaults!(order_as, ) html = searchgasm_state_for(:order_as, ) if ![:is_remote] html += link_to([:text], [:url], [:html]) else html += link_to_remote([:text], [:remote].merge(:url => [:url]), [:html]) end html end |
#order_by_link(order_by, options = {}) ⇒ Object
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: ▼, the indicator that this column is descending -
:asc_indicator
– default: ▲, 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.
32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/searchgasm/helpers/control_types/link.rb', line 32 def order_by_link(order_by, = {}) order_by = deep_stringify(order_by) add_order_by_link_defaults!(order_by, ) html = searchgasm_state_for(:order_by, ) + searchgasm_state_for(:order_as, ) if ![:is_remote] html += link_to([:text], [:url], [:html]) else html += link_to_remote([:text], [:remote].merge(:url => [:url]), [:html]) end html end |
#page_link(page, options = {}) ⇒ Object
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.
119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/searchgasm/helpers/control_types/link.rb', line 119 def page_link(page, = {}) add_page_link_defaults!(page, ) html = searchgasm_state_for(:page, ) if ![:is_remote] html += link_to([:text], [:url], [:html]) else html += link_to_remote([:text], [:remote].merge(:url => [:url]), [:html]) end html end |
#per_page_link(per_page, options = {}) ⇒ Object
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.
91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/searchgasm/helpers/control_types/link.rb', line 91 def per_page_link(per_page, = {}) add_per_page_link_defaults!(per_page, ) html = searchgasm_state_for(:per_page, ) if ![:is_remote] html += link_to([:text], [:url], [:html]) else html += link_to_remote([:text], [:remote].merge(:url => [:url]), [:html]) end html end |