Why: * Database stores created_at as timestamp with the timezone, so when comparing DATE(created_at) to something we have to convert it to DATE as well with the postresql native function, but using Time.current instead of Date.current to take into account the user timezone
23 lines
717 B
Ruby
23 lines
717 B
Ruby
class DirectMessage < ActiveRecord::Base
|
|
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('DATE(created_at) = DATE(?)', Time.current) }
|
|
|
|
def max_per_day
|
|
return if errors.any?
|
|
max = Setting[:direct_message_max_per_day]
|
|
|
|
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
|