diff --git a/app/models/conference.rb b/app/models/conference.rb index f1a4fa5..6ec49ee 100644 --- a/app/models/conference.rb +++ b/app/models/conference.rb @@ -8,21 +8,13 @@ class Conference < ActiveRecord::Base translates :title, :description - has_many :tracks, -> { order('id asc') }, dependent: :destroy - has_many :events, through: :tracks - has_many :candidate_speakers, through: :events - has_many :halls, dependent: :destroy + has_many :tracks + has_many :halls accepts_nested_attributes_for :tracks, :halls, reject_if: :all_blank, allow_destroy: true - before_save :close_all_other_calls_for_papers - private - def close_all_other_calls_for_papers - Conference.where.not(id: self.id).update_all call_for_papers_open: false - end - def start_date_is_before_end_date if start_date.present? and end_date.present? and start_date > end_date errors.add(:end_date, :cannot_be_before_start_date) diff --git a/spec/factories/halls.rb b/spec/factories/halls.rb new file mode 100644 index 0000000..0e97cd3 --- /dev/null +++ b/spec/factories/halls.rb @@ -0,0 +1,8 @@ +# Read about factories at https://github.com/thoughtbot/factory_girl + +FactoryGirl.define do + factory :hall do + name { |n| "Hall#{n}" } + conference + end +end diff --git a/spec/factories/tracks.rb b/spec/factories/tracks.rb index 156e920..c6b79d5 100644 --- a/spec/factories/tracks.rb +++ b/spec/factories/tracks.rb @@ -4,6 +4,7 @@ FactoryGirl.define do factory :track do name { |n| "Track#{n}" } color '#000000' + description 'Some description' conference end end diff --git a/spec/models/conference_spec.rb b/spec/models/conference_spec.rb index d7ec8d7..d615b52 100644 --- a/spec/models/conference_spec.rb +++ b/spec/models/conference_spec.rb @@ -50,4 +50,70 @@ 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 'tracks association' do + let(:conference) { build :conference } + let(:track) { build :track } + + before do + conference.save + track.conference = conference + track.save + end + + it 'links the conference to its tracks' do + expect(conference.tracks).to include track + end + end + + describe 'halls association' do + let(:conference) { build :conference } + let(:hall) { build :hall } + + before do + conference.save + hall.conference = conference + hall.save + end + + it 'has many associated halls' do + expect(conference.halls).to include hall + end + end + + it 'accepts nested attributes for tracks' do + track_attributes = build(:track, conference: nil).attributes + conference = create :conference + expect { conference.update tracks_attributes: [track_attributes]}.to change { conference.tracks.count }.by 1 + end + + it 'rejects nested attributes for tracks when they are all blank' do + conference = create :conference + expect { conference.update tracks_attributes: [{'name' => '', 'description' => ''}]}.to_not change { conference.tracks.count } + end + + it 'allows destruction of tracks by nested attributes' do + conference = create :conference + track_attributes = create(:track, conference: conference).attributes + track_attributes['_destroy'] = 1 + expect { conference.update tracks_attributes: [track_attributes]}.to change { conference.tracks.count }.by(-1) + end + + it 'accepts nested attributes for halls' do + hall_attributes = build(:hall, conference: nil).attributes + conference = create :conference + expect { conference.update halls_attributes: [hall_attributes]}.to change { conference.halls.count }.by 1 + end + + it 'rejects nested attributes for halls when they are all blank' do + conference = create :conference + expect { conference.update halls_attributes: [{'name' => '', 'description' => ''}]}.to_not change { conference.halls.count } + end + + it 'allows destruction of halls by nested attributes' do + conference = create :conference + hall_attributes = create(:hall, conference: conference).attributes + hall_attributes['_destroy'] = 1 + expect { conference.update halls_attributes: [hall_attributes]}.to change { conference.halls.count }.by(-1) + end end