Skip to content
This repository has been archived by the owner on Aug 23, 2022. It is now read-only.

Commit

Permalink
Add support for testing scope conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
xjunior committed Apr 30, 2021
1 parent f739b6d commit ffe4ad7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/consent/rspec/consent_view.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@ def failure_message_when_negated
private

def comparable_conditions(conditions)
conditions
case conditions.class.to_s
when 'ActiveRecord::Relation'
conditions.to_sql
else
conditions
end
end

def failure_message_base(failure) # rubocop:disable Metrics/MethodLength
Expand Down
8 changes: 8 additions & 0 deletions spec/consent/rspec_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,13 @@
it "invalidates when conditions don't match" do
expect(SomeModel).to_not consent_view(:self, owner_id: 14).to(user)
end

it 'validates when conditions are a scope' do
expect(SomeModel).to_not(
consent_view(:scoped_self)
.with_conditions(SomeModel.where(owner_id: 13))
.to(user)
)
end
end
end
4 changes: 4 additions & 0 deletions spec/permissions/some_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
{ owner_id: user.id }
end

view :scoped_self, 'Default view',
->(_user, _obj) { true }
->(user) { SomeModel.where(owner_id: user.id) }

view :view1, 'View 1'
view :lol, 'Lol Only' do |_|
{ name: 'lol' }
Expand Down

0 comments on commit ffe4ad7

Please sign in to comment.