lib/acl9/model_extensions.rb in acl9-0.11.0 vs lib/acl9/model_extensions.rb in acl9-0.12.0

- old
+ new

@@ -1,7 +1,7 @@ -require File.join(File.dirname(__FILE__), 'model_extensions', 'subject') -require File.join(File.dirname(__FILE__), 'model_extensions', 'object') +require File.join(File.dirname(__FILE__), 'model_extensions', 'for_subject') +require File.join(File.dirname(__FILE__), 'model_extensions', 'for_object') module Acl9 module ModelExtensions #:nodoc: def self.included(base) base.extend(ClassMethods) @@ -14,10 +14,12 @@ # @param [Hash] options the options for tuning # @option options [String] :role_class_name (Acl9::config[:default_role_class_name]) # Class name of the role class (e.g. 'AccountRole') # @option options [String] :join_table_name (Acl9::config[:default_join_table_name]) # Join table name (e.g. 'accounts_account_roles') + # @option options [String] :association_name (Acl9::config[:default_association_name]) + # Association name (e.g. ':roles') # @example # class User < ActiveRecord::Base # acts_as_authorization_subject # end # @@ -29,21 +31,25 @@ # # other functions from Acl9::ModelExtensions::Subject are made available # # @see Acl9::ModelExtensions::Subject # def acts_as_authorization_subject(options = {}) + assoc = options[:association_name] || Acl9::config[:default_association_name] role = options[:role_class_name] || Acl9::config[:default_role_class_name] join_table = options[:join_table_name] || Acl9::config[:default_join_table_name] || join_table_name(undecorated_table_name(self.to_s), undecorated_table_name(role)) - has_and_belongs_to_many :role_objects, :class_name => role, :join_table => join_table + has_and_belongs_to_many assoc, :class_name => role, :join_table => join_table - cattr_accessor :_auth_role_class_name, :_auth_subject_class_name + cattr_accessor :_auth_role_class_name, :_auth_subject_class_name, + :_auth_role_assoc_name + self._auth_role_class_name = role self._auth_subject_class_name = self.to_s + self._auth_role_assoc_name = assoc - include Acl9::ModelExtensions::Subject + include Acl9::ModelExtensions::ForSubject end # Add role query and set methods to the class (making it an auth object class). # # @param [Hash] options the options for tuning @@ -79,20 +85,20 @@ 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} + AND authorizable_id = #{column_for_attribute(self.class.primary_key).text? ? "'#{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 + include Acl9::ModelExtensions::ForObject end # Make a class an auth role class. # # You'll probably never create or use objects of this class directly.