Class: SpreeCmCommissioner::VotingSession

Inherits:
Base
  • Object
show all
Includes:
StoreMetadata
Defined in:
app/models/spree_cm_commissioner/voting_session.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.current_or_next_upcomingObject

Priority 1: Current session wins over Upcoming session. Priority 2: Follow manual position (Managed by acts_as_list).



53
54
55
56
57
58
# File 'app/models/spree_cm_commissioner/voting_session.rb', line 53

def self.current_or_next_upcoming
  current.or(upcoming).order(
    Arel.sql('CASE WHEN opens_at <= CURRENT_TIMESTAMP AND closes_at >= CURRENT_TIMESTAMP THEN 0 ELSE 1 END'),
    :position
  )
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'app/models/spree_cm_commissioner/voting_session.rb', line 35

def active?
  enabled? && Time.current.between?(opens_at, closes_at)
end

#can_vote?Boolean

Returns:

  • (Boolean)


106
107
108
# File 'app/models/spree_cm_commissioner/voting_session.rb', line 106

def can_vote?
  enabled? && Time.zone.now.between?(opens_at, closes_at)
end

#effective_fraud_configObject



98
99
100
101
102
103
104
# File 'app/models/spree_cm_commissioner/voting_session.rb', line 98

def effective_fraud_config
  return {} unless show

  config = parse_json_config(fraud_config)
  show_config = show.effective_fraud_config
  config.reverse_merge(show_config || {})
end

#effective_voting_configObject



90
91
92
93
94
95
96
# File 'app/models/spree_cm_commissioner/voting_session.rb', line 90

def effective_voting_config
  return {} unless show

  config = parse_json_config(voting_config)
  show_config = show.effective_voting_config
  config.reverse_merge(show_config || {})
end

#embedded_live_stream_urlObject



142
143
144
145
146
# File 'app/models/spree_cm_commissioner/voting_session.rb', line 142

def embedded_live_stream_url
  return nil if live_stream_url.blank?

  SpreeCmCommissioner::UrlEmbed::YoutubeEmbed.call(live_stream_url)
end

#find_or_create_voting_contestant(show_contestant) ⇒ Object



84
85
86
87
88
# File 'app/models/spree_cm_commissioner/voting_session.rb', line 84

def find_or_create_voting_contestant(show_contestant)
  voting_contestants.find_or_create_by!(show_contestant: show_contestant) do |vc|
    vc.show_id = show.id
  end
end

#first_in_episode?Boolean

True when this session is the first in its episode. e.g. position=1 → true; position=2 → false

Returns:

  • (Boolean)


118
119
120
# File 'app/models/spree_cm_commissioner/voting_session.rb', line 118

def first_in_episode?
  position == 1
end

#max_votes_per_userObject



130
131
132
# File 'app/models/spree_cm_commissioner/voting_session.rb', line 130

def max_votes_per_user
  effective_voting_config['max_votes_per_user'] || 10
end

#missing_show_contestantsObject

Show contestants expected in this session but not yet assigned. episode.season.show_contestants → all contestants on the season this episode belongs to. show_contestants → already assigned to this voting session. e.g. season has [Alice, Bob, Carol]; session only has [Alice] → returns [Bob, Carol]



126
127
128
# File 'app/models/spree_cm_commissioner/voting_session.rb', line 126

def missing_show_contestants
  episode.season.show_contestants.where.not(id: show_contestants.select(:id)).ordered
end

#next_in_episodeObject

Returns the next session within the same episode (by position). e.g. current session position=1 → returns session at position=2; last session → nil



112
113
114
# File 'app/models/spree_cm_commissioner/voting_session.rb', line 112

def next_in_episode
  episode.voting_sessions.where('position > ?', position).order(:position).first
end

#past?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'app/models/spree_cm_commissioner/voting_session.rb', line 43

def past?
  closed? || (closes_at.present? && Time.current > closes_at)
end

#paused?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'app/models/spree_cm_commissioner/voting_session.rb', line 47

def paused?
  disabled? && Time.current.between?(opens_at, closes_at)
end

#rate_limit_max_votesObject



138
139
140
# File 'app/models/spree_cm_commissioner/voting_session.rb', line 138

def rate_limit_max_votes
  effective_voting_config['rate_limit_max_votes'] || 5
end

#rate_limit_windowObject



134
135
136
# File 'app/models/spree_cm_commissioner/voting_session.rb', line 134

def rate_limit_window
  effective_voting_config['rate_limit_window'] || 60
end

#upcoming?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'app/models/spree_cm_commissioner/voting_session.rb', line 39

def upcoming?
  enabled? && Time.current < opens_at
end