Skip to content

Commit

Permalink
Allow legislation to be specified when bulk uploading
Browse files Browse the repository at this point in the history
This change means that an additional, optional 3rd column is permitted
in the bulk upload CSV 'legislation'. If the value in this field
exactly matches the `name` of an existing Legislation the uploaded
statement will be associated with that legislation.
  • Loading branch information
chrislo committed Jun 25, 2018
1 parent 5d3abe0 commit 638d1b0
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
4 changes: 2 additions & 2 deletions app/controllers/admin/bulk_uploads_controller.rb
Expand Up @@ -22,9 +22,9 @@ def alert_csv
end

def bulk_create(statement_params_array)
statement_params_array.each do |statement_params|
statement_params_array.each do |params|
begin
Statement.bulk_create!(statement_params['company_name'], statement_params['statement_url'])
Statement.bulk_create!(params['company_name'], params['statement_url'], params['legislation'])
rescue StandardError => e
logger.error(e)
end
Expand Down
6 changes: 4 additions & 2 deletions app/models/statement.rb
Expand Up @@ -40,12 +40,14 @@ def self.url_exists?(url)
exists?(url: uri.to_s)
end

def self.bulk_create!(company_name, statement_url)
def self.bulk_create!(company_name, statement_url, legislation_name)
return if Statement.url_exists?(statement_url)

begin
company = Company.find_or_create_by!(name: company_name)
company.statements.create!(url: statement_url)
legislation = Legislation.find_by(name: legislation_name)
statement = company.statements.create!(url: statement_url)
statement.legislations << legislation
rescue ActiveRecord::RecordInvalid => e
e.message += "\nCompany Name: '#{company_name}', Statement URL: '#{statement_url}'"
raise e
Expand Down
17 changes: 17 additions & 0 deletions features/bulk_upload.feature
Expand Up @@ -14,6 +14,23 @@ Feature: Bulk upload
| Approved by board | Unspecified |
| Link on front page | No |

Scenario: Administrator bulk uploads statements with legislation
Given the following legislations exist:
| Name | Include in compliance stats? |
| UK Modern Slavery Act | Yes |
| California Transparency in Supply Chains Act | No |
Given Patricia is logged in
When Patricia uploads a CSV with the following statements:
| company_name | statement_url | legislation |
| Cucumber Ltd | https://cucumber.io/anti-slavery-statement | UK Modern Slavery Act |
| BigCorp | https://bigcorp.com/anti-slavery-statement | California Transparency in Supply Chains Act |
Then Patricia should see 1 statement for "Cucumber Ltd" with:
| Statement URL | https://cucumber.io/anti-slavery-statement |
| Signed by director | No |
| Approved by board | Unspecified |
| Link on front page | No |
| Legislations | UK Modern Slavery Act |

Scenario: Administrator bulk uploads statements with some invalid URLs
Given Patricia is logged in
When Patricia uploads a CSV with the following statements:
Expand Down

0 comments on commit 638d1b0

Please sign in to comment.