Skip to content

Commit

Permalink
Merge pull request #372 from Vizzuality/csv-upload-instructions
Browse files Browse the repository at this point in the history
CSV upload instructions
  • Loading branch information
tsubik committed Mar 31, 2021
2 parents beb8dc6 + c63c751 commit b6008d2
Show file tree
Hide file tree
Showing 67 changed files with 2,714 additions and 376 deletions.
3 changes: 1 addition & 2 deletions app/admin/cp_assessments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,11 @@
year_columns = collection.flat_map(&:emissions_all_years).uniq.sort

column :id
column('Company id') { |a| a.company.id }
column('Company Id') { |a| a.company.id }
column(:company) { |a| a.company.name }
column :assessment_date
column :publication_date, &:publication_date_csv
column :last_reported_year

year_columns.map do |year|
column year do |a|
a.emissions[year]
Expand Down
13 changes: 12 additions & 1 deletion app/admin/data_uploads.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

actions :index, :show, :new, :create

collection_action :instruction, method: :get do
render :instruction, layout: false
end

permit_params :uploader, :file

filter :created_at, label: 'Uploaded at'
Expand Down Expand Up @@ -39,7 +43,7 @@
active_admin_comments
end

form html: {'data-controller' => 'check-modified'} do |f|
form html: {'data-controller' => 'check-modified upload-instructions'} do |f|
f.semantic_errors(*f.object.errors.keys)

div class: 'padding-20' do
Expand All @@ -48,9 +52,16 @@
end

f.inputs do
div do
link_to 'Show upload instructions', '',
class: 'padding-20',
'data-target': 'upload-instructions.showLink',
'data-action': 'click->upload-instructions#showInstructions'
end
f.input :uploader,
as: :select,
hint: 'Choose data model',
input_html: {'data-target': 'upload-instructions.uploaderSelect'},
collection: DataUpload::UPLOADERS
f.input :file,
as: :file,
Expand Down
3 changes: 2 additions & 1 deletion app/admin/events.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@
csv do
column :id
column :eventable_type
column :eventable_id
column 'Eventable Id', humanize_name: false, &:eventable_id
column :eventable_name
column :event_type
column :title
column :description
Expand Down
2 changes: 1 addition & 1 deletion app/admin/geographies.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
column :legislative_process
column :federal
column :federal_details
column :political_groups, &:political_groups_string
column :political_groups, &:political_groups_csv
column :percent_global_emissions
column :climate_risk_index
column :wb_income_group
Expand Down
12 changes: 6 additions & 6 deletions app/admin/legislations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,14 @@
column(:geography) { |l| l.geography.name }
column(:geography_iso) { |l| l.geography.iso }
column(:sectors) { |l| l.laws_sectors.map(&:name).join(Rails.application.config.csv_options[:entity_sep]) }
column :frameworks, &:frameworks_string
column :responses, &:responses_string
column :keywords, &:keywords_string
column :natural_hazards, &:natural_hazards_string
column :document_types, &:document_types_string
column :frameworks, &:frameworks_csv
column :responses, &:responses_csv
column :keywords, &:keywords_csv
column :natural_hazards, &:natural_hazards_csv
column :document_types, &:document_types_csv
column :instruments, &:instruments_csv
column :governances, &:governances_csv
column(:connected_litigation_ids) { |l| l.litigation_ids.join(Rails.application.config.csv_options[:entity_sep]) }
column(:litigation_ids) { |l| l.litigation_ids.join(Rails.application.config.csv_options[:entity_sep]) }
column :visibility_status
end

Expand Down
8 changes: 5 additions & 3 deletions app/admin/litigation_sides.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@

csv do
column :id
column 'Litigation ID', &:litigation_id
column 'Litigation Id', &:litigation_id
column('Litigation Title') { |l| l.litigation.title }
column :connected_entity_type
column 'Connected entity ID', &:connected_entity_id
column 'Connected entity id', &:connected_entity_id
column('Connected entity name') { |l| l.connected_entity.name if l.connected_entity.present? }
column :name
column :side_type
column :party_type
Expand All @@ -21,7 +23,7 @@ def index
end

def scoped_collection
results = super.includes(:connected_entity)
results = super.includes(:connected_entity, :litigation)

return results unless litigation_params

