Skip to content

Commit

Permalink
uniqueness of intent moved to validate_on_create
Browse files Browse the repository at this point in the history
  • Loading branch information
enigma314 committed Oct 17, 2009
1 parent 0f590ed commit 6330364
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
4 changes: 3 additions & 1 deletion app/models/sanction/role.rb
Expand Up @@ -11,7 +11,8 @@ class Sanction::Role < ActiveRecord::Base
validates_presence_of :name

validate :valid_role_definition
validate :uniqueness_of_intent

validate_on_create :uniqueness_of_intent

# Ensure the role is valid by definition
def valid_role_definition
Expand All @@ -33,6 +34,7 @@ def uniqueness_of_intent
conditions = conds.map {|c| self.class.merge_conditions(c)}.join(" AND ")
if Sanction::Role.exists?([conditions])
errors.add_to_base("This role is already captured by another.")
false
else
true
end
Expand Down
4 changes: 2 additions & 2 deletions tasks/sanction_tasks.rake
Expand Up @@ -41,15 +41,15 @@ namespace :sanction do

desc "Validate the current roles table by validating against the Sanction::Role::Definitions currently configured."
task :validate => :environment do
invalid_roles, valid_roles = (Sanction::Role.all || []).partition {|role| role.is_valid?}
valid_roles, invalid_roles = (Sanction::Role.all || []).partition {|role| role.valid?}

puts "#{invalid_roles.size} invalid roles."
puts "#{valid_roles.size} valid roles."
end

desc "Validates the current roles table and removes invalid roles according to the Sanction::Role::Definitions currently configured."
task :cleanse => :environment do
invalid_roles, valid_roles = (Sanction::Role.all || []).partition {|role| role.is_valid?}
valid_roles, invalid_roles = (Sanction::Role.all || []).partition {|role| role.valid?}

puts "#{invalid_roles.size} invalid roles."

Expand Down

0 comments on commit 6330364

Please sign in to comment.