From e4fe499e66aeaf17e9b4e74fea5c20ed36cce259 Mon Sep 17 00:00:00 2001 From: Amaia Castro Date: Tue, 6 Dec 2016 12:10:20 +0100 Subject: [PATCH] DraftVersion base model and very basic admin pages --- .../legislation/draft_versions_controller.rb | 43 +++++++++++ .../admin/legislation/processes_controller.rb | 1 - app/models/abilities/administrator.rb | 1 + app/models/legislation/draft_version.rb | 12 ++++ app/models/legislation/process.rb | 2 + .../legislation/draft_versions/_form.html.erb | 72 +++++++++++++++++++ .../legislation/draft_versions/edit.html.erb | 21 ++++++ .../legislation/draft_versions/index.html.erb | 38 ++++++++++ .../legislation/draft_versions/new.html.erb | 17 +++++ .../legislation/processes/_subnav.html.erb | 8 +++ .../admin/legislation/processes/edit.html.erb | 2 + .../legislation/processes/index.html.erb | 3 - config/i18n-tasks.yml | 1 + config/locales/activerecord.en.yml | 9 +++ config/locales/activerecord.es.yml | 9 +++ config/locales/admin.en.yml | 27 ++++++- config/locales/admin.es.yml | 27 ++++++- config/routes.rb | 4 +- db/dev_seeds.rb | 30 ++++++++ ...10441_create_legislation_draft_versions.rb | 16 +++++ db/schema.rb | 19 ++++- spec/factories.rb | 39 ++++++---- .../admin/legislation/draft_versions_spec.rb | 66 +++++++++++++++++ spec/models/legislation/draft_version_spec.rb | 9 +++ 24 files changed, 453 insertions(+), 23 deletions(-) create mode 100644 app/controllers/admin/legislation/draft_versions_controller.rb create mode 100644 app/models/legislation/draft_version.rb create mode 100644 app/views/admin/legislation/draft_versions/_form.html.erb create mode 100644 app/views/admin/legislation/draft_versions/edit.html.erb create mode 100644 app/views/admin/legislation/draft_versions/index.html.erb create mode 100644 app/views/admin/legislation/draft_versions/new.html.erb create mode 100644 app/views/admin/legislation/processes/_subnav.html.erb create mode 100644 db/migrate/20161205110441_create_legislation_draft_versions.rb create mode 100644 spec/features/admin/legislation/draft_versions_spec.rb create mode 100644 spec/models/legislation/draft_version_spec.rb diff --git a/app/controllers/admin/legislation/draft_versions_controller.rb b/app/controllers/admin/legislation/draft_versions_controller.rb new file mode 100644 index 000000000..3e0e22208 --- /dev/null +++ b/app/controllers/admin/legislation/draft_versions_controller.rb @@ -0,0 +1,43 @@ +class Admin::Legislation::DraftVersionsController < Admin::Legislation::BaseController + load_and_authorize_resource :process, class: "Legislation::Process" + load_and_authorize_resource :draft_version, class: "Legislation::DraftVersion", through: :process + + def index + @draft_versions = @process.draft_versions + end + + def create + @draft_version = @process.draft_versions.new(draft_version_params) + if @process.save + redirect_to admin_legislation_process_draft_versions_path + else + render :new + end + end + + def update + if @draft_version.update(draft_version_params) + redirect_to admin_legislation_process_draft_versions_path + else + render :edit + end + end + + def destroy + @draft_version.destroy + redirect_to admin_legislation_process_draft_versions_path + end + + private + + def draft_version_params + params.require(:legislation_draft_version).permit( + :legislation_process_id, + :title, + :changelog, + :status, + :final_version, + :body + ) + end +end diff --git a/app/controllers/admin/legislation/processes_controller.rb b/app/controllers/admin/legislation/processes_controller.rb index 83fae8812..b2ab765dd 100644 --- a/app/controllers/admin/legislation/processes_controller.rb +++ b/app/controllers/admin/legislation/processes_controller.rb @@ -17,7 +17,6 @@ class Admin::Legislation::ProcessesController < Admin::Legislation::BaseControll end def update - @process.assign_attributes(process_params) if @process.update(process_params) redirect_to admin_legislation_processes_path else diff --git a/app/models/abilities/administrator.rb b/app/models/abilities/administrator.rb index b866d226e..69a5afed2 100644 --- a/app/models/abilities/administrator.rb +++ b/app/models/abilities/administrator.rb @@ -45,6 +45,7 @@ module Abilities can [:search, :edit, :update, :create, :index, :destroy], Banner can [:manage], ::Legislation::Process + can [:manage], ::Legislation::DraftVersion end end end diff --git a/app/models/legislation/draft_version.rb b/app/models/legislation/draft_version.rb new file mode 100644 index 000000000..22833cf6a --- /dev/null +++ b/app/models/legislation/draft_version.rb @@ -0,0 +1,12 @@ +class Legislation::DraftVersion < ActiveRecord::Base + VALID_STATUSES = %w(draft published) + + acts_as_paranoid column: :hidden_at + include ActsAsParanoidAliases + + belongs_to :process, class_name: 'Legislation::Process', foreign_key: 'legislation_process_id' + + validates :title, presence: true + validates :body, presence: true + validates :status, presence: true, inclusion: { in: VALID_STATUSES } +end diff --git a/app/models/legislation/process.rb b/app/models/legislation/process.rb index 487a7e70e..43cca02f6 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 + has_many :draft_versions, class_name: 'Legislation::DraftVersion', foreign_key: 'legislation_process_id' + validates :title, presence: true validates :description, presence: true validates :target, presence: true diff --git a/app/views/admin/legislation/draft_versions/_form.html.erb b/app/views/admin/legislation/draft_versions/_form.html.erb new file mode 100644 index 000000000..67c21ba00 --- /dev/null +++ b/app/views/admin/legislation/draft_versions/_form.html.erb @@ -0,0 +1,72 @@ +<%= form_for [:admin, @process, @draft_version], url: url do |f| %> + + <% if @draft_version.errors.any? %> + +
+ + + + <%= @draft_version.errors.count %> + <%= t("admin.legislation.draft_versions.errors.form.error", count: @process.errors.count) %> + +
+ +<% end %> + +
+
+ <%= f.label :title %> +
+
+ <%= f.text_field :title, label: false %> +
+
+ +
+
+ <%= f.label :changelog %> +
+
+ <%= f.text_area :changelog, label: false, rows: 5 %> +
+
+ +
+
+ <%= f.label :status %> +
+
+ <% ::Legislation::DraftVersion::VALID_STATUSES.each do |status| %> + <%= f.radio_button :status, status, label: false %> + <%= f.label t("admin.legislation.draft_versions.statuses.#{status}") %> +
+ <% end %> +
+
+ +
+
+ <%= f.label :final_version %> +
+
+ <%= f.check_box :final_version, label: false %> +
+
+ +
+
+ <%= f.label :body %> +
+
+ <%= f.text_area :body, label: false, rows: 15 %> +
+
+ +
+
+ <%= f.submit(class: "button expanded", value: t("admin.legislation.draft_versions.#{admin_submit_action(@draft_version)}.submit_button")) %> +
+
+<% end %> diff --git a/app/views/admin/legislation/draft_versions/edit.html.erb b/app/views/admin/legislation/draft_versions/edit.html.erb new file mode 100644 index 000000000..b48c6409b --- /dev/null +++ b/app/views/admin/legislation/draft_versions/edit.html.erb @@ -0,0 +1,21 @@ +
+
+ <%= link_to admin_legislation_processes_path, class: "back" do %> + + <%= t("admin.legislation.draft_versions.edit.back") %> + <% end %> + +

