1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-11 22:24:21 +01:00
fab-manager/app/models/event.rb

134 lines
4.1 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
# Event is an happening organized by the Fablab about a general topic, which does not involve Machines or trainings member's skills.
2020-03-25 10:16:47 +01:00
class Event < ApplicationRecord
include NotificationAttachedObject
include ApplicationHelper
2015-05-05 03:10:25 +02:00
has_one :event_image, as: :viewable, dependent: :destroy
accepts_nested_attributes_for :event_image, allow_destroy: true
2015-05-05 03:10:25 +02:00
has_many :event_files, as: :viewable, dependent: :destroy
accepts_nested_attributes_for :event_files, allow_destroy: true, reject_if: :all_blank
belongs_to :category
validates :category, presence: true
2016-05-04 18:17:50 +02:00
has_many :reservations, as: :reservable, dependent: :destroy
has_many :events_event_themes, dependent: :destroy
has_many :event_themes, through: :events_event_themes
2016-06-28 15:56:37 +02:00
has_many :event_price_categories, dependent: :destroy
2016-08-24 12:30:48 +02:00
has_many :price_categories, through: :event_price_categories
accepts_nested_attributes_for :event_price_categories, allow_destroy: true
2016-08-24 12:30:48 +02:00
2016-06-28 15:56:37 +02:00
belongs_to :age_range
2015-05-05 03:10:25 +02:00
belongs_to :availability, dependent: :destroy
accepts_nested_attributes_for :availability
has_one :advanced_accounting, as: :accountable, dependent: :destroy
accepts_nested_attributes_for :advanced_accounting, allow_destroy: true
2022-12-28 17:51:27 +01:00
has_many :cart_item_event_reservations, class_name: 'CartItem::EventReservation', dependent: :destroy
2023-05-11 11:22:23 +02:00
validates :event_type, inclusion: { in: %w[standard nominative family] }, presence: true
2015-05-05 03:10:25 +02:00
attr_accessor :recurrence, :recurrence_end_at
before_save :update_nb_free_places
2022-07-20 15:27:01 +02:00
after_create :event_recurrence
2016-06-24 18:26:11 +02:00
# update event updated_at for index cache
after_save -> { touch } # rubocop:disable Rails/SkipsModelValidations
2015-05-05 03:10:25 +02:00
def name
title
end
2016-06-28 15:56:37 +02:00
def themes
event_themes
2016-06-28 15:56:37 +02:00
end
2015-05-05 03:10:25 +02:00
def recurrence_events
Event.includes(:availability)
.where('events.recurrence_id = ? AND events.id != ? AND availabilities.start_at >= ?', recurrence_id, id, Time.current)
.references(:availabilities)
2015-05-05 03:10:25 +02:00
end
2023-01-19 17:41:21 +01:00
def destroyable?
Reservation.where(reservable_type: 'Event', reservable_id: id).count.zero?
end
def soft_destroy!
update(deleted_at: Time.current)
2016-03-23 18:39:41 +01:00
end
##
# @deprecated
# <b>DEPRECATED:</b> Please use <tt>event_price_categories</tt> instead.
# This method is for backward compatibility only, do not use in new code
def reduced_amount
if ActiveRecord::Base.connection.column_exists?(:events, :reduced_amount)
read_attribute(:reduced_amount)
else
pc = PriceCategory.find_by(name: I18n.t('price_category.reduced_fare'))
reduced_fare = event_price_categories.where(price_category: pc).first
if reduced_fare.nil?
nil
else
reduced_fare.amount
end
end
end
def update_nb_free_places
if nb_total_places.nil?
self.nb_free_places = nil
else
reserved_places = reservations.joins(:slots_reservations)
.where('slots_reservations.canceled_at': nil)
.where.not('slots_reservations.validated_at': nil)
.map(&:total_booked_seats)
.inject(0) { |sum, t| sum + t }
self.nb_free_places = (nb_total_places - reserved_places)
end
end
def nb_places_for_pre_registration
reservations.joins(:slots_reservations)
.where('slots_reservations.canceled_at': nil)
.where('slots_reservations.validated_at': nil)
.map(&:total_booked_seats)
.inject(0) { |sum, t| sum + t }
end
2022-06-15 09:42:39 +02:00
def all_day?
availability.start_at.hour.zero?
end
2015-05-05 03:10:25 +02:00
private
2015-05-05 03:10:25 +02:00
def event_recurrence
return unless recurrence.present? && recurrence != 'none'
on = case recurrence
when 'week'
availability.start_at.wday
when 'month'
availability.start_at.day
when 'year'
[availability.start_at.month, availability.start_at.day]
else
nil
end
r = Recurrence.new(every: recurrence, on: on, starts: availability.start_at + 1.day, until: recurrence_end_at)
r.events.each do |date|
Event::CreateEventService.create_occurence(self, date)
2015-05-05 03:10:25 +02:00
end
update(recurrence_id: id)
2015-05-05 03:10:25 +02:00
end
end