Class: SpreeCmCommissioner::ShowEpisode

Inherits:
Spree::Product
  • Object
show all
Includes:
StoreMetadata
Defined in:
app/models/spree_cm_commissioner/show_episode.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.current_or_next_upcomingObject



66
67
68
69
70
71
72
# File 'app/models/spree_cm_commissioner/show_episode.rb', line 66

def self.current_or_next_upcoming
  current.or(upcoming)
         .reorder(
           Arel.sql("CAST((spree_products.private_metadata #>> '{}')::jsonb ->> 'scheduled_at' AS timestamp) ASC"),
           Arel.sql("CAST((spree_products.private_metadata #>> '{}')::jsonb ->> 'episode_number' AS integer) ASC")
         )
end

.model_nameObject



105
106
107
# File 'app/models/spree_cm_commissioner/show_episode.rb', line 105

def self.model_name
  Spree::Product.model_name
end

.polymorphic_nameObject



109
110
111
# File 'app/models/spree_cm_commissioner/show_episode.rb', line 109

def self.polymorphic_name
  name
end

Instance Method Details

#current?Boolean

Returns:

  • (Boolean)


74
75
76
77
# File 'app/models/spree_cm_commissioner/show_episode.rb', line 74

def current?
  (scheduled_at.present? && scheduled_at <= Time.zone.now) &&
    (ends_at >= Time.zone.now)
end

#first_in_season?Boolean

True when this episode is the first in the season by episode_number. e.g. season has [ep1, ep2]; self=ep1 → true; self=ep2 → false

Returns:

  • (Boolean)


93
94
95
# File 'app/models/spree_cm_commissioner/show_episode.rb', line 93

def first_in_season?
  season.episodes.ordered_by_episode_number.first&.id == id
end

#next_episode_first_sessionObject

Returns the first advanceable voting session (by position) of the next episode in the show. Excludes pre_vote sessions. e.g. next episode has sessions [pos:1 "Prelim", pos:2 "Final"] → returns "Prelim" session Returns nil when there is no next episode or the next episode has no advanceable sessions.



101
102
103
# File 'app/models/spree_cm_commissioner/show_episode.rb', line 101

def next_episode_first_session
  next_in_season&.voting_sessions&.where(session_type: %i[vote manual_advance])&.order(:position)&.first
end

#next_in_seasonObject

Returns the episode that follows this one within the same show, ordered by episode_number. e.g. show has [ep1, ep2, ep3]; self=ep2 → returns ep3; self=ep3 → nil



85
86
87
88
89
# File 'app/models/spree_cm_commissioner/show_episode.rb', line 85

def next_in_season
  eps = season.episodes.where(product_type: :show_episode).ordered_by_episode_number.to_a
  current_index = eps.index { |ep| ep.id == id }
  eps[current_index + 1] if current_index && current_index < eps.length - 1
end

#upcoming?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'app/models/spree_cm_commissioner/show_episode.rb', line 79

def upcoming?
  scheduled_at.present? && scheduled_at > Time.zone.now
end