diff --git a/app/assets/stylesheets/scaffolds.scss b/app/assets/stylesheets/scaffolds.scss index 4137aa83..d9b6da5a 100644 --- a/app/assets/stylesheets/scaffolds.scss +++ b/app/assets/stylesheets/scaffolds.scss @@ -50,5 +50,17 @@ footer { form { .form-group { padding-top: 12px; + + label { + font-weight: bold; + } + + .form-check-collection { + .form-check-label { + display: block; + padding-bottom: 12px; + font-weight: normal; + } + } } } diff --git a/app/controllers/recipient_lists_controller.rb b/app/controllers/recipient_lists_controller.rb index bc356c67..ffed41eb 100644 --- a/app/controllers/recipient_lists_controller.rb +++ b/app/controllers/recipient_lists_controller.rb @@ -1,4 +1,5 @@ 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] @@ -57,6 +58,13 @@ 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) diff --git a/app/controllers/welcome_controller.rb b/app/controllers/welcome_controller.rb index 7d0e8050..cbb3b2f7 100644 --- a/app/controllers/welcome_controller.rb +++ b/app/controllers/welcome_controller.rb @@ -1,6 +1,7 @@ class WelcomeController < ApplicationController def index + @schools = School.all end end diff --git a/app/views/recipient_lists/_form.html.haml b/app/views/recipient_lists/_form.html.haml index dbd9c0fc..ba893ef9 100644 --- a/app/views/recipient_lists/_form.html.haml +++ b/app/views/recipient_lists/_form.html.haml @@ -7,17 +7,18 @@ %ul - @recipient_list.errors.full_messages.each do |msg| %li= msg - .field + .form-group = f.label :name %br/ - = f.text_field :name - .field + = f.text_field :name, class: 'form-control' + .form-group = f.label :description %br/ - = f.text_area :description - .field - = f.label :recipient_ids + = f.text_area :description, class: 'form-control' + .form-group + = f.label :recipient_ids, 'Recipients' %br/ - = f.text_area :recipient_ids - .actions - = f.submit + .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}" } } + .form-group + = f.submit 'Save List', class: 'btn btn-primary' diff --git a/app/views/recipient_lists/new.html.haml b/app/views/recipient_lists/new.html.haml index 2db4c52f..1b190b31 100644 --- a/app/views/recipient_lists/new.html.haml +++ b/app/views/recipient_lists/new.html.haml @@ -1,3 +1,6 @@ -%h1 New recipient_list -= render 'form' -= link_to 'Back', school_recipient_lists_path(@recipient_list.school) +.row + .offset-sm-2.col-sm-8 + %h3 Create A Recipient List For This School + = render 'form' + %br + %p= link_to 'Back',@school diff --git a/app/views/recipients/new.html.haml b/app/views/recipients/new.html.haml index 3107b577..20d10ea0 100644 --- a/app/views/recipients/new.html.haml +++ b/app/views/recipients/new.html.haml @@ -3,4 +3,4 @@ %h3 Add A Recipient To This School = render 'form', recipient: @recipient %br - %p= link_to 'Back', school_recipients_path(@school) + %p= link_to 'Back', @school diff --git a/app/views/schools/show.html.haml b/app/views/schools/show.html.haml index ac2c0011..734be070 100644 --- a/app/views/schools/show.html.haml +++ b/app/views/schools/show.html.haml @@ -9,6 +9,8 @@ %p= link_to "Bulk Add Recipients", import_school_recipients_path(@school) +%p= link_to "Create Recipient List", new_school_recipient_list_path(@school) + = link_to 'Edit', edit_school_path(@school) | = link_to 'Back', root_path diff --git a/spec/controllers/recipient_lists_controller_spec.rb b/spec/controllers/recipient_lists_controller_spec.rb index a439fbc3..ff0d5c2c 100644 --- a/spec/controllers/recipient_lists_controller_spec.rb +++ b/spec/controllers/recipient_lists_controller_spec.rb @@ -28,7 +28,7 @@ RSpec.describe RecipientListsController, type: :controller do let(:valid_attributes) { { school_id: school.id, - recipient_ids: '1,2,3', + recipient_ids: ['', '1', '2', '3'], name: 'Parents', description: 'List of parents.' } @@ -88,6 +88,11 @@ RSpec.describe RecipientListsController, type: :controller do expect(assigns(:recipient_list)).to be_persisted end + it 'stores recipient_ids properly' do + post :create, params: {school_id: school.to_param, recipient_list: valid_attributes}, session: valid_session + expect(assigns(:recipient_list).recipient_ids).to eq('1,2,3') + end + it "redirects to the created recipient_list" do post :create, params: {school_id: school.to_param, recipient_list: valid_attributes}, session: valid_session expect(response).to redirect_to(school_recipient_list_path(school, RecipientList.last)) @@ -110,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_ids: ['', '3', '4', '5']} } it "updates the requested recipient_list" do diff --git a/spec/views/recipient_lists/edit.html.erb_spec.rb b/spec/views/recipient_lists/edit.html.erb_spec.rb index dcf6442f..0330df20 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 "textarea#recipient_list_recipient_ids[name=?]", "recipient_list[recipient_ids]" + assert_select "input[name=?]", "recipient_list[recipient_ids][]" 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 5ab14010..0cae93f1 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 "textarea#recipient_list_recipient_ids[name=?]", "recipient_list[recipient_ids]" + assert_select "input[name=?]", "recipient_list[recipient_ids][]" end end end