Files
nairobi/app/models/poll.rb
iagirre 166caa3b4c Deleted code in views and concerns that lets polls have more images and documentes attached, like it was at first.
Added missing text for english and new texts in english and spanish.

 Cambios para hacer commit:
	modificado:    app/assets/stylesheets/participation.scss
	modificado:    app/models/concerns/imageable.rb
	modificado:    app/models/poll.rb
	modificado:    app/views/admin/poll/polls/_form.html.erb
	modificado:    app/views/images/_admin_image.html.erb
	modificado:    config/locales/en/images.yml
	modificado:    config/locales/es/admin.yml
	modificado:    config/locales/es/images.yml
	modificado:    spec/features/admin/poll/polls_spec.rb
2017-10-02 12:59:26 +02:00

73 lines
2.3 KiB
Ruby

class Poll < ActiveRecord::Base
include Imageable
has_many :booth_assignments, class_name: "Poll::BoothAssignment"
has_many :booths, through: :booth_assignments
has_many :partial_results, through: :booth_assignments
has_many :white_results, through: :booth_assignments
has_many :null_results, through: :booth_assignments
has_many :total_results, through: :booth_assignments
has_many :voters
has_many :officer_assignments, through: :booth_assignments
has_many :officers, through: :officer_assignments
has_many :questions
has_and_belongs_to_many :geozones
validates :name, presence: true
validate :date_range
scope :current, -> { where('starts_at <= ? and ? <= ends_at', Time.current, Time.current) }
scope :incoming, -> { where('? < starts_at', Time.current) }
scope :expired, -> { where('ends_at < ?', Time.current) }
scope :published, -> { where('published = ?', true) }
scope :by_geozone_id, ->(geozone_id) { where(geozones: {id: geozone_id}.joins(:geozones)) }
scope :sort_for_list, -> { order(:geozone_restricted, :starts_at, :name) }
def current?(timestamp = DateTime.current)
starts_at <= timestamp && timestamp <= ends_at
end
def incoming?(timestamp = DateTime.current)
timestamp < starts_at
end
def expired?(timestamp = DateTime.current)
ends_at < timestamp
end
def self.current_or_incoming
current + incoming
end
def answerable_by?(user)
user.present? &&
user.level_two_or_three_verified? &&
current? &&
(!geozone_restricted || geozone_ids.include?(user.geozone_id))
end
def self.answerable_by(user)
return none if user.nil? || user.unverified?
current.joins('LEFT JOIN "geozones_polls" ON "geozones_polls"."poll_id" = "polls"."id"')
.where('geozone_restricted = ? OR geozones_polls.geozone_id = ?', false, user.geozone_id)
end
def votable_by?(user)
!document_has_voted?(user.document_number, user.document_type)
end
def document_has_voted?(document_number, document_type)
voters.where(document_number: document_number, document_type: document_type).exists?
end
def date_range
unless starts_at.present? && ends_at.present? && starts_at <= ends_at
errors.add(:starts_at, I18n.t('errors.messages.invalid_date_range'))
end
end
end