Module: Acl9::ModelExtensions::ClassMethods

Defined in:
lib/acl9/model_extensions.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_authorization_object(options = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/acl9/model_extensions.rb', line 21

def acts_as_authorization_object(options = {})
  subject = options[:subject_class_name] || Acl9::config[:default_subject_class_name]
  subj_table = subject.tableize
  subj_col = subject.underscore

  role       = options[:role_class_name] || Acl9::config[:default_role_class_name]
  role_table = role.tableize

  sql_tables = <<-EOS
    FROM #{subj_table}
    INNER JOIN #{role_table}_#{subj_table} ON #{subj_col}_id = #{subj_table}.id
    INNER JOIN #{role_table}               ON #{role_table}.id = #{role.underscore}_id
  EOS

  sql_where = <<-'EOS'
    WHERE authorizable_type = '#{self.class.base_class.to_s}'
    AND authorizable_id = #{id}"
  EOS
  
  has_many :accepted_roles, :as => :authorizable, :class_name => role, :dependent => :destroy

  has_many :"#{subj_table}",
    :finder_sql  => ("SELECT DISTINCT #{subj_table}.*" + sql_tables + sql_where),
    :counter_sql => ("SELECT COUNT(DISTINCT #{subj_table}.id)" + sql_tables + sql_where),
    :readonly => true
  
  include Acl9::ModelExtensions::Object
end

#acts_as_authorization_role(options = {}) ⇒ Object



50
51
52
53
54
55
# File 'lib/acl9/model_extensions.rb', line 50

def acts_as_authorization_role(options = {})
  subject = options[:subject_class_name] || Acl9::config[:default_subject_class_name]

  has_and_belongs_to_many subject.tableize.to_sym
  belongs_to :authorizable, :polymorphic => true
end

#acts_as_authorization_subject(options = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/acl9/model_extensions.rb', line 11

def acts_as_authorization_subject(options = {})
  role = options[:role_class_name] || Acl9::config[:default_role_class_name]
  has_and_belongs_to_many :roles, :class_name => role

  cattr_accessor :_auth_role_class_name
  self._auth_role_class_name = role

  include Acl9::ModelExtensions::Subject 
end