From cd4a9d232fea926e95cf1437800ecafa83c50351 Mon Sep 17 00:00:00 2001 From: Adam Gwozdowski Date: Sat, 28 Sep 2019 14:54:27 +0200 Subject: [PATCH 1/8] InstrumentType model --- app/admin/instrument_types.rb | 34 +++++++++++++++++++ app/decorators/instrument_type_decorator.rb | 7 ++++ app/models/instrument_type.rb | 13 +++++++ .../20190928120638_create_instrument_types.rb | 9 +++++ db/schema.rb | 8 ++++- spec/factories/instrument_types.rb | 15 ++++++++ spec/models/instrument_type_spec.rb | 22 ++++++++++++ 7 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 app/admin/instrument_types.rb create mode 100644 app/decorators/instrument_type_decorator.rb create mode 100644 app/models/instrument_type.rb create mode 100644 db/migrate/20190928120638_create_instrument_types.rb create mode 100644 spec/factories/instrument_types.rb create mode 100644 spec/models/instrument_type_spec.rb diff --git a/app/admin/instrument_types.rb b/app/admin/instrument_types.rb new file mode 100644 index 000000000..35052e72b --- /dev/null +++ b/app/admin/instrument_types.rb @@ -0,0 +1,34 @@ +ActiveAdmin.register InstrumentType do + menu parent: 'Laws', priority: 6 + config.batch_actions = false + + permit_params :name + + decorate_with InstrumentTypeDecorator + + filter :name_contains, label: 'Name' + + index do + column :name, :name_link + + actions + end + + show do + attributes_table do + row :name + end + + active_admin_comments + end + + form html: {'data-controller' => 'check-modified'} do |f| + f.semantic_errors(*f.object.errors.keys) + + f.inputs do + f.input :name + end + + f.actions + end +end diff --git a/app/decorators/instrument_type_decorator.rb b/app/decorators/instrument_type_decorator.rb new file mode 100644 index 000000000..13006db5f --- /dev/null +++ b/app/decorators/instrument_type_decorator.rb @@ -0,0 +1,7 @@ +class InstrumentTypeDecorator < Draper::Decorator + delegate_all + + def name_link + h.link_to model.name, h.admin_instrument_type_path(model) + end +end diff --git a/app/models/instrument_type.rb b/app/models/instrument_type.rb new file mode 100644 index 000000000..8be70db97 --- /dev/null +++ b/app/models/instrument_type.rb @@ -0,0 +1,13 @@ +# == Schema Information +# +# Table name: instrument_types +# +# id :bigint not null, primary key +# name :string +# created_at :datetime not null +# updated_at :datetime not null +# + +class InstrumentType < ApplicationRecord + validates_presence_of :name +end diff --git a/db/migrate/20190928120638_create_instrument_types.rb b/db/migrate/20190928120638_create_instrument_types.rb new file mode 100644 index 000000000..b8ab01ef5 --- /dev/null +++ b/db/migrate/20190928120638_create_instrument_types.rb @@ -0,0 +1,9 @@ +class CreateInstrumentTypes < ActiveRecord::Migration[5.2] + def change + create_table :instrument_types do |t| + t.string :name + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 140886bae..e6dd13811 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2019_09_19_193834) do +ActiveRecord::Schema.define(version: 2019_09_28_120638) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -187,6 +187,12 @@ t.index ["updated_by_id"], name: "index_geographies_on_updated_by_id" end + create_table "instrument_types", force: :cascade do |t| + t.string "name" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + create_table "legislations", force: :cascade do |t| t.string "title" t.text "description" diff --git a/spec/factories/instrument_types.rb b/spec/factories/instrument_types.rb new file mode 100644 index 000000000..63ab2ec04 --- /dev/null +++ b/spec/factories/instrument_types.rb @@ -0,0 +1,15 @@ +# == Schema Information +# +# Table name: instrument_types +# +# id :bigint not null, primary key +# name :string +# created_at :datetime not null +# updated_at :datetime not null +# + +FactoryBot.define do + factory :instrument_type do + sequence(:name) { |n| "instrument_type#{n}" } + end +end diff --git a/spec/models/instrument_type_spec.rb b/spec/models/instrument_type_spec.rb new file mode 100644 index 000000000..dd2c170a2 --- /dev/null +++ b/spec/models/instrument_type_spec.rb @@ -0,0 +1,22 @@ +# == Schema Information +# +# Table name: instrument_types +# +# id :bigint not null, primary key +# name :string +# created_at :datetime not null +# updated_at :datetime not null +# + +require 'rails_helper' + +RSpec.describe InstrumentType, type: :model do + subject { build(:instrument_type) } + + it { is_expected.to be_valid } + + it 'should be invalid without name' do + subject.name = nil + expect(subject).to have(1).errors_on(:name) + end +end From fdd69bb06baa60190a17e3dc7c9519c9c57642d3 Mon Sep 17 00:00:00 2001 From: Adam Gwozdowski Date: Sat, 28 Sep 2019 20:36:06 +0200 Subject: [PATCH 2/8] Soft-delete for instrument type --- app/admin/instrument_types.rb | 4 ++ .../command/destroy/instrument_type.rb | 15 +++++++ app/models/instrument_type.rb | 2 + ...928_add_discarded_at_to_instrument_type.rb | 6 +++ db/schema.rb | 4 +- .../admin/instrument_types_controller_spec.rb | 42 +++++++++++++++++++ 6 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 app/commands/command/destroy/instrument_type.rb create mode 100644 db/migrate/20190928182928_add_discarded_at_to_instrument_type.rb create mode 100644 spec/controllers/admin/instrument_types_controller_spec.rb diff --git a/app/admin/instrument_types.rb b/app/admin/instrument_types.rb index 35052e72b..2b17363f6 100644 --- a/app/admin/instrument_types.rb +++ b/app/admin/instrument_types.rb @@ -31,4 +31,8 @@ f.actions end + + controller do + include DiscardableController + end end diff --git a/app/commands/command/destroy/instrument_type.rb b/app/commands/command/destroy/instrument_type.rb new file mode 100644 index 000000000..378e07b83 --- /dev/null +++ b/app/commands/command/destroy/instrument_type.rb @@ -0,0 +1,15 @@ +module Command + module Destroy + class InstrumentType + def initialize(resource) + @resource = resource + end + + def call + ActiveRecord::Base.transaction do + @resource.tap(&:discard) + end + end + end + end +end diff --git a/app/models/instrument_type.rb b/app/models/instrument_type.rb index 8be70db97..de6f0a9c8 100644 --- a/app/models/instrument_type.rb +++ b/app/models/instrument_type.rb @@ -9,5 +9,7 @@ # class InstrumentType < ApplicationRecord + include DiscardableModel + validates_presence_of :name end diff --git a/db/migrate/20190928182928_add_discarded_at_to_instrument_type.rb b/db/migrate/20190928182928_add_discarded_at_to_instrument_type.rb new file mode 100644 index 000000000..d89a6fc2d --- /dev/null +++ b/db/migrate/20190928182928_add_discarded_at_to_instrument_type.rb @@ -0,0 +1,6 @@ +class AddDiscardedAtToInstrumentType < ActiveRecord::Migration[5.2] + def change + add_column :instrument_types, :discarded_at, :datetime + add_index :instrument_types, :discarded_at + end +end diff --git a/db/schema.rb b/db/schema.rb index e6dd13811..0280c9b2a 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2019_09_28_120638) do +ActiveRecord::Schema.define(version: 2019_09_28_182928) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -191,6 +191,8 @@ t.string "name" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.datetime "discarded_at" + t.index ["discarded_at"], name: "index_instrument_types_on_discarded_at" end create_table "legislations", force: :cascade do |t| diff --git a/spec/controllers/admin/instrument_types_controller_spec.rb b/spec/controllers/admin/instrument_types_controller_spec.rb new file mode 100644 index 000000000..1b5c86ed6 --- /dev/null +++ b/spec/controllers/admin/instrument_types_controller_spec.rb @@ -0,0 +1,42 @@ +require 'rails_helper' + +RSpec.describe Admin::InstrumentTypesController, type: :controller do + let(:admin) { create(:admin_user) } + before { sign_in admin } + + describe 'DELETE destroy' do + let!(:instrument_type) { create(:instrument_type, discarded_at: nil) } + + context 'with valid params' do + subject { delete :destroy, params: {id: instrument_type.id} } + + before do + expect { subject }.to change { InstrumentType.count }.by(-1) + end + + it 'set discarded_at date to instrument type object' do + expect(instrument_type.reload.discarded_at).to_not be_nil + end + + it 'shows proper notice' do + expect(flash[:notice]).to match('Successfully deleted selected InstrumentType') + end + end + + context 'with invalid params' do + let(:command) { double } + + subject { delete :destroy, params: {id: instrument_type.id} } + + before do + expect(::Command::Destroy::InstrumentType).to receive(:new).and_return(command) + expect(command).to receive(:call).and_return(nil) + end + + it 'redirects to index & renders alert message' do + expect(subject).to redirect_to(admin_instrument_types_path) + expect(flash[:alert]).to match('Could not delete selected InstrumentType') + end + end + end +end From 0b40dd15a6bf7b330f293fda9b023e8fdfc6d5f4 Mon Sep 17 00:00:00 2001 From: Adam Gwozdowski Date: Sun, 29 Sep 2019 14:35:11 +0200 Subject: [PATCH 3/8] Instrument model, migration, resource, form, soft-delete --- app/admin/instrument.rb | 43 +++++++++++++++++++ app/commands/command/destroy/instrument.rb | 15 +++++++ .../command/destroy/instrument_type.rb | 6 ++- app/decorators/instrument_decorator.rb | 7 +++ app/models/instrument.rb | 19 ++++++++ app/models/instrument_type.rb | 11 +++-- .../20190929104944_create_instruments.rb | 10 +++++ ...29110158_add_discarded_at_to_instrument.rb | 6 +++ db/schema.rb | 13 +++++- .../admin/instrument_controller_spec.rb | 42 ++++++++++++++++++ spec/factories/instrument_types.rb | 16 +++++-- spec/factories/instruments.rb | 18 ++++++++ spec/models/instrument_spec.rb | 29 +++++++++++++ spec/models/instrument_type_spec.rb | 9 ++-- 14 files changed, 230 insertions(+), 14 deletions(-) create mode 100644 app/admin/instrument.rb create mode 100644 app/commands/command/destroy/instrument.rb create mode 100644 app/decorators/instrument_decorator.rb create mode 100644 app/models/instrument.rb create mode 100644 db/migrate/20190929104944_create_instruments.rb create mode 100644 db/migrate/20190929110158_add_discarded_at_to_instrument.rb create mode 100644 spec/controllers/admin/instrument_controller_spec.rb create mode 100644 spec/factories/instruments.rb create mode 100644 spec/models/instrument_spec.rb diff --git a/app/admin/instrument.rb b/app/admin/instrument.rb new file mode 100644 index 000000000..7508d993f --- /dev/null +++ b/app/admin/instrument.rb @@ -0,0 +1,43 @@ +ActiveAdmin.register Instrument do + menu parent: 'Laws', priority: 7 + + permit_params :name, :instrument_type_id + + config.batch_actions = false + + decorate_with InstrumentDecorator + + filter :name_contains, label: 'Name' + filter :instrument_type + + index do + column :name, :name_link + column(:instrument_type) { |t| t.model.instrument_type } + + actions + end + + show do + attributes_table do + row :name + row(:instrument_type) { |t| t.model.instrument_type } + end + + active_admin_comments + end + + form html: {'data-controller' => 'check-modified'} do |f| + f.semantic_errors(*f.object.errors.keys) + + f.inputs do + f.input :name + f.input(:instrument_type) { |t| t.model.instrument_type } + end + + f.actions + end + + controller do + include DiscardableController + end +end diff --git a/app/commands/command/destroy/instrument.rb b/app/commands/command/destroy/instrument.rb new file mode 100644 index 000000000..8697ac2b3 --- /dev/null +++ b/app/commands/command/destroy/instrument.rb @@ -0,0 +1,15 @@ +module Command + module Destroy + class Instrument + def initialize(resource) + @resource = resource + end + + def call + ActiveRecord::Base.transaction do + @resource.tap(&:discard) + end + end + end + end +end diff --git a/app/commands/command/destroy/instrument_type.rb b/app/commands/command/destroy/instrument_type.rb index 378e07b83..195f99c5f 100644 --- a/app/commands/command/destroy/instrument_type.rb +++ b/app/commands/command/destroy/instrument_type.rb @@ -7,7 +7,11 @@ def initialize(resource) def call ActiveRecord::Base.transaction do - @resource.tap(&:discard) + @resource.tap do |r| + r.discard + + r.instruments = [] + end end end end diff --git a/app/decorators/instrument_decorator.rb b/app/decorators/instrument_decorator.rb new file mode 100644 index 000000000..5e22a270a --- /dev/null +++ b/app/decorators/instrument_decorator.rb @@ -0,0 +1,7 @@ +class InstrumentDecorator < Draper::Decorator + delegate_all + + def name_link + h.link_to model.name, h.admin_instrument_path(model) + end +end diff --git a/app/models/instrument.rb b/app/models/instrument.rb new file mode 100644 index 000000000..b653f39bd --- /dev/null +++ b/app/models/instrument.rb @@ -0,0 +1,19 @@ +# == Schema Information +# +# Table name: instruments +# +# id :bigint not null, primary key +# name :string +# instrument_type_id :bigint +# created_at :datetime not null +# updated_at :datetime not null +# discarded_at :datetime +# + +class Instrument < ApplicationRecord + include DiscardableModel + + belongs_to :instrument_type + + validates_presence_of :name +end diff --git a/app/models/instrument_type.rb b/app/models/instrument_type.rb index de6f0a9c8..3faa44578 100644 --- a/app/models/instrument_type.rb +++ b/app/models/instrument_type.rb @@ -2,14 +2,17 @@ # # Table name: instrument_types # -# id :bigint not null, primary key -# name :string -# created_at :datetime not null -# updated_at :datetime not null +# id :bigint not null, primary key +# name :string +# created_at :datetime not null +# updated_at :datetime not null +# discarded_at :datetime # class InstrumentType < ApplicationRecord include DiscardableModel validates_presence_of :name + + has_many :instruments end diff --git a/db/migrate/20190929104944_create_instruments.rb b/db/migrate/20190929104944_create_instruments.rb new file mode 100644 index 000000000..d291122d0 --- /dev/null +++ b/db/migrate/20190929104944_create_instruments.rb @@ -0,0 +1,10 @@ +class CreateInstruments < ActiveRecord::Migration[5.2] + def change + create_table :instruments do |t| + t.string :name + t.references :instrument_type, foreign_key: true + + t.timestamps + end + end +end diff --git a/db/migrate/20190929110158_add_discarded_at_to_instrument.rb b/db/migrate/20190929110158_add_discarded_at_to_instrument.rb new file mode 100644 index 000000000..697750753 --- /dev/null +++ b/db/migrate/20190929110158_add_discarded_at_to_instrument.rb @@ -0,0 +1,6 @@ +class AddDiscardedAtToInstrument < ActiveRecord::Migration[5.2] + def change + add_column :instruments, :discarded_at, :datetime + add_index :instruments, :discarded_at + end +end diff --git a/db/schema.rb b/db/schema.rb index 0280c9b2a..116cb2fc0 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2019_09_28_182928) do +ActiveRecord::Schema.define(version: 2019_09_29_110158) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -195,6 +195,16 @@ t.index ["discarded_at"], name: "index_instrument_types_on_discarded_at" end + create_table "instruments", force: :cascade do |t| + t.string "name" + t.bigint "instrument_type_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.datetime "discarded_at" + t.index ["discarded_at"], name: "index_instruments_on_discarded_at" + t.index ["instrument_type_id"], name: "index_instruments_on_instrument_type_id" + end + create_table "legislations", force: :cascade do |t| t.string "title" t.text "description" @@ -353,6 +363,7 @@ add_foreign_key "external_legislations", "geographies" add_foreign_key "geographies", "admin_users", column: "created_by_id" add_foreign_key "geographies", "admin_users", column: "updated_by_id" + add_foreign_key "instruments", "instrument_types" add_foreign_key "legislations", "admin_users", column: "created_by_id" add_foreign_key "legislations", "admin_users", column: "updated_by_id" add_foreign_key "legislations", "geographies" diff --git a/spec/controllers/admin/instrument_controller_spec.rb b/spec/controllers/admin/instrument_controller_spec.rb new file mode 100644 index 000000000..e87b2336d --- /dev/null +++ b/spec/controllers/admin/instrument_controller_spec.rb @@ -0,0 +1,42 @@ +require 'rails_helper' + +RSpec.describe Admin::InstrumentsController, type: :controller do + let(:admin) { create(:admin_user) } + before { sign_in admin } + + describe 'DELETE destroy' do + let!(:instrument) { create(:instrument, discarded_at: nil) } + + context 'with valid pspec/factories/instrument_types.rb:16arams' do + subject { delete :destroy, params: {id: instrument.id} } + + before do + expect { subject }.to change { Instrument.count }.by(-1) + end + + it 'set discarded_at date to instrument object' do + expect(instrument.reload.discarded_at).to_not be_nil + end + + it 'shows proper notice' do + expect(flash[:notice]).to match('Successfully deleted selected Instrument') + end + end + + context 'with invalid params' do + let(:command) { double } + + subject { delete :destroy, params: {id: instrument.id} } + + before do + expect(::Command::Destroy::Instrument).to receive(:new).and_return(command) + expect(command).to receive(:call).and_return(nil) + end + + it 'redirects to index & renders alert message' do + expect(subject).to redirect_to(admin_instruments_path) + expect(flash[:alert]).to match('Could not delete selected Instrument') + end + end + end +end diff --git a/spec/factories/instrument_types.rb b/spec/factories/instrument_types.rb index 63ab2ec04..92dd22ebc 100644 --- a/spec/factories/instrument_types.rb +++ b/spec/factories/instrument_types.rb @@ -2,14 +2,22 @@ # # Table name: instrument_types # -# id :bigint not null, primary key -# name :string -# created_at :datetime not null -# updated_at :datetime not null +# id :bigint not null, primary key +# name :string +# created_at :datetime not null +# updated_at :datetime not null +# discarded_at :datetime # FactoryBot.define do factory :instrument_type do sequence(:name) { |n| "instrument_type#{n}" } + + trait :with_instruments do + after(:create) do + create :instrument + create :instrument + end + end end end diff --git a/spec/factories/instruments.rb b/spec/factories/instruments.rb new file mode 100644 index 000000000..f4864d3d7 --- /dev/null +++ b/spec/factories/instruments.rb @@ -0,0 +1,18 @@ +# == Schema Information +# +# Table name: instruments +# +# id :bigint not null, primary key +# name :string +# instrument_type_id :bigint +# created_at :datetime not null +# updated_at :datetime not null +# discarded_at :datetime +# + +FactoryBot.define do + factory :instrument do + sequence(:name) { |n| "instrument#{n}" } + association :instrument_type + end +end diff --git a/spec/models/instrument_spec.rb b/spec/models/instrument_spec.rb new file mode 100644 index 000000000..0933d3cb4 --- /dev/null +++ b/spec/models/instrument_spec.rb @@ -0,0 +1,29 @@ +# == Schema Information +# +# Table name: instruments +# +# id :bigint not null, primary key +# name :string +# instrument_type_id :bigint +# created_at :datetime not null +# updated_at :datetime not null +# discarded_at :datetime +# + +require 'rails_helper' + +RSpec.describe Instrument, type: :model do + subject { build(:instrument) } + + it { is_expected.to be_valid } + + it 'should be invalid without name' do + subject.name = nil + expect(subject).to have(1).errors_on(:name) + end + + it 'should be invalid without instrument_type' do + subject.instrument_type = nil + expect(subject).to have(1).errors_on(:instrument_type) + end +end diff --git a/spec/models/instrument_type_spec.rb b/spec/models/instrument_type_spec.rb index dd2c170a2..d7346e9ad 100644 --- a/spec/models/instrument_type_spec.rb +++ b/spec/models/instrument_type_spec.rb @@ -2,10 +2,11 @@ # # Table name: instrument_types # -# id :bigint not null, primary key -# name :string -# created_at :datetime not null -# updated_at :datetime not null +# id :bigint not null, primary key +# name :string +# created_at :datetime not null +# updated_at :datetime not null +# discarded_at :datetime # require 'rails_helper' From ba6bacd3f43800907f8d45f5a2283c42c26b438a Mon Sep 17 00:00:00 2001 From: Adam Gwozdowski Date: Mon, 30 Sep 2019 09:22:50 +0200 Subject: [PATCH 4/8] Adding instruments to legislation --- app/admin/legislations.rb | 3 ++- app/commands/command/destroy/instrument.rb | 6 +++++- app/commands/command/destroy/legislation.rb | 1 + app/decorators/legislation_decorator.rb | 11 +++++++++++ app/models/instrument.rb | 1 + app/models/legislation.rb | 1 + app/views/admin/legislations/_form.html.erb | 1 + ...193356_create_join_table_legislation_instrument.rb | 8 ++++++++ db/schema.rb | 9 ++++++++- 9 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 db/migrate/20190929193356_create_join_table_legislation_instrument.rb diff --git a/app/admin/legislations.rb b/app/admin/legislations.rb index f5ad191fc..f5a7b7585 100644 --- a/app/admin/legislations.rb +++ b/app/admin/legislations.rb @@ -13,7 +13,7 @@ :created_by_id, :updated_by_id, :visibility_status, events_attributes: permit_params_for(:events), documents_attributes: permit_params_for(:documents), - framework_ids: [], document_type_ids: [] + framework_ids: [], document_type_ids: [], instrument_ids: [] filter :title_contains, label: 'Title' filter :date_passed @@ -62,6 +62,7 @@ row 'Keywords', &:keywords_string row 'Natural Hazards', &:natural_hazards_string list_row 'Documents', :document_links + list_row 'Instruments', :instrument_links end end diff --git a/app/commands/command/destroy/instrument.rb b/app/commands/command/destroy/instrument.rb index 8697ac2b3..384f0c69a 100644 --- a/app/commands/command/destroy/instrument.rb +++ b/app/commands/command/destroy/instrument.rb @@ -7,7 +7,11 @@ def initialize(resource) def call ActiveRecord::Base.transaction do - @resource.tap(&:discard) + @resource.tap do |r| + r.discard + + r.legislations = [] + end end end end diff --git a/app/commands/command/destroy/legislation.rb b/app/commands/command/destroy/legislation.rb index 34b08b8b7..dd2833f5e 100644 --- a/app/commands/command/destroy/legislation.rb +++ b/app/commands/command/destroy/legislation.rb @@ -15,6 +15,7 @@ def call r.litigations = [] r.targets = [] + r.instruments = [] end end end diff --git a/app/decorators/legislation_decorator.rb b/app/decorators/legislation_decorator.rb index eae67d4ec..a33b6c962 100644 --- a/app/decorators/legislation_decorator.rb +++ b/app/decorators/legislation_decorator.rb @@ -41,4 +41,15 @@ def document_links title: document.external? ? document.url : nil end end + + def instrument_links + return [] if model.instruments.empty? + + model.instruments.map do |instrument| + h.link_to instrument.name, + h.admin_instrument_path(instrument), + target: '_blank', + title: instrument.name + end + end end diff --git a/app/models/instrument.rb b/app/models/instrument.rb index b653f39bd..468b3aeb9 100644 --- a/app/models/instrument.rb +++ b/app/models/instrument.rb @@ -14,6 +14,7 @@ class Instrument < ApplicationRecord include DiscardableModel belongs_to :instrument_type + has_and_belongs_to_many :legislations validates_presence_of :name end diff --git a/app/models/legislation.rb b/app/models/legislation.rb index d506bc975..3c8469c57 100644 --- a/app/models/legislation.rb +++ b/app/models/legislation.rb @@ -43,6 +43,7 @@ class Legislation < ApplicationRecord has_many :events, as: :eventable, dependent: :destroy has_and_belongs_to_many :targets has_and_belongs_to_many :litigations + has_and_belongs_to_many :instruments with_options allow_destroy: true, reject_if: :all_blank do accepts_nested_attributes_for :documents diff --git a/app/views/admin/legislations/_form.html.erb b/app/views/admin/legislations/_form.html.erb index 2a8d5eff2..df42e2389 100644 --- a/app/views/admin/legislations/_form.html.erb +++ b/app/views/admin/legislations/_form.html.erb @@ -16,6 +16,7 @@ <%= f.input :title %> <%= f.input :description, as: :trix %> <%= f.input :document_type_ids, label: 'Document', as: :tags, collection: DocumentType.all %> + <%= f.input :instrument_ids, label: 'Instrument', as: :tags, collection: Instrument.all %> <%= Arbre::Context.new do columns do diff --git a/db/migrate/20190929193356_create_join_table_legislation_instrument.rb b/db/migrate/20190929193356_create_join_table_legislation_instrument.rb new file mode 100644 index 000000000..5156cdbbd --- /dev/null +++ b/db/migrate/20190929193356_create_join_table_legislation_instrument.rb @@ -0,0 +1,8 @@ +class CreateJoinTableLegislationInstrument < ActiveRecord::Migration[5.2] + def change + create_join_table :legislations, :instruments do |t| + t.index :legislation_id + t.index :instrument_id + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 116cb2fc0..04b9d6364 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2019_09_29_110158) do +ActiveRecord::Schema.define(version: 2019_09_29_193356) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -205,6 +205,13 @@ t.index ["instrument_type_id"], name: "index_instruments_on_instrument_type_id" end + create_table "instruments_legislations", id: false, force: :cascade do |t| + t.bigint "legislation_id", null: false + t.bigint "instrument_id", null: false + t.index ["instrument_id"], name: "index_instruments_legislations_on_instrument_id" + t.index ["legislation_id"], name: "index_instruments_legislations_on_legislation_id" + end + create_table "legislations", force: :cascade do |t| t.string "title" t.text "description" From 4c48e16c64460dfc6079bae1f3b141859ef2aebb Mon Sep 17 00:00:00 2001 From: Adam Gwozdowski Date: Mon, 30 Sep 2019 09:40:43 +0200 Subject: [PATCH 5/8] Use instrument_type in instrument views --- app/admin/instrument.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/admin/instrument.rb b/app/admin/instrument.rb index 7508d993f..17d1202df 100644 --- a/app/admin/instrument.rb +++ b/app/admin/instrument.rb @@ -12,7 +12,7 @@ index do column :name, :name_link - column(:instrument_type) { |t| t.model.instrument_type } + column :instrument_type actions end @@ -20,7 +20,7 @@ show do attributes_table do row :name - row(:instrument_type) { |t| t.model.instrument_type } + row :instrument_type end active_admin_comments @@ -31,7 +31,7 @@ f.inputs do f.input :name - f.input(:instrument_type) { |t| t.model.instrument_type } + f.input :instrument_type end f.actions From 18e3d615a9ddbb5ccc8ab0844629ff6fd25857fe Mon Sep 17 00:00:00 2001 From: Adam Gwozdowski Date: Mon, 30 Sep 2019 09:57:33 +0200 Subject: [PATCH 6/8] Remove unnecessary if --- app/decorators/legislation_decorator.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/app/decorators/legislation_decorator.rb b/app/decorators/legislation_decorator.rb index a33b6c962..d32bb3c5c 100644 --- a/app/decorators/legislation_decorator.rb +++ b/app/decorators/legislation_decorator.rb @@ -43,8 +43,6 @@ def document_links end def instrument_links - return [] if model.instruments.empty? - model.instruments.map do |instrument| h.link_to instrument.name, h.admin_instrument_path(instrument), From 0f638e10910e8f417f9317b89505e86ddb32e331 Mon Sep 17 00:00:00 2001 From: Adam Gwozdowski Date: Mon, 30 Sep 2019 10:07:10 +0200 Subject: [PATCH 7/8] Change order for Instruments and InstrumentTypes --- app/admin/instrument.rb | 2 +- app/admin/instrument_types.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/admin/instrument.rb b/app/admin/instrument.rb index 17d1202df..c61edc125 100644 --- a/app/admin/instrument.rb +++ b/app/admin/instrument.rb @@ -1,5 +1,5 @@ ActiveAdmin.register Instrument do - menu parent: 'Laws', priority: 7 + menu parent: 'Laws', priority: 6 permit_params :name, :instrument_type_id diff --git a/app/admin/instrument_types.rb b/app/admin/instrument_types.rb index 2b17363f6..9025d6130 100644 --- a/app/admin/instrument_types.rb +++ b/app/admin/instrument_types.rb @@ -1,5 +1,5 @@ ActiveAdmin.register InstrumentType do - menu parent: 'Laws', priority: 6 + menu parent: 'Laws', priority: 7 config.batch_actions = false permit_params :name From 0cfd84016b18fd6c952e7468fd00702b97cb67ee Mon Sep 17 00:00:00 2001 From: Adam Gwozdowski Date: Mon, 30 Sep 2019 13:03:26 +0200 Subject: [PATCH 8/8] Fix spec name --- spec/controllers/admin/instrument_controller_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/controllers/admin/instrument_controller_spec.rb b/spec/controllers/admin/instrument_controller_spec.rb index e87b2336d..06d57ccac 100644 --- a/spec/controllers/admin/instrument_controller_spec.rb +++ b/spec/controllers/admin/instrument_controller_spec.rb @@ -7,7 +7,7 @@ describe 'DELETE destroy' do let!(:instrument) { create(:instrument, discarded_at: nil) } - context 'with valid pspec/factories/instrument_types.rb:16arams' do + context 'with valid params' do subject { delete :destroy, params: {id: instrument.id} } before do