moves geozones from poll question to poll in models
This commit is contained in:
@@ -6,6 +6,8 @@ class Poll < ActiveRecord::Base
|
|||||||
has_many :officers, through: :officer_assignments
|
has_many :officers, through: :officer_assignments
|
||||||
has_many :questions
|
has_many :questions
|
||||||
|
|
||||||
|
has_and_belongs_to_many :geozones
|
||||||
|
|
||||||
validates :name, presence: true
|
validates :name, presence: true
|
||||||
|
|
||||||
validate :date_range
|
validate :date_range
|
||||||
@@ -14,6 +16,7 @@ class Poll < ActiveRecord::Base
|
|||||||
scope :incoming, -> { where('? < starts_at', Time.current) }
|
scope :incoming, -> { where('? < starts_at', Time.current) }
|
||||||
scope :expired, -> { where('ends_at < ?', Time.current) }
|
scope :expired, -> { where('ends_at < ?', Time.current) }
|
||||||
scope :published, -> { where('published = ?', true) }
|
scope :published, -> { where('published = ?', true) }
|
||||||
|
scope :by_geozone_id, ->(geozone_id) { where(geozones: {id: geozone_id}.joins(:geozones)) }
|
||||||
|
|
||||||
scope :sort_for_list, -> { order(:starts_at) }
|
scope :sort_for_list, -> { order(:starts_at) }
|
||||||
|
|
||||||
@@ -30,12 +33,15 @@ class Poll < ActiveRecord::Base
|
|||||||
end
|
end
|
||||||
|
|
||||||
def answerable_by?(user)
|
def answerable_by?(user)
|
||||||
user.present? && user.level_two_or_three_verified? && current?
|
user.present? &&
|
||||||
|
user.level_two_or_three_verified? &&
|
||||||
|
current? &&
|
||||||
|
(!geozone_restricted || geozone_ids.include?(user.geozone_id))
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.answerable_by(user)
|
def self.answerable_by(user)
|
||||||
return none if user.nil? || user.unverified?
|
return none if user.nil? || user.unverified?
|
||||||
current
|
current.joins(:geozones).where('geozone_restricted = ? or geozones.id = ?', false, user.geozone_id)
|
||||||
end
|
end
|
||||||
|
|
||||||
def document_has_voted?(document_number, document_type)
|
def document_has_voted?(document_number, document_type)
|
||||||
|
|||||||
@@ -11,7 +11,6 @@ class Poll::Question < ActiveRecord::Base
|
|||||||
has_many :comments, as: :commentable
|
has_many :comments, as: :commentable
|
||||||
has_many :answers
|
has_many :answers
|
||||||
has_many :partial_results
|
has_many :partial_results
|
||||||
has_and_belongs_to_many :geozones
|
|
||||||
belongs_to :proposal
|
belongs_to :proposal
|
||||||
|
|
||||||
validates :title, presence: true
|
validates :title, presence: true
|
||||||
@@ -21,9 +20,7 @@ class Poll::Question < ActiveRecord::Base
|
|||||||
validates :title, length: { in: 4..Poll::Question.title_max_length }
|
validates :title, length: { in: 4..Poll::Question.title_max_length }
|
||||||
validates :description, length: { maximum: Poll::Question.description_max_length }
|
validates :description, length: { maximum: Poll::Question.description_max_length }
|
||||||
|
|
||||||
scope :no_poll, -> { where(poll_id: nil) }
|
scope :by_poll_id, ->(poll_id) { where(poll_id: poll_id) }
|
||||||
scope :by_poll_id, -> (poll_id) { where(poll_id: poll_id) }
|
|
||||||
scope :by_geozone_id, -> (geozone_id) { where(geozones: {id: geozone_id}.joins(:geozones)) }
|
|
||||||
|
|
||||||
scope :sort_for_list, -> { order('poll_questions.proposal_id IS NULL', :created_at)}
|
scope :sort_for_list, -> { order('poll_questions.proposal_id IS NULL', :created_at)}
|
||||||
scope :for_render, -> { includes(:author, :proposal) }
|
scope :for_render, -> { includes(:author, :proposal) }
|
||||||
@@ -41,9 +38,7 @@ class Poll::Question < ActiveRecord::Base
|
|||||||
summary => 'B',
|
summary => 'B',
|
||||||
description => 'C',
|
description => 'C',
|
||||||
author.username => 'C',
|
author.username => 'C',
|
||||||
author_visible_name => 'C',
|
author_visible_name => 'C' }
|
||||||
geozones.pluck(:name).join(' ') => 'C'
|
|
||||||
}
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def description
|
def description
|
||||||
@@ -62,29 +57,17 @@ class Poll::Question < ActiveRecord::Base
|
|||||||
self.title = proposal.title
|
self.title = proposal.title
|
||||||
self.description = proposal.description
|
self.description = proposal.description
|
||||||
self.summary = proposal.summary
|
self.summary = proposal.summary
|
||||||
self.all_geozones = true
|
|
||||||
self.valid_answers = I18n.t('poll_questions.default_valid_answers')
|
self.valid_answers = I18n.t('poll_questions.default_valid_answers')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def answerable_by?(user)
|
def answerable_by?(user)
|
||||||
poll.answerable_by?(user) && (self.all_geozones || self.geozone_ids.include?(user.geozone_id))
|
poll.answerable_by?(user)
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.answerable_by(user)
|
def self.answerable_by(user)
|
||||||
return none if user.nil? || user.unverified?
|
return none if user.nil? || user.unverified?
|
||||||
|
where(poll_id: Poll.answerable_by(user).pluck(:id))
|
||||||
where(poll_id: answerable_polls(user),
|
|
||||||
geozones: { id: answerable_geozones(user) }).
|
|
||||||
joins(:geozones)
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.answerable_polls(user)
|
|
||||||
Poll.answerable_by(user)
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.answerable_geozones(user)
|
|
||||||
user.geozone || Geozone.city
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user