From dba5fd23e1083ad61baef544f2dc050d4ac07b83 Mon Sep 17 00:00:00 2001 From: albert Date: Wed, 6 Mar 2013 20:13:55 -0500 Subject: [PATCH] fixes #778 --- app/models/dmail.rb | 10 ++++++++++ test/unit/dmail_test.rb | 13 +++++++++++++ 2 files changed, 23 insertions(+) diff --git a/app/models/dmail.rb b/app/models/dmail.rb index 813e90afb..e7d0bd86d 100644 --- a/app/models/dmail.rb +++ b/app/models/dmail.rb @@ -3,6 +3,7 @@ class Dmail < ActiveRecord::Base validates_presence_of :from_id validates_format_of :title, :with => /\S/ validates_format_of :body, :with => /\S/ + validate :validate_sender_is_not_banned before_validation :initialize_from_id, :on => :create belongs_to :owner, :class_name => "User" belongs_to :to, :class_name => "User" @@ -153,6 +154,15 @@ class Dmail < ActiveRecord::Base include FactoryMethods extend SearchMethods + def validate_sender_is_not_banned + if from.is_banned? + errors[:base] = "Sender is banned and cannot send messages" + return false + else + return true + end + end + def quoted_body "[quote]#{body}[/quote]" end diff --git a/test/unit/dmail_test.rb b/test/unit/dmail_test.rb index 5443bd87b..d5066a66a 100644 --- a/test/unit/dmail_test.rb +++ b/test/unit/dmail_test.rb @@ -16,6 +16,19 @@ class DmailTest < ActiveSupport::TestCase CurrentUser.user = nil end + context "from a banned user" do + setup do + @user.update_attribute(:is_banned, true) + end + + should "not validate" do + dmail = FactoryGirl.build(:dmail, :title => "xxx", :owner => @user) + dmail.save + assert_equal(1, dmail.errors.size) + assert_equal(["Sender is banned and cannot send messages"], dmail.errors.full_messages) + end + end + context "search" do should "return results based on title contents" do dmail = FactoryGirl.create(:dmail, :title => "xxx", :owner => @user)