<%= @process.title %>

+ + <%= render 'admin/legislation/processes/subnav', process: @process, active: 'draft_versions' %> + +

<%= @draft_version.title %>

+ + <%= render 'form', url: admin_legislation_process_draft_version_path(@process, @draft_version) %> + + <%= link_to t("admin.legislation.processes.index.delete"), admin_legislation_process_draft_version_path(@process, @draft_version), + method: :delete, + class: 'button hollow alert' %> + +
+
diff --git a/app/views/admin/legislation/draft_versions/index.html.erb b/app/views/admin/legislation/draft_versions/index.html.erb new file mode 100644 index 000000000..db6a459d2 --- /dev/null +++ b/app/views/admin/legislation/draft_versions/index.html.erb @@ -0,0 +1,38 @@ +
+
+ <%= link_to admin_legislation_processes_path, class: "back" do %> + + <%= t("admin.legislation.processes.edit.back") %> + <% end %> + +

<%= @process.title %>

+ + <%= render 'admin/legislation/processes/subnav', process: @process, active: 'draft_versions' %> + + <%= link_to t("admin.legislation.draft_versions.index.create"), + new_admin_legislation_process_draft_version_path, class: "button float-right" %> + +

<%= t("admin.legislation.draft_versions.index.title") %>

+ + + + + + + + + + <% @process.draft_versions.each do |draft_version| %> + + + + + + + + <% end %> +
<%= t("admin.legislation.draft_versions.table.title") %><%= t("admin.legislation.draft_versions.table.created_at") %><%= t("admin.legislation.draft_versions.table.status") %><%= t("admin.legislation.draft_versions.table.comments") %><%= t("admin.legislation.draft_versions.table.final_version") %>
+ <%= link_to draft_version.title, edit_admin_legislation_process_draft_version_path(@process, draft_version) %> + <%= draft_version.created_at.to_date %><%= draft_version.status %><%#= draft_version.comments %><%= draft_version.final_version %>
+
+
diff --git a/app/views/admin/legislation/draft_versions/new.html.erb b/app/views/admin/legislation/draft_versions/new.html.erb new file mode 100644 index 000000000..c1ee1be90 --- /dev/null +++ b/app/views/admin/legislation/draft_versions/new.html.erb @@ -0,0 +1,17 @@ +
+
+ <%= link_to admin_legislation_processes_path, class: "back" do %> + + <%= t("admin.legislation.draft_versions.new.back") %> + <% end %> + +

