diff --git a/app/models/recipient.rb b/app/models/recipient.rb index c16e435b..e5106070 100644 --- a/app/models/recipient.rb +++ b/app/models/recipient.rb @@ -6,15 +6,15 @@ class Recipient < ApplicationRecord validates :name, presence: true - def self.import(file) + def self.import(school, file) CSV.foreach(file.path, headers: true) do |row| recipient_hash = row.to_hash - recipient = Recipient.where(phone: recipient_hash["phone"]) + recipient = school.recipients.where(phone: recipient_hash["phone"]) if recipient.count == 1 recipient.first.update_attributes(recipient_hash) else - Recipient.create!(recipient_hash) + school.recipients.create!(recipient_hash) end end end diff --git a/app/views/schools/show.html.haml b/app/views/schools/show.html.haml index 7ce258b1..ca5b69b9 100644 --- a/app/views/schools/show.html.haml +++ b/app/views/schools/show.html.haml @@ -9,7 +9,12 @@ %p= link_to "Bulk Add Recipients", import_school_recipients_path(@school) - = link_to 'Edit', edit_school_path(@school) | -= link_to 'Back', schools_path += link_to 'Back', root_path + +- @school.recipients.each do |recipient| + .recipient + %p + %strong= recipient.name + = recipient.phone diff --git a/spec/models/recipient_spec.rb b/spec/models/recipient_spec.rb index 1d864850..59e894ff 100644 --- a/spec/models/recipient_spec.rb +++ b/spec/models/recipient_spec.rb @@ -3,14 +3,16 @@ require 'rails_helper' describe Recipient do describe "Import" do + let(:school) { School.create!(name: 'School') } 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) + Recipient.import(school, file) expect(Recipient.count).to eq(3) expect(Recipient.all.map(&:name)).to eq(['Jared', 'Lauren', 'Abby']) + expect(Recipient.all.map(&:school).uniq).to eq([school]) end end end