From ba4c39ca9995569b5c53f5534e367ec5e08eb76f Mon Sep 17 00:00:00 2001 From: Sylvain Date: Wed, 20 May 2020 17:36:57 +0200 Subject: [PATCH] use slot duration from the db setting --- app/controllers/api/slots_controller.rb | 2 +- app/helpers/application_helper.rb | 3 --- app/models/availability.rb | 4 ++-- app/models/slot.rb | 2 +- app/services/availabilities/availabilities_service.rb | 4 ++-- app/services/availabilities/public_availabilities_service.rb | 4 ++-- app/views/application/index.html.erb | 2 +- app/views/exports/availabilities_index.xlsx.axlsx | 4 ++-- 8 files changed, 11 insertions(+), 14 deletions(-) diff --git a/app/controllers/api/slots_controller.rb b/app/controllers/api/slots_controller.rb index f3d4a52ea..e901efb2a 100644 --- a/app/controllers/api/slots_controller.rb +++ b/app/controllers/api/slots_controller.rb @@ -2,7 +2,7 @@ # API Controller for resources of type Slot # Slots are used to cut Availabilities into reservable slots. The duration of these slots is configured per -# availability by Availability.slot_duration, or otherwise globally by ApplicationHelper::SLOT_DURATION minutes +# availability by Availability.slot_duration, or otherwise globally by Setting.get('slot_duration') class API::SlotsController < API::ApiController before_action :authenticate_user! before_action :set_slot, only: %i[update cancel] diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 55735c7d2..f42c7661a 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -4,9 +4,6 @@ module ApplicationHelper require 'message_format' - ## machine/spaces availabilities are divided in multiple slots of 60 minutes - SLOT_DURATION ||= Setting.get('slot_duration') || 60 - ## # Verify if the provided attribute is in the provided attributes array, whatever it exists or not # @param attributes {Array|nil} diff --git a/app/models/availability.rb b/app/models/availability.rb index 6cc51bfe3..ff7e63d96 100644 --- a/app/models/availability.rb +++ b/app/models/availability.rb @@ -89,7 +89,7 @@ class Availability < ApplicationRecord def available_space_places return unless available_type == 'space' - duration = slot_duration || ApplicationHelper::SLOT_DURATION + duration = slot_duration || Setting.get('slot_duration') ((end_at - start_at) / duration.minutes).to_i * nb_total_places end @@ -162,7 +162,7 @@ class Availability < ApplicationRecord def length_must_be_slot_multiple return unless available_type == 'machines' || available_type == 'space' - duration = slot_duration || ApplicationHelper::SLOT_DURATION + duration = slot_duration || Setting.get('slot_duration') return unless end_at < (start_at + duration.minutes) errors.add(:end_at, I18n.t('availabilities.length_must_be_slot_multiple', MIN: duration)) diff --git a/app/models/slot.rb b/app/models/slot.rb index 9341d52ea..3af13f833 100644 --- a/app/models/slot.rb +++ b/app/models/slot.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true # Time range, slicing an Availability. -# Its duration is defined by globally by ApplicationHelper::SLOT_DURATION but can be overridden per availability +# Its duration is defined by globally by Setting.get('slot_duration') but can be overridden per availability # During a slot a Reservation is possible # Only reserved slots are persisted in DB, others are instantiated on the fly class Slot < ApplicationRecord diff --git a/app/services/availabilities/availabilities_service.rb b/app/services/availabilities/availabilities_service.rb index 9c4b77696..9ee40b121 100644 --- a/app/services/availabilities/availabilities_service.rb +++ b/app/services/availabilities/availabilities_service.rb @@ -17,7 +17,7 @@ class Availabilities::AvailabilitiesService slots = [] availabilities.each do |a| - slot_duration = a.slot_duration || ApplicationHelper::SLOT_DURATION + slot_duration = a.slot_duration || Setting.get('slot_duration') ((a.end_at - a.start_at) / slot_duration.minutes).to_i.times do |i| next unless (a.start_at + (i * slot_duration).minutes) > DateTime.current || user.admin? @@ -44,7 +44,7 @@ class Availabilities::AvailabilitiesService slots = [] availabilities.each do |a| - slot_duration = a.slot_duration || ApplicationHelper::SLOT_DURATION + slot_duration = a.slot_duration || Setting.get('slot_duration') ((a.end_at - a.start_at) / slot_duration.minutes).to_i.times do |i| next unless (a.start_at + (i * slot_duration).minutes) > DateTime.current || user.admin? diff --git a/app/services/availabilities/public_availabilities_service.rb b/app/services/availabilities/public_availabilities_service.rb index 1f55330ff..0e81b98fa 100644 --- a/app/services/availabilities/public_availabilities_service.rb +++ b/app/services/availabilities/public_availabilities_service.rb @@ -15,7 +15,7 @@ class Availabilities::PublicAvailabilitiesService .where(lock: false) slots = [] availabilities.each do |a| - slot_duration = a.slot_duration || ApplicationHelper::SLOT_DURATION + slot_duration = a.slot_duration || Setting.get('slot_duration') a.machines.each do |machine| next unless machine_ids&.include?(machine.id.to_s) @@ -46,7 +46,7 @@ class Availabilities::PublicAvailabilitiesService slots = [] availabilities.each do |a| - slot_duration = a.slot_duration || ApplicationHelper::SLOT_DURATION + slot_duration = a.slot_duration || Setting.get('slot_duration') space = a.spaces.first ((a.end_at - a.start_at) / slot_duration.minutes).to_i.times do |i| next unless (a.start_at + (i * slot_duration).minutes) > DateTime.current diff --git a/app/views/application/index.html.erb b/app/views/application/index.html.erb index a195e0528..2b592b512 100644 --- a/app/views/application/index.html.erb +++ b/app/views/application/index.html.erb @@ -31,7 +31,7 @@ Fablab.fablabWithoutWallet = ('<%= Rails.application.secrets.fablab_without_wallet %>' === 'true'); Fablab.bookSlotAtSameTime = ('<%= Setting.get('book_overlapping_slots') %>' === 'true'); Fablab.eventsInCalendar = ('<%= Rails.application.secrets.events_in_calendar %>' === 'true'); - Fablab.slotDuration = parseInt("<%= ApplicationHelper::SLOT_DURATION %>", 10); + Fablab.slotDuration = parseInt("<%= Setting.get('slot_duration') %>", 10); Fablab.featureTourDisplay = "<%= Rails.application.secrets.feature_tour_display %>"; Fablab.disqusShortname = "<%= Rails.application.secrets.disqus_shortname %>"; Fablab.defaultHost = "<%= Rails.application.secrets.default_host %>"; diff --git a/app/views/exports/availabilities_index.xlsx.axlsx b/app/views/exports/availabilities_index.xlsx.axlsx index cc8d3fd2f..7d9e28921 100644 --- a/app/views/exports/availabilities_index.xlsx.axlsx +++ b/app/views/exports/availabilities_index.xlsx.axlsx @@ -16,7 +16,7 @@ wb.add_worksheet(name: t('export_availabilities.machines')) do |sheet| # data rows @availabilities.where(available_type: 'machines').order(:start_at).each do |a| - slot_duration = a.slot_duration || ApplicationHelper::SLOT_DURATION + slot_duration = a.slot_duration || Setting.get('slot_duration') a.machines.each do |m| ((a.end_at - a.start_at) / slot_duration.minutes).to_i.times do |i| start_at = a.start_at + (i * slot_duration).minutes @@ -84,7 +84,7 @@ if Rails.application.secrets.fablab_without_spaces != 'false' # data rows @availabilities.where(available_type: 'space').order(:start_at).each do |a| - slot_duration = a.slot_duration || ApplicationHelper::SLOT_DURATION + slot_duration = a.slot_duration || Setting.get('slot_duration') ((a.end_at - a.start_at) / slot_duration.minutes).to_i.times do |i| start_at = a.start_at + (i * slot_duration).minutes end_at = a.start_at + (i * slot_duration).minutes + slot_duration.minutes