The method `Date#all_day` was added in Rails 5.1, and returns the same interval we were using while it makes the code simple.
24 lines
748 B
Ruby
24 lines
748 B
Ruby
class DirectMessage < ApplicationRecord
|
|
belongs_to :sender, class_name: "User", inverse_of: :direct_messages_sent
|
|
belongs_to :receiver, class_name: "User", inverse_of: :direct_messages_received
|
|
|
|
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.all_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
|