From b80aff3cce704d9e9893f78b84df256056c9dbce Mon Sep 17 00:00:00 2001 From: Ana Date: Mon, 15 Aug 2016 12:26:47 +0200 Subject: [PATCH] Add times to Conference Add start hour and end hour to conference to be able to make the schedule times changeable --- app/models/conference.rb | 18 ++++++++++++++- ...20160815094215_add_times_to_conferences.rb | 6 +++++ db/schema.rb | 2 ++ spec/factories/conferences.rb | 2 ++ spec/models/conference_spec.rb | 23 +++++++++++++++++++ 5 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20160815094215_add_times_to_conferences.rb diff --git a/app/models/conference.rb b/app/models/conference.rb index a54f4e22..c088feef 100644 --- a/app/models/conference.rb +++ b/app/models/conference.rb @@ -54,7 +54,9 @@ class Conference < ActiveRecord::Base validates_presence_of :title, :short_title, :start_date, - :end_date + :end_date, + :start_hour, + :end_hour validates_uniqueness_of :short_title validates_format_of :short_title, with: /\A[a-zA-Z0-9_-]*\z/ @@ -62,6 +64,7 @@ class Conference < ActiveRecord::Base # This validation is needed since a conference with a start date greater than the end date is not possible validate :valid_date_range? + validate :valid_times_range? before_create :generate_guid before_create :add_color before_create :create_email_settings @@ -678,6 +681,19 @@ class Conference < ActiveRecord::Base errors.add(:start_date, 'Start date is greater than End date') if start_date && end_date && start_date > end_date end + ## + # Checks if start hour of the conference is greater or equal than the end hour + # and that both hours are beetween 0 and 24 + # + # Reports an error when such a condition is found + def valid_times_range? + if start_hour && end_hour + errors.add(:start_hour, 'is lower than 0') if start_hour < 0 + errors.add(:end_hour, 'is lower or equal than start hour') if end_hour <= start_hour + errors.add(:end_hour, 'is greater than 24') if end_hour > 24 + end + end + ## # Calculates the weeks from a start and a end week. # diff --git a/db/migrate/20160815094215_add_times_to_conferences.rb b/db/migrate/20160815094215_add_times_to_conferences.rb new file mode 100644 index 00000000..758ed841 --- /dev/null +++ b/db/migrate/20160815094215_add_times_to_conferences.rb @@ -0,0 +1,6 @@ +class AddTimesToConferences < ActiveRecord::Migration + def change + add_column :conferences, :start_hour, :integer, default: 9 + add_column :conferences, :end_hour, :integer, default: 20 + end +end diff --git a/db/schema.rb b/db/schema.rb index d0d4de72..34c30a04 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -98,6 +98,8 @@ ActiveRecord::Schema.define(version: 20170213145807) do t.text "description" t.integer "registration_limit", default: 0 t.string "picture" + t.integer "start_hour", default: 9 + t.integer "end_hour", default: 20 end create_table "conferences_questions", id: false, force: :cascade do |t| diff --git a/spec/factories/conferences.rb b/spec/factories/conferences.rb index d92a7e88..8a9186a6 100644 --- a/spec/factories/conferences.rb +++ b/spec/factories/conferences.rb @@ -7,6 +7,8 @@ FactoryGirl.define do timezone { Faker::Address.time_zone } start_date { Date.today } end_date { 6.days.from_now } + start_hour 9 + end_hour 20 registration_limit 0 description { Faker::Hipster.paragraph } diff --git a/spec/models/conference_spec.rb b/spec/models/conference_spec.rb index 8520a978..aa824319 100755 --- a/spec/models/conference_spec.rb +++ b/spec/models/conference_spec.rb @@ -1545,6 +1545,14 @@ describe Conference do should validate_presence_of(:end_date) end + it 'is not valid without a start date' do + should validate_presence_of(:start_hour) + end + + it 'is not valid without an end date' do + should validate_presence_of(:end_hour) + end + it 'is not valid with a duplicate short title' do should validate_uniqueness_of(:short_title) end @@ -1572,6 +1580,21 @@ describe Conference do end end + describe 'valid_times_range?' do + + it 'is not valid if start hour is lower than 0' do + expect(subject.start_hour).to be >= 0 + end + + it 'is not valid if end hour is lower or equal than start hour' do + expect(subject.start_hour).to be < subject.end_hour + end + + it 'is not valid if end hour is greater than 24' do + expect(subject.end_hour).to be <= 24 + end + end + describe 'before create callbacks' do it 'has an email setting after creation' do