refactoring recipient_list

pull/1/head
Jared Cosulich 9 years ago
parent 645d7bc7d5
commit 198c8984c1

@ -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

@ -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

@ -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'

@ -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

@ -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

@ -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

@ -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

@ -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

@ -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

@ -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

Loading…
Cancel
Save