Merge branch 'master' into feature/1929#add_shift_task_usage

This commit is contained in:
Bertocq
2017-10-03 12:01:21 +02:00
92 changed files with 1683 additions and 317 deletions

View File

@@ -45,7 +45,7 @@ module Abilities
can [:read, :update, :valuate, :destroy, :summary], SpendingProposal
can [:index, :read, :new, :create, :update, :destroy, :calculate_winners], Budget
can [:index, :read, :new, :create, :update, :destroy, :calculate_winners, :read_results], Budget
can [:read, :create, :update, :destroy], Budget::Group
can [:read, :create, :update, :destroy], Budget::Heading
can [:hide, :update, :toggle_selection], Budget::Investment

View File

@@ -4,6 +4,10 @@ module Followable
included do
has_many :follows, as: :followable, dependent: :destroy
has_many :followers, through: :follows, source: :user
scope :followed_by_user, -> (user){
joins(:follows).where("follows.user_id = ?", user.id)
}
end
def followed_by?(user)

View File

@@ -37,14 +37,21 @@ class Debate < ActiveRecord::Base
scope :sort_by_random, -> { reorder("RANDOM()") }
scope :sort_by_relevance, -> { all }
scope :sort_by_flags, -> { order(flags_count: :desc, updated_at: :desc) }
scope :sort_by_recommendations, -> { order(cached_votes_total: :desc) }
scope :last_week, -> { where("created_at >= ?", 7.days.ago)}
scope :featured, -> { where("featured_at is not null")}
scope :public_for_api, -> { all }
# Ahoy setup
visitable # Ahoy will automatically assign visit_id on create
attr_accessor :link_required
def self.recommendations(user)
tagged_with(user.interests, any: true).
where("author_id != ?", user.id)
end
def searchable_values
{ title => 'A',
author.username => 'B',
@@ -135,4 +142,9 @@ class Debate < ActiveRecord::Base
featured_at.present?
end
def self.debates_orders(user)
orders = %w{hot_score confidence_score created_at relevance}
orders << "recommendations" if user.present?
orders
end
end

View File

@@ -2,9 +2,7 @@ class Poll < ActiveRecord::Base
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 :recounts, through: :booth_assignments
has_many :voters
has_many :officer_assignments, through: :booth_assignments
has_many :officers, through: :officer_assignments
@@ -61,6 +59,10 @@ class Poll < ActiveRecord::Base
voters.where(document_number: document_number, document_type: document_type).exists?
end
def voted_in_booth?(user)
Poll::Voter.where(poll: self, user: user, origin: "booth").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'))

View File

@@ -8,12 +8,13 @@ class Poll::Answer < ActiveRecord::Base
validates :question, presence: true
validates :author, presence: true
validates :answer, presence: true
validates :answer, inclusion: {in: ->(a) { a.question.valid_answers }}
validates :answer, inclusion: { in: ->(a) { a.question.valid_answers }},
unless: ->(a) { a.question.blank? }
scope :by_author, ->(author_id) { where(author_id: author_id) }
scope :by_question, ->(question_id) { where(question_id: question_id) }
def record_voter_participation
Poll::Voter.create!(user: author, poll: poll)
Poll::Voter.find_or_create_by!(user: author, poll: poll, origin: "web")
end
end

View File

@@ -7,8 +7,6 @@ class Poll
has_many :officers, through: :officer_assignments
has_many :voters
has_many :partial_results
has_many :white_results
has_many :null_results
has_many :total_results
has_many :recounts
end
end

View File

@@ -3,9 +3,7 @@ class Poll
belongs_to :officer
belongs_to :booth_assignment
has_many :partial_results
has_many :white_results
has_many :null_results
has_many :total_results
has_many :recounts
has_many :voters
validates :officer_id, presence: true

View File

@@ -0,0 +1,36 @@
class Poll::Recount < ActiveRecord::Base
VALID_ORIGINS = %w{web booth letter}.freeze
belongs_to :author, -> { with_hidden }, class_name: 'User', foreign_key: 'author_id'
belongs_to :booth_assignment
belongs_to :officer_assignment
validates :author, presence: true
validates :origin, inclusion: {in: VALID_ORIGINS}
scope :web, -> { where(origin: 'web') }
scope :booth, -> { where(origin: 'booth') }
scope :letter, -> { where(origin: 'letter') }
scope :by_author, ->(author_id) { where(author_id: author_id) }
before_save :update_logs
def update_logs
amounts_changed = false
[:white, :null, :total].each do |amount|
next unless send("#{amount}_amount_changed?") && send("#{amount}_amount_was").present?
self["#{amount}_amount_log"] += ":#{send("#{amount}_amount_was")}"
amounts_changed = true
end
update_officer_author if amounts_changed
end
def update_officer_author
self.officer_assignment_id_log += ":#{officer_assignment_id_was}"
self.author_id_log += ":#{author_id_was}"
end
end

View File

@@ -1,5 +1,8 @@
class Poll
class Voter < ActiveRecord::Base
VALID_ORIGINS = %w{ web booth }
belongs_to :poll
belongs_to :user
belongs_to :geozone
@@ -10,9 +13,13 @@ class Poll
validates :user_id, presence: true
validates :document_number, presence: true, uniqueness: { scope: [:poll_id, :document_type], message: :has_voted }
validates :origin, inclusion: { in: VALID_ORIGINS }
before_validation :set_demographic_info, :set_document_info
scope :web, -> { where(origin: 'web') }
scope :booth, -> { where(origin: 'booth') }
def set_demographic_info
return if user.blank?

View File

@@ -58,14 +58,27 @@ class Proposal < ActiveRecord::Base
scope :sort_by_relevance, -> { all }
scope :sort_by_flags, -> { order(flags_count: :desc, updated_at: :desc) }
scope :sort_by_archival_date, -> { archived.sort_by_confidence_score }
scope :sort_by_recommendations, -> { order(cached_votes_up: :desc) }
scope :archived, -> { where("proposals.created_at <= ?", Setting["months_to_archive_proposals"].to_i.months.ago) }
scope :not_archived, -> { where("proposals.created_at > ?", Setting["months_to_archive_proposals"].to_i.months.ago) }
scope :last_week, -> { where("proposals.created_at >= ?", 7.days.ago)}
scope :retired, -> { where.not(retired_at: nil) }
scope :not_retired, -> { where(retired_at: nil) }
scope :successful, -> { where("cached_votes_up >= ?", Proposal.votes_needed_for_success) }
scope :unsuccessful, -> { where("cached_votes_up < ?", Proposal.votes_needed_for_success) }
scope :public_for_api, -> { all }
def self.recommendations(user)
tagged_with(user.interests, any: true).
where("author_id != ?", user.id).
unsuccessful.
not_followed_by_user(user)
end
def self.not_followed_by_user(user)
where.not(id: followed_by_user(user).pluck(:id))
end
def to_param
"#{id}-#{title}".parameterize
end
@@ -185,6 +198,12 @@ class Proposal < ActiveRecord::Base
(voters + followers).uniq
end
def self.proposals_orders(user)
orders = %w{hot_score confidence_score created_at relevance archival_date}
orders << "recommendations" if user.present?
orders
end
protected
def set_responsible_name

View File

@@ -88,12 +88,12 @@ class User < ActiveRecord::Base
end
def debate_votes(debates)
voted = votes.for_debates(debates)
voted = votes.for_debates(Array(debates).map(&:id))
voted.each_with_object({}) { |v, h| h[v.votable_id] = v.value }
end
def proposal_votes(proposals)
voted = votes.for_proposals(proposals)
voted = votes.for_proposals(Array(proposals).map(&:id))
voted.each_with_object({}) { |v, h| h[v.votable_id] = v.value }
end