Merge branch 'master' into amiedes-api-dev-PRs-2

This commit is contained in:
Juanjo Bazán
2017-06-14 12:46:58 +02:00
committed by GitHub
37 changed files with 482 additions and 364 deletions

View File

@@ -19,7 +19,7 @@ module Abilities
can [:read, :print], Budget::Investment
can :read_results, Budget, phase: "finished"
can :new, DirectMessage
can [:read, :debate, :draft_publication, :allegations, :final_version_publication], Legislation::Process
can [:read, :debate, :draft_publication, :allegations, :result_publication], Legislation::Process
can [:read, :changes, :go_to_version], Legislation::DraftVersion
can [:read], Legislation::Question
can [:create], Legislation::Answer

View File

@@ -8,7 +8,7 @@ class DirectMessage < ActiveRecord::Base
validates :receiver, presence: true
validate :max_per_day
scope :today, lambda { where('DATE(created_at) = ?', Date.current) }
scope :today, lambda { where('DATE(created_at) = DATE(?)', Time.current) }
def max_per_day
return if errors.any?

View File

@@ -2,6 +2,8 @@ class Legislation::Process < ActiveRecord::Base
acts_as_paranoid column: :hidden_at
include ActsAsParanoidAliases
PHASES_AND_PUBLICATIONS = %i(debate_phase allegations_phase draft_publication result_publication).freeze
has_many :draft_versions, -> { order(:id) }, class_name: 'Legislation::DraftVersion', foreign_key: 'legislation_process_id', dependent: :destroy
has_one :final_draft_version, -> { where final_version: true, status: 'published' }, class_name: 'Legislation::DraftVersion', foreign_key: 'legislation_process_id'
has_many :questions, -> { order(:id) }, class_name: 'Legislation::Question', foreign_key: 'legislation_process_id', dependent: :destroy
@@ -19,48 +21,24 @@ class Legislation::Process < ActiveRecord::Base
scope :next, -> { where("start_date > ?", Date.current).order('id DESC') }
scope :past, -> { where("end_date < ?", Date.current).order('id DESC') }
def open_phase?(phase)
today = Date.current
case phase
when :debate
active_phase?(:debate) && today >= debate_start_date && today <= debate_end_date
when :draft_publication
active_phase?(:draft_publication) && today >= draft_publication_date
when :allegations
active_phase?(:allegations) && today >= allegations_start_date && today <= allegations_end_date
when :final_version_publication
active_phase?(:final_version_publication) && today >= final_publication_date
end
def debate_phase
Legislation::Process::Phase.new(debate_start_date, debate_end_date, debate_phase_enabled)
end
def show_phase?(phase)
# show past phases even if they're finished
today = Date.current
case phase
when :debate
active_phase?(:debate) && today >= debate_start_date
when :draft_publication
active_phase?(:draft_publication) && today >= draft_publication_date
when :allegations
active_phase?(:allegations) && today >= allegations_start_date
when :final_version_publication
active_phase?(:final_version_publication) && today >= final_publication_date
end
def allegations_phase
Legislation::Process::Phase.new(allegations_start_date, allegations_end_date, allegations_phase_enabled)
end
def active_phase?(phase)
case phase
when :debate
debate_start_date.present? && debate_end_date.present?
when :draft_publication
draft_publication_date.present?
when :allegations
allegations_start_date.present? && allegations_end_date.present?
when :final_version_publication
final_publication_date.present?
end
def draft_publication
Legislation::Process::Publication.new(draft_publication_date, draft_publication_enabled)
end
def result_publication
Legislation::Process::Publication.new(result_publication_date, result_publication_enabled)
end
def enabled_phases_and_publications_count
PHASES_AND_PUBLICATIONS.count { |process| send(process).enabled? }
end
def total_comments

View File

@@ -0,0 +1,23 @@
# frozen_string_literal: true
class Legislation::Process::Phase
def initialize(start_date, end_date, enabled)
@start_date = start_date
@end_date = end_date
@enabled = enabled
end
def enabled?
@enabled
end
def started?
enabled? && Date.current >= @start_date
end
def open?
started? && Date.current <= @end_date
end
end

View File

@@ -0,0 +1,22 @@
# frozen_string_literal: true
class Legislation::Process::Publication
def initialize(publication_date, enabled)
@publication_date = publication_date
@enabled = enabled
end
def enabled?
@enabled
end
def started?
enabled? && Date.current >= @publication_date
end
def open?
started?
end
end

View File

@@ -37,6 +37,6 @@ class Legislation::Question < ActiveRecord::Base
end
def comments_open?
process.open_phase?(:debate)
process.debate_phase.open?
end
end