mirror of
https://github.com/activerecord-hackery/ransack.git
synced 2022-11-09 13:47:45 -05:00
Added custom search param key feature refs #43
Including specs. I didn't find any other sane place where a behaviour should be changed. I don't understand why you define :q in Ransack::Naming::Name so I ignored it. Still seems to work pretty good in sort_link helper. Enabled global search_key configuration refs #81 You can configure new default search key parameter (default is still :q). Specs are included as well. New functionality is documented in README.md I also removed my specifis lines from .gitignore.
This commit is contained in:
parent
4accc86d49
commit
9a71cbf8b8
5 changed files with 52 additions and 2 deletions
23
README.md
23
README.md
|
@ -102,6 +102,29 @@ Once you've done so, you can make use of the helpers in Ransack::Helpers::FormBu
|
|||
construct much more complex search forms, such as the one on the
|
||||
[demo page](http://ransack-demo.heroku.com).
|
||||
|
||||
## Configuration
|
||||
|
||||
You can configure ransack. Best place where to put configuration is initializer. Create a file
|
||||
in config/initializers/ransack.rb with this content.
|
||||
|
||||
Ransack.configure do |config|
|
||||
config.set_search_key_name(:query) # Changes default search parameter key name
|
||||
# default is :q, this can be however overridden on
|
||||
# ransack search creation.
|
||||
end
|
||||
|
||||
## Custom search parameter key changing
|
||||
|
||||
Sometimes there are situations when you cannot use default search parameter name. E.g. you have
|
||||
two searches on one page. You can force another name using :search_key options. You must set
|
||||
it when creating a new search object.
|
||||
|
||||
# in controller:
|
||||
@search = Log.search(params[:log_search, :search_key => :log_search)
|
||||
|
||||
# in view:
|
||||
sort_link(@search) # this will read :search_key from @search object
|
||||
|
||||
**more docs to come**
|
||||
|
||||
## Contributions
|
||||
|
|
|
@ -4,8 +4,11 @@ require 'ransack/predicate'
|
|||
module Ransack
|
||||
module Configuration
|
||||
|
||||
mattr_accessor :predicates
|
||||
mattr_accessor :predicates, :options
|
||||
self.predicates = {}
|
||||
self.options = {
|
||||
:search_key => :q
|
||||
}
|
||||
|
||||
def configure
|
||||
yield self
|
||||
|
@ -31,5 +34,10 @@ module Ransack
|
|||
end if compounds
|
||||
end
|
||||
|
||||
# default search_key that, it can be overridden on sort_link level
|
||||
def set_search_key_name(name)
|
||||
self.options[:search_key] = name
|
||||
end
|
||||
|
||||
end
|
||||
end
|
|
@ -32,7 +32,7 @@ module Ransack
|
|||
@klass = @object.klass
|
||||
@join_dependency = join_dependency(@object)
|
||||
@join_type = options[:join_type] || Arel::OuterJoin
|
||||
@search_key = options[:search_key] || :q
|
||||
@search_key = options[:search_key] || Ransack.options[:search_key]
|
||||
@base = @join_dependency.join_base
|
||||
@engine = @base.arel_engine
|
||||
@default_table = Arel::Table.new(@base.table_name, :as => @base.aliased_table_name, :engine => @engine)
|
||||
|
|
|
@ -59,6 +59,7 @@ module Ransack
|
|||
query_hash = {}
|
||||
query_hash[search.context.search_key] = search_params.merge(:s => "#{attr_name} #{new_dir}")
|
||||
options.merge!(query_hash)
|
||||
|
||||
url = if routing_proxy
|
||||
send(routing_proxy).url_for(options)
|
||||
else
|
||||
|
|
|
@ -27,5 +27,23 @@ module Ransack
|
|||
Ransack.predicates.should_not have_key 'test_predicate_without_compound_any'
|
||||
Ransack.predicates.should_not have_key 'test_predicate_without_compound_all'
|
||||
end
|
||||
|
||||
it 'should have default value for search key' do
|
||||
Ransack.options[:search_key].should eq :q
|
||||
end
|
||||
|
||||
it 'changes default search key parameter' do
|
||||
# store original state so we can restore it later
|
||||
before = Ransack.options.clone
|
||||
|
||||
Ransack.configure do |config|
|
||||
config.set_search_key_name(:query)
|
||||
end
|
||||
|
||||
Ransack.options[:search_key].should eq :query
|
||||
|
||||
# restore original state so we don't break other tests
|
||||
Ransack.options = before
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue