New validate_on/before syntax.

This commit is contained in:
benni 2011-05-11 11:17:02 +02:00
parent d09d3922c1
commit 8db221ca5f
4 changed files with 10 additions and 7 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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}