-
Notifications
You must be signed in to change notification settings - Fork 3
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): bulk upload for Targets #56
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
057e79a
index page styling
kowal f227cf6
bulk data upload for Targets
kowal c32de16
update controller tests
kowal a77aa87
remove puts
kowal 4546694
styling
kowal b9c43a4
better error messages for CSV imports
kowal a2044c6
revemo id mapping
kowal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
module CSVImport | ||
class Targets < BaseImporter | ||
include UploaderHelpers | ||
|
||
def import | ||
import_each_csv_row(csv) do |row| | ||
target = prepare_target(row) | ||
|
||
target.assign_attributes(target_attributes(row)) | ||
|
||
was_new_record = target.new_record? | ||
any_changes = target.changed? | ||
|
||
target.save! | ||
|
||
update_import_results(was_new_record, any_changes) | ||
end | ||
end | ||
|
||
private | ||
|
||
def resource_klass | ||
Target | ||
end | ||
|
||
def prepare_target(row) | ||
find_record_by(:id, row) || resource_klass.new | ||
end | ||
|
||
# Builds resource attributes from a CSV row. | ||
# | ||
# If not present in DB, will create related: | ||
# - sector | ||
# | ||
def target_attributes(row) | ||
{ | ||
target_type: row[:target_type], | ||
description: row[:description], | ||
ghg_target: row[:ghg_target], | ||
year: row[:year], | ||
base_year_period: row[:base_year_period], | ||
single_year: row[:single_year], | ||
geography: geographies[row[:geography_iso]], | ||
sector: find_or_create_sector(row[:sector]), | ||
target_scope: find_target_scope(row[:target_scope]), | ||
visibility_status: row[:visibility_status] | ||
} | ||
end | ||
|
||
def find_or_create_sector(sector_name) | ||
return unless sector_name.present? | ||
|
||
Sector.where('lower(name) = ?', sector_name.downcase).first || | ||
Sector.new(name: sector_name) | ||
end | ||
|
||
def find_target_scope(target_scope_name) | ||
return unless target_scope_name.present? | ||
|
||
TargetScope.where('lower(name) = ?', target_scope_name.downcase).first | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,6 @@ | |
ghg_target { false } | ||
single_year { true } | ||
target_type { 'base_year_target' } | ||
visibility_status { Litigation::VISIBILITY.sample } | ||
visibility_status { Litigation::VISIBILITY.first } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's remove this randomness from specs ;) |
||
end | ||
end |
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
"Id","Target type","Description","Ghg target","Year","Base year period","Single year","Geography","Geography iso","Sector","Target scope","Visibility status" | ||
"201","no_document_submitted","description","true","1995","1998","false","Japan","JPN","Airlines","High","draft" | ||
"202","base_year_target","description","true","1994","1994","false","Poland","POL","Cement","Default Scope","published" | ||
"203","intensity_target_and_trajectory_target","description","false","2003","2001","true","Poland","POL","Electricity Utilities","Default Scope","pending" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as @tsubik suggested some time ago, I removed
_simple
suffix as it doesn't bring much value.