-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
340fc59
commit 89b432d
Showing
10 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
module CafeCar::Controller::Filtering | ||
extend ActiveSupport::Concern | ||
|
||
class_methods do | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
module CafeCar | ||
class FilterBuilder | ||
require "activerecord_where_assoc" | ||
|
||
attr_reader :scope | ||
|
||
def initialize(scope) | ||
@scope = scope | ||
end | ||
|
||
def update!(&) | ||
scope = instance_exec(@scope, &) | ||
@scope = scope if scope | ||
self | ||
end | ||
|
||
def association?(name) = @scope.reflect_on_association(name).present? | ||
|
||
def param!(key, value) | ||
association?(key) ? association!(key, value) : attribute!(key, value) | ||
end | ||
|
||
def attribute!(key, value) | ||
@scope.where!(key => value) | ||
self | ||
end | ||
|
||
def association!(name, value, ...) | ||
update! do | ||
case value | ||
when true then @scope.where_assoc_exists(name) | ||
when false then @scope.where_assoc_not_exists(name) | ||
else @scope.where_assoc_exists(name) { filtered(value, ...) } | ||
end | ||
end | ||
end | ||
|
||
def filter!(params = nil) | ||
params.each { param!(_1, _2) } if params | ||
self | ||
end | ||
|
||
def filter(...) = update!(&:all).filter!(...) | ||
end | ||
end | ||
|
||
# Article.filtered do | ||
# published | ||
# user { username(/bob/) } | ||
# user.username(/bob/) | ||
# end | ||
# | ||
# Article.filtered(published: true, user: {username: /bob/}) | ||
# | ||
# Article.published(true).where_assoc_exists(:user) { where(username: /bob/) } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
module CafeCar::Filterable | ||
extend ActiveSupport::Concern | ||
|
||
class_methods do | ||
def filtered(params) = filter_builder.filter(params).scope | ||
def filtered!(params) = filter_builder.filter!(params).scope | ||
|
||
def filter_builder | ||
CafeCar::FilterBuilder.new(self) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
require "test_helper" | ||
|
||
class CafeCar::FilterableTest < Minitest::Test | ||
def test_filter_attributes | ||
assert_includes Article.filtered(title: "bob").to_sql, %("title" = 'bob') | ||
end | ||
|
||
def test_filter_associations | ||
assert_includes Article.filtered(author: true).to_sql, %(EXISTS) | ||
assert_includes Article.filtered(author: {username: "bob"}).to_sql, %(EXISTS) | ||
end | ||
end |