Class: SpreeCmCommissioner::VotingSession
- Defined in:
- app/models/spree_cm_commissioner/voting_session.rb
Class Method Summary collapse
-
.current_or_next_upcoming ⇒ Object
Priority 1: Current session wins over Upcoming session.
Instance Method Summary collapse
- #active? ⇒ Boolean
- #can_vote? ⇒ Boolean
- #effective_fraud_config ⇒ Object
- #effective_voting_config ⇒ Object
-
#first_in_episode? ⇒ Boolean
True when this session is the first in its episode.
- #max_votes_per_user ⇒ Object
-
#missing_show_contestants ⇒ Object
Show contestants expected in this session but not yet assigned.
-
#next_in_episode ⇒ Object
Returns the next session within the same episode (by position).
- #past? ⇒ Boolean
- #paused? ⇒ Boolean
- #rate_limit_max_votes ⇒ Object
- #rate_limit_window ⇒ Object
- #upcoming? ⇒ Boolean
Class Method Details
.current_or_next_upcoming ⇒ Object
Priority 1: Current session wins over Upcoming session. Priority 2: Follow manual position (Managed by acts_as_list).
48 49 50 51 52 53 |
# File 'app/models/spree_cm_commissioner/voting_session.rb', line 48 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
30 31 32 |
# File 'app/models/spree_cm_commissioner/voting_session.rb', line 30 def active? enabled? && Time.current.between?(opens_at, closes_at) end |
#can_vote? ⇒ Boolean
71 72 73 |
# File 'app/models/spree_cm_commissioner/voting_session.rb', line 71 def can_vote? enabled? && Time.zone.now.between?(opens_at, closes_at) end |
#effective_fraud_config ⇒ Object
63 64 65 66 67 68 69 |
# File 'app/models/spree_cm_commissioner/voting_session.rb', line 63 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_config ⇒ Object
55 56 57 58 59 60 61 |
# File 'app/models/spree_cm_commissioner/voting_session.rb', line 55 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 |
#first_in_episode? ⇒ Boolean
True when this session is the first in its episode. e.g. position=1 → true; position=2 → false
83 84 85 |
# File 'app/models/spree_cm_commissioner/voting_session.rb', line 83 def first_in_episode? position == 1 end |
#max_votes_per_user ⇒ Object
95 96 97 |
# File 'app/models/spree_cm_commissioner/voting_session.rb', line 95 def max_votes_per_user effective_voting_config['max_votes_per_user'] || 10 end |
#missing_show_contestants ⇒ Object
Show contestants expected in this session but not yet assigned. show.show_contestants → all contestants on the show this session belongs to. show_contestants → already assigned to this voting session. e.g. show has [Alice, Bob, Carol]; session only has [Alice] → returns [Bob, Carol]
91 92 93 |
# File 'app/models/spree_cm_commissioner/voting_session.rb', line 91 def missing_show_contestants show.show_contestants.where.not(id: show_contestants.select(:id)).ordered end |
#next_in_episode ⇒ Object
Returns the next session within the same episode (by position). e.g. current session position=1 → returns session at position=2; last session → nil
77 78 79 |
# File 'app/models/spree_cm_commissioner/voting_session.rb', line 77 def next_in_episode episode.voting_sessions.where('position > ?', position).order(:position).first end |
#past? ⇒ Boolean
38 39 40 |
# File 'app/models/spree_cm_commissioner/voting_session.rb', line 38 def past? closed? || (closes_at.present? && Time.current > closes_at) end |
#paused? ⇒ Boolean
42 43 44 |
# File 'app/models/spree_cm_commissioner/voting_session.rb', line 42 def paused? disabled? && Time.current.between?(opens_at, closes_at) end |
#rate_limit_max_votes ⇒ Object
103 104 105 |
# File 'app/models/spree_cm_commissioner/voting_session.rb', line 103 def rate_limit_max_votes effective_voting_config['rate_limit_max_votes'] || 5 end |
#rate_limit_window ⇒ Object
99 100 101 |
# File 'app/models/spree_cm_commissioner/voting_session.rb', line 99 def rate_limit_window effective_voting_config['rate_limit_window'] || 60 end |
#upcoming? ⇒ Boolean
34 35 36 |
# File 'app/models/spree_cm_commissioner/voting_session.rb', line 34 def upcoming? enabled? && Time.current < opens_at end |