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

GovernanceType and Governance in legislation #76

Merged
merged 2 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions app/admin/governance.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
ActiveAdmin.register Governance do
menu parent: 'Laws', priority: 8

permit_params :name, :governance_type_id

config.batch_actions = false

decorate_with GovernanceDecorator

filter :name_contains, label: 'Name'
filter :governance_type

index do
column :name, :name_link
column :governance_type

actions
end

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

f.actions
end

controller do
include DiscardableController
end
end
38 changes: 38 additions & 0 deletions app/admin/governance_types.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
ActiveAdmin.register GovernanceType do
menu parent: 'Laws', priority: 9
config.batch_actions = false

permit_params :name

decorate_with GovernanceTypeDecorator

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
4 changes: 3 additions & 1 deletion app/admin/legislations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
: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: [], instrument_ids: []
framework_ids: [], document_type_ids: [], instrument_ids: [],
governance_ids: []

filter :title_contains, label: 'Title'
filter :date_passed
Expand Down Expand Up @@ -63,6 +64,7 @@
row 'Natural Hazards', &:natural_hazards_string
list_row 'Documents', :document_links
list_row 'Instruments', :instrument_links
list_row 'Governances', :governance_links
end
end

Expand Down
19 changes: 19 additions & 0 deletions app/commands/command/destroy/governance.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module Command
module Destroy
class Governance
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/governance_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module Command
module Destroy
class GovernanceType
def initialize(resource)
@resource = resource
end

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

r.governances = []
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 @@ -16,6 +16,7 @@ def call
r.litigations = []
r.targets = []
r.instruments = []
r.governances = []
end
end
end
Expand Down
7 changes: 7 additions & 0 deletions app/decorators/governance_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class GovernanceDecorator < Draper::Decorator
delegate_all

def name_link
h.link_to model.name, h.admin_governance_path(model)
end
end
7 changes: 7 additions & 0 deletions app/decorators/governance_type_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class GovernanceTypeDecorator < Draper::Decorator
delegate_all

def name_link
h.link_to model.name, h.admin_governance_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 @@ -50,4 +50,13 @@ def instrument_links
title: instrument.name
end
end

def governance_links
model.governances.map do |governance|
h.link_to governance.name,
h.admin_governance_path(governance),
target: '_blank',
title: governance.name
end
end
end
20 changes: 20 additions & 0 deletions app/models/governance.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# == Schema Information
#
# Table name: governances
#
# id :bigint not null, primary key
# name :string
# governance_type_id :bigint
# created_at :datetime not null
# updated_at :datetime not null
# discarded_at :datetime
#

class Governance < ApplicationRecord
include DiscardableModel

belongs_to :governance_type
has_and_belongs_to_many :legislations

validates_presence_of :name
end
18 changes: 18 additions & 0 deletions app/models/governance_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# == Schema Information
#
# Table name: governance_types
#
# id :bigint not null, primary key
# name :string
# created_at :datetime not null
# updated_at :datetime not null
# discarded_at :datetime
#

class GovernanceType < ApplicationRecord
include DiscardableModel

validates_presence_of :name

has_many :governances
end
1 change: 1 addition & 0 deletions app/models/legislation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class Legislation < ApplicationRecord
has_and_belongs_to_many :targets
has_and_belongs_to_many :litigations
has_and_belongs_to_many :instruments
has_and_belongs_to_many :governances

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 @@ -17,6 +17,7 @@
<%= 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 %>
<%= f.input :governance_ids, label: 'Governance', as: :tags, collection: Governance.all %>
<%=
Arbre::Context.new do
columns do
Expand Down
9 changes: 9 additions & 0 deletions db/migrate/20190930142601_create_governance_types.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CreateGovernanceTypes < ActiveRecord::Migration[5.2]
def change
create_table :governance_types do |t|
t.string :name

t.timestamps
end
end
end
10 changes: 10 additions & 0 deletions db/migrate/20190930143005_create_governances.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateGovernances < ActiveRecord::Migration[5.2]
def change
create_table :governances do |t|
t.string :name
t.references :governance_type, foreign_key: true

t.timestamps
end
end
end
6 changes: 6 additions & 0 deletions db/migrate/20190930143127_add_discarded_at_to_governance.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddDiscardedAtToGovernance < ActiveRecord::Migration[5.2]
def change
add_column :governances, :discarded_at, :datetime
add_index :governances, :discarded_at
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddDiscardedAtToGovernanceType < ActiveRecord::Migration[5.2]
def change
add_column :governance_types, :discarded_at, :datetime
add_index :governance_types, :discarded_at
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class CreateJoinTableLegislationGovernance < ActiveRecord::Migration[5.2]
def change
create_join_table :legislations, :governances do |t|
t.index :legislation_id
t.index :governance_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_29_193356) do
ActiveRecord::Schema.define(version: 2019_09_30_143410) 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 "governance_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_governance_types_on_discarded_at"
end

create_table "governances", force: :cascade do |t|
t.string "name"
t.bigint "governance_type_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "discarded_at"
t.index ["discarded_at"], name: "index_governances_on_discarded_at"
t.index ["governance_type_id"], name: "index_governances_on_governance_type_id"
end

create_table "governances_legislations", id: false, force: :cascade do |t|
t.bigint "legislation_id", null: false
t.bigint "governance_id", null: false
t.index ["governance_id"], name: "index_governances_legislations_on_governance_id"
t.index ["legislation_id"], name: "index_governances_legislations_on_legislation_id"
end

create_table "instrument_types", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
Expand Down Expand Up @@ -370,6 +395,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 "governances", "governance_types"
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"
Expand Down
42 changes: 42 additions & 0 deletions spec/controllers/admin/governance_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require 'rails_helper'

RSpec.describe Admin::GovernancesController, type: :controller do
let(:admin) { create(:admin_user) }
before { sign_in admin }

describe 'DELETE destroy' do
let!(:governance) { create(:governance, discarded_at: nil) }

context 'with valid params' do
subject { delete :destroy, params: {id: governance.id} }

before do
expect { subject }.to change { Governance.count }.by(-1)
end

it 'set discarded_at date to governance object' do
expect(governance.reload.discarded_at).to_not be_nil
end

it 'shows proper notice' do
expect(flash[:notice]).to match('Successfully deleted selected Governance')
end
end

context 'with invalid params' do
let(:command) { double }

subject { delete :destroy, params: {id: governance.id} }

before do
expect(::Command::Destroy::Governance).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_governances_path)
expect(flash[:alert]).to match('Could not delete selected Governance')
end
end
end
end
Loading