diff --git a/app/logical/user_promotion.rb b/app/logical/user_promotion.rb index a37ad1a33..d6a2d0e83 100644 --- a/app/logical/user_promotion.rb +++ b/app/logical/user_promotion.rb @@ -14,6 +14,7 @@ class UserPromotion validate create_transaction_log_item create_user_feedback + create_dmail user.save end @@ -46,7 +47,32 @@ private user.feedback.create( :category => "neutral", - :body => "#{body_prefix} by #{promoter.name} from #{user.level_string_was} to #{user.level_string}" + :body => "#{body_prefix} by #{promoter.name} from #{user.level_string_was} to #{user.level_string}", + :disable_dmail_notification => true + ) + end + + def create_dmail + if user.level >= user.level_was + create_promotion_dmail + else + create_demotion_dmail + end + end + + def create_promotion_dmail + Dmail.create_split( + :to_id => user.id, + :title => "You have been promoted", + :body => "You have been promoted to a #{user.level_string} level account." + ) + end + + def create_demotion_dmail + Dmail.create_split( + :to_id => user.id, + :title => "You have been demoted", + :body => "You have been demoted to a #{user.level_string} level account." ) end end diff --git a/app/models/user_feedback.rb b/app/models/user_feedback.rb index ed8f5e7ff..70933353b 100644 --- a/app/models/user_feedback.rb +++ b/app/models/user_feedback.rb @@ -3,7 +3,8 @@ class UserFeedback < ActiveRecord::Base belongs_to :user belongs_to :creator, :class_name => "User" before_validation :initialize_creator, :on => :create - attr_accessible :body, :user_id, :category, :user_name + attr_accessor :disable_dmail_notification + attr_accessible :body, :user_id, :category, :user_name, :disable_dmail_notification validates_presence_of :user, :creator, :body, :category validates_inclusion_of :category, :in => %w(positive negative neutral) validate :creator_is_gold @@ -74,8 +75,10 @@ class UserFeedback < ActiveRecord::Base end def create_dmail - body = %{#{creator_name} created a "#{category} record":/user_feedbacks?search[user_id]=#{user_id} for your account. #{body}} - Dmail.create_split(:to_id => user_id, :title => "Your user record has been updated", :body => body) + unless disable_dmail_notification + body = %{#{creator_name} created a "#{category} record":/user_feedbacks?search[user_id]=#{user_id} for your account. #{body}} + Dmail.create_split(:to_id => user_id, :title => "Your user record has been updated", :body => body) + end end def creator_is_gold diff --git a/test/unit/user_test.rb b/test/unit/user_test.rb index dddb9741b..297f09d08 100644 --- a/test/unit/user_test.rb +++ b/test/unit/user_test.rb @@ -32,6 +32,12 @@ class UserTest < ActiveSupport::TestCase assert_equal("Promoted by #{CurrentUser.user.name} from Member to Gold", @user.feedback.last.body) end + + should "create a dmail" do + assert_difference("Dmail.count", 2) do + @user.promote_to!(User::Levels::GOLD) + end + end end context "favoriting a post" do