From 6330940a344aeb489c803cd2b249819b66bb1067 Mon Sep 17 00:00:00 2001 From: Petko Bordjukov Date: Tue, 4 Nov 2014 21:16:06 +0200 Subject: [PATCH] Add tests for translatable Conference attributes Test if the title and the description attributes of the Conference model are translatable. Also introduce a custom matcher for translatable attributes as there will be many. Fix a typo in the process. --- app/models/conference.rb | 28 +++------------------- spec/models/conference_spec.rb | 29 ++++++++++------------- spec/support/matchers/has_translatable.rb | 14 +++++++++++ 3 files changed, 30 insertions(+), 41 deletions(-) create mode 100644 spec/support/matchers/has_translatable.rb diff --git a/app/models/conference.rb b/app/models/conference.rb index 58cc590..f1a4fa5 100644 --- a/app/models/conference.rb +++ b/app/models/conference.rb @@ -4,7 +4,7 @@ class Conference < ActiveRecord::Base validates :description, presence: true validates :start_date, presence: true validates :end_date, presence: true - validate :end_date_is_before_start_date + validate :start_date_is_before_end_date translates :title, :description @@ -15,37 +15,15 @@ class Conference < ActiveRecord::Base accepts_nested_attributes_for :tracks, :halls, reject_if: :all_blank, allow_destroy: true - scope :future, -> { where('start_date >= ?', Date.today).order('start_date ASC') } - before_save :close_all_other_calls_for_papers - def self.current - future.first || last - end - - def submissions_grouped_by_day - submissions = Event.where(track: tracks, created_at: created_at..start_date) - submissions = submissions.group('date(created_at)') - submissions = submissions.select('created_at, count(id) as number') - submissions.group_by { |s| s.created_at.to_date } - end - - def submissions_grouped_by_confirmation_day - submissions = Event.approved.where(track: tracks, created_at: created_at..start_date).where.not(confirmed_at: nil) - submissions = submissions.group('date(confirmed_at)') - submissions = submissions.select('confirmed_at, count(id) as number') - submissions.group_by { |s| s.confirmed_at.to_date } - end - private def close_all_other_calls_for_papers - if call_for_papers_open? and call_for_papers_open_changed? - Conference.where.not(id: self.id).update_all call_for_papers_open: false - end + Conference.where.not(id: self.id).update_all call_for_papers_open: false end - def end_date_is_before_start_date + 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) end diff --git a/spec/models/conference_spec.rb b/spec/models/conference_spec.rb index ab32526..d7ec8d7 100644 --- a/spec/models/conference_spec.rb +++ b/spec/models/conference_spec.rb @@ -10,6 +10,11 @@ RSpec.describe Conference, :type => :model do create :conference, title: 'ExampleConf' expect(build(:conference, title: 'ExampleConf')).to have_error_on :title end + + it 'must be translatable' do + conference = build(:conference) + expect(conference).to have_translatable :title + end end describe 'email' do @@ -24,8 +29,14 @@ RSpec.describe Conference, :type => :model do end end - it 'is invalid without a description' do - expect(build(:conference, description: '')).to have_error_on :description + describe 'description' do + it 'must be present' do + expect(build(:conference, description: '')).to have_error_on :description + end + + it 'must be translatable' do + expect(build(:conference)).to have_translatable :description + end end it 'is invalid without a start date' do @@ -39,18 +50,4 @@ 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 diff --git a/spec/support/matchers/has_translatable.rb b/spec/support/matchers/has_translatable.rb new file mode 100644 index 0000000..cef584d --- /dev/null +++ b/spec/support/matchers/has_translatable.rb @@ -0,0 +1,14 @@ +RSpec::Matchers.define :have_translatable do |expected| + match do |actual| + I18n.locale = :bg + actual.send "#{expected.to_s}=", 'Test Bulgarian' + + I18n.locale = :en + actual.send "#{expected.to_s}=", 'Test English' + + expect(actual.send expected).to eq 'Test English' + + I18n.locale = :bg + expect(actual.send expected).to eq 'Test Bulgarian' + end +end