diff --git a/lib/rolify/dynamic.rb b/lib/rolify/dynamic.rb index 686c7a41..4baf9b4e 100644 --- a/lib/rolify/dynamic.rb +++ b/lib/rolify/dynamic.rb @@ -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 diff --git a/lib/rolify/role.rb b/lib/rolify/role.rb index 34f6f13a..39e6a793 100644 --- a/lib/rolify/role.rb +++ b/lib/rolify/role.rb @@ -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 @@ -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 diff --git a/spec/rolify/shared_examples/shared_examples_for_dynamic.rb b/spec/rolify/shared_examples/shared_examples_for_dynamic.rb index 736f66f7..c62be2d5 100644 --- a/spec/rolify/shared_examples/shared_examples_for_dynamic.rb +++ b/spec/rolify/shared_examples/shared_examples_for_dynamic.rb @@ -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) } @@ -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) } @@ -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) } @@ -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) } diff --git a/spec/support/adapters/active_record.rb b/spec/support/adapters/active_record.rb index bf40cbcd..84930d30 100644 --- a/spec/support/adapters/active_record.rb +++ b/spec/support/adapters/active_record.rb @@ -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 @@ -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