Method: Acl9::ModelExtensions::ClassMethods#acts_as_authorization_object

Defined in:
lib/acl9/model_extensions.rb

#acts_as_authorization_object(options = {}) ⇒ Object

Add role query and set methods to the class (making it an auth object class).

Examples:

class Product < ActiveRecord::Base
  acts_as_authorization_object
end

product = Product.new
product.accepted_roles #=> returns Role objects, associated with the product
product.users          #=> returns User objects, associated with the product
product.accepts_role!(...)
product.accepts_no_role!(...)
# other functions from Acl9::ModelExtensions::Object are made available

Parameters:

  • options (Hash) (defaults to: {})

    the options for tuning

Options Hash (options):

  • :subject_class_name (String) — default: Acl9::config[:default_subject_class_name]

    Subject class name (e.g. ‘User’, or ‘Account)

  • :role_class_name (String) — default: Acl9::config[:default_role_class_name]

    Role class name (e.g. ‘AccountRole’)

See Also:

  • Object


74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/acl9/model_extensions.rb', line 74

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

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

  sql_tables = "    FROM \#{subj_table}\n    INNER JOIN \#{role_table}_\#{subj_table} ON \#{subj_col}_id = \#{subj_table}.id\n    INNER JOIN \#{role_table}               ON \#{role_table}.id = \#{role.underscore}_id\n  EOS\n\n  sql_where = <<-'EOS'\n    WHERE authorizable_type = '\#{self.class.base_class.to_s}'\n    AND authorizable_id = \#{column_for_attribute(self.class.primary_key).text? ? \"'\#{id}'\": id}\n  EOS\n\n  has_many :accepted_roles, :as => :authorizable, :class_name => role, :dependent => :destroy\n\n  has_many :\"\#{subj_table}\",\n    :finder_sql  => (\"SELECT DISTINCT \#{subj_table}.*\" + sql_tables + sql_where),\n    :counter_sql => (\"SELECT COUNT(DISTINCT \#{subj_table}.id)\" + sql_tables + sql_where),\n    :readonly => true\n\n  include Acl9::ModelExtensions::ForObject\nend\n"