Class: DemoMode::Session

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
SteadyState
Defined in:
app/models/demo_mode/session.rb

Constant Summary collapse

DEFAULT_VARIANT =
'default'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#pool_sessionObject

Returns the value of attribute pool_session.



11
12
13
# File 'app/models/demo_mode/session.rb', line 11

def pool_session
  @pool_session
end

Class Method Details

.claim_for(persona_name:, variant: DEFAULT_VARIANT, **generation_opts) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/models/demo_mode/session.rb', line 58

def self.claim_for(persona_name:, variant: DEFAULT_VARIANT, **generation_opts)
  persona = DemoMode.personas.find { |p| p.name.to_s == persona_name.to_s && p.variants.key?(variant) }
  pool_hit = false
  session = transaction do
    existing = available_for(persona_name, variant).lock.first
    pool_hit = existing.present?
    if existing
      claim_pool_session(existing, persona, variant)
    else
      new_claimed_session(persona_name, variant, generation_opts)
    end
  end
  ActiveSupport::Notifications.instrument('demo_mode.session.claimed',
    persona_name: persona_name, variant: variant, pool_hit: pool_hit)
  session
end

Instance Method Details

#claim!Object



97
98
99
100
101
102
103
104
# File 'app/models/demo_mode/session.rb', line 97

def claim!
  if new_record?
    self.claimed_at = Time.zone.now
    save!
  else
    lock!.update!(claimed_at: Time.zone.now, status: 'in_use')
  end
end

#personaObject

Heads up: finding a persona is not guaranteed (e.g. past sessions)



54
55
56
# File 'app/models/demo_mode/session.rb', line 54

def persona
  DemoMode.personas.find { |p| p.name.to_s == persona_name.to_s && p.variants.key?(variant) }
end

#save_and_generate_account!(**options) ⇒ Object



106
107
108
109
110
111
# File 'app/models/demo_mode/session.rb', line 106

def save_and_generate_account!(**options)
  transaction do
    save!
    AccountGenerationJob.perform_now(self, **options)
  end
end

#save_and_generate_account_later!(**options) ⇒ Object



113
114
115
116
117
118
# File 'app/models/demo_mode/session.rb', line 113

def (**options)
  transaction do
    save!
    AccountGenerationJob.perform_later(self, **options)
  end
end

#signinable_metadataObject



49
50
51
# File 'app/models/demo_mode/session.rb', line 49

def 
  available? || in_use? ? .call(self) : {}
end

#signinable_usernameObject



45
46
47
# File 'app/models/demo_mode/session.rb', line 45

def signinable_username
  signinable.public_send(DemoMode.signinable_username_method)
end