Add ability to pass boolean scope args in array
This commit is contained in:
parent
314e28f9af
commit
e38edb9560
|
@ -123,12 +123,16 @@ module Ransack
|
|||
if @context.scope_arity(key) == 1
|
||||
@scope_args[key] = args.is_a?(Array) ? args[0] : args
|
||||
else
|
||||
@scope_args[key] = args
|
||||
@scope_args[key] = args.is_a?(Array) ? sanitized_scope_args(args) : args
|
||||
end
|
||||
@context.chain_scope(key, scope_args(args))
|
||||
@context.chain_scope(key, sanitized_scope_args(args))
|
||||
end
|
||||
|
||||
def scope_args(args)
|
||||
def sanitized_scope_args(args)
|
||||
if args.is_a?(Array)
|
||||
args = args.map(&method(:sanitized_scope_args))
|
||||
end
|
||||
|
||||
if Ransack::Constants::TRUE_VALUES.include? args
|
||||
true
|
||||
elsif Ransack::Constants::FALSE_VALUES.include? args
|
||||
|
|
|
@ -20,7 +20,7 @@ module Ransack
|
|||
|
||||
context 'with scopes' do
|
||||
before do
|
||||
Person.stub :ransackable_scopes => [:active, :over_age]
|
||||
Person.stub :ransackable_scopes => [:active, :over_age, :of_age]
|
||||
end
|
||||
|
||||
it "applies true scopes" do
|
||||
|
@ -33,6 +33,16 @@ module Ransack
|
|||
search.result.to_sql.should include "active = 1"
|
||||
end
|
||||
|
||||
it "applies stringy boolean scopes with true value in an array" do
|
||||
search = Person.search('of_age' => ['true'])
|
||||
search.result.to_sql.should include "age >= 18"
|
||||
end
|
||||
|
||||
it "applies stringy boolean scopes with false value in an array" do
|
||||
search = Person.search('of_age' => ['false'])
|
||||
search.result.to_sql.should include "age < 18"
|
||||
end
|
||||
|
||||
it "ignores unlisted scopes" do
|
||||
search = Person.search('restricted' => true)
|
||||
search.result.to_sql.should_not include "restricted"
|
||||
|
|
|
@ -39,6 +39,7 @@ class Person < ActiveRecord::Base
|
|||
scope :restricted, lambda { where("restricted = 1") }
|
||||
scope :active, lambda { where("active = 1") }
|
||||
scope :over_age, lambda { |y| where(["age > ?", y]) }
|
||||
scope :of_age, lambda { |of_age| of_age ? where("age >= ?", 18) : where("age < ?", 18) }
|
||||
|
||||
ransacker :reversed_name, :formatter => proc { |v| v.reverse } do |parent|
|
||||
parent.table[:name]
|
||||
|
|
Loading…
Reference in New Issue