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

Rules should be able to take arrays in the hash conditions #29

Open
wants to merge 3 commits 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
6 changes: 5 additions & 1 deletion lib/cancancan/model_adapters/active_graph_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ def self.property_matches?(subject, property, value)
if subject.is_a?(ActiveGraph::Node::HasN::AssociationProxy)
subject.where(property => value).exists?
else
subject.send(property) == value
if value.is_a?(Enumerable)
subject.class.where(property => value, neo_id: subject.neo_id).exists?
Copy link
Contributor

Choose a reason for hiding this comment

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

if this line is correct then it should handle both cases without the if statement. However, all 10 lines look dubious. If the subject is not a ..Proxy then one should be created rather than querying by neo_id.

else
subject.send(property) == value
end
end
end

Expand Down
19 changes: 18 additions & 1 deletion spec/cancancan/model_adapters/neo4j_active_graph_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
ActiveGraph::Base.label_object(Mention.mapped_label_names.first).create_constraint(Mention.id_property_name, type: :unique)
ActiveGraph::Base.label_object(Namespace::TableX.mapped_label_names.first).create_constraint(Namespace::TableX.id_property_name, type: :unique)
ActiveGraph::Base.label_object(Namespace::TableZ.mapped_label_names.first).create_constraint(Namespace::TableZ.id_property_name, type: :unique)

Article.delete_all
Category.delete_all
Comment.delete_all
Expand Down Expand Up @@ -379,6 +379,23 @@
expect { Comment.accessible_by(@ability) }.to_not raise_error
end

it 'should support deeply nested conditions with an Enumerable value' do
@ability.can :read, Comment, article: { category: { name: ["Cars", "Sports"] } }

category1 = Category.create!(name: "Cars", visible: true)
category2 = Category.create!(name: "Sports", visible: true)
category3 = Category.create!(name: "Books", visible: true)

comment1 = Comment.create!(article: Article.create!(category: category1))
comment2 = Comment.create!(article: Article.create!(category: category2))
comment3 = Comment.create!(article: Article.create!(category: category3))

expect(Comment.accessible_by(@ability)).to match_array([comment1, comment2])
expect(@ability.can?(:read, comment1)).to be(true)
expect(@ability.can?(:read, comment2)).to be(true)
expect(@ability.can?(:read, comment3)).to be(false)
end

it 'returns empty set if no abilities match' do
expect(@ability.model_adapter(Article, :read).database_records).to be_empty
end
Expand Down