Allow passing stringy booleans as scope args

This implements the solution discussed in #403.

Fixes #403.
This commit is contained in:
Joshua Kovach 2014-10-30 13:24:43 -04:00
parent 25255d95d4
commit 314e28f9af
4 changed files with 36 additions and 10 deletions

View File

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

View File

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

View File

@ -125,7 +125,17 @@ module Ransack
else
@scope_args[key] = args
end
@context.chain_scope(key, args)
@context.chain_scope(key, scope_args(args))
end
def scope_args(args)
if Ransack::Constants::TRUE_VALUES.include? args
true
elsif Ransack::Constants::FALSE_VALUES.include? args
false
else
args
end
end
def collapse_multiparameter_attributes!(attrs)

View File

@ -28,6 +28,11 @@ module Ransack
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 "ignores unlisted scopes" do
search = Person.search('restricted' => true)
search.result.to_sql.should_not include "restricted"
@ -38,6 +43,11 @@ module Ransack
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"