Skip to content

Commit

Permalink
Disable submit on organizer/checkiner create form when no user selected
Browse files Browse the repository at this point in the history
The 'Assign' button is now disabled until select2 says
something was selected.

In the unlikely event that someone submits the form bypassing the
disabledness, the controller action will rerender with an error.

Fixes railsbridge#416
  • Loading branch information
tjgrathwell committed Dec 14, 2015
1 parent 05d8d07 commit d151b39
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 13 deletions.
18 changes: 15 additions & 3 deletions app/controllers/checkiners_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
14 changes: 12 additions & 2 deletions app/controllers/organizers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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?
Expand Down
2 changes: 2 additions & 0 deletions app/views/checkiners/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
<%= render :partial => 'shared/organizer_breadcrumb', locals: {current_page_title: 'Checkiner Assignments'} %>
<%= render 'shared/model_error_messages', model: @event %>

<h3>Potential Checkiners</h3>
<%= render 'shared/user_chooser',
action: event_checkiners_path(@event),
Expand Down
2 changes: 2 additions & 0 deletions app/views/organizers/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
<%= render :partial => 'shared/organizer_breadcrumb', locals: {current_page_title: 'Organizer Assignments'} %>
<%= render 'shared/model_error_messages', model: @event %>

<h3>Potential Organizers</h3>
<%= render 'shared/user_chooser',
action: event_organizers_path(@event),
Expand Down
8 changes: 6 additions & 2 deletions app/views/shared/_user_chooser.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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});
});
</script>
<%= 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 %>
</div>
</div>
36 changes: 36 additions & 0 deletions spec/controllers/checkiners_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -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
20 changes: 14 additions & 6 deletions spec/controllers/organizers_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit d151b39

Please sign in to comment.