Skip to content

Commit

Permalink
Allow passing stringy booleans as scope args
Browse files Browse the repository at this point in the history
This implements the solution discussed in #403.

Fixes #403.
  • Loading branch information
shekibobo committed Oct 31, 2014
1 parent 25255d9 commit 314e28f
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 10 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
12 changes: 11 additions & 1 deletion lib/ransack/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,17 @@ def add_scope(key, args)
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)
Expand Down
10 changes: 10 additions & 0 deletions spec/ransack/adapters/active_record/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ module ActiveRecord
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"
Expand All @@ -38,6 +43,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

0 comments on commit 314e28f

Please sign in to comment.