Files
nairobi/app/models/direct_message.rb
Javi Martín 42d2e5b3ad Apply Rails/InverseOf rubocop rule
Not doing so has a few gotchas when working with relations, particularly
with records which are not stored in the database.

I'm excluding the related content file because it's got a very peculiar
relationship with itself: the `has_one :opposite_related_content` has no
inverse; the relation itself is its inverse. It's a false positive since
the inverse condition is true:

```
content.opposite_related_content.opposite_related_content.object_id ==
  content.object_id
```
2019-10-25 19:29:12 +02:00

24 lines
782 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.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