Skip to content

Commit

Permalink
Fix ordering issues with student waitlist rsvps
Browse files Browse the repository at this point in the history
* student_waitlist_rsvps is now always ordered by waitlist_position
* waitlist on the Event Show page shows the real waitlist_position
instead of the fetched rsvp index (though they should now be the same)
* Reload event in-between destroying an rsvp and reordering waitlist,
otherwise the student_rsvps_count isn't reloaded and no-one is promoted
* Don't use find_each in reorder_waitlist!, because it
apparently doesn't respect order in Rails 4.1
  • Loading branch information
tjgrathwell committed Oct 17, 2014
1 parent 217b758 commit b3a7d8c
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 8 deletions.
2 changes: 1 addition & 1 deletion app/controllers/rsvps_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def update
def destroy
Rsvp.transaction do
@rsvp.destroy
@event.reorder_waitlist!
@event.reload.reorder_waitlist!
end
redirect_to events_path, notice: "You are now no longer signed up for #{@event.title}"
end
Expand Down
6 changes: 3 additions & 3 deletions app/models/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Event < ActiveRecord::Base
has_many :attendee_rsvps, -> { where(role_id: [Role::STUDENT.id, Role::VOLUNTEER.id], waitlist_position: nil) }, class_name: 'Rsvp', inverse_of: :event

has_many :student_rsvps, -> { where(role_id: Role::STUDENT.id, waitlist_position: nil) }, class_name: 'Rsvp', inverse_of: :event
has_many :student_waitlist_rsvps, -> { where("role_id = #{Role::STUDENT.id} AND waitlist_position IS NOT NULL") }, class_name: 'Rsvp', inverse_of: :event
has_many :student_waitlist_rsvps, -> { where("role_id = #{Role::STUDENT.id} AND waitlist_position IS NOT NULL").order(:waitlist_position) }, class_name: 'Rsvp', inverse_of: :event
has_many :students, through: :student_rsvps, source: :user, source_type: 'User'
has_many :legacy_students, through: :student_rsvps, source: :user, source_type: 'MeetupUser'

Expand Down Expand Up @@ -237,14 +237,14 @@ def reorder_waitlist!
Rsvp.transaction do
unless at_limit?
number_of_open_spots = student_rsvp_limit - student_rsvps_count
to_be_confirmed = student_waitlist_rsvps.order(:waitlist_position).limit(number_of_open_spots)
to_be_confirmed = student_waitlist_rsvps.limit(number_of_open_spots)
to_be_confirmed.each do |rsvp|
rsvp.promote_from_waitlist!
end
end

index = 1
student_waitlist_rsvps.order(:waitlist_position).find_each do |rsvp|
student_waitlist_rsvps.reload.each do |rsvp|
rsvp.update_attribute(:waitlist_position, index)
index += 1
end
Expand Down
4 changes: 2 additions & 2 deletions app/views/events/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@
<% if @event.student_waitlist_rsvps.any? %>
<h3><%= @event.student_waitlist_rsvps.length %> waitlisted!</h3>
<ul class="attendees">
<% @event.student_waitlist_rsvps.each_with_index do |rsvp, ix| %>
<li class='attendee'><%= render 'shared/viewed_gravatars', :email => rsvp.user.email %><%= link_to rsvp.user.full_name, rsvp.user.profile_path %><span class='waitlist-number pull-right'>#<%= ix + 1 %></span></li>
<% @event.student_waitlist_rsvps.each do |rsvp| %>
<li class='attendee'><%= render 'shared/viewed_gravatars', :email => rsvp.user.email %><%= link_to rsvp.user.full_name, rsvp.user.profile_path %><span class='waitlist-number pull-right'>#<%= rsvp.waitlist_position %></span></li>
<% end %>
</ul>
<% end %>
Expand Down
4 changes: 2 additions & 2 deletions spec/models/event_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -430,11 +430,11 @@ def add_session_rsvp(rsvp, session, checked_in)
end

it "returns only confirmed rsvps in #student_rsvps" do
@event.student_rsvps.should == [@confirmed_rsvp]
@event.student_rsvps.reload.should == [@confirmed_rsvp]
end

it "returns only waitlisted rsvps in #student_waitlist_rsvps" do
@event.student_waitlist_rsvps.should == [@waitlist_rsvp]
@event.student_waitlist_rsvps.reload.should == [@waitlist_rsvp]
end
end

Expand Down

0 comments on commit b3a7d8c

Please sign in to comment.