Skip to content

Commit

Permalink
Do not show Sessions section in RSVP form for single-session events.
Browse files Browse the repository at this point in the history
Users are automatically RSVP'd to the single existing session for these events.
  • Loading branch information
tjgrathwell committed Mar 25, 2013
1 parent 044fb2f commit a6b4c0a
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 32 deletions.
5 changes: 4 additions & 1 deletion app/models/rsvp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,11 @@ def formatted_preference
volunteer_preference.title
end

def set_attending_sessions session_ids
def set_attending_sessions session_ids=nil
rsvp_sessions.destroy_all
if event.event_sessions.length == 1
session_ids = [event.event_sessions.first.id]
end
session_ids.each do |session_id|
rsvp_sessions.create(event_session_id: session_id)
end
Expand Down
12 changes: 7 additions & 5 deletions app/views/rsvps/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,13 @@
<% end %>
</div>

<strong>Sessions you're attending</strong>
<% @rsvp.event.event_sessions.order('starts_at ASC').each do |session| %>
<%= label_tag do %>
<%= check_box_tag 'rsvp_sessions[]', session.id, @rsvp.new_record? ? true : @rsvp.rsvp_sessions.where(event_session_id: session.id).any? %>
<%= session.name %>: <%= formatted_session_date(session) %> <%= formatted_session_timerange(session) %>
<% if @rsvp.event.event_sessions.length > 1 %>
<strong>Sessions you're attending</strong>
<% @rsvp.event.event_sessions.order('starts_at ASC').each do |session| %>
<%= label_tag do %>
<%= check_box_tag 'rsvp_sessions[]', session.id, @rsvp.new_record? ? true : @rsvp.rsvp_sessions.where(event_session_id: session.id).any? %>
<%= session.name %>: <%= formatted_session_date(session) %> <%= formatted_session_timerange(session) %>
<% end %>
<% end %>
<% end %>

Expand Down
5 changes: 5 additions & 0 deletions spec/features/event_rsvp_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
visit new_event_rsvp_path(@event)
end

it "should not show checkboxes for events with only one session" do
@event.event_sessions.length.should == 1
page.should_not have_content(@event.event_sessions.first.name)
end

it "should ask if user needs childcare ask for more info" do
page.find("#rsvp_needs_childcare").should_not be_checked
page.find("#rsvp_childcare_info").find(:xpath, '..')['class'].
Expand Down
66 changes: 40 additions & 26 deletions spec/models/rsvp_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,40 +27,54 @@
end

describe '#set_attending_sessions' do
before do
@event = create(:event)
#ensure there are at least two sessions
@event.event_sessions << create(:event_session)
@num_sessions = @event.event_sessions.length
@rsvp = create(:rsvp, event: @event)

@session1 = @event.event_sessions.first
@session2 = @event.event_sessions.last
end

it "allows " do

context "when there is only one event session" do
before do
@event = create(:event)
@event.event_sessions.length.should == 1
@rsvp = create(:rsvp, event: @event)
end

expect {
@rsvp.set_attending_sessions(@event.event_sessions.map(&:id))
}.to change { @rsvp.rsvp_sessions.count }.by(@num_sessions)
@rsvp.rsvp_sessions.map(&:event_session_id).should =~ @event.event_sessions.map(&:id)
it "creates an rsvp_session record for that session" do
expect {
@rsvp.set_attending_sessions
}.to change { @rsvp.rsvp_sessions.count }.by(1)
@rsvp.rsvp_sessions.map(&:event_session_id).should =~ @event.event_sessions.map(&:id)
end
end

context "when some sessions are already being attended" do
context "when there are at least two sessions" do
before do
create(:rsvp_session, rsvp_id: @rsvp.id, event_session_id: @session1.id)
@event = create(:event)
@event.event_sessions << create(:event_session)
@num_sessions = @event.event_sessions.length
@rsvp = create(:rsvp, event: @event)

@session1 = @event.event_sessions.first
@session2 = @event.event_sessions.last
end

it "destroys rsvps when told to set to an empty array" do
@rsvp.set_attending_sessions([])
@rsvp.rsvp_sessions.count.should == 0
it "creates rsvp_session records for all ids sent in" do
expect {
@rsvp.set_attending_sessions(@event.event_sessions.map(&:id))
}.to change { @rsvp.rsvp_sessions.count }.by(@num_sessions)
@rsvp.rsvp_sessions.map(&:event_session_id).should =~ @event.event_sessions.map(&:id)
end

it "destroys existing attendance and creates new attendances using the provided ids" do
@rsvp.set_attending_sessions([@session2.id])
@rsvp.rsvp_sessions.count.should == 1
@rsvp.rsvp_sessions.first.event_session.should == @session2
context "when some sessions are already being attended" do
before do
create(:rsvp_session, rsvp_id: @rsvp.id, event_session_id: @session1.id)
end

it "destroys rsvps when told to set to an empty array" do
@rsvp.set_attending_sessions([])
@rsvp.rsvp_sessions.count.should == 0
end

it "destroys existing attendance and creates new attendances using the provided ids" do
@rsvp.set_attending_sessions([@session2.id])
@rsvp.rsvp_sessions.count.should == 1
@rsvp.rsvp_sessions.first.event_session.should == @session2
end
end
end
end
Expand Down

0 comments on commit a6b4c0a

Please sign in to comment.