Change Cfp scopes to class methods

When no record matches the requested criteria a scope will return all
the records
In this case, if no record can be found we want the result to be nil

Also, make some readability fixes in specs
This commit is contained in:
AEtherC0r3 2017-07-16 20:29:03 +03:00 committed by Stella Rouzi
parent d9faffc96a
commit f9903eba16
5 changed files with 59 additions and 30 deletions

View file

@ -3,9 +3,6 @@
class Cfp < ActiveRecord::Base
TYPES = %w(events booths tracks).freeze
scope :for_events, (-> { find_by(cfp_type: 'events') })
scope :for_tracks, (-> { find_by(cfp_type: 'tracks') })
has_paper_trail ignore: [:updated_at], meta: { conference_id: :conference_id }
belongs_to :program
@ -79,6 +76,24 @@ class Cfp < ActiveRecord::Base
(start_date..end_date).cover?(Date.current)
end
##
# Finds the cfp for events if it exists
#
# ====Returns
# * +Cfp+ -> The cfp with type 'events'
def self.for_events
find_by(cfp_type: 'events')
end
##
# Finds the cfp for tracks if it exists
#
# ====Returns
# * +Cfp+ -> The cfp with type 'tracks'
def self.for_tracks
find_by(cfp_type: 'tracks')
end
private
def before_end_of_conference

View file

@ -73,7 +73,7 @@ feature 'Has correct abilities' do
expect(current_path).to eq(edit_admin_conference_program_cfp_path(conference.short_title, conference.program.cfp))
# Event, booth, track cfps exist
cft = create(:cfp, cfp_type: 'tracks', program: conference.program)
call_for_tracks = create(:cfp, cfp_type: 'tracks', program: conference.program)
visit new_admin_conference_program_cfp_path(conference.short_title)
expect(current_path).to eq root_path
@ -83,7 +83,7 @@ feature 'Has correct abilities' do
expect(current_path).to eq new_admin_conference_program_cfp_path(conference.short_title)
# Only booth exists
cft.destroy!
call_for_tracks.destroy!
visit new_admin_conference_program_cfp_path(conference.short_title)
expect(current_path).to eq(new_admin_conference_program_cfp_path(conference.short_title))
@ -96,19 +96,19 @@ feature 'Has correct abilities' do
expect(current_path).to eq(new_admin_conference_program_cfp_path(conference.short_title))
# Only Tracks cfp exists
cft = create(:cfp, cfp_type: 'tracks', program: conference.program)
call_for_tracks = create(:cfp, cfp_type: 'tracks', program: conference.program)
visit new_admin_conference_program_cfp_path(conference.short_title)
expect(current_path).to eq new_admin_conference_program_cfp_path(conference.short_title)
visit edit_admin_conference_program_cfp_path(conference.short_title, cft)
expect(current_path).to eq edit_admin_conference_program_cfp_path(conference.short_title, cft)
visit edit_admin_conference_program_cfp_path(conference.short_title, call_for_tracks)
expect(current_path).to eq edit_admin_conference_program_cfp_path(conference.short_title, call_for_tracks)
# Event and track cfps exist
create(:cfp, cfp_type: 'events', program: conference.program)
visit new_admin_conference_program_cfp_path(conference.short_title)
expect(current_path).to eq new_admin_conference_program_cfp_path(conference.short_title)
cft.destroy!
call_for_tracks.destroy!
create(:event, program: conference.program)
visit edit_admin_conference_program_event_path(conference.short_title, conference.program.events.first)
expect(current_path).to eq(edit_admin_conference_program_event_path(conference.short_title, conference.program.events.first))

View file

@ -115,7 +115,7 @@ feature 'Has correct abilities' do
expect(current_path).to eq(edit_admin_conference_program_cfp_path(conference.short_title, conference.program.cfp))
# Event, booth, track cfps exist
cft = create(:cfp, cfp_type: 'tracks', program: conference.program)
call_for_tracks = create(:cfp, cfp_type: 'tracks', program: conference.program)
visit new_admin_conference_program_cfp_path(conference.short_title)
expect(current_path).to eq root_path
@ -125,7 +125,7 @@ feature 'Has correct abilities' do
expect(current_path).to eq new_admin_conference_program_cfp_path(conference.short_title)
# Only booth exists
cft.destroy!
call_for_tracks.destroy!
visit new_admin_conference_program_cfp_path(conference.short_title)
expect(current_path).to eq(new_admin_conference_program_cfp_path(conference.short_title))
@ -138,19 +138,19 @@ feature 'Has correct abilities' do
expect(current_path).to eq(new_admin_conference_program_cfp_path(conference.short_title))
# Only Tracks cfp exists
cft = create(:cfp, cfp_type: 'tracks', program: conference.program)
call_for_tracks = create(:cfp, cfp_type: 'tracks', program: conference.program)
visit new_admin_conference_program_cfp_path(conference.short_title)
expect(current_path).to eq new_admin_conference_program_cfp_path(conference.short_title)
visit edit_admin_conference_program_cfp_path(conference.short_title, cft)
expect(current_path).to eq edit_admin_conference_program_cfp_path(conference.short_title, cft)
visit edit_admin_conference_program_cfp_path(conference.short_title, call_for_tracks)
expect(current_path).to eq edit_admin_conference_program_cfp_path(conference.short_title, call_for_tracks)
# Event and track cfps exist
create(:cfp, cfp_type: 'events', program: conference.program)
visit new_admin_conference_program_cfp_path(conference.short_title)
expect(current_path).to eq new_admin_conference_program_cfp_path(conference.short_title)
cft.destroy!
call_for_tracks.destroy!
visit admin_conference_program_events_path(conference.short_title)
expect(current_path).to eq(admin_conference_program_events_path(conference.short_title))

