Skip to content
This repository has been archived by the owner on Mar 27, 2023. It is now read-only.

Commit

Permalink
Added Import and Export functionality for pension funds
Browse files Browse the repository at this point in the history
  • Loading branch information
shivashankar-ror committed Aug 6, 2019
1 parent 23ab3c4 commit 29f912c
Show file tree
Hide file tree
Showing 13 changed files with 328 additions and 7 deletions.
1 change: 1 addition & 0 deletions app/assets/javascripts/application.js
Expand Up @@ -4,3 +4,4 @@

//= require bootstrap-sprockets
//= require ./translations
//= require ./pension_funds
11 changes: 11 additions & 0 deletions app/assets/javascripts/pension_funds.js
@@ -0,0 +1,11 @@
$(document).ready(function(e) {
$(document).on('click', '.pf-export-btn', function(e) {
var country_code = $('#pf-country-code').val();
if (country_code != '') {
location.href = '/pension_funds/export?country_code=' + country_code;
} else {
alert('Select a country to continue ...');
return false;
}
});
});
24 changes: 24 additions & 0 deletions app/controllers/pension_funds_controller.rb
Expand Up @@ -20,6 +20,30 @@ def create
end
end

def export
if params[:country_code].present?
data = PensionFund.filter_by_country_code(params['country_code']).to_json(except: 'id')
send_data data, type: 'application/json; header=present',
disposition: "attachment; filename=pension-funds-#{params[:country_code]}.json"
else
redirect_to pension_funds_url
end
end

def upload
@pension_funds = []
render('upload') && return unless request.post?

uploaded_file = params[:json_file]
@json_importer = PensionFundsJsonImporter.new(uploaded_file, params[:country_code])

if @json_importer.import
redirect_to pension_funds_url, notice: t('pension_funds.upload.notice')
else
render :upload
end
end

def edit
# Intentionally left blank.
end
Expand Down
12 changes: 9 additions & 3 deletions app/models/pension_fund.rb
Expand Up @@ -23,13 +23,13 @@ class PensionFund < ApplicationRecord

validates :country_code, presence: true

