Class: ActionView::Base
- Inherits:
-
Object
- Object
- ActionView::Base
- Defined in:
- lib/searchlogic/rails_helpers.rb
Instance Method Summary collapse
-
#fields_for_with_searchlogic(*args, &block) ⇒ Object
Automatically adds an “order” hidden field in your form to preserve how the data is being ordered.
-
#form_for_with_searchlogic(*args, &block) ⇒ Object
Automatically makes the form method :get if a Searchlogic::Search and sets the params scope to :search.
-
#order(search, options = {}, html_options = {}) ⇒ Object
Creates a link that alternates between acending and descending.
Instance Method Details
#fields_for_with_searchlogic(*args, &block) ⇒ Object
Automatically adds an “order” hidden field in your form to preserve how the data is being ordered.
71 72 73 74 75 76 77 |
# File 'lib/searchlogic/rails_helpers.rb', line 71 def fields_for_with_searchlogic(*args, &block) if search_obj = args.find { |arg| arg.is_a?(Searchlogic::Search) } args.unshift(:search) if args.first == search_obj concat(content_tag("div", hidden_field_tag("#{args.first}[order]", search_obj.order)) + "\n") end fields_for_without_searchlogic(*args, &block) end |
#form_for_with_searchlogic(*args, &block) ⇒ Object
Automatically makes the form method :get if a Searchlogic::Search and sets the params scope to :search
57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/searchlogic/rails_helpers.rb', line 57 def form_for_with_searchlogic(*args, &block) if search_obj = args.find { |arg| arg.is_a?(Searchlogic::Search) } = args. [:html] ||= {} [:html][:method] ||= :get [:url] ||= url_for args.unshift(:search) if args.first == search_obj args << end form_for_without_searchlogic(*args, &block) end |
#order(search, options = {}, html_options = {}) ⇒ Object
Creates a link that alternates between acending and descending. It basically alternates between calling 2 named scopes: “ascend_by_*” and “descend_by_*”
By default Searchlogic gives you these named scopes for all of your columns, but if you wanted to create your own, it will work with those too.
Examples:
order @search, :by => :username
order @search, :by => :created_at, :as => "Created"
order(@search, {:by => :last_login_at, :as => 'Login time', :arrow => true}, {:class => 'order'})
This helper accepts the following options:
-
:by
- the name of the named scope. This helper will prepend this value with “ascend_by_” and “descend_by_” -
:as
- the text used in the link, defaults to whatever is passed to :by -
:ascend_scope
- what scope to call for ascending the data, defaults to “ascend_by_:by” -
:descend_scope
- what scope to call for descending the data, defaults to “descend_by_:by” -
:params
- hash with additional params which will be added to generated url -
:params_scope
- the name of the params key to scope the order condition by, defaults to :search -
:default_scope
- either :asc or :desc, defaults to ascend_scope -
:arrow
- set to true if you want an ascii arrow showing the ordering -
:css_classes
- Array of what css classes you want to add, defaults to [:asc, :desc]. For old behaviour use [:ascending, :descending] here
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/searchlogic/rails_helpers.rb', line 27 def order(search, = {}, = {}) [:params_scope] ||= :search [:as] ||= [:by].to_s.humanize [:ascend_scope] ||= "ascend_by_#{[:by]}" [:descend_scope] ||= "descend_by_#{[:by]}" [:css_classes] ||= [:asc, :desc] selected = [[:ascend_scope], [:descend_scope]].include?(search.order.to_s) if selected ascending = search.order.to_s == [:ascend_scope] new_scope = ascending ? [:descend_scope] : [:ascend_scope] css_classes = [:class] ? [:class].split(" ") : [] if ascending [:as] = "▲ #{[:as]}" if [:arrow] css_classes << [:css_classes].first else [:as] = "▼ #{[:as]}" if [:arrow] css_classes << [:css_classes].last end [:class] = css_classes.join(" ") else new_scope = ([:default_scope] == :desc) ? [:descend_scope] : [:ascend_scope] end = { [:params_scope] => search.conditions.merge( { :order => new_scope } ) }.deep_merge([:params] || {}) link_to [:as], url_for(), end |