Add ability to pass boolean scope args in array

This commit is contained in:
Joshua Kovach 2014-10-31 11:53:37 -04:00
parent 314e28f9af
commit e38edb9560
3 changed files with 19 additions and 4 deletions

View File

@ -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

View File

@ -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"

View File

@ -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]