We were inconsistent on this one. I consider it particularly useful when a method starts with a `return` statement. In other cases, we probably shouldn't have a guard rule in the middle of a method in any case, but that's a different refactoring.
24 lines
762 B
Ruby
24 lines
762 B
Ruby
class DirectMessage < ApplicationRecord
|
|
belongs_to :sender, class_name: "User", foreign_key: "sender_id"
|
|
belongs_to :receiver, class_name: "User", foreign_key: "receiver_id"
|
|
|
|
validates :title, presence: true
|
|
validates :body, presence: true
|
|
validates :sender, presence: true
|
|
validates :receiver, presence: true
|
|
validate :max_per_day
|
|
|
|
scope :today, lambda { where(created_at: Date.current.beginning_of_day..Date.current.end_of_day) }
|
|
|
|
def max_per_day
|
|
return if errors.any?
|
|
|
|
max = Setting[:direct_message_max_per_day]
|
|
return unless max
|
|
|
|
if sender.direct_messages_sent.today.count >= max.to_i
|
|
errors.add(:title, I18n.t("activerecord.errors.models.direct_message.attributes.max_per_day.invalid"))
|
|
end
|
|
end
|
|
end
|