Module: Searchgasm::Helpers::Utilities

Defined in:
lib/searchgasm/helpers/utilities.rb

Instance Method Summary collapse

Instance Method Details

#searchgasm_params(options = {}) ⇒ Object

Builds a hash of params for creating a url and preserves any existing params. You can pass this into url_for and build your url. Although most rails helpers accept a hash.

Let’s take the page_link helper. Here is the code behind that helper:

link_to("Page 2", searchgasm_params(:search_params => {:page => 2}))

That’s pretty much it. So if you wanted to roll your own link to execute a search, go for it. It’s pretty simple. Pass conditions instead of the page, set how the search will be ordered, etc.

Be careful when taking this approach though. Searchgasm helps you out when you use form_for. For example, when you use the per_page_select helper, it adds in a hidden form field with the value of the page. So when your search form is submitted it searches the document for that element, finds the current value, which is the current per_page value, and includes that in the search. So when a user searches the per_page value stays consistent. If you use the searchgasm_params you are on your own. I am always curious how people are using searchgasm. So if you are building your own helpers contact me and maybe I can help you and add in a helper for you, making it an official feature.

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.



24
25
26
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
54
55
56
57
58
59
60
# File 'lib/searchgasm/helpers/utilities.rb', line 24

def searchgasm_params(options = {})
  add_searchgasm_defaults!(options)
  options[:search_params] ||= {}
  options[:literal_search_params] ||= {}
  
  options[:params] ||= {}
  params_copy = params.deep_dup.with_indifferent_access
  search_params = options[:params_scope].blank? ? params_copy : params_copy.delete(options[:params_scope])
  search_params ||= {}
  search_params = search_params.with_indifferent_access
  search_params.delete(:commit)
  search_params.delete(:page)
  search_params.deep_delete_duplicate_keys(options[:literal_search_params])
  search_params.deep_delete(options[:exclude_search_params])
  
  if options[:search_params]
    
    #raise params_copy.inspect if options[:search_params][:order_by] == :id
    search_params.deep_merge!(options[:search_params])
    
    if options[:search_params][:order_by] && !options[:search_params][:order_as]
      search_params[:order_as] = (searchgasm_ordering_by?(options[:search_params][:order_by], options) && options[:search_obj].asc?) ? "DESC" : "ASC" 
    end
    
    [:order_by, :priority_order_by].each { |base64_field| search_params[base64_field] = searchgasm_base64_value(search_params[base64_field]) if search_params.has_key?(base64_field) }
  end
  
  new_params = params_copy
  new_params.deep_merge!(options[:params])
  new_params.deep_delete(options[:exclude_params])
  
  if options[:params_scope].blank? || search_params.blank?
    new_params
  else
    new_params.merge(options[:params_scope] => search_params)
  end
end

#searchgasm_state(options = {}) ⇒ Object

When you set up a search form using form_for for remote_form_for searchgasm adds in some magic for you.

Take the instance where a user orders the data by something other than the default, and then does a search. The user would expect the search to remember what the user selected to order the data by, right? What searchgasm does is add in some hidden fields, somewhere in the page, the represent the searchgasm “state”. These are automatically added for you when you use the searchgasm helpers. Such as: page_links, page_link, order_by_link, per_page_select, etc. So if you are using those you do not need to worry about this helper.

If for some reason you do not use any of these you need to put the searchgasm state on your page somewhere. Somewhere where the state will always be up-to-date, which would be most likely be in the partial that renders your search results (assuming you are using AJAX). Otherwise when the user starts a new search, the state will be reset. Meaning the order_by, per_page, etc will all be reset.

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.



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/searchgasm/helpers/utilities.rb', line 108

def searchgasm_state(options = {})
  return "" if @added_searchgasm_state
  add_searchgasm_defaults!(options)
  html = ""
  (Search::Base::SPECIAL_FIND_OPTIONS - [:page, :priority_order]).each do |option|
    value = options[:search_obj].send(option)
    html += hidden_field(options[:params_scope], option, :value => (option == :order_by ? searchgasm_base64_value(value) : value))
  end
  @added_searchgasm_state = true
  html
end

#searchgasm_url(options = {}) ⇒ Object

Similar to searchgasm_hash, but instead returns a string url. The reason this exists is to assist in creating urls in javascript. It’s the muscle behind all of the select helpers that searchgasm provides. Take the instance where you want to do:

:onchange => "window.location = '#{url_for(searchgasm_params)}&my_param=' + this.value;"

Well the above obviously won’t work. Do you need to apped the url with a ? or a &? What about that tricky :params_scope? That’s where this is handy, beacuse it does all of the params string building for you. Check it out:

:onchange => "window.location = '" + searchgasm_url(:literal_search_params => {:per_page => "' + escape(this.value) + '"}) + "';"

or what about something a little more tricky?

:onchange => "window.location = '" + searchgasm_url(:literal_search_params => {:conditions => {:name_contains => "' + escape(this.value) + '"}}) + "';"

I have personally used this for an event calendar. Above the calendar there was a drop down for each month. Here is the code:

:onchange => "window.location = '" + searchgasm_url(:literal_search_params => {:conditions => {:occurs_at_after => "' + escape(this.value) + '"}}) + "';"

Now when the user changes the month in the drop down it just runs a new search that sets my conditions to occurs_at_after = selected month. Then in my controller I set occurs_at_before = occurs_at_after.at_end_of_month.

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.

  • :literal_search_params – default: nil, Additional search params to add to the url, but are not escaped. So you can add javascript into the URL: :literal_search_params => => “‘ + escape(this.value) + ’”

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



89
90
91
92
93
94
# File 'lib/searchgasm/helpers/utilities.rb', line 89

def searchgasm_url(options = {})
  search_params = searchgasm_params(options)
  url = url_for(search_params)
  literal_param_strings = literal_param_strings(options[:literal_search_params], options[:params_scope].blank? ? "" : "#{options[:params_scope]}")
  url += (url.last == "?" ? "" : (url.include?("?") ? "&" : "?")) + literal_param_strings.join("&")
end