Skip to content

Commit

Permalink
Replacements for :finder_sql and :counter_sql.
Browse files Browse the repository at this point in the history
  • Loading branch information
coryveilleux committed Oct 5, 2014
1 parent 883fe64 commit 85f7d48
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions lib/acl9/model_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 85f7d48

Please sign in to comment.