View file

@ -121,7 +121,7 @@ feature 'Has correct abilities' do
expect(current_path).to eq(edit_admin_conference_program_cfp_path(conference.short_title, conference.program.cfp))
# Event, booth, track cfps exist
cft = create(:cfp, cfp_type: 'tracks', program: conference.program)
call_for_tracks = create(:cfp, cfp_type: 'tracks', program: conference.program)
visit new_admin_conference_program_cfp_path(conference.short_title)
expect(current_path).to eq root_path
@ -131,7 +131,7 @@ feature 'Has correct abilities' do
expect(current_path).to eq new_admin_conference_program_cfp_path(conference.short_title)
# Only booth exists
cft.destroy!
call_for_tracks.destroy!
visit new_admin_conference_program_cfp_path(conference.short_title)
expect(current_path).to eq(new_admin_conference_program_cfp_path(conference.short_title))
@ -144,19 +144,19 @@ feature 'Has correct abilities' do
expect(current_path).to eq(new_admin_conference_program_cfp_path(conference.short_title))
# Only Tracks cfp exists
cft = create(:cfp, cfp_type: 'tracks', program: conference.program)
call_for_tracks = create(:cfp, cfp_type: 'tracks', program: conference.program)
visit new_admin_conference_program_cfp_path(conference.short_title)
expect(current_path).to eq new_admin_conference_program_cfp_path(conference.short_title)
visit edit_admin_conference_program_cfp_path(conference.short_title, cft)
expect(current_path).to eq edit_admin_conference_program_cfp_path(conference.short_title, cft)
visit edit_admin_conference_program_cfp_path(conference.short_title, call_for_tracks)
expect(current_path).to eq edit_admin_conference_program_cfp_path(conference.short_title, call_for_tracks)
# Event and track cfps exist
create(:cfp, cfp_type: 'events', program: conference.program)
visit new_admin_conference_program_cfp_path(conference.short_title)
expect(current_path).to eq new_admin_conference_program_cfp_path(conference.short_title)
cft.destroy!
call_for_tracks.destroy!
visit admin_conference_program_events_path(conference.short_title)
expect(current_path).to eq(admin_conference_program_events_path(conference.short_title))

View file

@ -5,21 +5,35 @@ describe Cfp do
let!(:conference) { create(:conference, end_date: Date.today) }
let!(:cfp) { create(:cfp, start_date: Date.today - 2, end_date: Date.today - 1, program_id: conference.program.id) }
describe 'scope' do
describe '#for_events' do
it 'returns the cfp for events' do
expect(conference.program.cfps.for_events).to be_a Cfp
expect(conference.program.cfps.for_events.cfp_type).to eq('events')
end
end
end
describe 'validations' do
it { is_expected.to validate_presence_of(:cfp_type) }
it { is_expected.to validate_inclusion_of(:cfp_type).in_array(Cfp::TYPES) }
it { is_expected.to validate_uniqueness_of(:cfp_type).scoped_to(:program_id).case_insensitive }
end
describe '.for_events' do
it 'returns the cfp for events when it exists' do
expect(conference.program.cfps.for_events).to be_a Cfp
expect(conference.program.cfps.for_events.cfp_type).to eq('events')
end
it 'returns nil when the cfp for events doesn\'t exist' do
conference.program.cfp.destroy
expect(conference.program.cfps.for_events).to eq nil
end
end
describe '.for_tracks' do
it 'returns the cfp for tracks when it exists' do
call_for_tracks = create(:cfp, cfp_type: 'tracks', program: conference.program, end_date: Date.today)
expect(conference.program.cfps.for_tracks).to eq call_for_tracks
end
it 'returns nil when the cfp for tracks doesn\'t exist' do
expect(conference.program.cfps.for_tracks).to eq nil
end
end
describe '#before_end_of_conference' do
describe 'fails to save cfp' do
it 'when cfp end_date is after conference end_date' do