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

Support nested operators in abilities #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 30 additions & 18 deletions lib/cancancan/model_adapters/mongoid_adapter.rb
Original file line number Diff line number Diff line change
@@ -1,29 +1,41 @@
module CanCan
module ModelAdapters
class MongoidAdapter < AbstractAdapter
def self.for_class?(model_class)
model_class <= Mongoid::Document
end

def self.override_conditions_hash_matching?(subject, conditions)
conditions.any? do |k, _v|
key_is_not_symbol = -> { !k.is_a?(Symbol) }
subject_value_is_array = lambda do
subject.respond_to?(k) && subject.send(k).is_a?(Array)
class << self

def for_class?(model_class)
model_class <= Mongoid::Document
end

def override_conditions_hash_matching?(subject, conditions)
conditions.any? do |k, v|
hash_override?(subject, k, v)
end
end

key_is_not_symbol.call || subject_value_is_array.call
def matches_conditions_hash?(subject, conditions)
# To avoid hitting the db, retrieve the raw Mongo selector from
# the Mongoid Criteria and use Mongoid::Matchers#matches?
q = subject.class.where(conditions).selector
if subject.respond_to?(:_matches?)
subject._matches?(q)
else
subject.matches?(q)
end
end
end

def self.matches_conditions_hash?(subject, conditions)
# To avoid hitting the db, retrieve the raw Mongo selector from
# the Mongoid Criteria and use Mongoid::Matchers#matches?
q = subject.class.where(conditions).selector
if subject.respond_to?(:_matches?)
subject._matches?(q)
else
subject.matches?(q)
private

def hash_override?(subject, key, value)
return true unless key.is_a?(Symbol)

return true if value.is_a?(Hash) && value.keys.any? { |k| k.to_s.start_with?('$') }

subject_value = subject.respond_to?(key) && subject.send(key)
return true if subject_value.is_a?(Array)

false
end
end

Expand Down
11 changes: 11 additions & 0 deletions spec/cancancan/model_adapters/mongoid_adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@
expect(@ability).not_to be_able_to(:foo, two_to_five)
end

it 'is able to use nested operators in abilities' do
sir = MongoidProject.create(title: 'Sir')
lord = MongoidProject.create(title: 'Lord')
dude = MongoidProject.create(title: 'Dude')

@ability.can :foo, MongoidProject, title: { '$in' => %w[Sir Lord] }
expect(@ability).to be_able_to(:foo, sir)
expect(@ability).to be_able_to(:foo, lord)
expect(@ability).not_to be_able_to(:foo, dude)
end

it 'returns [] when no ability is defined so no records are found' do
MongoidProject.create(title: 'Sir')
MongoidProject.create(title: 'Lord')
Expand Down