diff --git a/lib/acl9/model_extensions.rb b/lib/acl9/model_extensions.rb index c26ba6b..26f2ade 100644 --- a/lib/acl9/model_extensions.rb +++ b/lib/acl9/model_extensions.rb @@ -91,15 +91,20 @@ def acts_as_authorization_object(options = {}) has_many :accepted_roles, :as => :authorizable, :class_name => role, :dependent => :destroy has_many :"#{subj_table}", - :finder_sql => proc { "SELECT DISTINCT #{subj_table}.* " + + -> { + subj_table.constantize.find_by_sql("SELECT DISTINCT #{subj_table}.* " + "FROM #{subj_table} INNER JOIN #{join_table} ON #{subj_col}_id = #{subj_table}.id " + "INNER JOIN #{role_table} ON #{role_table}.id = #{role.underscore}_id " + - "WHERE authorizable_type = '#{self.class.base_class.to_s}' AND authorizable_id = #{id} "}, - :counter_sql => proc { "SELECT COUNT(DISTINCT #{subj_table}.id)" + - "FROM #{subj_table} INNER JOIN #{join_table} ON #{subj_col}_id = #{subj_table}.id " + - "INNER JOIN #{role_table} ON #{role_table}.id = #{role.underscore}_id " + - "WHERE authorizable_type = '#{self.class.base_class.to_s}' AND authorizable_id = #{id} "}, - :readonly => true + "WHERE authorizable_type = '#{self.class.base_class.to_s}' AND authorizable_id = #{id} ") + }, + :readonly => true do + def count + subj_table.constantize.count_by_sql("SELECT COUNT(DISTINCT #{subj_table}.id)" + + "FROM #{subj_table} INNER JOIN #{join_table} ON #{subj_col}_id = #{subj_table}.id " + + "INNER JOIN #{role_table} ON #{role_table}.id = #{role.underscore}_id " + + "WHERE authorizable_type = '#{self.class.base_class.to_s}' AND authorizable_id = #{id} ") + end + end include Acl9::ModelExtensions::ForObject end