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



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

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



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

def self.model_name
  Spree::Product.model_name
end

.polymorphic_nameObject



111
112
113
# File 'app/models/spree_cm_commissioner/show_episode.rb', line 111

def self.polymorphic_name
  name
end

Instance Method Details

#current?Boolean

Returns:

  • (Boolean)


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

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)


95
96
97
# File 'app/models/spree_cm_commissioner/show_episode.rb', line 95

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.



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

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



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

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)


81
82
83
# File 'app/models/spree_cm_commissioner/show_episode.rb', line 81

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