Add current and future scopes for Conference

This commit is contained in:
Petko Bordjukov 2014-08-10 22:05:25 +03:00
parent 89de56d94d
commit fa63c14e47
3 changed files with 27 additions and 0 deletions

View File

@ -9,6 +9,9 @@ class Conference < ActiveRecord::Base
has_many :tracks has_many :tracks
has_many :events, through: :tracks has_many :events, through: :tracks
scope :future, -> { where 'start_date >= ?', Date.today }
scope :current, -> { future.first || last }
private private
def end_date_is_before_start_date def end_date_is_before_start_date

View File

@ -7,5 +7,15 @@ FactoryGirl.define do
description 'MyText' description 'MyText'
start_date '2014-07-29 21:29:13' start_date '2014-07-29 21:29:13'
end_date '2014-07-31 21:29:13' end_date '2014-07-31 21:29:13'
factory :past_conference do
start_date Date.today - 10.days
end_date Date.today - 5.days
end
factory :future_conference do
start_date Date.today + 5.days
end_date Date.today + 10.days
end
end end
end end

View File

@ -39,4 +39,18 @@ RSpec.describe Conference, :type => :model do
it 'is invalid when the end date is before the start date' do it 'is invalid when the end date is before the start date' do
expect(build(:conference, start_date: '2014-07-29 21:29:13', end_date: '2014-07-28 01:00:00')).to have_error_on :end_date expect(build(:conference, start_date: '2014-07-29 21:29:13', end_date: '2014-07-28 01:00:00')).to have_error_on :end_date
end end
describe '::current' do
it 'returns the next conference if there is one' do
create :past_conference
conference = create :future_conference
create :future_conference, start_date: Date.today + 100.years, end_date: Date.today + 150.years
expect(Conference.current).to eq conference
end
it 'returns the last conference if there is no future conference' do
conference = create :past_conference
expect(Conference.current).to eq conference
end
end
end end