From 198c8984c137c17fe8033c87e058159cf04b8d4c Mon Sep 17 00:00:00 2001 From: Jared Cosulich Date: Fri, 3 Mar 2017 11:01:22 -0500 Subject: [PATCH] refactoring recipient_list --- app/controllers/recipient_lists_controller.rb | 10 +-------- app/models/recipient_list.rb | 21 +++++++++++++++++++ app/views/recipient_lists/_form.html.haml | 2 +- app/views/recipient_lists/show.html.haml | 12 +++++++---- app/views/schools/show.html.haml | 18 ++++++++++++++++ .../recipient_lists_controller_spec.rb | 6 +++--- spec/models/recipient_list_spec.rb | 17 +++++++++++++++ .../recipient_lists/edit.html.erb_spec.rb | 2 +- .../recipient_lists/new.html.erb_spec.rb | 2 +- .../recipient_lists/show.html.erb_spec.rb | 10 +++++++-- 10 files changed, 79 insertions(+), 21 deletions(-) create mode 100644 spec/models/recipient_list_spec.rb diff --git a/app/controllers/recipient_lists_controller.rb b/app/controllers/recipient_lists_controller.rb index ffed41eb..b327716d 100644 --- a/app/controllers/recipient_lists_controller.rb +++ b/app/controllers/recipient_lists_controller.rb @@ -1,5 +1,4 @@ class RecipientListsController < ApplicationController - before_action :stringify_recipient_ids, only: [:create, :update] before_action :set_school before_action :set_recipient_list, only: [:show, :edit, :update, :destroy] @@ -58,15 +57,8 @@ class RecipientListsController < ApplicationController @recipient_list = @school.recipient_lists.find(params[:id]) end - def stringify_recipient_ids - ids = params[:recipient_list][:recipient_ids] - if ids.present? && ids.is_a?(Array) - params[:recipient_list][:recipient_ids] = ids.reject { |id| id.empty? }.join(',') - end - end - # Only allow a trusted parameter "white list" through. def recipient_list_params - params.require(:recipient_list).permit(:name, :description, :recipient_ids) + params.require(:recipient_list).permit(:name, :description, recipient_id_array: []) end end diff --git a/app/models/recipient_list.rb b/app/models/recipient_list.rb index 6f33aec8..c82af6ef 100644 --- a/app/models/recipient_list.rb +++ b/app/models/recipient_list.rb @@ -3,4 +3,25 @@ class RecipientList < ApplicationRecord validates :name, presence: true validates :recipient_ids, presence: true + + attr_accessor :recipient_id_array + before_validation :convert_recipient_id_array + after_initialize :set_recipient_id_array + + def recipients + recipient_id_array.collect { |id| school.recipients.where(id: id).first } + end + + private + + def convert_recipient_id_array + return if recipient_id_array.blank? + self.recipient_ids = recipient_id_array.reject { |id| id.to_s.empty? }.join(',') + end + + def set_recipient_id_array + return if recipient_ids.blank? + self.recipient_id_array = recipient_ids.split(',').map(&:to_i) + end + end diff --git a/app/views/recipient_lists/_form.html.haml b/app/views/recipient_lists/_form.html.haml index ba893ef9..3ced760f 100644 --- a/app/views/recipient_lists/_form.html.haml +++ b/app/views/recipient_lists/_form.html.haml @@ -19,6 +19,6 @@ = f.label :recipient_ids, 'Recipients' %br/ .form-check.form-check-collection - = f.collection_check_boxes(:recipient_ids, @school.recipients.all, :id, :name) { |c| c.label(class: 'form-check-label') { c.check_box(class: 'form-check-input') + " #{c.text}" } } + = f.collection_check_boxes(:recipient_id_array, @school.recipients.all, :id, :name) { |c| c.label(class: 'form-check-label') { c.check_box(class: 'form-check-input') + " #{c.text}" } } .form-group = f.submit 'Save List', class: 'btn btn-primary' diff --git a/app/views/recipient_lists/show.html.haml b/app/views/recipient_lists/show.html.haml index b31897ba..b92cee78 100644 --- a/app/views/recipient_lists/show.html.haml +++ b/app/views/recipient_lists/show.html.haml @@ -1,4 +1,6 @@ -%p#notice= notice +%p + %b School: + = @school.name %p %b Name: = @recipient_list.name @@ -6,8 +8,10 @@ %b Description: = @recipient_list.description %p - %b Recipient ids: - = @recipient_list.recipient_ids + %b Recipients: + - @recipient_list.recipients.each do |recipient| + %p= recipient.name + = link_to 'Edit', edit_school_recipient_list_path(@recipient_list.school, @recipient_list) | -= link_to 'Back', school_recipient_lists_path(@recipient_list.school) += link_to 'Back', @recipient_list.school diff --git a/app/views/schools/show.html.haml b/app/views/schools/show.html.haml index 734be070..600002c1 100644 --- a/app/views/schools/show.html.haml +++ b/app/views/schools/show.html.haml @@ -15,6 +15,24 @@ | = link_to 'Back', root_path +%br +%br +%br +%h4 School Recipient Lists +%table{style: 'width: 100%;'} + %tbody + %thead{style: 'font-weight: bold;'} + %th Name + %th Descriptin + %th{colspan: 2} Actions + - @school.recipient_lists.each do |recipient_list| + %tr.recipient + %td= link_to recipient_list.name, [@school, recipient_list] + %td= recipient_list.description + %td= link_to('Edit', edit_school_recipient_list_path(@school, recipient_list)) + %td= link_to('Delete', school_recipient_list_path(@school, recipient_list), method: :delete, data: {confirm: 'Are you sure you want to delete this list?'}) + + %br %br %br diff --git a/spec/controllers/recipient_lists_controller_spec.rb b/spec/controllers/recipient_lists_controller_spec.rb index ff0d5c2c..b3ccd509 100644 --- a/spec/controllers/recipient_lists_controller_spec.rb +++ b/spec/controllers/recipient_lists_controller_spec.rb @@ -28,14 +28,14 @@ RSpec.describe RecipientListsController, type: :controller do let(:valid_attributes) { { school_id: school.id, - recipient_ids: ['', '1', '2', '3'], + recipient_id_array: ['', '1', '2', '3'], name: 'Parents', description: 'List of parents.' } } let(:invalid_attributes) { - {school_id: school.id, name: 'Empty List', recipient_ids: ''} + {school_id: school.id, name: 'Empty List', recipient_id_array: ['']} } # This should return the minimal set of values that should be in the session @@ -115,7 +115,7 @@ RSpec.describe RecipientListsController, type: :controller do describe "PUT #update" do context "with valid params" do let(:new_attributes) { - {recipient_ids: ['', '3', '4', '5']} + {recipient_id_array: ['', '3', '4', '5']} } it "updates the requested recipient_list" do diff --git a/spec/models/recipient_list_spec.rb b/spec/models/recipient_list_spec.rb new file mode 100644 index 00000000..6396d2f3 --- /dev/null +++ b/spec/models/recipient_list_spec.rb @@ -0,0 +1,17 @@ +require 'rails_helper' + +describe Recipient do + describe "Save" do + + it "should convert the recipient_id_array into the recipient_ids attribute" do + recipient_list = RecipientList.create(name: 'Name', recipient_id_array: ['', '1', '2', '3']) + expect(recipient_list).to be_a(RecipientList) + expect(recipient_list).to be_persisted + expect(recipient_list.recipient_ids).to eq('1,2,3') + + recipient_list.update(recipient_id_array: ['3', '', '4', '5', '6']) + expect(recipient_list.reload.recipient_ids).to eq('3,4,5,6') + end + + end +end diff --git a/spec/views/recipient_lists/edit.html.erb_spec.rb b/spec/views/recipient_lists/edit.html.erb_spec.rb index 0330df20..70841308 100644 --- a/spec/views/recipient_lists/edit.html.erb_spec.rb +++ b/spec/views/recipient_lists/edit.html.erb_spec.rb @@ -23,7 +23,7 @@ RSpec.describe "recipient_lists/edit", type: :view do assert_select "textarea#recipient_list_description[name=?]", "recipient_list[description]" - assert_select "input[name=?]", "recipient_list[recipient_ids][]" + assert_select "input[name=?]", "recipient_list[recipient_id_array][]" end end end diff --git a/spec/views/recipient_lists/new.html.erb_spec.rb b/spec/views/recipient_lists/new.html.erb_spec.rb index 0cae93f1..b47a0909 100644 --- a/spec/views/recipient_lists/new.html.erb_spec.rb +++ b/spec/views/recipient_lists/new.html.erb_spec.rb @@ -20,7 +20,7 @@ RSpec.describe "recipient_lists/new", type: :view do assert_select "textarea#recipient_list_description[name=?]", "recipient_list[description]" - assert_select "input[name=?]", "recipient_list[recipient_ids][]" + assert_select "input[name=?]", "recipient_list[recipient_id_array][]" end end end diff --git a/spec/views/recipient_lists/show.html.erb_spec.rb b/spec/views/recipient_lists/show.html.erb_spec.rb index 79796e5a..14b57efc 100644 --- a/spec/views/recipient_lists/show.html.erb_spec.rb +++ b/spec/views/recipient_lists/show.html.erb_spec.rb @@ -3,10 +3,15 @@ require 'rails_helper' RSpec.describe "recipient_lists/show", type: :view do before(:each) do @school = assign(:school, School.create!(name: 'School')) + + recipients = ['Jared Cosulich', 'Lauren Cosulich'].collect do |name| + @school.recipients.create!(name: name) + end + @recipient_list = assign(:recipient_list, RecipientList.create!( :name => "Name", :description => "MyText", - :recipient_ids => "MyText", + :recipient_id_array => recipients.map(&:id), :school_id => @school.id )) end @@ -15,6 +20,7 @@ RSpec.describe "recipient_lists/show", type: :view do render expect(rendered).to match(/Name/) expect(rendered).to match(/MyText/) - expect(rendered).to match(/MyText/) + expect(rendered).to match(/Jared Cosulich/) + expect(rendered).to match(/Lauren Cosulich/) end end