From 16133cdd9d1a4806c9a00c1a225f826d6e30e142 Mon Sep 17 00:00:00 2001 From: Jared Cosulich Date: Wed, 1 Mar 2017 15:13:44 -0500 Subject: [PATCH] bulk import recipients --- app/controllers/recipients_controller.rb | 7 +++++++ app/models/recipient.rb | 15 +++++++++++++++ app/views/recipients/import.html.haml | 9 +++++++++ app/views/schools/show.html.haml | 2 ++ config/routes.rb | 9 +++++++-- spec/models/recipient_spec.rb | 16 ++++++++++++++++ 6 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 app/views/recipients/import.html.haml create mode 100644 spec/models/recipient_spec.rb diff --git a/app/controllers/recipients_controller.rb b/app/controllers/recipients_controller.rb index 2da06e35..ad3bb4a4 100644 --- a/app/controllers/recipients_controller.rb +++ b/app/controllers/recipients_controller.rb @@ -38,6 +38,13 @@ class RecipientsController < ApplicationController end end + def import + render and return if request.get? + + Recipient.import(params[:file]) + redirect_to @school, notice: "Recipients imported." + end + # PATCH/PUT /recipients/1 # PATCH/PUT /recipients/1.json def update diff --git a/app/models/recipient.rb b/app/models/recipient.rb index d5e580f7..c16e435b 100644 --- a/app/models/recipient.rb +++ b/app/models/recipient.rb @@ -1,7 +1,22 @@ +require 'csv' + class Recipient < ApplicationRecord belongs_to :school validates_associated :school validates :name, presence: true + def self.import(file) + CSV.foreach(file.path, headers: true) do |row| + recipient_hash = row.to_hash + recipient = Recipient.where(phone: recipient_hash["phone"]) + + if recipient.count == 1 + recipient.first.update_attributes(recipient_hash) + else + Recipient.create!(recipient_hash) + end + end + end + end diff --git a/app/views/recipients/import.html.haml b/app/views/recipients/import.html.haml new file mode 100644 index 00000000..a2f8f196 --- /dev/null +++ b/app/views/recipients/import.html.haml @@ -0,0 +1,9 @@ +.row + .offset-sm-2.col-sm-8 + %h3 Import Recipients To This School + = form_tag import_school_recipients_path(@school), multipart: true do + %br + .form-group + = file_field_tag :file, class: 'form-control-file' + .form-group + = submit_tag "Import", class: 'btn btn-primary' diff --git a/app/views/schools/show.html.haml b/app/views/schools/show.html.haml index 18be3f2c..7ce258b1 100644 --- a/app/views/schools/show.html.haml +++ b/app/views/schools/show.html.haml @@ -7,6 +7,8 @@ %p= link_to "Add Recipient", new_school_recipient_path(@school) +%p= link_to "Bulk Add Recipients", import_school_recipients_path(@school) + = link_to 'Edit', edit_school_path(@school) | diff --git a/config/routes.rb b/config/routes.rb index 5b6711eb..ac1ed306 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,8 +1,13 @@ Rails.application.routes.draw do resources :districts - + resources :schools do - resources :recipients + resources :recipients do + collection do + get :import + post :import + end + end end devise_for :users diff --git a/spec/models/recipient_spec.rb b/spec/models/recipient_spec.rb new file mode 100644 index 00000000..1d864850 --- /dev/null +++ b/spec/models/recipient_spec.rb @@ -0,0 +1,16 @@ +require 'rails_helper' + +describe Recipient do + describe "Import" do + + let(:data) { "name,phone\rJared,111-222-333\rLauren,222-333-4444\rAbby,333-444-5555\r" } + let(:file) { instance_double('File', path: 'path') } + + it "should parse file contents and return a result" do + expect(File).to receive(:open).with('path', universal_newline: false, headers: true) { StringIO.new(data) } + Recipient.import(file) + expect(Recipient.count).to eq(3) + expect(Recipient.all.map(&:name)).to eq(['Jared', 'Lauren', 'Abby']) + end + end +end