Module: ActiveRecord::Aggregations::HasRoles::Macro

Defined in:
lib/active_record/aggregations/has_roles.rb

Instance Method Summary collapse

Instance Method Details

#has_roles(options = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/active_record/aggregations/has_roles.rb', line 9

def has_roles(options = {})
  return unless Role.table_exists?

  write_inheritable_attribute :privileges_table_name, options[:join_table] || "privileges"

  has_and_belongs_to_many :roles, :join_table => read_inheritable_attribute(:privileges_table_name), :uniq => true

  include InstanceMethods
  extend ClassMethods

  Role.all.each do |role|
    define_method("#{role}?") do
      is?(role.to_s)
    end
  end

  Role.instance_eval <<-TXT
    has_and_belongs_to_many :#{class_name.downcase.pluralize}, :join_table => :#{read_inheritable_attribute(:privileges_table_name)}, :uniq => true
  TXT
end