diff --git a/app/models/conference.rb b/app/models/conference.rb index 37a2107..05f7d18 100644 --- a/app/models/conference.rb +++ b/app/models/conference.rb @@ -9,6 +9,9 @@ class Conference < ActiveRecord::Base has_many :tracks has_many :events, through: :tracks + scope :future, -> { where 'start_date >= ?', Date.today } + scope :current, -> { future.first || last } + private def end_date_is_before_start_date diff --git a/spec/factories/conferences.rb b/spec/factories/conferences.rb index 0cb8c16..457a71d 100644 --- a/spec/factories/conferences.rb +++ b/spec/factories/conferences.rb @@ -7,5 +7,15 @@ FactoryGirl.define do description 'MyText' start_date '2014-07-29 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 diff --git a/spec/models/conference_spec.rb b/spec/models/conference_spec.rb index 2294fd0..ab32526 100644 --- a/spec/models/conference_spec.rb +++ b/spec/models/conference_spec.rb @@ -39,4 +39,18 @@ RSpec.describe Conference, :type => :model 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 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