Skip to content

Commit

Permalink
Merge pull request brainspec#386 from brainspec/negative-scope
Browse files Browse the repository at this point in the history
Add support for negative scopes.
  • Loading branch information
nashby authored Nov 12, 2021
2 parents 84ce06b + 758724f commit a16175c
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### enhancements

* Add support for Rails 7.0.0.alpha. (by [@f-mer](https://github.com/f-mer))
* Add support for negative shallow scopes. (by [@nashby](https://github.com/nashby))

## 2.4.0 (December 12, 2020)

Expand Down
4 changes: 4 additions & 0 deletions lib/enumerize/scope/activerecord.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ def _define_activerecord_shallow_scopes!(attribute_name)
define_singleton_method(value_obj) do
where(attribute_name => value_obj.value)
end

define_singleton_method("not_#{value_obj}") do
where.not(attribute_name => value_obj.value)
end
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions lib/enumerize/scope/mongoid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ def _define_mongoid_shallow_scopes!(attribute_name)
define_singleton_method(value_obj) do
self.in(attribute_name => value_obj.value)
end

define_singleton_method("not_#{value_obj}") do
self.not_in(attribute_name => value_obj.value)
end
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions lib/enumerize/scope/sequel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ def _define_sequel_shallow_scopes!(attribute_name)
def_dataset_method(value_obj) do
where(attribute_name => value_obj.value.to_s)
end

def_dataset_method("not_#{value_obj}") do
exclude(attribute_name => value_obj.value.to_s)
end
end
end
end
Expand Down
7 changes: 5 additions & 2 deletions test/activerecord_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -375,8 +375,8 @@ def self.name
it 'adds scope' do
User.delete_all

user_1 = User.create!(status: :active, role: :admin)
user_2 = User.create!(status: :blocked)
user_1 = User.create!(sex: :female, skill: :noob, status: :active, role: :admin)
user_2 = User.create!(sex: :female, skill: :casual, status: :blocked)
user_3 = User.create!(sex: :male, skill: :pro)

expect(User.with_status(:active)).must_equal [user_1]
Expand All @@ -388,6 +388,9 @@ def self.name

expect(User.male).must_equal [user_3]
expect(User.pro).must_equal [user_3]

expect(User.not_male.to_set).must_equal [user_1, user_2].to_set
expect(User.not_pro.to_set).must_equal [user_1, user_2].to_set
end

it 'ignores not enumerized values that passed to the scope method' do
Expand Down
7 changes: 5 additions & 2 deletions test/mongoid_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ class MongoidUser
it 'adds scope' do
model.delete_all

user_1 = model.create!(sex: :male, role: :admin)
user_2 = model.create!(sex: :female, role: :user)
user_1 = model.create!(sex: :male, skill: :noob, role: :admin, account_type: :basic)
user_2 = model.create!(sex: :female, skill: :noob, role: :user, account_type: :basic)
user_3 = model.create!(skill: :pro, account_type: :premium)

expect(model.with_sex(:male).to_a).must_equal [user_1]
Expand All @@ -136,6 +136,9 @@ class MongoidUser

expect(model.pro.to_a).must_equal [user_3]
expect(model.premium.to_a).must_equal [user_3]

expect(model.not_pro.to_set).must_equal [user_1, user_2].to_set
expect(model.not_premium.to_set).must_equal [user_1, user_2].to_set
end

it 'chains scopes' do
Expand Down
7 changes: 5 additions & 2 deletions test/sequel_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,8 @@ class SkipValidationsLambdaWithParamUser < Sequel::Model(:users)
it 'adds scope' do
User.filter{ true }.delete

user_1 = User.create(status: :active, role: :admin)
user_2 = User.create(status: :blocked)
user_1 = User.create(sex: :female, skill: :noob, status: :active, role: :admin)
user_2 = User.create(sex: :female, skill: :casual, status: :blocked)
user_3 = User.create(sex: :male, skill: :pro)

expect(User.with_status(:active).to_a).must_equal [user_1]
Expand All @@ -271,6 +271,9 @@ class SkipValidationsLambdaWithParamUser < Sequel::Model(:users)
expect(User.having_role(:admin).to_a).must_equal [user_1]
expect(User.male.to_a).must_equal [user_3]
expect(User.pro.to_a).must_equal [user_3]

expect(User.not_male.to_set).must_equal [user_1, user_2].to_set
expect(User.not_pro.to_set).must_equal [user_1, user_2].to_set
end

it 'allows either key or value as valid' do
Expand Down

0 comments on commit a16175c

Please sign in to comment.