Add times to Conference

Add start hour and end hour to conference to be able to make the schedule times changeable
This commit is contained in:
Ana 2016-08-15 12:26:47 +02:00 committed by Ana María Martínez Gómez
parent 7000e63700
commit b80aff3cce
5 changed files with 50 additions and 1 deletions

View file

@ -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.
#

View file

@ -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

View file

@ -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|

View file

@ -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 }

View file

@ -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