Permalink
Browse files

Allowing configurable roles table name.

This is for compatibility with Spree, which has its own roles table.
  • Loading branch information...
1 parent 1b248fd commit 1aab965145fc78fa2afed12a2999b7797cd521d4 @crystalneth crystalneth committed Mar 4, 2010
Showing with 12 additions and 12 deletions.
  1. +9 −9 app/models/sanction/role.rb
  2. +3 −3 lib/sanction/principal/base.rb
@@ -1,7 +1,7 @@
# Instances of Roles within the system. Uses double-sided polymorphism to attribute
# roles to principals over permissionables. Allows blanket class attributation.
#
-class Sanction::Role < ActiveRecord::Base
+class Sanction::Role < ActiveRecord::Base
#--------------------------------------------------#
# Associations #
#--------------------------------------------------#
@@ -22,13 +22,13 @@ def valid_role_definition
# See if the intent of this role is captured by another role
def uniqueness_of_intent
conds = []
- conds << ["roles.principal_type = ? AND (roles.principal_id = ? OR roles.principal_id IS NULL)", principal_type, (principal_id || "")]
- conds << ["roles.name = ?", name]
+ conds << ["#{table_name}.principal_type = ? AND (#{table_name}.principal_id = ? OR #{table_name}.principal_id IS NULL)", principal_type, (principal_id || "")]
+ conds << ["#{table_name}.name = ?", name]
if global?
- conds << ["roles.global = ?", true]
+ conds << ["#{table_name}.global = ?", true]
else
- conds << ["roles.permissionable_type = ? AND (roles.permissionable_id = ? OR roles.permissionable_id IS NULL)", permissionable_type, (permissionable_id || "")]
+ conds << ["#{table_name}.permissionable_type = ? AND (#{table_name}.permissionable_id = ? OR #{table_name}.permissionable_id IS NULL)", permissionable_type, (permissionable_id || "")]
end
conditions = conds.map {|c| self.class.merge_conditions(c)}.join(" AND ")
@@ -58,11 +58,11 @@ def uniqueness_of_intent
conds = []
permissionables_by_klass.each do |(klass, ids)|
- conds << ["roles.permissionable_type = ? AND (roles.permissionable_id IN (?) OR roles.permissionable_id IS NULL)", klass, ids]
+ conds << ["#{table_name}.permissionable_type = ? AND (#{table_name}.permissionable_id IN (?) OR #{table_name}.permissionable_id IS NULL)", klass, ids]
end
conditions = conds.map { |c| merge_conditions(c) }.join(" OR ")
- {:select => "DISTINCT roles.*", :conditions => conditions}
+ {:select => "DISTINCT #{table_name}.*", :conditions => conditions}
}
# Expects an array of Principal instances.
@@ -77,11 +77,11 @@ def uniqueness_of_intent
conds = []
pricipals_by_klass.each do |(klass, ids)|
- conds << ["roles.principal_type = ? AND (roles.principal_id IN (?) OR roles.principal_id IS NULL)", klass, ids]
+ conds << ["#{table_name}.principal_type = ? AND (#{table_name}.principal_id IN (?) OR #{table_name}.principal_id IS NULL)", klass, ids]
end
conditions = conds.map { |c| merge_conditions(c) }.join(" OR ")
- {:select => "DISTINCT roles.*", :conditions => conditions}
+ {:select => "DISTINCT #{table_name}.*", :conditions => conditions}
}
#--------------------------------------------------#
@@ -4,7 +4,7 @@ module Base
def self.extended(base)
base.class_eval %q{
has_many :principal_roles, :as => :principal, :class_name => "Sanction::Role",
- :finder_sql => 'SELECT * FROM roles WHERE roles.principal_type = "#{self.class.name.to_s}" AND (roles.principal_id = "#{id}" OR roles.principal_id IS NULL)'
+ :finder_sql => 'SELECT * FROM #{Sanction::Role.table_name} WHERE #{Sanction::Role.table_name}.principal_type = "#{self.class.name.to_s}" AND (#{Sanction::Role.table_name}.principal_id = "#{id}" OR #{Sanction::Role.table_name}.principal_id IS NULL)'
has_many :specific_principal_roles, :as => :principal, :class_name => "Sanction::Role", :dependent => :destroy
}
@@ -14,7 +14,7 @@ def self.extended(base)
returned_scope = {:conditions => ["#{ROLE_ALIAS}.principal_type = ?", base.name.to_s], :select => "DISTINCT #{base.table_name}.*"}
unless already_joined
- returned_scope.merge( {:joins => "INNER JOIN roles AS #{ROLE_ALIAS} ON #{ROLE_ALIAS}.principal_type = '#{base.name.to_s}' AND
+ returned_scope.merge( {:joins => "INNER JOIN #{Sanction::Role.table_name} AS #{ROLE_ALIAS} ON #{ROLE_ALIAS}.principal_type = '#{base.name.to_s}' AND
(#{ROLE_ALIAS}.principal_id = #{base.table_name}.#{base.primary_key.to_s} OR #{ROLE_ALIAS}.principal_id IS NULL)"} )
end
}
@@ -24,7 +24,7 @@ def self.extended(base)
returned_scope = {:conditions => ["#{klass_instance.class.table_name}.#{klass_instance.class.primary_key.to_s} = ?", klass_instance.id], :select => "DISTINCT #{klass_instance.class.table_name.to_s}.*"}
unless already_joined
- returned_scope.merge({:joins => "INNER JOIN roles AS #{ROLE_ALIAS} ON #{ROLE_ALIAS}.principal_type = '#{klass_instance.class.name.to_s}' AND
+ returned_scope.merge({:joins => "INNER JOIN #{Sanction::Role.table_name} AS #{ROLE_ALIAS} ON #{ROLE_ALIAS}.principal_type = '#{klass_instance.class.name.to_s}' AND
(#{ROLE_ALIAS}.principal_id = '#{klass_instance.id}' OR #{ROLE_ALIAS}.principal_id IS NULL)"})
end
}

0 comments on commit 1aab965

Please sign in to comment.