mirror of
https://github.com/Fishwaldo/role_model.git
synced 2025-03-16 03:51:59 +00:00
Refactor dynamic methods
1. Add setter methods for each role 2. Remove duplicative methods and add ability to add a prefix. Tests: 100% Pass (cherry picked from commit 3bba2a18cfbcc34ae6432b2715de728638fafe34)
This commit is contained in:
parent
a4b8c45e9b
commit
f33de4f8c1
3 changed files with 58 additions and 25 deletions
22
README.rdoc
22
README.rdoc
|
@ -27,7 +27,9 @@ It works like this:
|
|||
|
||||
# declare the valid roles -- do not change the order if you add more
|
||||
# roles later, always append them at the end!
|
||||
roles :admin, :manager, :author
|
||||
#
|
||||
# Set dynamic: false to skip creating the dynamic methods for the roles.
|
||||
roles :admin, :manager, :author, prefix: "is_"
|
||||
end
|
||||
|
||||
#
|
||||
|
@ -40,11 +42,20 @@ It works like this:
|
|||
# role assignment
|
||||
>> u.roles = [:admin] # ['admin'] works as well
|
||||
=> [:admin]
|
||||
|
||||
# adding roles (remove via delete or re-assign)
|
||||
|
||||
# adding roles
|
||||
>> u.roles << :manager
|
||||
>> u.roles.add(:manager)
|
||||
>> u.manager = true # if dynamic is enabled (by default) or...
|
||||
>> u.is_manager = true # If :prefix => "is_"
|
||||
=> [:admin, :manager]
|
||||
|
||||
|
||||
# removing roles
|
||||
>> u.roles.remove(:manager)
|
||||
>> u.manager = false # if dynamic is enabled (by default) or...
|
||||
>> u.is_manager = false # If :prefix => "is_"
|
||||
=> [:admin]
|
||||
|
||||
# querying roles...
|
||||
|
||||
# get all valid roles that have been declared
|
||||
|
@ -60,7 +71,8 @@ It works like this:
|
|||
=> false
|
||||
|
||||
# ... check for individual roles with dynamic methods (set dynamic: false to disable)
|
||||
>> u.is_author?
|
||||
>> u.author? # Or...
|
||||
>> u.is_author? # If :prefix => "is_"
|
||||
=> false
|
||||
|
||||
# ... check for multiple roles
|
||||
|
|
|
@ -41,21 +41,20 @@ module RoleModel
|
|||
opts = roles.last.is_a?(Hash) ? roles.pop : {}
|
||||
self.valid_roles = roles.flatten.map(&:to_sym)
|
||||
unless (opts[:dynamic] == false)
|
||||
self.define_dynamic_queries(self.valid_roles)
|
||||
self.define_dynamic_queries(self.valid_roles, opts[:prefix])
|
||||
end
|
||||
end
|
||||
|
||||
# Defines dynamic queries for :role
|
||||
# #is_<:role>?
|
||||
|
||||
# Defines dynamic queries for :role. Providing a prefix is optional.
|
||||
# #<:role>?
|
||||
# #<:role>=(true|false)
|
||||
#
|
||||
# Defines new methods which call #is?(:role)
|
||||
def define_dynamic_queries(roles)
|
||||
# Defines new methods which call #is?(:role) and roles.(add|remove)
|
||||
def define_dynamic_queries(roles, prefix=nil)
|
||||
dynamic_module = Module.new do
|
||||
roles.each do |role|
|
||||
["#{role}?".to_sym, "is_#{role}?".to_sym].each do |method|
|
||||
define_method(method) { is? role }
|
||||
end
|
||||
define_method("#{prefix}#{role}?") { is? role }
|
||||
define_method("#{prefix}#{role}=") { |x| self.roles.send(x ? :add : :delete, role) }
|
||||
end
|
||||
end
|
||||
include dynamic_module
|
||||
|
|
|
@ -3,13 +3,15 @@ require 'spec_helper'
|
|||
describe RoleModel do
|
||||
|
||||
let(:model_class) { Class.new }
|
||||
let(:role_options) { {} }
|
||||
|
||||
before(:each) do
|
||||
options = role_options
|
||||
model_class.instance_eval do
|
||||
attr_accessor :roles_mask
|
||||
attr_accessor :custom_roles_mask
|
||||
include RoleModel
|
||||
roles :foo, :bar, :third
|
||||
roles :foo, :bar, :third, options
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -345,25 +347,21 @@ describe RoleModel do
|
|||
subject { model_class.new }
|
||||
|
||||
it "should return true when the given role was assigned" do
|
||||
subject.roles = :foo
|
||||
subject.is_foo?.should be true
|
||||
subject.foo = true
|
||||
subject.foo?.should be true
|
||||
end
|
||||
|
||||
it "should return false when the given role was not assigned" do
|
||||
subject.roles = :bar
|
||||
subject.is_foo?.should be false
|
||||
subject.foo = false
|
||||
subject.foo?.should be false
|
||||
end
|
||||
|
||||
it "should return false when no role was assigned" do
|
||||
subject.is_foo?.should be false
|
||||
subject.bar?.should be false
|
||||
end
|
||||
|
||||
it "should throw NoMethodError when asked for an undefined role" do
|
||||
lambda { subject.baz? }.should raise_error(NoMethodError)
|
||||
lambda { subject.is_baz? }.should raise_error(NoMethodError)
|
||||
end
|
||||
|
||||
it "should not define dynamic finders when opting out" do
|
||||
|
@ -390,7 +388,7 @@ describe RoleModel do
|
|||
include RoleModel
|
||||
roles :foo, :bar, :baz
|
||||
|
||||
def is_baz?
|
||||
def baz?
|
||||
return false
|
||||
end
|
||||
|
||||
|
@ -406,11 +404,35 @@ describe RoleModel do
|
|||
model.foo?.should be false
|
||||
|
||||
model.bar?.should be false
|
||||
model.is_bar?.should be true
|
||||
model.roles.should include(:bar)
|
||||
|
||||
model.is_baz?.should be false
|
||||
model.baz?.should be true
|
||||
model.baz?.should be false
|
||||
model.roles.should include(:baz)
|
||||
end
|
||||
|
||||
context 'when prefix set to "is_"' do
|
||||
let(:role_options) { {prefix: 'is_'} }
|
||||
|
||||
it "should return true when the given role was assigned" do
|
||||
subject.is_foo = true
|
||||
subject.is_foo?.should be true
|
||||
end
|
||||
|
||||
it "should return false when the given role was not assigned" do
|
||||
subject.is_foo = false
|
||||
subject.is_foo?.should be false
|
||||
end
|
||||
|
||||
it "should return false when no role was assigned" do
|
||||
subject.is_foo?.should be false
|
||||
end
|
||||
|
||||
it "should throw NoMethodError when asked for an undefined role" do
|
||||
lambda { subject.baz? }.should raise_error(NoMethodError)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
context "query for multiple roles" do
|
||||
|
|
Loading…
Add table
Reference in a new issue