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

Add only_with_role finder #437

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
10 changes: 7 additions & 3 deletions lib/rolify/finders.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ def without_role(role_name, resource = nil)
self.adapter.all_except(self, self.with_role(role_name, resource))
end

def only_with_role(role_name, resource = nil)
with_role(role_name, resource).select { |record| record.roles.count == 1 }
end

def with_all_roles(*args)
users = []
parse_args(args, users) do |users_to_add|
Expand All @@ -26,9 +30,9 @@ def with_any_role(*args)
users.uniq
end
end

private

def parse_args(args, users, &block)
args.each do |arg|
if arg.is_a? Hash
Expand All @@ -41,4 +45,4 @@ def parse_args(args, users, &block)
block.call(users_to_add)
end
end
end
end
52 changes: 50 additions & 2 deletions spec/rolify/shared_examples/shared_examples_for_finders.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,55 @@
end
end
end


describe ".only_with_role" do
it { should respond_to(:only_with_role).with(1).argument }
it { should respond_to(:only_with_role).with(2).arguments }

context "with a global role" do
it { subject.only_with_role("admin".send(param_method)).should be_empty }
it { subject.only_with_role("moderator".send(param_method)).should be_empty }
it { subject.only_with_role("visitor".send(param_method)).should be_empty }
end

context "with a class scoped role" do
context "on Forum class" do
it { subject.only_with_role("admin".send(param_method), Forum).should be_empty }
it { subject.only_with_role("moderator".send(param_method), Forum).should be_empty }
it { subject.only_with_role("visitor".send(param_method), Forum).should be_empty }
end

context "on Group class" do
it { subject.only_with_role("admin".send(param_method), Group).should be_empty }
it { subject.only_with_role("moderator".send(param_method), Group).should be_empty }
it { subject.only_with_role("visitor".send(param_method), Group).should be_empty }
end
end

context "with an instance scoped role" do
context "on Forum.first instance" do
it { subject.only_with_role("admin".send(param_method), Forum.first).should be_empty }
it { subject.only_with_role("moderator".send(param_method), Forum.first).should be_empty }
it { subject.only_with_role("visitor".send(param_method), Forum.first).should be_empty }
end

context "on Forum.last instance" do
it { subject.only_with_role("admin".send(param_method), Forum.last).should be_empty }
it { subject.only_with_role("moderator".send(param_method), Forum.last).should be_empty }
it { subject.only_with_role("visitor".send(param_method), Forum.last).should eq[visitor] } # =~ doesn't pass using mongoid, don't know why...
end

context "on Group.first instance" do
it { subject.only_with_role("admin".send(param_method), Group.first).should be_empty }
it { subject.only_with_role("moderator".send(param_method), Group.first).should be_empty }
it { subject.only_with_role("visitor".send(param_method), Group.first).should be_empty }
end

context "on Company.first_instance" do
it { subject.only_with_role("owner".send(param_method), Company.first).should be_empty }
end
end
end

describe ".with_all_roles" do
it { should respond_to(:with_all_roles) }
Expand Down Expand Up @@ -128,4 +176,4 @@
it { subject.with_any_role({ :name => "visitor".send(param_method), :resource => :any }, { :name => "moderator".send(param_method), :resource => :any }).should =~ [ root, modo, visitor ] }
end
end
end
end