<%= @process.title %>

+ + <%= render 'admin/legislation/processes/subnav', process: @process, active: 'draft_versions' %> + +

<%= t("admin.legislation.draft_versions.new.title") %>

+ + <%= render 'form', url: admin_legislation_process_draft_versions_path(@process) %> + +
+
diff --git a/app/views/admin/legislation/processes/_subnav.html.erb b/app/views/admin/legislation/processes/_subnav.html.erb new file mode 100644 index 000000000..a645faef4 --- /dev/null +++ b/app/views/admin/legislation/processes/_subnav.html.erb @@ -0,0 +1,8 @@ + diff --git a/app/views/admin/legislation/processes/edit.html.erb b/app/views/admin/legislation/processes/edit.html.erb index 013bebaa3..398dc1ec6 100644 --- a/app/views/admin/legislation/processes/edit.html.erb +++ b/app/views/admin/legislation/processes/edit.html.erb @@ -8,6 +8,8 @@

<%= @process.title %>

+ <%= render 'subnav', process: @process, active: 'info' %> + <%= render "form" %> diff --git a/app/views/admin/legislation/processes/index.html.erb b/app/views/admin/legislation/processes/index.html.erb index 812812856..c3ca87c51 100644 --- a/app/views/admin/legislation/processes/index.html.erb +++ b/app/views/admin/legislation/processes/index.html.erb @@ -19,9 +19,6 @@ - <%= link_to t("admin.legislation.processes.index.edit"), edit_admin_legislation_process_path(process), - class: 'edit-banner button hollow' %> - <%= link_to t("admin.legislation.processes.index.delete"), admin_legislation_process_path(process), method: :delete, class: 'button hollow alert' %> diff --git a/config/i18n-tasks.yml b/config/i18n-tasks.yml index 55edc13a1..b4476329a 100644 --- a/config/i18n-tasks.yml +++ b/config/i18n-tasks.yml @@ -119,6 +119,7 @@ ignore_unused: - 'admin.activity.show.filter*' - 'admin.legislation.processes.index.filter*' - 'admin.legislation.processes.*.submit_button' + - 'admin.legislation.draft_versions.*.submit_button' - 'admin.comments.index.hidden_*' - 'admin.settings.index.features.*' - 'moderation.comments.index.filter*' diff --git a/config/locales/activerecord.en.yml b/config/locales/activerecord.en.yml index 91d4458a4..ad7b20e8d 100644 --- a/config/locales/activerecord.en.yml +++ b/config/locales/activerecord.en.yml @@ -37,6 +37,9 @@ en: legislation/process: one: "Process" other: "Processes" + legislation/draft_versions: + one: "Draft version" + other: "Draft versions" attributes: comment: body: "Comment" @@ -86,6 +89,12 @@ en: allegations_start_date: Allegations start date allegations_end_date: Allegations end date final_publication_date: Final result publication date + legislation/draft_version: + title: Version title + body: Text + changelog: Changes + status: Status + final_version: Final version errors: models: user: diff --git a/config/locales/activerecord.es.yml b/config/locales/activerecord.es.yml index d4a6e2800..7d6147a54 100644 --- a/config/locales/activerecord.es.yml +++ b/config/locales/activerecord.es.yml @@ -37,6 +37,9 @@ es: legislation/process: one: "Proceso" other: "Procesos" + legislation/draft_texts: + one: "Borrador" + other: "Borradores" attributes: comment: body: "Comentario" @@ -86,6 +89,12 @@ es: allegations_start_date: Fecha de inicio de alegaciones allegations_end_date: Fecha de fin de alegaciones final_publication_date: Fecha de publicación del resultado final + legislation/draft_version: + title: Título de la version + body: Texto + changelog: Cambios + status: Estado + final_version: Versión final errors: models: user: diff --git a/config/locales/admin.en.yml b/config/locales/admin.en.yml index 3514f9bd6..9041793da 100755 --- a/config/locales/admin.en.yml +++ b/config/locales/admin.en.yml @@ -93,7 +93,6 @@ en: index: create: New process delete: Delete - edit: Edit title: Legislation processess filters: open: Open @@ -106,6 +105,32 @@ en: submit_button: Create process process: title: Collaborative legislation process title + subnav: + info: Information + draft_texts: Text + draft_versions: + edit: + back: Back + submit_button: Save changes + errors: + form: + error: Error + index: + title: Draft versions + create: Create version + new: + back: Back + title: Create new version + submit_button: Create version + statuses: + draft: Draft + published: Published + table: + title: Títle + created_at: Created at + comments: Comments + final_version: Final version + status: Status managers: index: title: Managers diff --git a/config/locales/admin.es.yml b/config/locales/admin.es.yml index aa638f455..9308f453f 100644 --- a/config/locales/admin.es.yml +++ b/config/locales/admin.es.yml @@ -91,7 +91,6 @@ es: index: create: Nuevo proceso delete: Borrar - edit: Editar title: Procesos de legislación colaborativa filters: open: Abiertos @@ -104,6 +103,32 @@ es: submit_button: Crear proceso process: title: Título del proceso de legislación colaborativa + subnav: + info: Información + draft_texts: Texto + draft_versions: + edit: + back: Volver + submit_button: Guardar cambios + errors: + form: + error: Error + index: + title: Versiones del borrador + create: Crear versión + new: + back: Volver + title: Crear nueva versión + submit_button: Crear versión + statuses: + draft: Borrador + published: Publicado + table: + title: Título + created_at: Creado + comments: Comentarios + final_version: Versión final + status: Estado managers: index: title: Gestores diff --git a/config/routes.rb b/config/routes.rb index e7043d327..56dfc46d0 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -190,7 +190,9 @@ Rails.application.routes.draw do end namespace :legislation do - resources :processes + resources :processes do + resources :draft_versions + end end namespace :api do diff --git a/db/dev_seeds.rb b/db/dev_seeds.rb index 086751ac0..2f2a28c1c 100644 --- a/db/dev_seeds.rb +++ b/db/dev_seeds.rb @@ -355,3 +355,33 @@ Proposal.last(3).each do |proposal| created_at: rand((Time.current - 1.week) .. Time.current)) puts " #{banner.title}" end + +puts "Creating legislation processes" + +(1..5).each do |i| + process = ::Legislation::Process.create!(title: Faker::Lorem.sentence(3).truncate(60), + description: Faker::Lorem.paragraphs.join("\n\n"), + target: Faker::Lorem.paragraphs.join("\n\n"), + how_to_participate: Faker::Lorem.paragraphs.join("\n\n"), + additional_info: Faker::Lorem.paragraphs.join("\n\n"), + start_date: Date.current - 3.days, + end_date: Date.current + 3.days, + debate_start_date: Date.current - 3.days, + debate_end_date: Date.current - 1.day, + 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 + ) + puts " #{process.title}" +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") + ) + puts " #{version.title}" + end +end + diff --git a/db/migrate/20161205110441_create_legislation_draft_versions.rb b/db/migrate/20161205110441_create_legislation_draft_versions.rb new file mode 100644 index 000000000..1eb15eebe --- /dev/null +++ b/db/migrate/20161205110441_create_legislation_draft_versions.rb @@ -0,0 +1,16 @@ +class CreateLegislationDraftVersions < ActiveRecord::Migration + def change + create_table :legislation_draft_versions do |t| + t.references :legislation_process, index: true, foreign_key: true + t.string :title + t.text :changelog + t.string :status, index: true, default: :draft + t.boolean :final_version, default: false + t.text :body + + t.datetime :hidden_at, index: true + + t.timestamps null: false + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 213ecffbc..ba661579a 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: 20161117135624) do +ActiveRecord::Schema.define(version: 20161205110441) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -228,6 +228,22 @@ ActiveRecord::Schema.define(version: 20161117135624) do t.datetime "updated_at", null: false end + create_table "legislation_draft_versions", force: :cascade do |t| + t.integer "legislation_process_id" + t.string "title" + t.text "changelog" + t.string "status", default: "draft" + t.boolean "final_version", default: false + t.text "body" + t.datetime "hidden_at" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + add_index "legislation_draft_versions", ["hidden_at"], name: "index_legislation_draft_versions_on_hidden_at", using: :btree + add_index "legislation_draft_versions", ["legislation_process_id"], name: "index_legislation_draft_versions_on_legislation_process_id", using: :btree + add_index "legislation_draft_versions", ["status"], name: "index_legislation_draft_versions_on_status", using: :btree + create_table "legislation_processes", force: :cascade do |t| t.string "title" t.text "description" @@ -584,6 +600,7 @@ ActiveRecord::Schema.define(version: 20161117135624) do add_foreign_key "failed_census_calls", "users" add_foreign_key "flags", "users" add_foreign_key "identities", "users" + add_foreign_key "legislation_draft_versions", "legislation_processes" add_foreign_key "locks", "users" add_foreign_key "managers", "users" add_foreign_key "moderators", "users" diff --git a/spec/factories.rb b/spec/factories.rb index cdfede19b..a68d47665 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -1,19 +1,4 @@ FactoryGirl.define do - factory :legislation_process, class: 'Legislation::Process' do - title "A collaborative legislation process" - description "Description of the process" - target "Who will affected by this law?" - how_to_participate "You can participate by answering some questions" - start_date "2016-11-16" - end_date "2016-11-16" - debate_start_date "2016-11-16" - debate_end_date "2016-11-16" - draft_publication_date "2016-11-16" - allegations_start_date "2016-11-16" - allegations_end_date "2016-11-16" - final_publication_date "2016-11-16" - end - sequence(:document_number) { |n| "#{n.to_s.rjust(8, '0')}X" } factory :user do @@ -357,4 +342,28 @@ FactoryGirl.define do association :sender, factory: :user association :receiver, factory: :user end + + factory :legislation_process, class: 'Legislation::Process' do + title "A collaborative legislation process" + description "Description of the process" + target "Who will affected by this law?" + how_to_participate "You can participate by answering some questions" + start_date "2016-11-16" + end_date "2016-11-16" + debate_start_date "2016-11-16" + debate_end_date "2016-11-16" + draft_publication_date "2016-11-16" + allegations_start_date "2016-11-16" + allegations_end_date "2016-11-16" + final_publication_date "2016-11-16" + end + + factory :legislation_draft_version, class: 'Legislation::DraftVersion' do + process factory: :legislation_process + title "Version 1" + changelog "What changed in this version" + status "draft" + final_version false + body "Body of the legislation text" + end end diff --git a/spec/features/admin/legislation/draft_versions_spec.rb b/spec/features/admin/legislation/draft_versions_spec.rb new file mode 100644 index 000000000..3bc1c057a --- /dev/null +++ b/spec/features/admin/legislation/draft_versions_spec.rb @@ -0,0 +1,66 @@ +require 'rails_helper' + +feature 'Admin legislation draft versions' do + + background do + admin = create(:administrator) + login_as(admin.user) + end + + context "Feature flag" do + + scenario 'Disabled with a feature flag' do + Setting['feature.legislation'] = nil + process = create(:legislation_process) + expect{ visit admin_legislation_process_draft_versions_path(process) }.to raise_exception(FeatureFlags::FeatureDisabled) + end + + end + + context "Index" do + + scenario 'Displaying legislation process draft versions' do + process = create(:legislation_process, title: 'An example legislation process') + draft_version = create(:legislation_draft_version, process: process, title: 'Version 1') + + visit admin_legislation_processes_path(filter: 'all') + + click_link 'An example legislation process' + click_link 'Text' + click_link 'Version 1' + + expect(page).to have_content(draft_version.title) + expect(page).to have_content(draft_version.changelog) + end + end + + context 'Create' do + scenario 'Valid legislation draft_version' do + process = create(:legislation_process, title: 'An example legislation process') + + visit admin_root_path + + within('#side_menu') do + click_link "Collaborative Legislation" + end + + click_link "All" + + expect(page).to have_content 'An example legislation process' + + click_link 'An example legislation process' + click_link 'Text' + + click_link 'Create version' + + fill_in 'legislation_draft_version_title', with: 'Version 3' + fill_in 'legislation_draft_version_changelog', with: 'Version 3 changes' + fill_in 'legislation_draft_version_body', with: 'Version 3 body' + + click_button 'Create version' + + expect(page).to have_content 'An example legislation process' + expect(page).to have_content 'Version 3' + end + end +end diff --git a/spec/models/legislation/draft_version_spec.rb b/spec/models/legislation/draft_version_spec.rb new file mode 100644 index 000000000..a6709110a --- /dev/null +++ b/spec/models/legislation/draft_version_spec.rb @@ -0,0 +1,9 @@ +require 'rails_helper' + +RSpec.describe Legislation::DraftVersion, type: :model do + let(:legislation_draft_version) { build(:legislation_draft_version) } + + it "should be valid" do + expect(legislation_draft_version).to be_valid + end +end