Expand Down
8 changes: 4 additions & 4 deletions app/admin/litigations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@
column :jurisdiction
column :citation_reference_number
column :summary
column(:sector) { |l| l.laws_sectors.map(&:name).join(Rails.application.config.csv_options[:entity_sep]) }
column :responses, &:responses_string
column :keywords, &:keywords_string
column(:sectors) { |l| l.laws_sectors.map(&:name).join(Rails.application.config.csv_options[:entity_sep]) }
column :responses, &:responses_csv
column :keywords, &:keywords_csv
column :at_issue
column(:visibility_status) { |l| l.visibility_status&.humanize }
column :visibility_status
column(:legislation_ids) { |l| l.legislation_ids.join(Rails.application.config.csv_options[:entity_sep]) }
end

Expand Down
1 change: 1 addition & 0 deletions app/admin/tags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
NaturalHazard
PoliticalGroup
Response
Scope
].each do |tag_class|
ActiveAdmin.register tag_class.constantize do
config.sort_order = 'name_asc'
Expand Down
4 changes: 2 additions & 2 deletions app/admin/targets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,16 @@
csv do
column :id
column(:target_type) { |t| t.model.target_type }
column :source
column :description
column :ghg_target
column :year
column :base_year_period
column :single_year
column :source
column(:geography) { |t| t.geography.name }
column(:geography_iso) { |t| t.geography.iso }
column(:sector) { |t| t.sector&.name }
column(:connected_law_ids) { |t| t.legislation_ids.join(Rails.application.config.csv_options[:entity_sep]) }
column(:legislation_ids) { |t| t.legislation_ids.join(Rails.application.config.csv_options[:entity_sep]) }
column :scopes, &:scopes_string
column :visibility_status
end
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 31 additions & 0 deletions app/assets/stylesheets/admin/models/data_uploads.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
.dialog-no-title {
.ui-dialog-title {
display: none;
}
}

#instruction_modal {
display: block;
padding: 20px;

line-height: 1.5rem;

h2 {
font-size: 1.25rem;
font-weight: bold;
margin-bottom: 15px;
margin-top: 15px;
}

h3 {
font-size: 1.15rem;
font-weight: bold;
margin-bottom: 15px;
margin-top: 15px;
}

.well {
padding: 20px;
background: #eeeeee;
}
}
4 changes: 4 additions & 0 deletions app/decorators/event_decorator.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
class EventDecorator < Draper::Decorator
delegate_all

def eventable_name
eventable.try(:title) || eventable.try(:name) || eventable.to_s
end

def title_link
h.link_to model.title, h.admin_event_path(model)
end
Expand Down
44 changes: 44 additions & 0 deletions app/javascript/admin/controllers/upload_instructions_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { Controller } from 'stimulus';

export default class extends Controller {
static targets = ['uploaderSelect', 'showLink']

showInstructions(e) {
e.preventDefault();
const uploader = this.uploaderSelectTarget.value;
fetch(`/admin/data_uploads/instruction?uploader=${uploader}`)
.then((res) => {
if (!res.ok) throw new Error('error');
return res.text();
}).then((html) => {
let instructionModal = document.querySelector('#instruction_modal');
if (!instructionModal) {
instructionModal = document.createElement('div');
instructionModal.id = 'instruction_modal';
document.body.appendChild(instructionModal);
}
instructionModal.innerHTML = html;
$(instructionModal).dialog({
dialogClass: 'dialog-no-title',
width: window.innerWidth * 0.9,
modal: true,
buttons: [
{
text: 'Close',
click() {
$(this).dialog('close');
}
}
],
open() {
$('.ui-widget-overlay').bind('click', () => {
$(this).dialog('close');
});
}
});
})
.catch(() => {
alert('Error occured while showing instructions'); // eslint-disable-line
});
}
}
18 changes: 18 additions & 0 deletions app/models/concerns/dirty_associations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module DirtyAssociations
attr_accessor :dirty_associations

def mark_changed(_object)
self.dirty_associations = true
end

alias dirty_associations? dirty_associations

def changed?
dirty_associations? || super
end

def reload
self.dirty_associations = false
super
end
end
12 changes: 11 additions & 1 deletion app/models/concerns/taggable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@ module Taggable
def tag_with(name, **attrs)
singular_name = name.to_s.singularize
class_name = attrs[:class_name] || singular_name.camelize
has_many name, -> { order(:id) }, through: :taggings, source: :tag, class_name: class_name

has_many_attrs = {}
has_many_attrs[:class_name] = class_name
has_many_attrs[:after_add] = :mark_changed if instance_methods.include?(:mark_changed)
has_many_attrs[:after_remove] = :mark_changed if instance_methods.include?(:mark_changed)

has_many name, -> { order(:id) }, through: :taggings, source: :tag, **has_many_attrs