validates :email, format: {
validates :email, format: {
with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i,
allow_blank: true
}

validates :fund, presence: true
validates :name, presence: true
validates :fund, presence: true
validates :name, presence: true

scope :sorted, -> { order('country_code, created_at') }
scope :sorted_by_created_at, -> { order('created_at') }
Expand All @@ -52,9 +52,15 @@ def self.list(params = {})
arel
end

def self.filter_by_country_code(country_code)
PensionFund.select('uuid, fund, name, email, country_code').where(country_code: country_code.to_s.strip)
end

private

def set_uuid
return uuid if uuid.present?

self.uuid = SecureRandom.uuid.delete('-')
end
end
34 changes: 34 additions & 0 deletions app/services/pension_funds_json_importer.rb
@@ -0,0 +1,34 @@
class PensionFundsJsonImporter
attr_reader :country_code, :file, :errors

def initialize(file, country_code)
@file = file
@country_code = country_code
@errors = []
end

def import
return false unless valid?

begin
funds = JSON.parse(File.read(file.tempfile))
funds.each do |f|
record = PensionFund.find_or_initialize_by(uuid: f['uuid'])
record.fund = f['fund']
record.name = f['name']
record.email = f['email']
record.country_code = country_code
@errors << record.errors.full_messages.to_sentence unless record.save
end
rescue StandardError => e
@errors << e.message
end
@errors.empty?
end

def valid?
@errors << 'Select a country' unless country_code.present?
@errors << 'Select a JSON file to continue' unless file.present?
@errors.empty?
end
end
10 changes: 7 additions & 3 deletions app/views/pension_funds/index.html.slim
Expand Up @@ -5,13 +5,17 @@
= form_with url: pension_funds_path, method: 'get' do |form|
.row
.col-md-3
= form.select :country_code, options_for_select(sorted_countries_list, params[:country_code]), {include_blank: 'All Countries'}, class: 'form-control'
= form.select :country_code, options_for_select(sorted_countries_list, params[:country_code]), {include_blank: 'All Countries'}, class: 'form-control', id: 'pf-country-code'
.col-md-5
= form.text_field :search_text, {value: params[:search_text], class: 'form-control', placeholder: 'Enter name or email or fund'}
.col-md-2
.col-md-1
= form.submit 'Search', class: 'btn btn-primary'
.col-md-1
= link_to 'Import', upload_pension_funds_path, class: 'btn btn-info'
.col-md-1
= link_to 'Export', '#', class: 'btn btn-info pf-export-btn'
br
.row
.row
.col-md-12

table.table-bordered.table-striped.table
Expand Down
26 changes: 26 additions & 0 deletions app/views/pension_funds/upload.html.slim
@@ -0,0 +1,26 @@
= render 'shared/sidebar', action: :index, resource: :pension_funds

.edit-block
h1.edit-block__title= t('pension_funds.upload.title')
= form_with url: upload_pension_funds_path, method: 'post' do |form|
.row
.col-md-3
= form.select :country_code, options_for_select(sorted_countries_list, params[:country_code]), {include_blank: '-- Select Country --'}, class: 'form-control'
.col-md-5
= form.file_field :json_file, class: 'form-control'
.col-md-1
= form.button 'Upload', class: 'btn btn-info'

br
.row
.col-md-12
table.table-bordered.table-striped.table
thead
tr
th #
th = t('pension_funds.upload.error_message')
ody
- @json_importer.try(:errors).to_a.each_with_index do |error, index|
tr
td = (index += 1)
td = error
7 changes: 7 additions & 0 deletions config/locales/champaign.en.yml
Expand Up @@ -430,8 +430,15 @@ en:
notice: "Pension fund has been created."
update:
notice: "Pension fund has been updated."
upload:
notice: "Pension fund has been imported successfully."
error: "Error occurred while importing Pension Funds"
new:
title: "Create new pension fund"
upload:
title: "Upload pension fund"
error_message: 'Error Message'
notice: 'Pension funds data imported successfully'
index:
title: "Pension Funds"
new: "Create new pension fund"
Expand Down
9 changes: 8 additions & 1 deletion config/routes.rb
Expand Up @@ -39,7 +39,14 @@
resources :uris, except: %i[new edit]
resources :campaigns
resources :donation_bands, except: %i[show destroy]
resources :pension_funds, except: %i[show destroy]

resources :pension_funds, except: %i[show destroy] do
collection do
get :export
get :upload
post :upload
end
end

resources :clone_pages

Expand Down
65 changes: 65 additions & 0 deletions spec/fixtures/new-pension-funds.json
@@ -0,0 +1,65 @@
[
{
"country_code":"AU",
"uuid":"1be3fff55b0f4534a4e25145117acb0b",
"fund":"New Fund name2",
"name":"New user2",
"email":"newuser2@example.com"
},
{
"country_code":"AU",
"uuid":"aabbadb539c945b89760b3584e4b6c1d",
"fund":"New Fund name",
"name":"New user",
"email":"newuser@example.com"
},
{
"country_code":"AU",
"uuid":"86d505bb81fb4b5bb957656a0f5e0525",
"fund":"Test Fund",
"name":"Test user One",
"email":"test@example.com"
},
{
"country_code":"AU",
"uuid":"519717276b524297b50d7975b5353bf4",
"fund":"Energy Industries Superannuation Scheme Pty Ltd.",
"name":"Alexander",
"email":"alex.hutchison@eisuper.com.au"
},
{
"country_code":"AU",
"uuid":"0a8d57637517485dac8e6d7ac72946f2",
"fund":"Christian Super Pty. Limited",
"name":"Peter",
"email":"pmurphy@christiansuper.com.au"
},
{
"country_code":"AU",
"uuid":"bc77a2784f1546ffad3aba26a02434fa",
"fund":"Care Super",
"name":"Xinting",
"email":"xjia@caresuper.com.au"
},
{
"country_code":"AU",
"uuid":"e3a0ef44156d4e7f80747e1e484efb6d",
"fund":"BUSS (Queensland) Proprietary Limited",
"name":"Bob",
"email":"blette@bussq.com.au"
},
{
"country_code":"AU",
"uuid":"fcc44635784343ae93fbbd34111b3b22",
"fund":"BOC Gases Superannuation Fund",
"name":"Kathy",
"email":"kathy.hocker@boc.com"
},
{
"country_code":"AU",
"uuid":"45959225972945aa8e3c7901b2150881",
"fund":"Australian Catholic Superannuation and Retirement Fund",
"name":"Greg",
"email":"greg.cantor@catholicsuper.com.au"
}
]
71 changes: 71 additions & 0 deletions spec/fixtures/updated-pension-funds.json
@@ -0,0 +1,71 @@
[
{
"country_code":"AU",
"uuid":"1be3fff55b0f4534a4e25145117acb0b",
"fund":"New Fund name2",
"name":"New user2",
"email":"newuser2@example.com"
},
{
"country_code":"AU",
"uuid":"aabbadb539c945b89760b3584e4b6c1d",
"fund":"New Fund name",
"name":"New user",
"email":"newuser@example.com"
},
{
"country_code":"AU",
"uuid":"86d505bb81fb4b5bb957656a0f5e0525",
"fund":"Test Fund",
"name":"Test user One",
"email":"test@example.com"
},
{
"country_code":"AU",
"uuid":"519717276b524297b50d7975b5353bf4",
"fund":"Energy Industries Superannuation Scheme Pty Ltd.",
"name":"Alexander",
"email":"alex.hutchison@eisuper.com.au"
},
{
"country_code":"AU",
"uuid":"0a8d57637517485dac8e6d7ac72946f2",
"fund":"Christian Super Pty. Limited",
"name":"Peter",
"email":"pmurphy@christiansuper.com.au"
},
{
"country_code":"AU",
"uuid":"bc77a2784f1546ffad3aba26a02434fa",
"fund":"Care Super",
"name":"Xinting",
"email":"xjia@caresuper.com.au"
},
{
"country_code":"AU",
"uuid":"e3a0ef44156d4e7f80747e1e484efb6d",
"fund":"BUSS (Queensland) Proprietary Limited",
"name":"Bob",
"email":"blette@bussq.com.au"
},
{
"country_code":"AU",
"uuid":"fcc44635784343ae93fbbd34111b3b22",
"fund":"BOC Gases Superannuation Fund",
"name":"Kathy",
"email":"kathy.hocker@boc.com"
},
{
"country_code":"AU",
"uuid":"45959225972945aa8e3c7901b2150881",
"fund":"Sample Fund",
"name":"Watson",
"email":"watson@example.com"
},
{
"country_code":"AU",
"fund":"Example fund",
"name":"Sample One",
"email":"watson100@example.com"
}
]
15 changes: 15 additions & 0 deletions spec/models/pension_fund_spec.rb
Expand Up @@ -21,6 +21,9 @@
require 'rails_helper'

RSpec.describe PensionFund, type: :model do
let(:au_json_file) { OpenStruct.new(tempfile: Rails.root.to_s + '/spec/fixtures/pension_funds/au.json') }
let(:be_json_file) { OpenStruct.new(tempfile: Rails.root.to_s + '/spec/fixtures/pension_funds/be.json') }

describe 'validations' do
it { should validate_presence_of(:country_code) }
it { should validate_presence_of(:fund) }
Expand Down Expand Up @@ -55,4 +58,16 @@
expect(@pension_fund.uuid).to eql uuid
end
end

describe 'filter_by_country_code' do
before do
PensionFundsJsonImporter.new(au_json_file, 'AU').import
PensionFundsJsonImporter.new(be_json_file, 'BE').import
end

it 'should list out respective country funds' do
expect(PensionFund.filter_by_country_code('AU').size).to eql 42
expect(PensionFund.filter_by_country_code('BE').size).to eql 1
end
end
end

0 comments on commit 29f912c

Please sign in to comment.