Allow max_attendees to be nil

This commit is contained in:
Stella Rouzi 2016-05-15 14:01:30 +03:00
parent 65ee166570
commit 528243c8b9
7 changed files with 50 additions and 41 deletions

View file

@ -615,6 +615,3 @@ DEPENDENCIES
web-console (~> 2.0)
webmock
whenever
BUNDLED WITH
1.11.2

View file

@ -35,7 +35,6 @@ class Event < ActiveRecord::Base
validates :program, presence: true
validates :max_attendees, numericality: { only_integer: true, greater_than_or_equal_to: 1, allow_nil: true }
validate :max_attendees_and_require_registration
validate :max_attendees_no_more_than_room_size
scope :confirmed, -> { where(state: 'confirmed') }
@ -78,7 +77,8 @@ class Event < ActiveRecord::Base
end
def registration_possible?
return false unless max_attendees
return false unless require_registration && state == 'confirmed'
return true if max_attendees.nil?
registrations.count < max_attendees
end
@ -225,14 +225,6 @@ class Event < ActiveRecord::Base
private
##
# If max_attendees variable is set (higher than 0)
# variable require_registration must also be set
def max_attendees_and_require_registration
errors.add(:require_registration, 'must be enabled, when you set max_attendees') if max_attendees && !require_registration
errors.add(:max_attendees, 'must be enabled, when you set require_registration') if require_registration && max_attendees.nil?
end
##
# Do not allow, for the event, more attendees than the size of the room
def max_attendees_no_more_than_room_size

View file

@ -13,8 +13,7 @@ class Program < ActiveRecord::Base
end
def with_registration_open
where(require_registration: true, state: :confirmed).
map { |e| e if e.max_attendees > e.registrations.count }.compact
select { |e| e if e.registration_possible? }
end
# All confirmed events of the conference with attribute require_registration

View file

@ -2,7 +2,11 @@
.col-md-12
.page-header
%h1
Registrations (#{@event_registrations.length}/#{@event.max_attendees})
Registrations
- if @event.max_attendees
(#{@event.events_registrations.length}/#{@event.max_attendees})
- else
(#{@event.events_registrations.length})
.text-muted
for
= @event.title

View file

@ -3,7 +3,11 @@
.col-md-10.col-md-offset-1
.page-header
%h1
Registrations (#{@event.events_registrations.length}/#{@event.max_attendees})
Registrations
- if @event.max_attendees
(#{@event.events_registrations.length}/#{@event.max_attendees})
- else
(#{@event.events_registrations.length})
.text-muted
for
= @event.title

View file

@ -416,6 +416,24 @@ describe ProposalController do
before { event.update_attributes(state: 'unconfirmed') }
context 'confirmed successfully' do
describe 'when require_registration is set' do
before :each do
event.require_registration = true
event.max_attendees = nil
event.save!
patch :confirm, conference_id: conference.short_title, id: event.id
end
it 'assigns url variable' do
expect(assigns(:url)).to eq '/conference/lama101/program/proposal/1'
end
it 'change state of event to confirmed' do
event.reload
expect(event.confirmed?).to be true
end
end
describe 'general actions' do
before { patch :confirm, conference_id: conference.short_title, id: event.id }

View file

@ -24,30 +24,6 @@ describe Event do
it { is_expected.to validate_presence_of(:program) }
it { is_expected.to validate_presence_of(:event_type) }
describe '#max_attendees_and_require_registration' do
it 'allows user to set max_attendees, only if require_registration is set' do
event.require_registration = true
event.max_attendees = 2
expect(event.valid?).to eq true
end
it 'does not allow max_attendees to be set without require_registration' do
event.max_attendees = 2
expect(event.valid?).to eq false
expect(event.errors[:require_registration]).to eq ['must be enabled, when you set max_attendees']
end
it 'does not allow require_registration to be set without max_attendees' do
event.require_registration = true
event.max_attendees = nil
expect(event.valid?).to eq false
expect(event.errors[:max_attendees]).to eq ['must be enabled, when you set require_registration']
end
end
describe 'max_attendees_no_more_than_room_size' do
before :each do
event.room = create(:room, size: 3)
@ -148,20 +124,39 @@ describe Event do
describe '#registration_possible?' do
describe 'when the event requires registration' do
before :each do
event.state = 'confirmed'
event.require_registration = true
event.max_attendees = 3
event.registrations << create(:registration)
event.save!
end
it 'returns true, if the event has no max_attendees' do
event.max_attendees = nil
event.save!
expect(event.registration_possible?).to eq true
end
it 'returns true, if the limit has not been reached' do
expect(event.registration_possible?).to eq true
end
it 'returns true, if the event is confirmed' do
event.save!
expect(event.registration_possible?).to eq true
end
it 'returns false, if the limit has been reached' do
event.registrations << create(:registration)
event.registrations << create(:registration)
expect(event.registration_possible?).to eq false
end
it 'returns false, if the event is not confirmed' do
event.state = 'new'
event.save!
expect(event.registration_possible?).to eq false
end
end
describe 'when the event does not require registration' do