Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

issue #5: postgres compatibility & configurable table_name

  • Loading branch information...
commit b57951ecd908a223161f56a1a3f6bd170a1260c6 2 parents ed5614c + 442b1a7
@matthewvermaak authored
View
34 app/models/sanction/role.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 << ["#{self.class.table_name}.principal_type = ? AND (#{self.class.table_name}.principal_id = ? OR #{self.class.table_name}.principal_id IS NULL)", principal_type, (principal_id || nil)]
+ conds << ["#{self.class.table_name}.name = ?", name]
if global?
- conds << ["roles.global = ?", true]
+ conds << ["#{self.class.table_name}.global = ?", true]
else
- conds << ["roles.permissionable_type = ? AND (roles.permissionable_id = ? OR roles.permissionable_id IS NULL)", permissionable_type, (permissionable_id || "")]
+ conds << ["#{self.class.table_name}.permissionable_type = ? AND (#{self.class.table_name}.permissionable_id = ? OR #{self.class.table_name}.permissionable_id IS NULL)", permissionable_type, (permissionable_id || nil)]
end
conditions = conds.map {|c| self.class.merge_conditions(c)}.join(" AND ")
@@ -47,7 +47,7 @@ def uniqueness_of_intent
named_scope :global, :conditions => {:global => true }
# Expects an array of Permissionable instances or klasses
- named_scope :over, lambda {|*permissionable_set|
+ named_scope( :over, lambda {|*permissionable_set|
permissionables_by_klass = {}
blanket_permissionables = []
permissionable_set.each do |perm|
@@ -61,18 +61,18 @@ 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 << ["#{self.table_name}.permissionable_type = ? AND (#{self.table_name}.permissionable_id IN (?) OR #{self.table_name}.permissionable_id IS NULL)", klass, ids]
end
- blanket_permissionables.each do |klass|
- conds << ["roles.permissionable_type = ?", klass]
+ blanket_permissionables.each do |klass|
+ conds << ["#{self.table_name}.permissionable_type = ?", klass]
end
conditions = conds.map { |c| merge_conditions(c) }.join(" OR ")
- {:select => "DISTINCT roles.*", :conditions => conditions}
- }
+ {:select => "DISTINCT #{self.table_name}.*", :conditions => conditions}
+ })
# Expects an array of Principal instances or klasses
- named_scope :for, lambda {|*principal_set|
+ named_scope( :for, lambda {|*principal_set|
pricipals_by_klass = {}
blanket_principals = []
principal_set.each do |prin|
@@ -86,15 +86,15 @@ 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 << ["#{self.table_name}.principal_type = ? AND (#{self.table_name}.principal_id IN (?) OR #{self.table_name}.principal_id IS NULL)", klass, ids]
end
- blanket_principals.each do |klass|
- conds << ["roles.principal_type = ?", klass]
+ blanket_principals.each do |klass|
+ conds << ["#{self.table_name}.principal_type = ?", klass]
end
conditions = conds.map { |c| merge_conditions(c) }.join(" OR ")
- {:select => "DISTINCT roles.*", :conditions => conditions}
- }
+ {:select => "DISTINCT #{self.table_name}.*", :conditions => conditions}
+ })
#--------------------------------------------------#
# Convenience #
View
10 lib/sanction/permissionable/base.rb
@@ -19,8 +19,9 @@ def self.permissionable_roles
returned_scope = {:conditions => ["#{ROLE_ALIAS}.permissionable_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}.permissionable_type = '#{base.name.to_s}' AND
- (#{ROLE_ALIAS}.permissionable_id = #{base.table_name}.#{base.primary_key.to_s} OR #{ROLE_ALIAS}.permissionable_id IS NULL)"})
+ returned_scope.merge({:joins => "INNER JOIN #{Sanction::Role.table_name} AS #{ROLE_ALIAS} ON (
+ (#{ROLE_ALIAS}.permissionable_id = #{base.table_name}.#{base.primary_key.to_s} OR #{ROLE_ALIAS}.permissionable_id IS NULL)
+ AND #{ROLE_ALIAS}.permissionable_type = '#{base.name.to_s}')"})
end
}
@@ -29,8 +30,9 @@ def self.permissionable_roles
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}.*"}
unless already_joined
- returned_scope.merge({:joins => "INNER JOIN roles AS #{ROLE_ALIAS} ON #{ROLE_ALIAS}.permissionable_type = '#{klass_instance.class.name.to_s}' AND
- (#{ROLE_ALIAS}.permissionable_id = '#{klass_instance.id}' OR #{ROLE_ALIAS}.permissionable_id IS NULL)"})
+ returned_scope.merge({:joins => "INNER JOIN #{Sanction::Role.table_name} AS #{ROLE_ALIAS} ON
+ (#{ROLE_ALIAS}.permissionable_id = '#{klass_instance.id}' OR #{ROLE_ALIAS}.permissionable_id IS NULL)
+ AND #{ROLE_ALIAS}.permissionable_type = '#{klass_instance.class.name.to_s}'"})
end
}
end
View
10 lib/sanction/principal/base.rb
@@ -19,8 +19,9 @@ def self.principal_roles
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
- (#{ROLE_ALIAS}.principal_id = #{base.table_name}.#{base.primary_key.to_s} OR #{ROLE_ALIAS}.principal_id IS NULL)"} )
+ returned_scope.merge( {:joins => "INNER JOIN #{Sanction::Role.table_name} AS #{ROLE_ALIAS} ON
+ (#{ROLE_ALIAS}.principal_id = #{base.table_name}.#{base.primary_key.to_s} OR #{ROLE_ALIAS}.principal_id IS NULL)
+ AND #{ROLE_ALIAS}.principal_type = '#{base.name.to_s}'"} )
end
}
@@ -29,8 +30,9 @@ def self.principal_roles
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
- (#{ROLE_ALIAS}.principal_id = '#{klass_instance.id}' OR #{ROLE_ALIAS}.principal_id IS NULL)"})
+ returned_scope.merge({:joins => "INNER JOIN #{Sanction::Role.table_name} AS #{ROLE_ALIAS} ON
+ (#{ROLE_ALIAS}.principal_id = '#{klass_instance.id}' OR #{ROLE_ALIAS}.principal_id IS NULL) AND
+ #{ROLE_ALIAS}.principal_type = '#{klass_instance.class.name.to_s}'"})
end
}
end
Please sign in to comment.
Something went wrong with that request. Please try again.