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

improve respond_to handling for is_..? methods #587

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 19 additions & 7 deletions lib/rolify/dynamic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,26 @@
module Rolify
module Dynamic
def define_dynamic_method(role_name, resource)
class_eval do
define_method("is_#{role_name}?".to_sym) do
has_role?("#{role_name}")
end if !method_defined?("is_#{role_name}?".to_sym) && self.adapter.where_strict(self.role_class, name: role_name).exists?
class_eval do
if self.adapter.where_strict(self.role_class, name: role_name).exists?
if !method_defined?("is_#{role_name}?".to_sym)
define_method("is_#{role_name}?".to_sym) do
has_role?("#{role_name}")
end
end
else
undef_method("is_#{role_name}?".to_sym) if method_defined?("is_#{role_name}?".to_sym)
end

define_method("is_#{role_name}_of?".to_sym) do |arg|
has_role?("#{role_name}", arg)
end if !method_defined?("is_#{role_name}_of?".to_sym) && resource && self.adapter.where_strict(self.role_class, name: role_name, resource: resource).exists?
if resource && self.adapter.where_strict(self.role_class, name: role_name, resource: resource).exists?
if !method_defined?("is_#{role_name}_of?".to_sym)
define_method("is_#{role_name}_of?".to_sym) do |arg|
has_role?("#{role_name}", arg)
end
end
else
undef_method("is_#{role_name}_of?".to_sym) if method_defined?("is_#{role_name}_of?".to_sym)
end
end
end
end
Expand Down
11 changes: 6 additions & 5 deletions lib/rolify/role.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ def only_has_role?(role_name, resource = nil)

def remove_role(role_name, resource = nil)
self.class.adapter.remove(self, role_name.to_s, resource)
self.class.define_dynamic_method(role_name, resource) if Rolify.dynamic_shortcuts
end

alias_method :revoke, :remove_role
Expand All @@ -101,12 +102,12 @@ def method_missing(method, *args, &block)
def respond_to?(method, include_private = false)
if Rolify.dynamic_shortcuts && (method.to_s.match(/^is_(\w+)_of[?]$/) || method.to_s.match(/^is_(\w+)[?]$/))
query = self.class.role_class.where(:name => $1)
query = self.class.adapter.exists?(query, :resource_type) if method.to_s.match(/^is_(\w+)_of[?]$/)
return true if query.count > 0
false
else
super
if query.exists?
query = self.class.adapter.exists?(query, :resource_type) if method.to_s.match(/^is_(\w+)_of[?]$/)
return query.count > 0
end
end
super
end
end
end
12 changes: 12 additions & 0 deletions spec/rolify/shared_examples/shared_examples_for_dynamic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
it { should respond_to(:is_moderator_of?).with(1).arguments }
it { should_not respond_to(:is_god?) }

it { should respond_to(:is_waiting?) }
it { should_not respond_to(:is_awaiting?) }

it { subject.is_admin?.should be(true) }
it { subject.is_admin?.should be(true) }
it { subject.is_admin?.should be(true) }
Expand Down Expand Up @@ -49,6 +52,9 @@
it { should_not respond_to(:is_god?) }
it { should_not respond_to(:is_god_of?) }

it { should respond_to(:is_waiting?) }
it { should_not respond_to(:is_awaiting?) }

it { subject.is_moderator?.should be(false) }
it { subject.is_moderator_of?(Forum).should be(false) }
it { subject.is_moderator_of?(Forum.first).should be(true) }
Expand Down Expand Up @@ -80,6 +86,9 @@
it { should_not respond_to(:is_god?) }
it { should_not respond_to(:is_god_of?) }

it { should respond_to(:is_waiting?) }
it { should_not respond_to(:is_awaiting?) }

it { subject.is_manager?.should be(false) }
it { subject.is_manager_of?(Forum).should be(true) }
it { subject.is_manager_of?(Forum.first).should be(true) }
Expand Down Expand Up @@ -127,6 +136,9 @@
it { should_not respond_to(:is_god?) }
it { should_not respond_to(:is_god_of?) }

it { should respond_to(:is_waiting?) }
it { should_not respond_to(:is_awaiting?) }

it { subject.is_batman?.should be(false) }
it { subject.is_batman_of?(Forum).should be(false) }
it { subject.is_batman_of?(Forum.first).should be(false) }
Expand Down
8 changes: 8 additions & 0 deletions spec/support/adapters/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ class HumanResource < ActiveRecord::Base
# Custom role and class names
class Customer < ActiveRecord::Base
rolify :role_cname => "Privilege"

def is_waiting?
true
end
end

class Privilege < ActiveRecord::Base
Expand All @@ -52,6 +56,10 @@ def self.table_name_prefix

class Moderator < ActiveRecord::Base
rolify :role_cname => "Admin::Right", :role_join_table_name => "moderators_rights"

def is_waiting?
true
end
end

class Right < ActiveRecord::Base
Expand Down