Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow negation #8

Merged
merged 2 commits into from
Mar 9, 2022
Merged

Allow negation #8

merged 2 commits into from
Mar 9, 2022

Conversation

dim
Copy link
Member

@dim dim commented Mar 8, 2022

It's a bigger change than it may seem, will explain inline

@@ -17,15 +17,15 @@ class Comment < ActiveRecord::Base
belongs_to :post

filterable_by :post_id, :user_id
filterable_by :post_author_id do |scope, value|
scope.joins(:posts).where(:'posts.author_id' => value)
filterable_by :post_author_id do |value|
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so the big change is that you don't now need scope as a block argument, but the old method is still supported (although deprecated)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will we still be able to handle when value is nil cases?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

value cannot be nil, these blocks are only run unless value.bank?

Comment.filter_by({ 'post_id' => ['1', '2'] })
# => WHERE post_id IN (1, 2)

Comment.filter_by({ 'post_id_not' => '3' })
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the other change is that you now can append _not and exclude something

@dim
Copy link
Member Author

dim commented Mar 9, 2022

@jpmedforth @andyborn ?

@@ -17,15 +17,15 @@ class Comment < ActiveRecord::Base
belongs_to :post

filterable_by :post_id, :user_id
filterable_by :post_author_id do |scope, value|
scope.joins(:posts).where(:'posts.author_id' => value)
filterable_by :post_author_id do |value|
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will we still be able to handle when value is nil cases?

@@ -17,15 +17,15 @@ class Comment < ActiveRecord::Base
belongs_to :post

filterable_by :post_id, :user_id
filterable_by :post_author_id do |scope, value|
scope.joins(:posts).where(:'posts.author_id' => value)
filterable_by :post_author_id do |value|
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

value cannot be nil, these blocks are only run unless value.bank?

def self.merge(scope, unscoped, hash, name, **opts, &block)
key = name
positive = normalize(hash[key]) if hash.key?(key)
if positive.present?
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@andyborn here we only apply block/limit scope if params['post_author_id'] is present


key = "#{name}_not"
negative = normalize(hash[key]) if hash.key?(key)
if negative.present?
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same with params['post_author_id_not']

@dim dim merged commit d3b4dd5 into main Mar 9, 2022
@dim dim deleted the feature/not branch March 9, 2022 12:50
@dim
Copy link
Member Author

dim commented Mar 9, 2022

Released v0.6.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants