Skip to content

Commit

Permalink
Merge pull request #39 from Vizzuality/feature/external-legislation-a…
Browse files Browse the repository at this point in the history
…dd-to-litigations

connect litigations with external legislations
  • Loading branch information
tsubik authored Aug 26, 2019
2 parents 6a1def1 + e81059c commit 17c24a2
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 9 deletions.
4 changes: 3 additions & 1 deletion app/admin/litigations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
events_attributes: [
:id, :_destroy, :title, :event_type, :description, :url, :date
],
legislation_ids: []
legislation_ids: [],
external_legislation_ids: []

filter :title_contains
filter :summary_contains
Expand Down Expand Up @@ -81,6 +82,7 @@
row :created_by
list_row 'Documents', :document_links
list_row 'Legislations', :legislation_links
list_row 'External Legislations', :external_legislation_links
end
end

Expand Down
10 changes: 10 additions & 0 deletions app/decorators/litigation_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,14 @@ def legislation_links
title: legislation.title
end
end

def external_legislation_links
return [] if model.external_legislations.empty?

model.external_legislations.map do |legislation|
h.link_to legislation.name,
h.admin_external_legislation_path(legislation),
target: '_blank'
end
end
end
1 change: 1 addition & 0 deletions app/models/external_legislation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#

class ExternalLegislation < ApplicationRecord
has_and_belongs_to_many :litigations
belongs_to :geography

validates_presence_of :name
Expand Down
1 change: 1 addition & 0 deletions app/models/litigation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class Litigation < ApplicationRecord
has_many :documents, as: :documentable, dependent: :destroy
has_many :events, as: :eventable, dependent: :destroy
has_and_belongs_to_many :legislations
has_and_belongs_to_many :external_legislations

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/litigations/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<%= f.input :summary, as: :trix %>
<%= f.input :core_objective, as: :trix %>
<%= f.input :legislation_ids, as: :selected_list, label: 'Connected Legislations', fields: [:title], display_name: :title, order: 'title_asc' %>
<%= f.input :external_legislation_ids, as: :selected_list, label: 'Connected External Legislations', fields: [:name], display_name: :name, order: 'name_asc' %>
<%= f.input :visibility_status, as: :select %>
<%= f.input :keywords_string, as: :tags, label: 'Keywords', hint: t('hint.tag'), collection: Keyword.all.pluck(:name) %>
<% end %>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class CreateJoinTableLitigationExternalLegislation < ActiveRecord::Migration[5.2]
def change
create_join_table :litigations, :external_legislations do |t|
t.index :litigation_id
t.index :external_legislation_id, name: 'index_external_legislations_litigations', unique: true
end
end
end
9 changes: 8 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_08_22_112921) do
ActiveRecord::Schema.define(version: 2019_08_23_131259) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -144,6 +144,13 @@
t.index ["geography_id"], name: "index_external_legislations_on_geography_id"
end

create_table "external_legislations_litigations", id: false, force: :cascade do |t|
t.bigint "litigation_id", null: false
t.bigint "external_legislation_id", null: false
t.index ["external_legislation_id"], name: "index_external_legislations_litigations", unique: true
t.index ["litigation_id"], name: "index_external_legislations_litigations_on_litigation_id"
end

create_table "geographies", force: :cascade do |t|
t.string "geography_type", null: false
t.string "iso", null: false
Expand Down
21 changes: 14 additions & 7 deletions spec/controllers/admin/litigations_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@
it 'creates a new Litigation' do
expect { subject }.to change(Litigation, :count).by(1)

expected_documents_attrs = [
['doc 1', 'en', 'https://test.com'], ['doc 2', 'en', '']
]
expected_events_attrs = [
['Event 1', 'case_started', 'Description 1', 'https://validurl1.com'],
['Event 2', 'case_dismissed', 'Description 2', 'https://validurl2.com']
]

last_litigation_created.tap do |l|
expect(l.title).to eq('Litigation POST title')
expect(l.summary).to eq('Litigation POST summary')
Expand All @@ -95,13 +103,12 @@
expect(l.geography_id).to eq(geography.id)
expect(l.jurisdiction_id).to eq(geography.id)
expect(l.litigation_sides.pluck(:party_type)).to eq(%w[individual corporation government])
expect(l.documents.pluck(:name, :language, :external_url).sort)
.to eq([['doc 1', 'en', 'https://test.com'], ['doc 2', 'en', '']])
expect(l.events.order(:date).pluck(:title, :event_type, :description, :url))
.to eq([
['Event 1', 'case_started', 'Description 1', 'https://validurl1.com'],
['Event 2', 'case_dismissed', 'Description 2', 'https://validurl2.com']
])
expect(
l.documents.pluck(:name, :language, :external_url).sort
).to eq(expected_documents_attrs)
expect(
l.events.order(:date).pluck(:title, :event_type, :description, :url)
).to eq(expected_events_attrs)
end
end

Expand Down

0 comments on commit 17c24a2

Please sign in to comment.