Module: EasyRoles

Included in:
ActiveRecord::Base
Defined in:
lib/easy_roles.rb,
lib/generators/easy_roles/easy_roles_generator.rb

Defined Under Namespace

Modules: ClassMethods, Generators

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



2
3
4
5
6
# File 'lib/easy_roles.rb', line 2

def self.included(base)
  base.extend ClassMethods
  base.send :alias_method_chain, :method_missing, :roles
  base.send :alias_method_chain, :respond_to?, :roles
end

Instance Method Details

#method_missing_with_roles(method_id, *args, &block) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
# File 'lib/easy_roles.rb', line 109

def method_missing_with_roles(method_id, *args, &block)
  match = method_id.to_s.match(/^is_(\w+)[?]$/)
  if match && respond_to?('has_role?')
    self.class.send(:define_method, "is_#{match[1]}?") do
      send :has_role?, "#{match[1]}"
    end
    send "is_#{match[1]}?"
  else
    method_missing_without_roles(method_id, *args, &block)
  end
end

#respond_to_with_roles?(method_id, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


121
122
123
124
125
126
127
128
# File 'lib/easy_roles.rb', line 121

def respond_to_with_roles?(method_id, include_private = false)
  match = method_id.to_s.match(/^is_(\w+)[?]$/)
  if match && respond_to?('has_role?')
    true
  else
    respond_to_without_roles?(method_id, include_private = false)
  end
end