diff --git a/app/controllers/admin/legislation/processes_controller.rb b/app/controllers/admin/legislation/processes_controller.rb index 8d349633b..995e34eee 100644 --- a/app/controllers/admin/legislation/processes_controller.rb +++ b/app/controllers/admin/legislation/processes_controller.rb @@ -45,7 +45,11 @@ class Admin::Legislation::ProcessesController < Admin::Legislation::BaseControll :draft_publication_date, :allegations_start_date, :allegations_end_date, - :final_publication_date + :result_publication_date, + :debate_phase_enabled, + :allegations_phase_enabled, + :draft_publication_enabled, + :result_publication_enabled ) end end diff --git a/app/controllers/legislation/annotations_controller.rb b/app/controllers/legislation/annotations_controller.rb index dc3ff4834..56f851316 100644 --- a/app/controllers/legislation/annotations_controller.rb +++ b/app/controllers/legislation/annotations_controller.rb @@ -29,7 +29,7 @@ class Legislation::AnnotationsController < ApplicationController end def create - if !@process.open_phase?(:allegations) || @draft_version.final_version? + if !@process.allegations_phase.open? || @draft_version.final_version? render json: {}, status: :not_found and return end diff --git a/app/controllers/legislation/answers_controller.rb b/app/controllers/legislation/answers_controller.rb index 372398e41..6d3c33580 100644 --- a/app/controllers/legislation/answers_controller.rb +++ b/app/controllers/legislation/answers_controller.rb @@ -9,7 +9,7 @@ class Legislation::AnswersController < Legislation::BaseController respond_to :html, :js def create - if @process.open_phase?(:debate) + if @process.debate_phase.open? @answer.user = current_user @answer.save track_event diff --git a/app/controllers/legislation/processes_controller.rb b/app/controllers/legislation/processes_controller.rb index 7aff709d0..21951fa6f 100644 --- a/app/controllers/legislation/processes_controller.rb +++ b/app/controllers/legislation/processes_controller.rb @@ -8,9 +8,9 @@ class Legislation::ProcessesController < Legislation::BaseController end def show - if @process.active_phase?(:allegations) && @process.show_phase?(:allegations) && draft_version = @process.draft_versions.published.last + if @process.allegations_phase.enabled? && @process.allegations_phase.started? && draft_version = @process.draft_versions.published.last redirect_to legislation_process_draft_version_path(@process, draft_version) - elsif @process.active_phase?(:debate) + elsif @process.debate_phase.enabled? redirect_to legislation_process_debate_path(@process) else redirect_to legislation_process_allegations_path(@process) @@ -18,9 +18,10 @@ class Legislation::ProcessesController < Legislation::BaseController end def debate - phase :debate + set_process + @phase = :debate_phase - if @process.show_phase?(:debate) + if @process.debate_phase.started? render :debate else render :phase_not_open @@ -28,9 +29,10 @@ class Legislation::ProcessesController < Legislation::BaseController end def draft_publication - phase :draft_publication + set_process + @phase = :draft_publication - if @process.show_phase?(@phase) + if @process.draft_publication.started? if draft_version = @process.draft_versions.published.last redirect_to legislation_process_draft_version_path(@process, draft_version) else @@ -42,9 +44,10 @@ class Legislation::ProcessesController < Legislation::BaseController end def allegations - phase :allegations + set_process + @phase = :allegations_phase - if @process.show_phase?(@phase) + if @process.allegations_phase.started? if draft_version = @process.draft_versions.published.last redirect_to legislation_process_draft_version_path(@process, draft_version) else @@ -55,10 +58,11 @@ class Legislation::ProcessesController < Legislation::BaseController end end - def final_version_publication - phase :final_version_publication + def result_publication + set_process + @phase = :result_publication - if @process.show_phase?(@phase) + if @process.result_publication.started? if final_version = @process.final_draft_version redirect_to legislation_process_draft_version_path(@process, final_version) else @@ -71,8 +75,7 @@ class Legislation::ProcessesController < Legislation::BaseController private - def phase(phase) + def set_process @process = ::Legislation::Process.find(params[:process_id]) - @phase = phase end end diff --git a/app/models/abilities/everyone.rb b/app/models/abilities/everyone.rb index c22a3f7e4..eaa971016 100644 --- a/app/models/abilities/everyone.rb +++ b/app/models/abilities/everyone.rb @@ -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 diff --git a/app/models/direct_message.rb b/app/models/direct_message.rb index b36cca705..ae14e652d 100644 --- a/app/models/direct_message.rb +++ b/app/models/direct_message.rb @@ -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? diff --git a/app/models/legislation/process.rb b/app/models/legislation/process.rb index 32468b834..61dc1f7a0 100644 --- a/app/models/legislation/process.rb +++ b/app/models/legislation/process.rb @@ -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 diff --git a/app/models/legislation/process/phase.rb b/app/models/legislation/process/phase.rb new file mode 100644 index 000000000..5d677251d --- /dev/null +++ b/app/models/legislation/process/phase.rb @@ -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 diff --git a/app/models/legislation/process/publication.rb b/app/models/legislation/process/publication.rb new file mode 100644 index 000000000..650914f95 --- /dev/null +++ b/app/models/legislation/process/publication.rb @@ -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 diff --git a/app/models/legislation/question.rb b/app/models/legislation/question.rb index 374c43eb5..4381b752a 100644 --- a/app/models/legislation/question.rb +++ b/app/models/legislation/question.rb @@ -37,6 +37,6 @@ class Legislation::Question < ActiveRecord::Base end def comments_open? - process.open_phase?(:debate) + process.debate_phase.open? end end diff --git a/app/views/admin/legislation/processes/_form.html.erb b/app/views/admin/legislation/processes/_form.html.erb index ac49f8a7d..7d2b21703 100644 --- a/app/views/admin/legislation/processes/_form.html.erb +++ b/app/views/admin/legislation/processes/_form.html.erb @@ -70,8 +70,7 @@ id: "debate_end_date" %>
<%= t('legislation.processes.shared.key_dates') %>
+ <% if process.enabled_phases_and_publications_count.positive? %> + <% column_width = 12 / process.enabled_phases_and_publications_count %> +<%= t('legislation.processes.shared.key_dates') %>
+<%= format_date(process.debate_start_date) %> - <%= format_date(process.debate_end_date) %>
+<%= format_date(process.debate_start_date) %> - <%= format_date(process.debate_end_date) %>
+<%= format_date(process.draft_publication_date) %>
+<%= format_date(process.allegations_start_date) %> - <%= format_date(process.allegations_end_date) %>
+<%= format_date(process.result_publication_date) %>
+<%= format_date(process.draft_publication_date) %>
-<%= format_date(process.allegations_start_date) %> - <%= format_date(process.allegations_end_date) %>
-<%= format_date(process.final_publication_date) %>
-#{Faker::Lorem.paragraphs.join('
')}
" debate = Debate.create!(author: author, title: Faker::Lorem.sentence(3).truncate(60), - created_at: rand((Time.current - 1.week) .. Time.current), + created_at: rand((Time.current - 1.week)..Time.current), description: description, tag_list: tags.sample(3).join(','), geozone: Geozone.reorder("RANDOM()").first, terms_of_service: "1") end - tags = ActsAsTaggableOn::Tag.where(kind: 'category') -(1..30).each do +30.times do author = User.reorder("RANDOM()").first description = "#{Faker::Lorem.paragraphs.join('
')}
" debate = Debate.create!(author: author, title: Faker::Lorem.sentence(3).truncate(60), - created_at: rand((Time.current - 1.week) .. Time.current), + created_at: rand((Time.current - 1.week)..Time.current), description: description, tag_list: tags.sample(3).join(','), geozone: Geozone.reorder("RANDOM()").first, terms_of_service: "1") end - puts " ✅" print "Creating Proposals" tags = Faker::Lorem.words(25) -(1..30).each do |i| +30.times do author = User.reorder("RANDOM()").first description = "#{Faker::Lorem.paragraphs.join('
')}
" proposal = Proposal.create!(author: author, @@ -191,7 +189,7 @@ tags = Faker::Lorem.words(25) responsible_name: Faker::Name.name, external_url: Faker::Internet.url, description: description, - created_at: rand((Time.current - 1.week) .. Time.current), + created_at: rand((Time.current - 1.week)..Time.current), tag_list: tags.sample(3).join(','), geozone: Geozone.reorder("RANDOM()").first, terms_of_service: "1") @@ -201,7 +199,7 @@ puts " ✅" print "Creating Archived Proposals" tags = Faker::Lorem.words(25) -(1..5).each do +5.times do author = User.reorder("RANDOM()").first description = "#{Faker::Lorem.paragraphs.join('
')}
" proposal = Proposal.create!(author: author, @@ -221,7 +219,7 @@ puts " ✅" print "Creating Successful Proposals" tags = Faker::Lorem.words(25) -(1..10).each do |i| +10.times do author = User.reorder("RANDOM()").first description = "#{Faker::Lorem.paragraphs.join('
')}
" proposal = Proposal.create!(author: author, @@ -231,16 +229,15 @@ tags = Faker::Lorem.words(25) responsible_name: Faker::Name.name, external_url: Faker::Internet.url, description: description, - created_at: rand((Time.current - 1.week) .. Time.current), + created_at: rand((Time.current - 1.week)..Time.current), tag_list: tags.sample(3).join(','), geozone: Geozone.reorder("RANDOM()").first, terms_of_service: "1", cached_votes_up: Setting["votes_for_proposal_success"]) end - tags = ActsAsTaggableOn::Tag.where(kind: 'category') -(1..30).each do +30.times do author = User.reorder("RANDOM()").first description = "#{Faker::Lorem.paragraphs.join('
')}
" proposal = Proposal.create!(author: author, @@ -250,94 +247,89 @@ tags = ActsAsTaggableOn::Tag.where(kind: 'category') responsible_name: Faker::Name.name, external_url: Faker::Internet.url, description: description, - created_at: rand((Time.current - 1.week) .. Time.current), + created_at: rand((Time.current - 1.week)..Time.current), tag_list: tags.sample(3).join(','), geozone: Geozone.reorder("RANDOM()").first, terms_of_service: "1") end - puts " ✅" print "Commenting Debates" -(1..100).each do +100.times do author = User.reorder("RANDOM()").first debate = Debate.reorder("RANDOM()").first Comment.create!(user: author, - created_at: rand(debate.created_at .. Time.current), + created_at: rand(debate.created_at..Time.current), commentable: debate, body: Faker::Lorem.sentence) end - puts " ✅" print "Commenting Proposals" -(1..100).each do |i| +100.times do author = User.reorder("RANDOM()").first proposal = Proposal.reorder("RANDOM()").first Comment.create!(user: author, - created_at: rand(proposal.created_at .. Time.current), + created_at: rand(proposal.created_at..Time.current), commentable: proposal, body: Faker::Lorem.sentence) end - puts " ✅" print "Commenting Comments" -(1..200).each do +200.times do author = User.reorder("RANDOM()").first parent = Comment.reorder("RANDOM()").first Comment.create!(user: author, - created_at: rand(parent.created_at .. Time.current), + created_at: rand(parent.created_at..Time.current), commentable_id: parent.commentable_id, commentable_type: parent.commentable_type, body: Faker::Lorem.sentence, parent: parent) end - puts " ✅" print "Voting Debates, Proposals & Comments" -(1..100).each do +100.times do voter = not_org_users.level_two_or_three_verified.reorder("RANDOM()").first vote = [true, false].sample debate = Debate.reorder("RANDOM()").first debate.vote_by(voter: voter, vote: vote) end -(1..100).each do |i| +100.times do voter = not_org_users.reorder("RANDOM()").first vote = [true, false].sample comment = Comment.reorder("RANDOM()").first comment.vote_by(voter: voter, vote: vote) end -(1..100).each do +100.times do voter = not_org_users.level_two_or_three_verified.reorder("RANDOM()").first proposal = Proposal.reorder("RANDOM()").first proposal.vote_by(voter: voter, vote: true) end - puts " ✅" print "Flagging Debates & Comments" -(1..40).each do +40.times do debate = Debate.reorder("RANDOM()").first flagger = User.where(["users.id <> ?", debate.author_id]).reorder("RANDOM()").first Flag.flag(flagger, debate) end -(1..40).each do +40.times do comment = Comment.reorder("RANDOM()").first flagger = User.where(["users.id <> ?", comment.user_id]).reorder("RANDOM()").first Flag.flag(flagger, comment) end -(1..40).each do +40.times do proposal = Proposal.reorder("RANDOM()").first flagger = User.where(["users.id <> ?", proposal.author_id]).reorder("RANDOM()").first Flag.flag(flagger, proposal) @@ -348,7 +340,7 @@ print "Creating Spending Proposals" tags = Faker::Lorem.words(10) -(1..60).each do +60.times do geozone = Geozone.reorder("RANDOM()").first author = User.reorder("RANDOM()").first description = "#{Faker::Lorem.paragraphs.join('
')}
" @@ -356,17 +348,17 @@ tags = Faker::Lorem.words(10) valuation_finished = [true, false].sample feasible = [true, false].sample spending_proposal = SpendingProposal.create!(author: author, - title: Faker::Lorem.sentence(3).truncate(60), - external_url: Faker::Internet.url, - description: description, - created_at: rand((Time.current - 1.week) .. Time.current), - geozone: [geozone, nil].sample, - feasible: feasible, - feasible_explanation: feasible_explanation, - valuation_finished: valuation_finished, - tag_list: tags.sample(3).join(','), - price: rand(1000000), - terms_of_service: "1") + title: Faker::Lorem.sentence(3).truncate(60), + external_url: Faker::Internet.url, + description: description, + created_at: rand((Time.current - 1.week)..Time.current), + geozone: [geozone, nil].sample, + feasible: feasible, + feasible_explanation: feasible_explanation, + valuation_finished: valuation_finished, + tag_list: tags.sample(3).join(','), + price: rand(1000000), + terms_of_service: "1") end puts " ✅" @@ -376,13 +368,14 @@ print "Creating Valuation Assignments" SpendingProposal.reorder("RANDOM()").first.valuators << valuator.valuator end - puts " ✅" print "Creating Budgets" Budget::PHASES.each_with_index do |phase, i| - descriptions = Hash[Budget::PHASES.map{ |p| ["description_#{p}", - "#{Faker::Lorem.paragraphs(2).join('
')}
"] }] + descriptions = Hash[Budget::PHASES.map do |p| + ["description_#{p}", + "#{Faker::Lorem.paragraphs(2).join('
')}
"] + end] budget = Budget.create!( descriptions.merge( name: (Date.current - 10 + i).to_s, @@ -397,18 +390,15 @@ Budget::PHASES.each_with_index do |phase, i| geozones = Geozone.reorder("RANDOM()").limit([2, 5, 6, 7].sample) geozones.each do |geozone| group.headings << group.headings.create!(name: geozone.name, - #geozone: geozone, - price: rand(1 .. 100) * 100000) - + price: rand(1..100) * 100000) end end end - puts " ✅" print "Creating Investments" tags = Faker::Lorem.words(10) -(1..100).each do |i| +100.times do heading = Budget::Heading.reorder("RANDOM()").first investment = Budget::Investment.create!( @@ -419,13 +409,14 @@ tags = Faker::Lorem.words(10) title: Faker::Lorem.sentence(3).truncate(60), external_url: Faker::Internet.url, description: "#{Faker::Lorem.paragraphs.join('
')}
", - created_at: rand((Time.current - 1.week) .. Time.current), + created_at: rand((Time.current - 1.week)..Time.current), feasibility: %w{undecided unfeasible feasible feasible feasible feasible}.sample, unfeasibility_explanation: Faker::Lorem.paragraph, valuation_finished: [false, true].sample, tag_list: tags.sample(3).join(','), - price: rand(1 .. 100) * 100000, - terms_of_service: "1") + price: rand(1..100) * 100000, + terms_of_service: "1" + ) end puts " ✅" @@ -438,7 +429,7 @@ puts " ✅" print "Winner Investments" budget = Budget.where(phase: "finished").last -(1..100).each do |i| +100.times do heading = budget.headings.reorder("RANDOM()").first investment = Budget::Investment.create!( author: User.reorder("RANDOM()").first, @@ -448,12 +439,13 @@ budget = Budget.where(phase: "finished").last title: Faker::Lorem.sentence(3).truncate(60), external_url: Faker::Internet.url, description: "#{Faker::Lorem.paragraphs.join('
')}
", - created_at: rand((Time.current - 1.week) .. Time.current), + created_at: rand((Time.current - 1.week)..Time.current), feasibility: "feasible", valuation_finished: true, selected: true, - price: rand(10000 .. heading.price), - terms_of_service: "1") + price: rand(10000..heading.price), + terms_of_service: "1" + ) end budget.headings.each do |heading| Budget::Result.new(budget, heading).calculate_winners @@ -466,16 +458,13 @@ print "Creating Valuation Assignments" Budget::Investment.reorder("RANDOM()").first.valuators << valuator.valuator end - puts " ✅" print "Ignoring flags in Debates, comments & proposals" - Debate.flagged.reorder("RANDOM()").limit(10).each(&:ignore_flag) Comment.flagged.reorder("RANDOM()").limit(30).each(&:ignore_flag) Proposal.flagged.reorder("RANDOM()").limit(10).each(&:ignore_flag) - puts " ✅" print "Hiding debates, comments & proposals" @@ -483,7 +472,6 @@ Comment.with_hidden.flagged.reorder("RANDOM()").limit(30).each(&:hide) Debate.with_hidden.flagged.reorder("RANDOM()").limit(5).each(&:hide) Proposal.with_hidden.flagged.reorder("RANDOM()").limit(10).each(&:hide) - puts " ✅" print "Confirming hiding in debates, comments & proposals" @@ -504,9 +492,9 @@ Proposal.last(3).each do |proposal| image: ["banner-img banner-img-one", "banner-img banner-img-two", "banner-img banner-img-three"].sample, target_url: Rails.application.routes.url_helpers.proposal_path(proposal), - post_started_at: rand((Time.current - 1.week) .. (Time.current - 1.day)), - post_ended_at: rand((Time.current - 1.day) .. (Time.current + 1.week)), - created_at: rand((Time.current - 1.week) .. Time.current)) + post_started_at: rand((Time.current - 1.week)..(Time.current - 1.day)), + post_ended_at: rand((Time.current - 1.day)..(Time.current + 1.week)), + created_at: rand((Time.current - 1.week)..Time.current)) end puts " ✅" @@ -530,19 +518,14 @@ print "Active Polls" ends_at: 1.month.from_now, geozone_restricted: false) end -(4..5).each do |i| +(1..5).each do |i| poll = Poll.create(name: "Active Poll #{i}", starts_at: 1.month.ago, ends_at: 1.month.from_now, geozone_restricted: true, - geozones: Geozone.reorder("RANDOM()").limit(3) - ) + geozones: Geozone.reorder("RANDOM()").limit(3)) end - - - - puts " ✅" print "Upcoming Poll" poll = Poll.create(name: "Upcoming Poll", @@ -552,17 +535,17 @@ poll = Poll.create(name: "Upcoming Poll", puts " ✅" print "Expired Poll" poll = Poll.create(name: "Expired Poll", - starts_at: 2.months.ago, - ends_at: 1.months.ago) + starts_at: 2.months.ago, + ends_at: 1.month.ago) puts " ✅" print "Creating Poll Questions" -(1..50).each do |i| +50.times do poll = Poll.reorder("RANDOM()").first author = User.reorder("RANDOM()").first description = "#{Faker::Lorem.paragraphs.join('
')}
" - open_at = rand(2.months.ago .. 2.months.from_now) + open_at = rand(2.months.ago..2.months.from_now) question = Poll::Question.create!(author: author, title: Faker::Lorem.sentence(3).truncate(60), description: description, @@ -594,21 +577,21 @@ end puts " ✅" print "Creating Poll Recounts" do -(1..15).to_a.sample.times do |i| - poll_officer.poll_officer.officer_assignments.all.sample(i).each do |officer_assignment| - Poll::Recount.create(officer_assignment: officer_assignment, - booth_assignment: officer_assignment.booth_assignment, - date: officer_assignment.date, - count: (1..5000).to_a.sample) + (1..15).to_a.sample.times do |i| + poll_officer.poll_officer.officer_assignments.all.sample(i).each do |officer_assignment| + Poll::Recount.create(officer_assignment: officer_assignment, + booth_assignment: officer_assignment.booth_assignment, + date: officer_assignment.date, + count: (1..5000).to_a.sample) + end end -end end puts " ✅" print "Creating Poll Questions from Proposals" -(1..3).each do +3.times do proposal = Proposal.reorder("RANDOM()").first poll = Poll.current.first question = Poll::Question.create(valid_answers: "Yes, No") @@ -619,7 +602,7 @@ end puts " ✅" print "Creating Successful Proposals" -(1..10).each do +10.times do proposal = Proposal.reorder("RANDOM()").first poll = Poll.current.first question = Poll::Question.create(valid_answers: "Yes, No") @@ -630,11 +613,11 @@ end puts " ✅" print "Commenting Poll Questions" -(1..30).each do +30.times do author = User.reorder("RANDOM()").first question = Poll::Question.reorder("RANDOM()").first Comment.create!(user: author, - created_at: rand(question.created_at .. Time.current), + created_at: rand(question.created_at..Time.current), commentable: question, body: Faker::Lorem.sentence) end @@ -642,7 +625,7 @@ end puts " ✅" print "Creating Poll Voters" -(1..10).each do +10.times do poll = Poll.all.sample user = User.level_two_verified.sample Poll::Voter.create(poll: poll, user: user) @@ -651,7 +634,7 @@ end puts " ✅" print "Creating legislation processes" -(1..5).each do |i| +5.times do process = ::Legislation::Process.create!(title: Faker::Lorem.sentence(3).truncate(60), description: Faker::Lorem.paragraphs.join("\n\n"), summary: Faker::Lorem.paragraph, @@ -663,15 +646,18 @@ print "Creating legislation processes" draft_publication_date: Date.current + 1.day, allegations_start_date: Date.current + 2.days, allegations_end_date: Date.current + 3.days, - final_publication_date: Date.current + 4.days + result_publication_date: Date.current + 4.days, + debate_phase_enabled: true, + allegations_phase_enabled: true, + draft_publication_enabled: true, + result_publication_enabled: true ) end ::Legislation::Process.all.each do |process| (1..3).each do |i| version = process.draft_versions.create!(title: "Version #{i}", - body: Faker::Lorem.paragraphs.join("\n\n") - ) + body: Faker::Lorem.paragraphs.join("\n\n")) end end diff --git a/db/migrate/20170613174317_rename_legislation_process_final_pub_to_result_pub.rb b/db/migrate/20170613174317_rename_legislation_process_final_pub_to_result_pub.rb new file mode 100644 index 000000000..e71314531 --- /dev/null +++ b/db/migrate/20170613174317_rename_legislation_process_final_pub_to_result_pub.rb @@ -0,0 +1,5 @@ +class RenameLegislationProcessFinalPubToResultPub < ActiveRecord::Migration + def change + rename_column :legislation_processes, :final_publication_date, :result_publication_date + end +end diff --git a/db/migrate/20170613203256_add_phase_pub_enabled_status_to_legislative_process.rb b/db/migrate/20170613203256_add_phase_pub_enabled_status_to_legislative_process.rb new file mode 100644 index 000000000..7e6ecda96 --- /dev/null +++ b/db/migrate/20170613203256_add_phase_pub_enabled_status_to_legislative_process.rb @@ -0,0 +1,8 @@ +class AddPhasePubEnabledStatusToLegislativeProcess < ActiveRecord::Migration + def change + add_column :legislation_processes, :debate_phase_enabled, :boolean, default: false + add_column :legislation_processes, :allegations_phase_enabled, :boolean, default: false + add_column :legislation_processes, :draft_publication_enabled, :boolean, default: false + add_column :legislation_processes, :result_publication_enabled, :boolean, default: false + end +end diff --git a/db/schema.rb b/db/schema.rb index cd1ed6c0a..8fae3e8dd 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20170610211027) do +ActiveRecord::Schema.define(version: 20170613203256) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -411,11 +411,15 @@ ActiveRecord::Schema.define(version: 20170610211027) do t.date "draft_publication_date" t.date "allegations_start_date" t.date "allegations_end_date" - t.date "final_publication_date" + t.date "result_publication_date" t.datetime "hidden_at" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.text "summary" + t.boolean "debate_phase_enabled", default: false + t.boolean "allegations_phase_enabled", default: false + t.boolean "draft_publication_enabled", default: false + t.boolean "result_publication_enabled", default: false end add_index "legislation_processes", ["allegations_end_date"], name: "index_legislation_processes_on_allegations_end_date", using: :btree @@ -424,8 +428,8 @@ ActiveRecord::Schema.define(version: 20170610211027) do add_index "legislation_processes", ["debate_start_date"], name: "index_legislation_processes_on_debate_start_date", using: :btree add_index "legislation_processes", ["draft_publication_date"], name: "index_legislation_processes_on_draft_publication_date", using: :btree add_index "legislation_processes", ["end_date"], name: "index_legislation_processes_on_end_date", using: :btree - add_index "legislation_processes", ["final_publication_date"], name: "index_legislation_processes_on_final_publication_date", using: :btree add_index "legislation_processes", ["hidden_at"], name: "index_legislation_processes_on_hidden_at", using: :btree + add_index "legislation_processes", ["result_publication_date"], name: "index_legislation_processes_on_result_publication_date", using: :btree add_index "legislation_processes", ["start_date"], name: "index_legislation_processes_on_start_date", using: :btree create_table "legislation_question_options", force: :cascade do |t| diff --git a/spec/factories.rb b/spec/factories.rb index f64198818..7527a0ba1 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -612,7 +612,11 @@ FactoryGirl.define do draft_publication_date Date.current - 1.day allegations_start_date Date.current allegations_end_date Date.current + 3.days - final_publication_date Date.current + 5.days + result_publication_date Date.current + 5.days + debate_phase_enabled true + allegations_phase_enabled true + draft_publication_enabled true + result_publication_enabled true trait :next do start_date Date.current + 2.days @@ -622,7 +626,7 @@ FactoryGirl.define do draft_publication_date Date.current + 5.day allegations_start_date Date.current + 5.days allegations_end_date Date.current + 7.days - final_publication_date Date.current + 8.days + result_publication_date Date.current + 8.days end trait :past do @@ -633,7 +637,7 @@ FactoryGirl.define do draft_publication_date Date.current - 8.day allegations_start_date Date.current - 8.days allegations_end_date Date.current - 4.days - final_publication_date Date.current - 2.days + result_publication_date Date.current - 2.days end trait :in_debate_phase do @@ -644,7 +648,7 @@ FactoryGirl.define do draft_publication_date Date.current + 1.day allegations_start_date Date.current + 2.days allegations_end_date Date.current + 3.days - final_publication_date Date.current + 5.days + result_publication_date Date.current + 5.days end end diff --git a/spec/features/admin/legislation/processes_spec.rb b/spec/features/admin/legislation/processes_spec.rb index 60a872190..e945f6ce8 100644 --- a/spec/features/admin/legislation/processes_spec.rb +++ b/spec/features/admin/legislation/processes_spec.rb @@ -51,7 +51,7 @@ feature 'Admin legislation processes' do fill_in 'legislation_process[draft_publication_date]', with: (base_date + 3.days).strftime("%d/%m/%Y") fill_in 'legislation_process[allegations_start_date]', with: (base_date + 3.days).strftime("%d/%m/%Y") fill_in 'legislation_process[allegations_end_date]', with: (base_date + 5.days).strftime("%d/%m/%Y") - fill_in 'legislation_process[final_publication_date]', with: (base_date + 7.days).strftime("%d/%m/%Y") + fill_in 'legislation_process[result_publication_date]', with: (base_date + 7.days).strftime("%d/%m/%Y") click_button 'Create process' @@ -73,7 +73,7 @@ feature 'Admin legislation processes' do end context 'Update' do - scenario 'Deactivate debate phase', js: true do + scenario 'Remove summary text', js: true do process = create(:legislation_process, title: 'An example legislation process', summary: 'Summarizing the process', @@ -87,19 +87,43 @@ feature 'Admin legislation processes' do click_link "An example legislation process" expect(page).to have_selector("h2", text: "An example legislation process") - expect(find("#debate_phase_active")).to be_checked + expect(find("#legislation_process_debate_phase_enabled")).to be_checked - uncheck "debate_phase_active" fill_in 'legislation_process_summary', with: '' click_button "Save changes" expect(page).to have_content "Process updated successfully" - expect(find("#debate_start_date").value).to be_blank - expect(find("#debate_end_date").value).to be_blank visit legislation_processes_path expect(page).not_to have_content 'Summarizing the process' expect(page).to have_content 'Description of the process' end + + scenario 'Deactivate draft publication', js: true do + process = create(:legislation_process, + title: 'An example legislation process', + summary: 'Summarizing the process', + description: 'Description of the process') + visit admin_root_path + + within('#side_menu') do + click_link "Collaborative Legislation" + end + + click_link "An example legislation process" + + expect(find("#legislation_process_draft_publication_enabled")).to be_checked + + uncheck "legislation_process_draft_publication_enabled" + click_button "Save changes" + + expect(page).to have_content "Process updated successfully" + expect(find("#debate_start_date").value).not_to be_blank + expect(find("#debate_end_date").value).not_to be_blank + + click_link 'Click to visit' + + expect(page).not_to have_content 'Draft publication' + end end end diff --git a/spec/features/legislation/processes_spec.rb b/spec/features/legislation/processes_spec.rb index 66356dff8..3ed4ccb74 100644 --- a/spec/features/legislation/processes_spec.rb +++ b/spec/features/legislation/processes_spec.rb @@ -104,17 +104,17 @@ feature 'Legislation' do context 'final version publication phase' do scenario 'not open' do - process = create(:legislation_process, final_publication_date: Date.current + 1.day) + process = create(:legislation_process, result_publication_date: Date.current + 1.day) - visit legislation_process_final_version_publication_path(process) + visit legislation_process_result_publication_path(process) expect(page).to have_content("This phase is not open yet") end scenario 'open' do - process = create(:legislation_process, final_publication_date: Date.current) + process = create(:legislation_process, result_publication_date: Date.current) - visit legislation_process_final_version_publication_path(process) + visit legislation_process_result_publication_path(process) expect(page).to have_content("Nothing published yet") end diff --git a/spec/models/direct_message_spec.rb b/spec/models/direct_message_spec.rb index 05c3f75cd..28b30b0ce 100644 --- a/spec/models/direct_message_spec.rb +++ b/spec/models/direct_message_spec.rb @@ -65,9 +65,9 @@ describe DirectMessage do describe "today" do it "should return direct messages created today" do - direct_message1 = create(:direct_message, created_at: Time.current.beginning_of_day + 3.hours) - direct_message2 = create(:direct_message, created_at: Time.current) - direct_message3 = create(:direct_message, created_at: Time.current.end_of_day) + direct_message1 = create(:direct_message, created_at: Time.now.utc.beginning_of_day + 3.hours) + direct_message2 = create(:direct_message, created_at: Time.now.utc) + direct_message3 = create(:direct_message, created_at: Time.now.utc.end_of_day) expect(DirectMessage.today.count).to eq 3 end diff --git a/spec/models/legislation/process/phase_spec.rb b/spec/models/legislation/process/phase_spec.rb new file mode 100644 index 000000000..40d6296b2 --- /dev/null +++ b/spec/models/legislation/process/phase_spec.rb @@ -0,0 +1,99 @@ +require 'rails_helper' + +RSpec.describe Legislation::Process::Phase, type: :model do + let(:process) { create(:legislation_process) } + + describe "#enabled?" do + it "checks debate phase" do + expect(process.debate_phase.enabled?).to be true + + process.update_attributes(debate_phase_enabled: false) + expect(process.debate_phase.enabled?).to be false + end + + it "checks allegations phase" do + expect(process.allegations_phase.enabled?).to be true + + process.update_attributes(allegations_phase_enabled: false) + expect(process.allegations_phase.enabled?).to be false + end + end + + describe "#started?" do + it "checks debate phase" do + # future + process.update_attributes(debate_start_date: Date.current + 2.days, debate_end_date: Date.current + 3.days) + expect(process.debate_phase.started?).to be false + + # started + process.update_attributes(debate_start_date: Date.current - 2.days, debate_end_date: Date.current + 1.day) + expect(process.debate_phase.started?).to be true + + # starts today + process.update_attributes(debate_start_date: Date.current, debate_end_date: Date.current + 1.day) + expect(process.debate_phase.started?).to be true + + # past + process.update_attributes(debate_start_date: Date.current - 2.days, debate_end_date: Date.current - 1.day) + expect(process.debate_phase.started?).to be true + end + + it "checks allegations phase" do + # future + process.update_attributes(allegations_start_date: Date.current + 2.days, allegations_end_date: Date.current + 3.days) + expect(process.allegations_phase.started?).to be false + + # started + process.update_attributes(allegations_start_date: Date.current - 2.days, allegations_end_date: Date.current + 1.day) + expect(process.allegations_phase.started?).to be true + + # starts today + process.update_attributes(allegations_start_date: Date.current, allegations_end_date: Date.current + 1.day) + expect(process.allegations_phase.started?).to be true + + # past + process.update_attributes(allegations_start_date: Date.current - 2.days, allegations_end_date: Date.current - 1.day) + expect(process.allegations_phase.started?).to be true + end + end + + describe "#open?" do + it "checks debate phase" do + # future + process.update_attributes(debate_start_date: Date.current + 2.days, debate_end_date: Date.current + 3.days) + expect(process.debate_phase.open?).to be false + + # started + process.update_attributes(debate_start_date: Date.current - 2.days, debate_end_date: Date.current + 1.day) + expect(process.debate_phase.open?).to be true + + # starts today + process.update_attributes(debate_start_date: Date.current, debate_end_date: Date.current + 1.day) + expect(process.debate_phase.open?).to be true + + # past + process.update_attributes(debate_start_date: Date.current - 2.days, debate_end_date: Date.current - 1.day) + expect(process.debate_phase.open?).to be false + end + + it "checks allegations phase" do + + # future + process.update_attributes(allegations_start_date: Date.current + 2.days, allegations_end_date: Date.current + 3.days) + expect(process.allegations_phase.open?).to be false + + # started + process.update_attributes(allegations_start_date: Date.current - 2.days, allegations_end_date: Date.current + 1.day) + expect(process.allegations_phase.open?).to be true + + # starts today + process.update_attributes(allegations_start_date: Date.current, allegations_end_date: Date.current + 1.day) + expect(process.allegations_phase.open?).to be true + + # past + process.update_attributes(allegations_start_date: Date.current - 2.days, allegations_end_date: Date.current - 1.day) + expect(process.allegations_phase.open?).to be false + end + end + +end diff --git a/spec/models/legislation/process/publication_spec.rb b/spec/models/legislation/process/publication_spec.rb new file mode 100644 index 000000000..a489568d4 --- /dev/null +++ b/spec/models/legislation/process/publication_spec.rb @@ -0,0 +1,81 @@ +require 'rails_helper' + +RSpec.describe Legislation::Process::Publication, type: :model do + let(:process) { create(:legislation_process) } + + describe "#enabled?" do + it "checks draft publication" do + expect(process.draft_publication.enabled?).to be true + + process.update_attributes(draft_publication_enabled: false) + expect(process.draft_publication.enabled?).to be false + end + + it "checks result publication" do + expect(process.result_publication.enabled?).to be true + + process.update_attributes(result_publication_enabled: false) + expect(process.result_publication.enabled?).to be false + end + end + + describe "#started?" do + it "checks draft publication" do + # future + process.update_attributes(draft_publication_date: Date.current + 2.days) + expect(process.draft_publication.started?).to be false + + # past + process.update_attributes(draft_publication_date: Date.current - 2.days) + expect(process.draft_publication.started?).to be true + + # starts today + process.update_attributes(draft_publication_date: Date.current) + expect(process.draft_publication.started?).to be true + end + + it "checks result publication" do + # future + process.update_attributes(result_publication_date: Date.current + 2.days) + expect(process.result_publication.started?).to be false + + # past + process.update_attributes(result_publication_date: Date.current - 2.days) + expect(process.result_publication.started?).to be true + + # starts today + process.update_attributes(result_publication_date: Date.current) + expect(process.result_publication.started?).to be true + end + end + + describe "#open?" do + it "checks draft publication" do + # future + process.update_attributes(draft_publication_date: Date.current + 2.days) + expect(process.draft_publication.open?).to be false + + # past + process.update_attributes(draft_publication_date: Date.current - 2.days) + expect(process.draft_publication.open?).to be true + + # starts today + process.update_attributes(draft_publication_date: Date.current) + expect(process.draft_publication.open?).to be true + end + + it "checks result publication" do + # future + process.update_attributes(result_publication_date: Date.current + 2.days) + expect(process.result_publication.open?).to be false + + # past + process.update_attributes(result_publication_date: Date.current - 2.days) + expect(process.result_publication.open?).to be true + + # starts today + process.update_attributes(result_publication_date: Date.current) + expect(process.result_publication.open?).to be true + end + end +end diff --git a/spec/models/legislation/process_spec.rb b/spec/models/legislation/process_spec.rb index f89a4bfcf..f14e81c8e 100644 --- a/spec/models/legislation/process_spec.rb +++ b/spec/models/legislation/process_spec.rb @@ -100,139 +100,6 @@ RSpec.describe Legislation::Process, type: :model do end end - describe "#open_phase?" do - it "checks debate phase" do - # future - process.update_attributes(debate_start_date: Date.current + 2.days, debate_end_date: Date.current + 3.days) - expect(process.open_phase?(:debate)).to be false - - # started - process.update_attributes(debate_start_date: Date.current - 2.days, debate_end_date: Date.current + 1.day) - expect(process.open_phase?(:debate)).to be true - - # starts today - process.update_attributes(debate_start_date: Date.current, debate_end_date: Date.current + 1.day) - expect(process.open_phase?(:debate)).to be true - - # past - process.update_attributes(debate_start_date: Date.current - 2.days, debate_end_date: Date.current - 1.day) - expect(process.open_phase?(:debate)).to be false - end - - it "checks allegations phase" do - - # future - process.update_attributes(allegations_start_date: Date.current + 2.days, allegations_end_date: Date.current + 3.days) - expect(process.open_phase?(:allegations)).to be false - - # started - process.update_attributes(allegations_start_date: Date.current - 2.days, allegations_end_date: Date.current + 1.day) - expect(process.open_phase?(:allegations)).to be true - - # starts today - process.update_attributes(allegations_start_date: Date.current, allegations_end_date: Date.current + 1.day) - expect(process.open_phase?(:allegations)).to be true - - # past - process.update_attributes(allegations_start_date: Date.current - 2.days, allegations_end_date: Date.current - 1.day) - expect(process.open_phase?(:allegations)).to be false - end - - it "checks draft publication phase" do - # future - process.update_attributes(draft_publication_date: Date.current + 2.days) - expect(process.open_phase?(:draft_publication)).to be false - - # past - process.update_attributes(draft_publication_date: Date.current - 2.days) - expect(process.open_phase?(:draft_publication)).to be true - - # starts today - process.update_attributes(draft_publication_date: Date.current) - expect(process.open_phase?(:draft_publication)).to be true - end - - it "checks final version publication phase" do - # future - process.update_attributes(final_publication_date: Date.current + 2.days) - expect(process.open_phase?(:final_version_publication)).to be false - - # past - process.update_attributes(final_publication_date: Date.current - 2.days) - expect(process.open_phase?(:final_version_publication)).to be true - - # starts today - process.update_attributes(final_publication_date: Date.current) - expect(process.open_phase?(:final_version_publication)).to be true - end - end - - describe "#show_phase?" do - it "checks debate phase" do - # future - process.update_attributes(debate_start_date: Date.current + 2.days, debate_end_date: Date.current + 3.days) - expect(process.show_phase?(:debate)).to be false - - # started - process.update_attributes(debate_start_date: Date.current - 2.days, debate_end_date: Date.current + 1.day) - expect(process.show_phase?(:debate)).to be true - - # starts today - process.update_attributes(debate_start_date: Date.current, debate_end_date: Date.current + 1.day) - expect(process.show_phase?(:debate)).to be true - - # past - process.update_attributes(debate_start_date: Date.current - 2.days, debate_end_date: Date.current - 1.day) - expect(process.show_phase?(:debate)).to be true - end - - it "checks allegations phase" do - # future - process.update_attributes(allegations_start_date: Date.current + 2.days, allegations_end_date: Date.current + 3.days) - expect(process.show_phase?(:allegations)).to be false - - # started - process.update_attributes(allegations_start_date: Date.current - 2.days, allegations_end_date: Date.current + 1.day) - expect(process.show_phase?(:allegations)).to be true - - # starts today - process.update_attributes(allegations_start_date: Date.current, allegations_end_date: Date.current + 1.day) - expect(process.show_phase?(:allegations)).to be true - - # past - process.update_attributes(allegations_start_date: Date.current - 2.days, allegations_end_date: Date.current - 1.day) - expect(process.show_phase?(:allegations)).to be true - end - - it "checks draft publication phase" do - # future - process.update_attributes(draft_publication_date: Date.current + 2.days) - expect(process.show_phase?(:draft_publication)).to be false - - # past - process.update_attributes(draft_publication_date: Date.current - 2.days) - expect(process.show_phase?(:draft_publication)).to be true - - # starts today - process.update_attributes(draft_publication_date: Date.current) - expect(process.show_phase?(:draft_publication)).to be true - end - - it "checks final version publication phase" do - # future - process.update_attributes(final_publication_date: Date.current + 2.days) - expect(process.show_phase?(:final_version_publication)).to be false - - # past - process.update_attributes(final_publication_date: Date.current - 2.days) - expect(process.show_phase?(:final_version_publication)).to be true - - # starts today - process.update_attributes(final_publication_date: Date.current) - expect(process.show_phase?(:final_version_publication)).to be true - end - end - describe "#status" do it "should detect planned phase" do process.update_attributes(start_date: Date.current + 2.days)
- <% if process.active_phase?(:debate) %> -- >
+ <% if process.debate_phase.enabled? %>
+
- >
<%= link_to legislation_process_debate_path(process) do %>
<% end %>
- <% if process.active_phase?(:draft_publication) %>
+ <% if process.draft_publication.enabled? %>
- >
<%= link_to legislation_process_draft_publication_path(process) do %>
<% end %>
- <% if process.active_phase?(:allegations) %>
- - >
+ <% if process.allegations_phase.enabled? %>
+
- >
<%= link_to legislation_process_allegations_path(process) do %>
<% end %>
- <% if process.active_phase?(:final_version_publication) %>
- - >
- <%= link_to legislation_process_final_version_publication_path(process) do %>
-
- >
+ <%= link_to legislation_process_result_publication_path(process) do %>
+
<% end %>
diff --git a/app/views/legislation/processes/_process.html.erb b/app/views/legislation/processes/_process.html.erb
index 77ee985d9..d5e44076a 100644
--- a/app/views/legislation/processes/_process.html.erb
+++ b/app/views/legislation/processes/_process.html.erb
@@ -17,28 +17,42 @@
<%= t('legislation.processes.shared.debate_dates') %>
<%= format_date(process.debate_start_date) %> - <%= format_date(process.debate_end_date) %>
@@ -16,7 +16,7 @@<%= t('legislation.processes.shared.draft_publication_date') %>
@@ -25,8 +25,8 @@<%= t('legislation.processes.shared.allegations_dates') %>
<%= format_date(process.allegations_start_date) %> - <%= format_date(process.allegations_end_date) %>
@@ -34,11 +34,11 @@<%= t('legislation.processes.shared.final_publication_date') %>
-<%= format_date(process.final_publication_date) %>
+ <% if process.result_publication.enabled? %> +<%= t('legislation.processes.shared.result_publication_date') %>
+<%= format_date(process.result_publication_date) %>
<% end %>