Skip to content

Commit

Permalink
Merge pull request #460 from shekibobo/fix-scope-boolean-values
Browse files Browse the repository at this point in the history
Allow passing stringy booleans as scope args. Fixes #403.
  • Loading branch information
jonatack committed Oct 31, 2014
2 parents 25255d9 + e38edb9 commit 69d13b2
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 12 deletions.
18 changes: 12 additions & 6 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
This change log was started in August 2014. All notable changes to this project
henceforth should be documented here.

## Master (Unreleased)
### Fixed

* Add support for passing stringy booleans for ransackable scopes. ([pull request](https://github.com/activerecord-hackery/ransack/pull/460)).

*Josh Kovach*

## Version 1.5.1 - 2014-10-30
### Added

Expand All @@ -27,14 +34,13 @@ henceforth should be documented here.

*Jon Atack*


## Version 1.5.0 - 2014-10-26
### Added

* Add support for multiple sort fields and default orders in Ransack
`sort_link` helpers
([pull request](https://github.com/activerecord-hackery/ransack/pull/438)).

*Caleb Land*, *James u007*

* Add tests for `lteq`, `lt`, `gteq` and `gt` predicates. They are also
Expand All @@ -44,15 +50,15 @@ henceforth should be documented here.
*Jon Atack*

* Add tests for unknown attribute names.

*Joe Yates*

* Add tests for attribute names containing '_or_' and '_and_'.

*Joe Yates*, *Jon Atack*

* Add tests for attribute names ending with '_start' and '_end'.

*Jon Atack*, *Timo Schilling*

* Add tests for `start`, `not_start`, `end` and `not_end` predicates, with
Expand Down Expand Up @@ -164,7 +170,7 @@ henceforth should be documented here.
* Rewrite much of the Ransack README documentation, including the
Associations section code examples and the Authorizations section detailing
how to whitelist attributes, associations, sorts and scopes.

*Jon Atack*

## Version 1.3.0 - 2014-08-23
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,8 @@ Employee.search({ active: true, hired_since: '2013-01-01' })
Employee.search({ salary_gt: 100_000 }, { auth_object: current_user })
```

If the `true` value is being passed via url params or by some other mechanism that will convert it to a string (i.e. `"active" => "true"`), the true value will *not* be passed to the scope. If you want to pass a `'true'` string to the scope, you should wrap it in an array (i.e. `"active" => ['true']`).

Scopes are a recent addition to Ransack and currently have a few caveats:
First, a scope involving child associations needs to be defined in the parent
table model, not in the child model. Second, scopes with an array as an
Expand All @@ -470,9 +472,7 @@ wrapped in an array to function (see
which is not compatible with Ransack form helpers. For this use case, it may be
better for now to use [ransackers]
(https://github.com/activerecord-hackery/ransack/wiki/Using-Ransackers) instead
where feasible. Finally, there is also
[this issue](https://github.com/activerecord-hackery/ransack/issues/403)
to be aware of. Pull requests with solutions and tests are welcome!
where feasible. Pull requests with solutions and tests are welcome!

### Grouping queries by OR instead of AND

Expand Down
18 changes: 16 additions & 2 deletions lib/ransack/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,23 @@ def add_scope(key, args)
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, sanitized_scope_args(args))
end

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
false
else
args
end
@context.chain_scope(key, args)
end

def collapse_multiparameter_attributes!(attrs)
Expand Down
22 changes: 21 additions & 1 deletion spec/ransack/adapters/active_record/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,29 @@ module ActiveRecord

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
search = Person.search('active' => true)
search.result.to_sql.should include "active = 1"
end

it "applies stringy true scopes" do
search = Person.search('active' => 'true')
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"
Expand All @@ -38,6 +53,11 @@ module ActiveRecord
search.result.to_sql.should_not include "active"
end

it "ignores stringy false scopes" do
search = Person.search('active' => 'false')
search.result.to_sql.should_not include "active"
end

it "passes values to scopes" do
search = Person.search('over_age' => 18)
search.result.to_sql.should include "age > 18"
Expand Down
1 change: 1 addition & 0 deletions spec/support/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 69d13b2

Please sign in to comment.