From b80aff3cce704d9e9893f78b84df256056c9dbce Mon Sep 17 00:00:00 2001 From: Ana Date: Mon, 15 Aug 2016 12:26:47 +0200 Subject: [PATCH 1/6] 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 From 9b9d255e3b6252278acd0bb6f45c091d678fb90d Mon Sep 17 00:00:00 2001 From: Ana Date: Mon, 15 Aug 2016 12:39:39 +0200 Subject: [PATCH 2/6] Add fields in conference edit form for hours Make possible to change the start and end hours. --- app/controllers/admin/conferences_controller.rb | 3 ++- app/views/admin/conferences/edit.html.haml | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/app/controllers/admin/conferences_controller.rb b/app/controllers/admin/conferences_controller.rb index b7063e6e..e4309b7a 100644 --- a/app/controllers/admin/conferences_controller.rb +++ b/app/controllers/admin/conferences_controller.rb @@ -177,7 +177,8 @@ module Admin def conference_params params.require(:conference).permit(:title, :short_title, :description, :timezone, - :start_date, :end_date, :rooms_attributes, :tracks_attributes, + :start_date, :end_date, :start_hour, :end_hour, + :rooms_attributes, :tracks_attributes, :tickets_attributes, :event_types_attributes, :picture, :picture_cache, :questions_attributes, :question_ids, :answers_attributes, :answer_ids, :difficulty_levels_attributes, diff --git a/app/views/admin/conferences/edit.html.haml b/app/views/admin/conferences/edit.html.haml index dfc6d72a..8ba8f80e 100644 --- a/app/views/admin/conferences/edit.html.haml +++ b/app/views/admin/conferences/edit.html.haml @@ -21,6 +21,8 @@ = f.input :timezone, :as => :time_zone, :hint => "The conference time zone" = f.input :start_date, :as => :string, :input_html => { :id => "conference-start-datepicker", :readonly => "readonly" } = f.input :end_date, :as => :string, :input_html => { :id => "conference-end-datepicker", :readonly => "readonly" } + = f.input :start_hour, :input_html => {size: 2, type: 'number', min: 0, max: 23} + = f.input :end_hour, :input_html => {size: 2, type: 'number', min: 1, max: 24} = f.inputs name: "Registrations" do = f.input :registration_limit, as: :number, in: 0..9999, hint: "Limit the number of registrations to the conference (0 no limit)" = f.action :submit, :as => :button, :button_html => {:class => "btn btn-primary"} From 7eb9a57c342bfe39e042872509288a3dd6368161 Mon Sep 17 00:00:00 2001 From: Ana Date: Mon, 15 Aug 2016 13:05:35 +0200 Subject: [PATCH 3/6] Adapt admin schedule to use the confence hours --- app/views/admin/schedules/_day_tab.html.haml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/admin/schedules/_day_tab.html.haml b/app/views/admin/schedules/_day_tab.html.haml index 57be4999..61f6e862 100644 --- a/app/views/admin/schedules/_day_tab.html.haml +++ b/app/views/admin/schedules/_day_tab.html.haml @@ -9,7 +9,7 @@ .room-name - room_date_event_schedules = date_event_schedules.select{ |e| e.room == room } = room.name - - (9*cells_per_hour..18*cells_per_hour).each do |slot| + - (@conference.start_hour * cells_per_hour..@conference.end_hour * cells_per_hour).each do |slot| - hour = slot / cells_per_hour - minutes = (EventType::LENGTH_STEP * (slot % cells_per_hour)).to_s.rjust(2, '0') - time = "#{hour}:#{minutes}" From b3bbaf87a419894e74df94639d7b0e83dac8983f Mon Sep 17 00:00:00 2001 From: Ana Date: Mon, 15 Aug 2016 13:11:33 +0200 Subject: [PATCH 4/6] Adapt public schedule to use the confence hours --- app/controllers/schedules_controller.rb | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/app/controllers/schedules_controller.rb b/app/controllers/schedules_controller.rb index 91559aa1..0b135328 100644 --- a/app/controllers/schedules_controller.rb +++ b/app/controllers/schedules_controller.rb @@ -14,16 +14,15 @@ class SchedulesController < ApplicationController @events_xml = schedules.map(&:event).group_by{ |event| event.time.to_date } if schedules @dates = @conference.start_date..@conference.end_date @step_minutes = EventType::LENGTH_STEP.minutes - @conf_start = 9 - conf_end = 20 - @conf_period = conf_end - @conf_start + @conf_start = @conference.start_hour + @conf_period = @conference.end_hour - @conf_start # the schedule takes you to today if it is a date of the schedule @current_day = @conference.current_conference_day @day = @current_day.present? ? @current_day : @dates.first return unless @current_day # the schedule takes you to the current time if it is beetween the start and the end time. - @hour_column = @conference.hours_from_start_time(@conf_start, conf_end) + @hour_column = @conference.hours_from_start_time(@conf_start, @conference.end_hour) end def events From bde2ee3d7d6b1f4c0d4cb66732a575c2a99f0e4c Mon Sep 17 00:00:00 2001 From: Ana Date: Mon, 15 Aug 2016 20:31:23 +0200 Subject: [PATCH 5/6] Delete Eventschedules out of conference hours --- app/controllers/admin/conferences_controller.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/app/controllers/admin/conferences_controller.rb b/app/controllers/admin/conferences_controller.rb index e4309b7a..84f92838 100644 --- a/app/controllers/admin/conferences_controller.rb +++ b/app/controllers/admin/conferences_controller.rb @@ -82,6 +82,7 @@ module Admin short_title = @conference.short_title @conference.assign_attributes(conference_params) send_mail_on_conf_update = @conference.notify_on_dates_changed? + delete_event_schedules if @conference.start_hour_changed? || @conference.end_hour_changed? if @conference.update_attributes(conference_params) ConferenceDateUpdateMailJob.perform_later(@conference) if send_mail_on_conf_update @@ -188,5 +189,14 @@ module Admin :targets, :targets_attributes, :campaigns, :campaigns_attributes, :registration_limit) end + + def delete_event_schedules + event_schedules = EventSchedule.select do |e| + e.start_time.strftime('%H').to_i < @conference.start_hour || + e.end_time.strftime('%H').to_i > @conference.end_hour || + (e.end_time.strftime('%H').to_i == @conference.end_hour && e.end_time.strftime('%M').to_i > 0) + end + event_schedules.each(&:destroy) + end end end From ebb52038875e4d094cdc5fdaea339b954ec9f4f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ana=20Mar=C3=ADa=20Mart=C3=ADnez=20G=C3=B3mez?= Date: Wed, 21 Dec 2016 11:42:19 +0100 Subject: [PATCH 6/6] Exclude conference model from ClassLength cop Exclude conference.rb file from ClassLength Rubocop cop and decrease the max value of this cop to 300, as the following class in number of lines is event.rb with 218. Also, remove unused comments from Conference model class. --- .rubocop.yml | 4 +++- app/models/conference.rb | 4 ---- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 497d45e8..6500f653 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -95,7 +95,9 @@ Metrics/BlockNesting: Max: 4 Metrics/ClassLength: - Max: 575 + Max: 300 + Exclude: + - 'app/models/conference.rb' # avoid redundunt curly braces when it is obvious that hash is used Style/BracesAroundHashParameters: diff --git a/app/models/conference.rb b/app/models/conference.rb index c088feef..b5e01a09 100644 --- a/app/models/conference.rb +++ b/app/models/conference.rb @@ -1,6 +1,3 @@ -# rubocop:disable Metrics/ClassLength -## -# This class represents a conference class Conference < ActiveRecord::Base require 'uri' serialize :events_per_week, Hash @@ -1081,4 +1078,3 @@ class Conference < ActiveRecord::Base result end end -# rubocop:enable Metrics/ClassLength