Introduce CallForParticipation status mutation methods

Add the #open! and #close! methods to CallForParticipation in order to
provide an easy way of setting the opens_at and closes_at times.
This commit is contained in:
Petko Bordjukov 2015-05-23 18:42:00 +03:00
parent 5e15a055e1
commit 463e43226e
3 changed files with 42 additions and 4 deletions

View File

@ -1,5 +1,17 @@
class CallForParticipation < ActiveRecord::Base class CallForParticipation < ActiveRecord::Base
belongs_to :conference belongs_to :conference
def open!
self.opens_at = Time.now unless self.opens_at.present?
self.closes_at = nil
save
end
def close!
self.closes_at = Time.now
save
end
def open? def open?
self.opens_at.present? and self.opens_at < Time.now self.opens_at.present? and self.opens_at < Time.now
end end

View File

@ -1,8 +1,4 @@
FactoryGirl.define do FactoryGirl.define do
factory :call_for_participation do factory :call_for_participation do
conference nil
opens_at "2015-04-20 18:40:43"
closes_at "2015-04-20 18:40:43"
end end
end end

View File

@ -70,4 +70,34 @@ RSpec.describe CallForParticipation, type: :model do
expect(cfp.in_progress?).to be false expect(cfp.in_progress?).to be false
end end
end end
describe '#open!' do
it 'changes the status of the CFP to open' do
expect { cfp.open! }.to change { cfp.open? }.from(false).to(true)
end
it 'does not change the open time of the CFP when it has already been set' do
cfp.opens_at = 10.minutes.ago
expect { cfp.open! }.to_not change { cfp.opens_at }
end
it 'unsets the close time of the CFP' do
cfp.closes_at = 10.minutes.ago
expect { cfp.open! }.to change { cfp.closes_at }.to(nil)
end
it 'saves the record' do
expect { cfp.open! }.to change { cfp.persisted? }.from(false).to(true)
end
end
describe '#close!' do
it 'changes the status of the CFP to closed' do
expect { cfp.close! }.to change { cfp.closed? }.from(false).to(true)
end
it 'saves the record' do
expect { cfp.close! }.to change { cfp.persisted? }.from(false).to(true)
end
end
end end