Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # This service offers an interface to load the events in a date range
- class EventsOccurrencesService
- def initialize(options)
- @product_edition = options[:product_edition]
- @operator = %w(= >=).include?(options[:operator]) ? options[:operator] : '='
- @hours_late = options[:hours_late] || 2
- @locales = if options[:locales].try(:is_array?)
- options[:locales]
- else
- [I18n.default_locale]
- end
- # @fallback_locale = options[:fallback_locale]
- @collection_edition = options[:collection_edition]
- @category = options[:category]
- @exclude_categories = options[:exclude_categories]
- @publication_status = Event::STATES.include?(options[:publication_status]) ? options[:publication_status] : 'published'
- end
- def occurrences(date)
- occurrences_ids = ids_of_occurrences(date)
- @occurrences_relation = EventDate.where(id: occurrences_ids).includes(:times, {venue: :city}, {alt_venue: :city}).includes(
- event: [:categories, :collection_editions, :image, :promoters]
- )
- include_texts_with_required_locale!
- collection_edition_conditions!
- category_conditions!
- exclude_categories_conditions!
- @occurrences_relation
- end
- def day(options)
- operator = %w(= >=).include?(options[:op]) ? options[:op] : '='
- date = options[:date] || Date.today
- order = (operator == '>=') ? 'event_dates.start_date, event_times.time_start' : 'event_times.time_start'
- event_dates_ids = date_ids(operator, date)
- # Language constraint (locale & default_locale)
- expression = EventDate.where(id: event_dates_ids).includes(:times, {venue: :city}, {alt_venue: :city})
- expression = expression.includes(event: [:texts, :categories])
- expression = expression.with_languages() unless options[:skip_languages]
- expression = expression.includes(dates: [:times, :venue, :alt_venue]).includes(:texts, :categories) unless options[:skip_includes]
- expression.order(order).all
- end
- def ids_of_occurrences(date)
- @ids_relation = EventDate.joins(:event).where(
- events: {status: @publication_status, home_control: %w(off show)}
- ).joins("INNER JOIN product_eds_events ON product_eds_events.event_id = events.id").where(
- ['product_eds_events.product_edition_id = :product_edition', product_edition: @product_edition]
- )
- time_based_ordering!
- date_conditions!(date, @operator)
- times_conditions!(date, @operator)
- @ids_relation.pluck(:id)
- end
- def time_based_ordering!()
- order = (@operator == '>=') ? 'event_dates.start_date, event_times.time_start' : 'event_times.time_start'
- @ids_relation = @ids_relation.joins("LEFT JOIN event_times ON event_times.date_id = event_dates.id").order(order)
- end
- def date_conditions!(date, operator)
- @ids_relation = @ids_relation.where(
- [
- "event_dates.start_date #{operator} :date
- OR
- ( event_dates.end_date IS NOT NULL AND :date BETWEEN event_dates.start_date AND event_dates.end_date )",
- date: date
- ]
- )
- end
- def times_conditions!(date, operator)
- if date == Date.today && operator == '='
- @ids_relation= @ids_relation.where(
- [
- %Q(
- event_dates.allday = 1
- OR ( event_times.time_end IS NULL AND :x_hours_ago <= event_times.time_start )
- OR ( event_times.time_end IS NOT NULL AND ( :now <= event_times.time_start OR :now < event_times.time_end ) )
- ),
- {x_hours_ago: (Time.now - @hours_late.hours), now: Time.now}
- ]
- )
- end
- end
- def include_texts_with_required_locale!()
- languages = @locales.map{|locale| "'#{locale}'"}.join(',')
- @occurrences_relation= @occurrences_relation.joins(:event).joins(
- "INNER JOIN event_texts ON event_texts.event_id = events.id AND event_texts.language IN (#{languages})"
- ).includes(event: :texts)
- end
- def collection_edition_conditions!()
- if @collection_edition
- @occurrences_relation= @occurrences_relation.where(['collection_editions.id = ?', @collection_edition])
- end
- end
- def category_conditions!()
- if @category
- @occurrences_relation= @occurrences_relation.where(['event_categories.id = ?', @category.id])
- end
- end
- def exclude_categories_conditions!()
- if @exclude_categories
- @occurrences_relation= @occurrences_relation.where(
- [
- 'event_categories.id NOT IN ( :categories ) OR events.home_control = :control',
- categories: @exclude_categories, control: 'show'
- ]
- )
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement