diff --git a/app/controllers/checkiners_controller.rb b/app/controllers/checkiners_controller.rb index 2258f3075..86f511677 100644 --- a/app/controllers/checkiners_controller.rb +++ b/app/controllers/checkiners_controller.rb @@ -3,12 +3,16 @@ class CheckinersController < ApplicationController before_action :validate_organizer! def index - @checkiner_rsvps = @event.attendee_rsvps.where(checkiner: true).includes(:user) - @potential_checkiners = @event.attendee_rsvps.where(checkiner: false).includes(:user) + render_index end def create - @rsvp = @event.rsvps.find(params[:event_checkiner][:rsvp_id]) + @rsvp = @event.rsvps.find_by(id: params.fetch(:event_checkiner, {})[:rsvp_id]) + unless @rsvp + @event.errors.add(:base, 'Please select a user!') + return render_index + end + @rsvp.update_attribute(:checkiner, true) redirect_to event_checkiners_path(@event), notice: "#{@rsvp.user.full_name} is now a checkiner for #{@event.title}!" end @@ -18,4 +22,12 @@ def destroy @rsvp.update_attribute(:checkiner, false) redirect_to event_checkiners_path(@event), notice: "#{@rsvp.user.full_name} is no longer a checkiner for #{@event.title}!" end + + private + + def render_index + @checkiner_rsvps = @event.attendee_rsvps.where(checkiner: true).includes(:user) + @potential_checkiners = @event.attendee_rsvps.where(checkiner: false).includes(:user) + render :index + end end diff --git a/app/controllers/organizers_controller.rb b/app/controllers/organizers_controller.rb index 74a7cca5d..5a69d0fca 100644 --- a/app/controllers/organizers_controller.rb +++ b/app/controllers/organizers_controller.rb @@ -4,7 +4,7 @@ class OrganizersController < ApplicationController before_action :validate_published! def index - @organizer_rsvps = @event.organizer_rsvps.includes(:user) + render_index end def potential @@ -16,7 +16,12 @@ def potential end def create - @user = User.find(params[:event_organizer][:user_id]) + @user = User.find_by(id: params.fetch(:event_organizer, {})[:user_id]) + unless @user + @event.errors.add(:base, 'Please select a user!') + return render_index + end + rsvp = @event.rsvps.where(user_id: @user.id).first_or_initialize rsvp.user = @user rsvp.role = Role::ORGANIZER @@ -33,6 +38,11 @@ def destroy private + def render_index + @organizer_rsvps = @event.organizer_rsvps.includes(:user) + render :index + end + def validate_published! @event ||= Event.find(params[:event_id]) unless @event.published? diff --git a/app/views/checkiners/index.html.erb b/app/views/checkiners/index.html.erb index 49d2884c6..b12f6a626 100644 --- a/app/views/checkiners/index.html.erb +++ b/app/views/checkiners/index.html.erb @@ -3,6 +3,8 @@ <%= render :partial => 'shared/organizer_breadcrumb', locals: {current_page_title: 'Checkiner Assignments'} %> +<%= render 'shared/model_error_messages', model: @event %> +

Potential Checkiners

<%= render 'shared/user_chooser', action: event_checkiners_path(@event), diff --git a/app/views/organizers/index.html.erb b/app/views/organizers/index.html.erb index 3651dae32..bb05d34f1 100644 --- a/app/views/organizers/index.html.erb +++ b/app/views/organizers/index.html.erb @@ -3,6 +3,8 @@ <%= render :partial => 'shared/organizer_breadcrumb', locals: {current_page_title: 'Organizer Assignments'} %> +<%= render 'shared/model_error_messages', model: @event %> +

Potential Organizers

<%= render 'shared/user_chooser', action: event_organizers_path(@event), diff --git a/app/views/shared/_user_chooser.html.erb b/app/views/shared/_user_chooser.html.erb index 8d64eb82e..fc7066821 100644 --- a/app/views/shared/_user_chooser.html.erb +++ b/app/views/shared/_user_chooser.html.erb @@ -24,9 +24,13 @@ } }); <% end %> - $('#<%= select_id %>_<%= foreign_key %>').select2(options); + var $select = $('#<%= select_id %>_<%= foreign_key %>'); + $select.select2(options); + $select.change(function () { + $select.closest('form').find('input[type=submit]').attr({disabled: !this.value}); + }); - <%= submit_tag "Assign", class: "btn btn-primary", style: 'margin: 10px 0;' %> + <%= submit_tag "Assign", class: "btn btn-primary", style: 'margin: 10px 0;', disabled: true %> <% end %> \ No newline at end of file diff --git a/spec/controllers/checkiners_controller_spec.rb b/spec/controllers/checkiners_controller_spec.rb new file mode 100644 index 000000000..a2e5f9602 --- /dev/null +++ b/spec/controllers/checkiners_controller_spec.rb @@ -0,0 +1,36 @@ +require 'rails_helper' + +describe CheckinersController do + before do + @event = create(:event) + @user = create(:user) + end + + context "a user that is logged in and is an organizer for a published event" do + before do + @event.organizers << @user + + sign_in @user + end + + it "can see list of checkiners" do + get :index, event_id: @event.id + expect(response).to be_success + end + + describe "assigning checkiners" do + it "can promote a user to checkiner" do + other_user_rsvp = create(:rsvp, event: @event) + expect { + post :create, event_id: @event.id, event_checkiner: {rsvp_id: other_user_rsvp.id} + }.to change { other_user_rsvp.reload.checkiner } + expect(response).to redirect_to(event_checkiners_path(@event)) + end + + it "shows an error if no user is provided" do + post :create, event_id: @event.id + expect(assigns(:event).errors[:base].length).to be >= 1 + end + end + end +end diff --git a/spec/controllers/organizers_controller_spec.rb b/spec/controllers/organizers_controller_spec.rb index 76407d38e..c19158cdf 100644 --- a/spec/controllers/organizers_controller_spec.rb +++ b/spec/controllers/organizers_controller_spec.rb @@ -68,7 +68,6 @@ context "a user that is logged in and is an organizer for a published event" do before do @other_user = create(:user) - @event = create(:event) @event.organizers << @user sign_in @user @@ -79,11 +78,20 @@ expect(response).to be_success end - it "can create an organizer and redirect to the event organizer assignment page" do - expect { - post :create, event_id: @event.id, event_organizer: {event_id: @event.id, user_id: @other_user.id} - }.to change(Rsvp, :count).by(1) - expect(response).to redirect_to(event_organizers_path(@event)) + describe "assigning organizers" do + it "can create an organizer and redirect to the event organizer assignment page" do + expect { + post :create, event_id: @event.id, event_organizer: {user_id: @other_user.id} + }.to change(Rsvp, :count).by(1) + expect(response).to redirect_to(event_organizers_path(@event)) + end + + it "shows an error if no user is provided" do + expect { + post :create, event_id: @event.id + }.not_to change(Rsvp, :count) + expect(assigns(:event).errors[:base].length).to be >= 1 + end end it "can promote an existing volunteer to organizer" do