diff --git a/lib/rolify/finders.rb b/lib/rolify/finders.rb index b17557d5..451cb2db 100644 --- a/lib/rolify/finders.rb +++ b/lib/rolify/finders.rb @@ -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| @@ -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 @@ -41,4 +45,4 @@ def parse_args(args, users, &block) block.call(users_to_add) end end -end \ No newline at end of file +end diff --git a/spec/rolify/shared_examples/shared_examples_for_finders.rb b/spec/rolify/shared_examples/shared_examples_for_finders.rb index 0c9194c7..4fe766c5 100644 --- a/spec/rolify/shared_examples/shared_examples_for_finders.rb +++ b/spec/rolify/shared_examples/shared_examples_for_finders.rb @@ -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) } @@ -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 \ No newline at end of file +end