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



44
45
46
47
48
49
50
# File 'app/models/spree_cm_commissioner/show_episode.rb', line 44

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



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

def self.model_name
  Spree::Product.model_name
end

.polymorphic_nameObject



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

def self.polymorphic_name
  name
end

Instance Method Details

#current?Boolean

Returns:

  • (Boolean)


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

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)


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

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

#next_episode_first_sessionObject

Returns the first voting session (by position) of the next episode in the show. 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 sessions.



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

def next_episode_first_session
  next_in_season&.voting_sessions&.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



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

def next_in_season
  eps = season.episodes.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)


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

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

#votable_idObject



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

def votable_id
  ['votable_id']
end

#votable_typeObject



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

def votable_type
  ['votable_type']
end