Files
nairobi/app/models/legislation/question.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

55 lines
1.7 KiB
Ruby

class Legislation::Question < ApplicationRecord
acts_as_paranoid column: :hidden_at
include ActsAsParanoidAliases
include Notifiable
translates :title, touch: true
include Globalizable
belongs_to :author, -> { with_hidden }, class_name: "User", inverse_of: :legislation_questions
belongs_to :process, foreign_key: "legislation_process_id", inverse_of: :questions
has_many :question_options, -> { order(:id) }, class_name: "Legislation::QuestionOption", foreign_key: "legislation_question_id",
dependent: :destroy, inverse_of: :question
has_many :answers, class_name: "Legislation::Answer", foreign_key: "legislation_question_id", dependent: :destroy, inverse_of: :question
has_many :comments, as: :commentable, inverse_of: :commentable, dependent: :destroy
accepts_nested_attributes_for :question_options, reject_if: proc { |attributes| attributes.all? { |k, v| v.blank? } }, allow_destroy: true
validates :process, presence: true
validates_translation :title, presence: true
scope :sorted, -> { order("id ASC") }
def next_question_id
@next_question_id ||= process.questions.where("id > ?", id).sorted.limit(1).pluck(:id).first
end
def first_question_id
@first_question_id ||= process.questions.sorted.limit(1).pluck(:id).first
end
def answer_for_user(user)
answers.find_by(user: user)
end
def comments_for_verified_residents_only?
true
end
def comments_closed?
!comments_open?
end
def comments_open?
process.debate_phase.open?
end
def best_comments(number)
Comment.where(commentable_id: id)
.where(commentable_type: "Legislation::Question")
.order("cached_votes_up - cached_votes_down DESC")
.take(number)
end
end