From 8db221ca5f8ce07ca568e06a2e60fb1b4a4229d8 Mon Sep 17 00:00:00 2001 From: benni Date: Wed, 11 May 2011 11:17:02 +0200 Subject: [PATCH] New validate_on/before syntax. --- app/models/group.rb | 5 +++-- app/models/invite.rb | 4 +++- app/models/message.rb | 4 ++-- app/models/page.rb | 4 ++-- 4 files changed, 10 insertions(+), 7 deletions(-) diff --git a/app/models/group.rb b/app/models/group.rb index 36953268..468c06d5 100644 --- a/app/models/group.rb +++ b/app/models/group.rb @@ -6,6 +6,7 @@ class Group < ActiveRecord::Base validates_length_of :name, :in => 1..25 validates_uniqueness_of :name + validate :last_admin_on_earth, :on => :update # Returns true if the given user if is an member of this group. def member?(user) @@ -32,8 +33,8 @@ class Group < ActiveRecord::Base end # add validation check on update - def validate_on_update - # error if this is the last group with admin role and role_admin should set to false + # Return an error if this is the last group with admin role and role_admin should set to false + def last_admin_on_earth if self.role_admin == false && Group.find_all_by_role_admin(true).size == 1 && self == Group.find(:first, :conditions => "role_admin = 1") errors.add(:role_admin, "Der letzten Gruppe mit Admin-Rechten darf die Admin-Rolle nicht entzogen werden") end diff --git a/app/models/invite.rb b/app/models/invite.rb index 88d802ae..b9c921eb 100644 --- a/app/models/invite.rb +++ b/app/models/invite.rb @@ -10,6 +10,8 @@ class Invite < ActiveRecord::Base validates_presence_of :group validates_presence_of :token validates_presence_of :expires_at + validate :email_not_already_registered, :on => :create + protected @@ -27,7 +29,7 @@ class Invite < ActiveRecord::Base private # Custom validation: check that email does not already belong to a registered user. - def validate_on_create + def email_not_already_registered unless User.find_by_email(self.email).nil? errors.add(:email, 'ist bereits in Verwendung. Person ist schon Mitglied der Foodcoop.') end diff --git a/app/models/message.rb b/app/models/message.rb index 9604d261..ccfd9768 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -19,9 +19,9 @@ class Message < ActiveRecord::Base validates_length_of :subject, :in => 1..255 validates_inclusion_of :email_state, :in => EMAIL_STATE.values + before_validation :clean_up_recipient_ids, :on => :create - # clean up the recipients_ids - def before_validation_on_create + def clean_up_recipient_ids self.recipients_ids = recipients_ids.uniq.reject { |id| id.blank? } unless recipients_ids.nil? self.recipients_ids = User.all.collect(&:id) if sent_to_all == "1" end diff --git a/app/models/page.rb b/app/models/page.rb index 65b55e28..3ab33612 100644 --- a/app/models/page.rb +++ b/app/models/page.rb @@ -11,8 +11,8 @@ class Page < ActiveRecord::Base validates_presence_of :title, :body validates_uniqueness_of :permalink, :title - before_validation_on_create :set_permalink - before_validation_on_update :update_permalink + before_validation :set_permalink, :on => :create + before_validation :update_permalink, :on => :update after_update :create_redirect named_scope :non_redirected, :conditions => {:redirect => nil}