diff --git a/app/controllers/legislation/draft_versions_controller.rb b/app/controllers/legislation/draft_versions_controller.rb index bf455fa5a..705eb16bd 100644 --- a/app/controllers/legislation/draft_versions_controller.rb +++ b/app/controllers/legislation/draft_versions_controller.rb @@ -6,14 +6,15 @@ class Legislation::DraftVersionsController < Legislation::BaseController end def show + load_version(params[:id]) end def changes - @draft_version = @process.draft_versions.find(params[:draft_version_id]) + load_version(params[:draft_version_id]) end def go_to_version - version = @process.draft_versions.find(params[:draft_version_id]) + version = @process.draft_versions.published.find(params[:draft_version_id]) if params[:redirect_action] == 'changes' redirect_to legislation_process_draft_version_changes_path(@process, version) @@ -21,4 +22,14 @@ class Legislation::DraftVersionsController < Legislation::BaseController redirect_to legislation_process_draft_version_path(@process, version) end end + + private + + def load_version(id_param) + if current_user && current_user.administrator? + @draft_version = @process.draft_versions.find(id_param) + else + @draft_version = @process.draft_versions.published.find(id_param) + end + end end diff --git a/app/models/legislation/draft_version.rb b/app/models/legislation/draft_version.rb index b68903b5e..3cbbce567 100644 --- a/app/models/legislation/draft_version.rb +++ b/app/models/legislation/draft_version.rb @@ -10,6 +10,8 @@ class Legislation::DraftVersion < ActiveRecord::Base validates :body, presence: true validates :status, presence: true, inclusion: { in: VALID_STATUSES } + scope :published, -> { where(status: 'published').order('id DESC') } + before_save :render_html def render_html diff --git a/app/views/legislation/draft_versions/changes.html.erb b/app/views/legislation/draft_versions/changes.html.erb index a14a2e700..5b46c2282 100644 --- a/app/views/legislation/draft_versions/changes.html.erb +++ b/app/views/legislation/draft_versions/changes.html.erb @@ -11,7 +11,7 @@

<%= t('.seeing_changelog_version') %>

<%= form_tag go_to_version_legislation_process_draft_versions_path(@process), method: :get, id: "draft_version_go_to_version" do %> - <%= select_tag "draft_version_id", options_from_collection_for_select(@process.draft_versions, 'id', 'title', @draft_version.id), "aria-label": t('legislation.draft_versions.show.select_draft_version') %> + <%= select_tag "draft_version_id", options_from_collection_for_select(@process.draft_versions.published, 'id', 'title', @draft_version.id), "aria-label": t('legislation.draft_versions.show.select_draft_version') %> <%= hidden_field_tag "redirect_action", "changes" %> <%= submit_tag t('legislation.draft_versions.show.select_version_submit'), class: "button" %> <% end %> diff --git a/app/views/legislation/draft_versions/show.html.erb b/app/views/legislation/draft_versions/show.html.erb index 14a7cc1f3..af880c10d 100644 --- a/app/views/legislation/draft_versions/show.html.erb +++ b/app/views/legislation/draft_versions/show.html.erb @@ -11,7 +11,7 @@

<%= t('.seeing_version') %>

<%= form_tag go_to_version_legislation_process_draft_versions_path(@process), method: :get, id: "draft_version_go_to_version" do %> - <%= select_tag "draft_version_id", options_from_collection_for_select(@process.draft_versions, 'id', 'title', @draft_version.id), "aria-label": t('.select_draft_version') %> + <%= select_tag "draft_version_id", options_from_collection_for_select(@process.draft_versions.published, 'id', 'title', @draft_version.id), "aria-label": t('.select_draft_version') %> <%= submit_tag t('.select_version_submit'), class: "button" %> <% end %> <%= link_to t('.see_changes'), legislation_process_draft_version_changes_path(@process, @draft_version) %> diff --git a/spec/features/legislation/draft_versions_spec.rb b/spec/features/legislation/draft_versions_spec.rb index 6414593e6..8fef62213 100644 --- a/spec/features/legislation/draft_versions_spec.rb +++ b/spec/features/legislation/draft_versions_spec.rb @@ -1,18 +1,44 @@ require 'rails_helper' feature 'Legislation Draft Versions' do + let(:user) { create(:user) } + let(:administrator) do + create(:administrator, user: user) + user + end context "See draft text page" do before(:each) do @process = create(:legislation_process) - @draft_version_1 = create(:legislation_draft_version, process: @process, title: "Version 1", body: "Body of the first version") - @draft_version_2 = create(:legislation_draft_version, process: @process, title: "Version 2", body: "Body of the second version") + @draft_version_1 = create(:legislation_draft_version, process: @process, title: "Version 1", body: "Body of the first version", status: "published") + @draft_version_2 = create(:legislation_draft_version, process: @process, title: "Version 2", body: "Body of the second version", status: "published") + @draft_version_3 = create(:legislation_draft_version, process: @process, title: "Version 3", body: "Body of the third version", status: "draft") end it "shows the text body for this version" do visit legislation_process_draft_version_path(@process, @draft_version_1) expect(page).to have_content("Body of the first version") + + within('select#draft_version_id') do + expect(page).to have_content("Version 1") + expect(page).to have_content("Version 2") + expect(page).to_not have_content("Version 3") + end + end + + it "shows an unpublished version to admins" do + login_as(administrator) + + visit legislation_process_draft_version_path(@process, @draft_version_3) + + expect(page).to have_content("Body of the third version") + + within('select#draft_version_id') do + expect(page).to have_content("Version 1") + expect(page).to have_content("Version 2") + expect(page).to_not have_content("Version 3") + end end it "switches to another version without js" do @@ -40,14 +66,35 @@ feature 'Legislation Draft Versions' do context "See changes page" do before(:each) do @process = create(:legislation_process) - @draft_version_1 = create(:legislation_draft_version, process: @process, title: "Version 1", body: "Body of the first version", changelog: "Changes for first version") - @draft_version_2 = create(:legislation_draft_version, process: @process, title: "Version 2", body: "Body of the second version", changelog: "Changes for second version") + @draft_version_1 = create(:legislation_draft_version, process: @process, title: "Version 1", body: "Body of the first version", changelog: "Changes for first version", status: "published") + @draft_version_2 = create(:legislation_draft_version, process: @process, title: "Version 2", body: "Body of the second version", changelog: "Changes for second version", status: "published") + @draft_version_3 = create(:legislation_draft_version, process: @process, title: "Version 3", body: "Body of the third version", changelog: "Changes for third version", status: "draft") end - it "shows the text body for this version" do + it "shows the changes for this version" do visit legislation_process_draft_version_changes_path(@process, @draft_version_1) expect(page).to have_content("Changes for first version") + + within('select#draft_version_id') do + expect(page).to have_content("Version 1") + expect(page).to have_content("Version 2") + expect(page).to_not have_content("Version 3") + end + end + + it "shows an unpublished version to admins" do + login_as(administrator) + + visit legislation_process_draft_version_changes_path(@process, @draft_version_3) + + expect(page).to have_content("Changes for third version") + + within('select#draft_version_id') do + expect(page).to have_content("Version 1") + expect(page).to have_content("Version 2") + expect(page).to_not have_content("Version 3") + end end it "switches to another version without js" do