require 'subj_models/concerns/comprising_external_id' module SubjModels module EventModule def self.included(including_class) including_class.class_eval do include SubjModels::ComprisingExternalId belongs_to :manager belongs_to :event_type belongs_to :nomenclature belongs_to :document_file belongs_to :brand has_one :event_schedule has_one :nomenclature_price has_many :order_items has_many :event_bookings has_and_belongs_to_many :user_specializations has_and_belongs_to_many :nomenclatures validates :document_file, :title, presence: true scope :from_today, -> { Event.joins(:event_schedule).where('event_schedules.event_date >= ?', DateTime.now.beginning_of_day ) .includes(:event_schedule).order( 'event_schedules.event_date ASC' ).uniq } scope :where_nomenclature_ids, -> (ids) do joins(:nomenclatures).where(nomenclatures: {id: ids}) end scope :office_ids, -> (ids) do joins(event_schedule: [:office]).where(event_schedule: { office: {id: ids}}) end scope :type_ids, -> (ids) do joins(:event_type).where(event_type: {id: ids}) end scope :brand_ids, -> (ids) do joins(:brand).where(brand: { id: ids}) end scope :only_free, -> do joins(:event_schedule).where(event_schedule: { is_free: true } ) end scope :specialities_ids, -> (ids) do joins(:user_specializations).where(user_specializations: { id: ids}) end scope :date_range, -> (date) do joins(:event_schedule) .where(event_schedule: { event_date: DateTime.parse(date['from']).beginning_of_day..DateTime.parse(date['to']).end_of_day } ) end scope :today_events, -> { includes(:event_schedule).where(event_schedule: { event_date: DateTime.now.beginning_of_day..DateTime.now.end_of_day } ) } scope :is_public, -> { where(is_public: true) } end end def to_s title.to_s end end end