define_method("#{singular_name}_ids=") do |value|
super(value&.uniq)
Expand Down Expand Up @@ -61,6 +67,10 @@ def tag_with(name, **attrs)
send("#{name}_list")&.join(', ')
end

define_method("#{name}_csv") do
send("#{name}_list")&.join(Rails.application.config.csv_options[:entity_sep])
end

alias_method "#{name}_string=", "#{name}_list="
end

Expand Down
31 changes: 16 additions & 15 deletions app/models/data_upload.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,26 @@
class DataUpload < ApplicationRecord
has_one_attached :file

DEV_UPLOADERS = Rails.env.development? || Rails.env.test? ? %w[Documents] : []
UPLOADERS = (%w[
CPAssessments
CPBenchmarks
Companies
Events
ExternalLegislations
Geographies
Legislations
LitigationSides
Litigations
MQAssessments
Targets
] + DEV_UPLOADERS).freeze
DEV_UPLOADERS = %w[Documents].freeze
UPLOADERS = {
'Carbon Performance Assessments' => 'CPAssessments',
'Carbon Performance Benchmarks' => 'CPBenchmarks',
'Companies' => 'Companies',
'Documents' => 'Documents',
'Events' => 'Events',
'External Legislations' => 'ExternalLegislations',
'Geographies' => 'Geographies',
'Legislations' => 'Legislations',
'Litigation Sides' => 'LitigationSides',
'Litigations' => 'Litigations',
'Management Quality Assessments' => 'MQAssessments',
'Targets' => 'Targets'
}.reject { |k| !(Rails.env.test? || Rails.env.development?) && DEV_UPLOADERS.include?(k) }.freeze

belongs_to :uploaded_by, class_name: 'AdminUser', optional: true

validates :file, attached: true, content_type: {in: ['text/csv', 'application/vnd.ms-excel']}
validates :uploader, inclusion: {in: UPLOADERS}
validates :uploader, inclusion: {in: UPLOADERS.values}

before_create :set_uploaded_by

Expand Down
4 changes: 3 additions & 1 deletion app/models/external_legislation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
#

class ExternalLegislation < ApplicationRecord
has_and_belongs_to_many :litigations
include DirtyAssociations

has_and_belongs_to_many :litigations, after_add: :mark_changed, after_remove: :mark_changed
belongs_to :geography

validates_presence_of :name
Expand Down
11 changes: 6 additions & 5 deletions app/models/legislation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#

class Legislation < ApplicationRecord
include DirtyAssociations
include Eventable
include UserTrackable
include Taggable
Expand Down Expand Up @@ -51,11 +52,11 @@ class Legislation < ApplicationRecord
belongs_to :geography
belongs_to :parent, class_name: 'Legislation', foreign_key: 'parent_id', optional: true
has_many :documents, as: :documentable, dependent: :destroy
has_and_belongs_to_many :targets
has_and_belongs_to_many :litigations
has_and_belongs_to_many :instruments
has_and_belongs_to_many :governances
has_and_belongs_to_many :laws_sectors
has_and_belongs_to_many :targets, after_add: :mark_changed, after_remove: :mark_changed
has_and_belongs_to_many :litigations, after_add: :mark_changed, after_remove: :mark_changed
has_and_belongs_to_many :instruments, after_add: :mark_changed, after_remove: :mark_changed
has_and_belongs_to_many :governances, after_add: :mark_changed, after_remove: :mark_changed
has_and_belongs_to_many :laws_sectors, after_add: :mark_changed, after_remove: :mark_changed

scope :laws, -> { legislative }
scope :policies, -> { executive }
Expand Down
7 changes: 4 additions & 3 deletions app/models/litigation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#

class Litigation < ApplicationRecord
include DirtyAssociations
include Eventable
include UserTrackable
include Taggable
Expand Down Expand Up @@ -67,11 +68,11 @@ class Litigation < ApplicationRecord
tag_with :responses

belongs_to :geography
has_and_belongs_to_many :laws_sectors
has_and_belongs_to_many :laws_sectors, after_add: :mark_changed, after_remove: :mark_changed
has_many :litigation_sides, -> { order(:side_type) }, inverse_of: :litigation
has_many :documents, as: :documentable, dependent: :destroy
has_and_belongs_to_many :legislations
has_and_belongs_to_many :external_legislations
has_and_belongs_to_many :legislations, after_add: :mark_changed, after_remove: :mark_changed
has_and_belongs_to_many :external_legislations, after_add: :mark_changed, after_remove: :mark_changed

with_options allow_destroy: true, reject_if: :all_blank do
accepts_nested_attributes_for :documents
Expand Down
Loading

0 comments on commit b6008d2

Please sign in to comment.