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



50
51
52
53
54
55
56
# File 'app/models/spree_cm_commissioner/show_episode.rb', line 50

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



89
90
91
# File 'app/models/spree_cm_commissioner/show_episode.rb', line 89

def self.model_name
  Spree::Product.model_name
end

.polymorphic_nameObject



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

def self.polymorphic_name
  name
end

Instance Method Details

#current?Boolean

Returns:

  • (Boolean)


58
59
60
61
# File 'app/models/spree_cm_commissioner/show_episode.rb', line 58

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)


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

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.



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

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



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

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)


63
64
65
# File 'app/models/spree_cm_commissioner/show_episode.rb', line 63

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