Class: SpectatorSport::Label
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- SpectatorSport::Label
- Defined in:
- app/models/spectator_sport/label.rb
Class Method Summary collapse
- .migrated? ⇒ Boolean
- .mysql_lock(lockable, &block) ⇒ Object
- .record(recording: nil, session_window: nil, value:, key: nil, strategy: "many") ⇒ Object
Instance Method Summary collapse
Class Method Details
.migrated? ⇒ Boolean
9 10 11 12 13 14 |
# File 'app/models/spectator_sport/label.rb', line 9 def self.migrated? return true if table_exists? Rails.logger.warn "SpectatorSport: pending migration for spectator_sport_labels table. Run: rails spectator_sport:install:migrations && rails db:migrate" false end |
.mysql_lock(lockable, &block) ⇒ Object
46 47 48 49 50 51 52 |
# File 'app/models/spectator_sport/label.rb', line 46 def self.mysql_lock(lockable, &block) if connection.adapter_name =~ /mysql/i lockable.with_lock(&block) else block.call end end |
.record(recording: nil, session_window: nil, value:, key: nil, strategy: "many") ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'app/models/spectator_sport/label.rb', line 16 def self.record(recording: nil, session_window: nil, value:, key: nil, strategy: "many") owner = recording || session_window owner_attrs = recording ? { recording: recording } : { session_window: session_window } value = value.to_s.presence key = key.to_s.presence strategy = key ? strategy.to_s : "many" return unless value case strategy when "first" mysql_lock(owner) do find_or_create_by(**owner_attrs, key: key) do |l| l.value = value l.multiple = false end end when "one" mysql_lock(owner) do label = find_or_initialize_by(**owner_attrs, key: key) label.assign_attributes(value: value, multiple: false) label.save end else # "many" find_or_create_by(**owner_attrs, key: key, value: value) do |l| l.multiple = true end end end |
Instance Method Details
#display ⇒ Object
54 55 56 |
# File 'app/models/spectator_sport/label.rb', line 54 def display key.present? ? "#{key}: #{value}" : value end |