Files
grecia/app/models/direct_message.rb
Javi Martín 3ba8b05cbb Use Date#all_day in DirectMessage.today
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.
2020-04-27 19:26:37 +02:00

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