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



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

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



103
104
105
# File 'app/models/spree_cm_commissioner/show_episode.rb', line 103

def self.model_name
  Spree::Product.model_name
end

.polymorphic_nameObject



107
108
109
# File 'app/models/spree_cm_commissioner/show_episode.rb', line 107

def self.polymorphic_name
  name
end

Instance Method Details

#current?Boolean

Returns:

  • (Boolean)


72
73
74
75
# File 'app/models/spree_cm_commissioner/show_episode.rb', line 72

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)


91
92
93
# File 'app/models/spree_cm_commissioner/show_episode.rb', line 91

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.



99
100
101
# File 'app/models/spree_cm_commissioner/show_episode.rb', line 99

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



83
84
85
86
87
# File 'app/models/spree_cm_commissioner/show_episode.rb', line 83

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)


77
78
79
# File 'app/models/spree_cm_commissioner/show_episode.rb', line 77

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