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/litigation sides edit #9

Merged
merged 15 commits into from
Jul 26, 2019
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ group :development, :test do
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
gem 'capybara'
gem 'factory_bot_rails'
gem 'rails-controller-testing'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this used anywhere? I can't see any assigns or assert_template.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, I've been debugging failed test and I needed assigns. Maybe it can stay for debugging.

gem 'rspec-collection_matchers'
gem 'rspec-rails', '~> 3.8'
end
Expand Down
5 changes: 5 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,10 @@ GEM
bundler (>= 1.3.0)
railties (= 5.2.3)
sprockets-rails (>= 2.0.0)
rails-controller-testing (1.0.4)
actionpack (>= 5.0.1.x)
actionview (>= 5.0.1.x)
activesupport (>= 5.0.1.x)
rails-dom-testing (2.0.3)
activesupport (>= 4.2.0)
nokogiri (>= 1.6)
Expand Down Expand Up @@ -390,6 +394,7 @@ DEPENDENCIES
pg (>= 0.18, < 2.0)
puma (~> 3.11)
rails (~> 5.2.3)
rails-controller-testing
rspec-collection_matchers
rspec-rails (~> 3.8)
rubocop
Expand Down
2 changes: 1 addition & 1 deletion app/admin/admin_users.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
ActiveAdmin.register AdminUser do
menu priority: 2
menu priority: 2, parent: 'Administration'

permit_params :email, :password, :password_confirmation

Expand Down
16 changes: 15 additions & 1 deletion app/admin/companies.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
ActiveAdmin.register Company do
menu priority: 4
menu priority: 0, parent: 'TPI'

permit_params :name, :isin, :sector_id, :location_id, :headquarter_location_id, :ca100,
:size
Expand Down Expand Up @@ -94,6 +94,20 @@
end
end
end

tab :litigations do
panel 'Connected Litigations' do
if resource.litigations.empty?
div class: 'padding-20' do
'No Litigations connected with this company'
end
else
table_for resource.litigations.decorate do
column :title, &:title_link
end
end
end
end
end
end

Expand Down
2 changes: 1 addition & 1 deletion app/admin/cp_assessments.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
ActiveAdmin.register CP::Assessment do
config.sort_order = 'publication_date_desc'

menu priority: 6, parent: 'Assessments', label: 'Carbon Performance Assessments'
menu parent: 'TPI', priority: 2, label: 'Carbon Performance Assessments'

decorate_with CP::AssessmentDecorator

Expand Down
2 changes: 1 addition & 1 deletion app/admin/dashboard.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
ActiveAdmin.register_page 'Dashboard' do
menu priority: 1, label: 'Dashboard'
menu priority: 0, label: 'Dashboard'

content do
div class: 'blank_slate_container' do
Expand Down
2 changes: 1 addition & 1 deletion app/admin/documents.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
ActiveAdmin.register Document do
menu priority: 3
menu priority: 6

decorate_with DocumentDecorator

Expand Down
2 changes: 2 additions & 0 deletions app/admin/legislations.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
ActiveAdmin.register Legislation do
decorate_with LegislationDecorator

menu parent: 'Laws', priority: 1

permit_params :title, :description, :framework, :location_id, :law_id

filter :title_contains, label: 'Title'
Expand Down
10 changes: 7 additions & 3 deletions app/admin/litigations.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
ActiveAdmin.register Litigation do
menu priority: 3
menu parent: 'Laws', priority: 2

decorate_with LitigationDecorator

permit_params :title, :location_id, :document_type, :summary, :core_objective,
permit_params :title, :location_id, :jurisdiction_id, :document_type, :summary, :core_objective,
litigation_sides_attributes: [
:id, :_destroy, :name, :side_type, :party_type, :connected_with
],
documents_attributes: [
:id, :_destroy, :name, :external_url, :type, :file
]
Expand Down Expand Up @@ -33,6 +36,7 @@
row :title
row :slug
row :location
row :jurisdiction
row :document_type
row :citation_reference_number
row :summary
Expand All @@ -45,7 +49,7 @@

tab :sides do
panel 'Litigation Sides' do
table_for resource.litigation_sides.order(:side_type).decorate do
table_for resource.litigation_sides.decorate do
column :side_type
column :name
column :party_type
Expand Down
2 changes: 1 addition & 1 deletion app/admin/locations.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
ActiveAdmin.register Location do
menu priority: 3
menu priority: 1

decorate_with LocationDecorator

Expand Down
2 changes: 1 addition & 1 deletion app/admin/mq_assessments.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
ActiveAdmin.register MQ::Assessment do
config.sort_order = 'publication_date_desc'

menu priority: 6, parent: 'Assessments', label: 'Management Quality Assessments'
menu parent: 'TPI', priority: 1, label: 'Management Quality Assessments'

decorate_with MQ::AssessmentDecorator

Expand Down
38 changes: 33 additions & 5 deletions app/assets/stylesheets/active_admin.scss
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@import 'admin/settings';
@import 'admin/mixins';
@import 'trix/dist/trix';
@import 'activeadmin_addons/all';

Expand Down Expand Up @@ -109,6 +110,13 @@ form {
list-style: disc;
color: $am-theme-error;
}

fieldset.inputs {
> legend {
font-size: 18px;
font-weight: bold;
}
}
}

// Comments
Expand Down Expand Up @@ -141,24 +149,22 @@ form {
}
}

// Documents

