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
# File 'app/models/demo_mode/session.rb', line 58

def self.claim_for(persona_name:, variant: DEFAULT_VARIANT, **generation_opts)
  pool_hit = false
  session = transaction do
    existing = available_for(persona_name, variant).lock.first
    pool_hit = existing.present?
    (existing || new(persona_name: persona_name, variant: variant)).tap do |s|
      s.claim!
      AccountGenerationJob.perform_later(s, **generation_opts) if s.signinable.blank?
    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



73
74
75
76
77
78
79
80
# File 'app/models/demo_mode/session.rb', line 73

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



82
83
84
85
86
87
# File 'app/models/demo_mode/session.rb', line 82

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

#save_and_generate_account_later!(**options) ⇒ Object



89
90
91
92
93
94
# File 'app/models/demo_mode/session.rb', line 89

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