From 528243c8b9b3d2100bb8e35585032c4f5db91377 Mon Sep 17 00:00:00 2001 From: Stella Rouzi Date: Sun, 15 May 2016 14:01:30 +0300 Subject: [PATCH] Allow max_attendees to be nil --- Gemfile.lock | 3 -- app/models/event.rb | 12 +----- app/models/program.rb | 3 +- .../admin/events/registrations.html.haml | 6 ++- app/views/proposal/registrations.html.haml | 6 ++- spec/controllers/proposal_controller_spec.rb | 18 ++++++++ spec/models/event_spec.rb | 43 ++++++++----------- 7 files changed, 50 insertions(+), 41 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index ef102920..113fefa5 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -615,6 +615,3 @@ DEPENDENCIES web-console (~> 2.0) webmock whenever - -BUNDLED WITH - 1.11.2 diff --git a/app/models/event.rb b/app/models/event.rb index 3e0c9d13..1e118095 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -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 diff --git a/app/models/program.rb b/app/models/program.rb index e336be21..36df5829 100644 --- a/app/models/program.rb +++ b/app/models/program.rb @@ -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 diff --git a/app/views/admin/events/registrations.html.haml b/app/views/admin/events/registrations.html.haml index bbc83f55..21042349 100644 --- a/app/views/admin/events/registrations.html.haml +++ b/app/views/admin/events/registrations.html.haml @@ -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 diff --git a/app/views/proposal/registrations.html.haml b/app/views/proposal/registrations.html.haml index b7e88778..e46798ec 100644 --- a/app/views/proposal/registrations.html.haml +++ b/app/views/proposal/registrations.html.haml @@ -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 diff --git a/spec/controllers/proposal_controller_spec.rb b/spec/controllers/proposal_controller_spec.rb index f9b350ae..56a8c6ee 100644 --- a/spec/controllers/proposal_controller_spec.rb +++ b/spec/controllers/proposal_controller_spec.rb @@ -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 } diff --git a/spec/models/event_spec.rb b/spec/models/event_spec.rb index 6bbb416c..2f699d86 100644 --- a/spec/models/event_spec.rb +++ b/spec/models/event_spec.rb @@ -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