.document-list {
.nested-list {
&__actions {
padding: 16px;
}
}

.inline-fields {
display: flex;
justify-content: space-around;
justify-content: space-between;
align-items: center;

li {
margin: 0;

&.input:not(.error) {
input {
input, .select2, select {
margin-bottom: 20px;
}
}
Expand All @@ -168,3 +174,25 @@ form {
flex-grow: 1;
}
}

.litigation-sides-fields {
li[id*=side_type] {
min-width: 150px;
}

li[id*=party_type] {
min-width: 250px;
}

li[id*=connected_with] {
min-width: 300px;
}

li[id*=_name] {
flex-grow: 1;
}

@include desktop-large {
width: 70%;
}
}
17 changes: 17 additions & 0 deletions app/assets/stylesheets/admin/mixins.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
@mixin tablet {
@media (min-width: $breakpoint-tablet) {
@content;
}
}

@mixin desktop {
@media (min-width: $breakpoint-desktop) {
@content;
}
}

@mixin desktop-large {
@media (min-width: $breakpoint-desktop-large) {
@content;
}
}
4 changes: 4 additions & 0 deletions app/assets/stylesheets/admin/settings.scss
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ $admin-red: red;
$am-theme-primary: #00689f;
$am-theme-accent: #7dd2f7;
$am-theme-accent-fallback: $admin-red;

$breakpoint-tablet: 768px;
$breakpoint-desktop: 992px;
$breakpoint-desktop-large: 1400px;
13 changes: 13 additions & 0 deletions app/decorators/litigation_side_decorator.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
class LitigationSideDecorator < Draper::Decorator
delegate_all

def name
return h.link_to model.name, connected_entity_url if connected_entity.present?

model.name
end

def party_type
model.party_type&.humanize
end

def side_type
model.side_type&.humanize
end

private

def connected_entity_url
return h.admin_company_path(connected_entity) if connected_entity.is_a?(Company)
return h.admin_location_path(connected_entity) if connected_entity.is_a?(Location)
end
end
44 changes: 44 additions & 0 deletions app/javascript/controllers/litigation_side_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { Controller } from 'stimulus';

const ENTITY_TYPE_PARTY_TYPE_MAP = {
'Company': 'corporation',
'Location': 'government'
};

export default class extends Controller {
connect() {
$(this.connectedWithSelect).on('change', this._setDefaults.bind(this));
}

_setDefaults() {
if (this.connectedWithSelectedOption) {
const entityType = this.connectedWithSelectedOption.value.split('-')[0];
const partyType = ENTITY_TYPE_PARTY_TYPE_MAP[entityType];

if (partyType) {
$(this.partyTypeSelect).val(partyType);
$(this.partyTypeSelect).trigger('change');
}

this.nameInput.value = this.connectedWithSelectedOption.text;
}
}

get connectedWithSelect () {
return this.element.querySelector('select[id*="_connected_with"');
}

get connectedWithSelectedOption () {
return this.connectedWithSelect.selectedOptions &&
this.connectedWithSelect.selectedOptions.length &&
this.connectedWithSelect.selectedOptions[0];
}

get nameInput() {
return this.element.querySelector('input[id*="_name"');
}

get partyTypeSelect() {
return this.element.querySelector('select[id*="_party_type"');
}
}
4 changes: 4 additions & 0 deletions app/javascript/controllers/nested_list_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ export default class extends Controller {
.replace(/NEW_RECORD/g, new Date().getTime());

this.linksTarget.insertAdjacentHTML('beforebegin', content);

// very nasty trick, using dynamic list instead of AA has_many forms
// many plugins listen to this event to reinitialize, for example select2 from activeadmin addons
$(document).trigger('has_many_add:after');
}

removeRecord(event) {
Expand Down
24 changes: 24 additions & 0 deletions app/javascript/controllers/tabs_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Controller } from 'stimulus';

export default class extends Controller {
static targets = ['activeTab']

connect() {
$(this.element).tabs();

$(this.element.querySelectorAll('.nav-tabs a')).on('click', (event) => {
event.preventDefault();
this._changeActiveTabTo(event.target.hash);
});

const activeTab = this.activeTabTarget.value;
if (activeTab) {
$(this.element).find(`.nav-tabs a[href="${activeTab}"]`).click();
}
}

_changeActiveTabTo(hash) {
window.location.hash = hash;
this.activeTabTarget.value = hash;
}
}
6 changes: 6 additions & 0 deletions app/models/company.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class Company < ApplicationRecord

has_many :mq_assessments, class_name: 'MQ::Assessment', inverse_of: :company
has_many :cp_assessments, class_name: 'CP::Assessment', inverse_of: :company
has_many :litigation_sides, as: :connected_entity
has_many :litigations, through: :litigation_sides

delegate :level, :status, :status_description_short,
to: :latest_assessment, prefix: :mq, allow_nil: true
Expand All @@ -39,4 +41,8 @@ class Company < ApplicationRecord
def latest_assessment
mq_assessments.first
end

def to_s
name
end
end
4 changes: 3 additions & 1 deletion app/models/litigation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ class Litigation < ApplicationRecord
enum document_type: array_to_enum_hash(DOCUMENT_TYPES)

belongs_to :location
has_many :litigation_sides
belongs_to :jurisdiction, class_name: 'Location'
has_many :litigation_sides, -> { order(:side_type) }, inverse_of: :litigation
has_many :documents, as: :documentable, dependent: :destroy

accepts_nested_attributes_for :documents, allow_destroy: true
accepts_nested_attributes_for :litigation_sides, allow_destroy: true

validates_presence_of :title, :slug, :document_type
end
Loading