Skip to content

Commit

Permalink
Make sure Rsvp model can only have 1 or higher positive attendees. Fi…
Browse files Browse the repository at this point in the history
…x user reservation page to show empty additional attendee names field when only 1/self reservation.
  • Loading branch information
metaskills committed Sep 28, 2009
1 parent 71baf9c commit 014a103
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
7 changes: 7 additions & 0 deletions app/helpers/rsvps_helper.rb
Expand Up @@ -4,5 +4,12 @@ def render_rsvp_errors
error_messages_for :rsvp, :class => 'flash_bad', :object_name => 'Reservation', :header_message => nil, :message => nil
end

def render_attendee_row(name=nil)
# If this changes, make sure to update JS ReservationForm#addAttendee method.
content_tag :div, :class => 'attendee_name vmiddle_all pb10' do
text_field_tag 'rsvp[attendee_names][]', name, :id => nil
end
end


end
5 changes: 5 additions & 0 deletions app/models/rsvp.rb
Expand Up @@ -20,6 +20,11 @@ def reserved!
update_attribute :reserved, true unless reserved?
end

def attendees=(value)
value = 1 if value.blank? || value.to_i <= 0
self[:attendees] = value
end

def attendee_names
self['attendee_names'].nil? ? default_attendee_names : unserialize_attribute('attendee_names')
end
Expand Down
8 changes: 5 additions & 3 deletions app/views/rsvps/_edit.html.haml
Expand Up @@ -12,9 +12,11 @@
%tr
%th{:class => 'vtop pt10'} Additional Attendee Names:
%td{:id => 'attendee_names'}
- @rsvp.attendee_names.each_with_index do |name,index|
%div{:class => 'attendee_name vmiddle_all pb10'}
= text_field_tag 'rsvp[attendee_names][]', name, :id => nil
- if @rsvp.attendees == 1
= render_attendee_row
- else
- @rsvp.attendee_names.each do |name|
= render_attendee_row(name)
%tr
%td
%td
Expand Down
9 changes: 9 additions & 0 deletions test/unit/rsvp_test.rb
Expand Up @@ -10,6 +10,15 @@ class RsvpTest < ActiveSupport::TestCase
setup do
@rsvp = Rsvp.new(:name => 'Test', :email => 'test@test.com')
end

should 'never allow attendees to be 0' do
@rsvp.attendee_names = []
assert_equal 1, @rsvp.attendees
@rsvp.attendees = 0
assert_equal 1, @rsvp.attendees
@rsvp.attendees = -420
assert_equal 1, @rsvp.attendees
end

should 'not allow mas assignment of default false reserved attribute' do
assert !@rsvp.reserved?
Expand Down

0 comments on commit 014a103

Please sign in to comment.