Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/instrument type and instrument #74

Merged
merged 8 commits into from
Sep 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 43 additions & 0 deletions app/admin/instrument.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
ActiveAdmin.register Instrument do
menu parent: 'Laws', priority: 6

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

actions
end

show do
attributes_table do
row :name
row :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
end

f.actions
end

controller do
include DiscardableController
end
end
38 changes: 38 additions & 0 deletions app/admin/instrument_types.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
ActiveAdmin.register InstrumentType do
menu parent: 'Laws', priority: 7
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

controller do
include DiscardableController
end
end
3 changes: 2 additions & 1 deletion app/admin/legislations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
19 changes: 19 additions & 0 deletions app/commands/command/destroy/instrument.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module Command
module Destroy
class Instrument
def initialize(resource)
@resource = resource
end

def call
ActiveRecord::Base.transaction do
@resource.tap do |r|
r.discard

r.legislations = []
end
end
end
end
end
end
19 changes: 19 additions & 0 deletions app/commands/command/destroy/instrument_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module Command
module Destroy
class InstrumentType
def initialize(resource)
@resource = resource
end

def call
ActiveRecord::Base.transaction do
@resource.tap do |r|
r.discard

r.instruments = []
end
end
end
end
end
end
1 change: 1 addition & 0 deletions app/commands/command/destroy/legislation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def call

r.litigations = []
r.targets = []
r.instruments = []
end
end
end
Expand Down
7 changes: 7 additions & 0 deletions app/decorators/instrument_decorator.rb
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions app/decorators/instrument_type_decorator.rb
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions app/decorators/legislation_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,13 @@ def document_links
title: document.external? ? document.url : nil
end
end

def instrument_links
model.instruments.map do |instrument|
h.link_to instrument.name,
h.admin_instrument_path(instrument),
target: '_blank',
title: instrument.name
end
end
end
20 changes: 20 additions & 0 deletions app/models/instrument.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# == 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
has_and_belongs_to_many :legislations

validates_presence_of :name
end
18 changes: 18 additions & 0 deletions app/models/instrument_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# == Schema Information
#
# Table name: instrument_types
#
# 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
1 change: 1 addition & 0 deletions app/models/legislation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions app/views/admin/legislations/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions db/migrate/20190928120638_create_instrument_types.rb
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions db/migrate/20190929104944_create_instruments.rb
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions db/migrate/20190929110158_add_discarded_at_to_instrument.rb
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
28 changes: 27 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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_29_193356) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -187,6 +187,31 @@
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
t.datetime "discarded_at"
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 "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"
Expand Down Expand Up @@ -345,6 +370,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"
Expand Down
42 changes: 42 additions & 0 deletions spec/controllers/admin/instrument_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -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 params' 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