Skip to content

Commit

Permalink
refactored scopes code for Role class
Browse files Browse the repository at this point in the history
added a parameter to ask for roles binded to specific resource
closes #67
  • Loading branch information
EppO committed Apr 26, 2012
1 parent da1ec59 commit 2093150
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 19 deletions.
4 changes: 1 addition & 3 deletions lib/generators/rolify/role/templates/role-active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,5 @@ class <%= role_cname.camelize %> < ActiveRecord::Base
has_and_belongs_to_many :<%= user_cname.tableize %>, :join_table => :<%= "#{user_cname.tableize}_#{role_cname.tableize}" %>
belongs_to :resource, :polymorphic => true

scope :global, where(:resource_type => nil, :resource_id => nil)
scope :class_scoped, where("resource_type IS NOT NULL AND resource_id IS NULL")
scope :instance_scoped, where("resource_type IS NOT NULL AND resource_id IS NOT NULL")
extend Rolify::Adapter::Scopes
end
4 changes: 1 addition & 3 deletions lib/generators/rolify/role/templates/role-mongoid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,5 @@ class <%= role_cname.camelize %>
:unique => true
)

scope :global, where(:resource_type => nil, :resource_id => nil)
scope :class_scoped, where(:resource_type.ne => nil, :resource_id => nil)
scope :instance_scoped, where(:resource_type.ne => nil, :resource_id.ne => nil)
extend Rolify::Adapter::Scopes
end
27 changes: 27 additions & 0 deletions lib/rolify/adapters/active_record/scopes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module Rolify
module Adapter
module Scopes
def global
where(:resource_type => nil, :resource_id => nil)
end

def class_scoped(resource_type = nil)
where_conditions = "resource_type IS NOT NULL AND resource_id IS NULL"
where_conditions = [ "resource_type = ? AND resource_id IS NULL", resource_type.name ] if resource_type
where(where_conditions)
end

def instance_scoped(resource_type = nil)
where_conditions = "resource_type IS NOT NULL AND resource_id IS NOT NULL"
if resource_type
if resource_type.is_a? Class
where_conditions = [ "resource_type = ? AND resource_id IS NOT NULL", resource_type.name ]
else
where_conditions = [ "resource_type = ? AND resource_id = ?", resource_type.class.name, resource_type.id ]
end
end
where(where_conditions)
end
end
end
end
1 change: 1 addition & 0 deletions lib/rolify/adapters/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def role_table

def self.create(adapter, role_cname, user_cname)
load "rolify/adapters/#{Rolify.orm}/#{adapter}.rb"
load "rolify/adapters/#{Rolify.orm}/scopes.rb"
Rolify::Adapter.const_get(adapter.camelize.to_sym).new(role_cname, user_cname)
end
end
Expand Down
27 changes: 27 additions & 0 deletions lib/rolify/adapters/mongoid/scopes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module Rolify
module Adapter
module Scopes
def global
where(:resource_type => nil, :resource_id => nil)
end

def class_scoped(resource_type = nil)
where_conditions = { :resource_type.ne => nil, :resource_id => nil }
where_conditions = { :resource_type => resource_type.name, :resource_id => nil } if resource_type
where(where_conditions)
end

def instance_scoped(resource_type = nil)
where_conditions = { :resource_type.ne => nil, :resource_id.ne => nil }
if resource_type
if resource_type.is_a? Class
where_conditions = { :resource_type => resource_type.name, :resource_id.ne => nil }
else
where_conditions = { :resource_type => resource_type.class.name, :resource_id => resource_type.id }
end
end
where(where_conditions)
end
end
end
end
10 changes: 9 additions & 1 deletion spec/rolify/shared_examples/shared_examples_for_scopes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,20 @@
let!(:moderator_role) { subject.add_role :moderator, Forum }

it { subject.roles.class_scoped.should == [ manager_role, moderator_role ] }
it { subject.roles.class_scoped(Group).should == [ manager_role ] }
it { subject.roles.class_scoped(Forum).should == [ moderator_role ] }
end

describe ".instance_scoped" do
let!(:visitor_role) { subject.add_role :visitor, Forum.first }
let!(:zombie_role) { subject.add_role :visitor, Forum.last }
let!(:anonymous_role) { subject.add_role :anonymous, Group.last }

it { subject.roles.instance_scoped.should == [ visitor_role, anonymous_role ]}
it { subject.roles.instance_scoped.should == [ visitor_role, zombie_role, anonymous_role ] }
it { subject.roles.instance_scoped(Forum).should == [ visitor_role, zombie_role ] }
it { subject.roles.instance_scoped(Forum.first).should == [ visitor_role ] }
it { subject.roles.instance_scoped(Forum.last).should == [ zombie_role ] }
it { subject.roles.instance_scoped(Group.last).should == [ anonymous_role ] }
it { subject.roles.instance_scoped(Group.first).should be_empty }
end
end
8 changes: 2 additions & 6 deletions spec/support/adapters/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ class Role < ActiveRecord::Base
has_and_belongs_to_many :users, :join_table => :users_roles
belongs_to :resource, :polymorphic => true

scope :global, where(:resource_type => nil, :resource_id => nil)
scope :class_scoped, where("resource_type IS NOT NULL AND resource_id IS NULL")
scope :instance_scoped, where("resource_type IS NOT NULL AND resource_id IS NOT NULL")
extend Rolify::Adapter::Scopes
end

class Forum < ActiveRecord::Base
Expand All @@ -35,7 +33,5 @@ class Privilege < ActiveRecord::Base
has_and_belongs_to_many :customers, :join_table => :customers_privileges
belongs_to :resource, :polymorphic => true

scope :global, where(:resource_type => nil, :resource_id => nil)
scope :class_scoped, where("resource_type IS NOT NULL AND resource_id IS NULL")
scope :instance_scoped, where("resource_type IS NOT NULL AND resource_id IS NOT NULL")
extend Rolify::Adapter::Scopes
end
8 changes: 2 additions & 6 deletions spec/support/adapters/mongoid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ class Role
:unique => true
)

scope :global, where(:resource_type => nil, :resource_id => nil)
scope :class_scoped, where(:resource_type.ne => nil, :resource_id => nil)
scope :instance_scoped, where(:resource_type.ne => nil, :resource_id.ne => nil)
extend Rolify::Adapter::Scopes
end

class Forum
Expand Down Expand Up @@ -78,7 +76,5 @@ class Privilege
:unique => true
)

scope :global, where(:resource_type => nil, :resource_id => nil)
scope :class_scoped, where(:resource_type.ne => nil, :resource_id => nil)
scope :instance_scoped, where(:resource_type.ne => nil, :resource_id.ne => nil)
extend Rolify::Adapter::Scopes
end

0 comments on commit 2093150

Please sign in to comment.