demo_mode 3.7.2 → 3.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b855f5dbc8f1a8ff5a37f7ee07c8780f5da7bec9aedaa1558164a8f7bb18d769
4
- data.tar.gz: 92aaf8d2e014423e8e37be02356a5efc1d70c0e9ecc740d1436b05f956782d50
3
+ metadata.gz: 7e73fd7fddd766bef9d1545a62dc2ddd66afc0b8b9c54520eaaf0a565133b475
4
+ data.tar.gz: a4738c449db124a9a0bcd78f75ef4f5d3a0f5de70bc3d841ef42b8e602f3d971
5
5
  SHA512:
6
- metadata.gz: dd54802030d6570054be23f529fe02e7358f23468474341390ff35580208a3bc12b49ce6cf21e3c0f898d6fcc5496e45b278d42d7cfe00fc7e4befa2677aa002
7
- data.tar.gz: 4a906c94e4c1a8541ba612927c0b5a936c24223976583dbdf9123099e9a0599c9736d8aec00a1284b93dadf3162c0b40f1950fbfc06c175fe66eea0b1871dcbf
6
+ metadata.gz: 74f400f23e3c529a3b928861a6870ac5b39e234cd0fa69174bd88176bb6ac416518a46b390d78dd4a671958b164dd2aa4fabc95a557c051b6835e70f4a555d2d
7
+ data.tar.gz: 06d3c2e5dd2dd757b11c3195124ec237839b56f68f46b693b8cde88c5d7875fdf21bd19333e732ca9c0cd49e8848c112cf9d95e8fa5abba6b57e09c09dc08fd9
@@ -3,14 +3,20 @@
3
3
  module DemoMode
4
4
  class AccountGenerationJob < DemoMode.base_job_name.constantize
5
5
  def perform(session, **options)
6
- session.with_lock do
6
+ session.with_lock(requires_new: true) do
7
7
  session.update!(status: 'processing') if session.failed?
8
8
  persona = session.persona
9
9
  raise "Unknown persona: #{session.persona_name}" if persona.blank?
10
10
 
11
11
  signinable = persona.generate!(variant: session.variant, password: session.signinable_password, options: options)
12
+ session.update!(signinable: signinable, persona_checksum: persona.file_checksum)
13
+
14
+ if session.claimed_at?
15
+ persona.effective_at_claim_callback(session.variant)&.call(signinable)
16
+ end
17
+
12
18
  new_status = session.claimed_at? ? 'in_use' : 'available'
13
- session.update!(signinable: signinable, status: new_status, persona_checksum: persona.file_checksum)
19
+ session.update!(status: new_status)
14
20
  end
15
21
  rescue StandardError => e
16
22
  session.update!(status: 'failed')
@@ -14,7 +14,7 @@ module DemoMode
14
14
  state 'processing', default: true, from: 'failed'
15
15
  state 'available', from: 'processing'
16
16
  state 'in_use', from: %w(processing available)
17
- state 'failed', from: 'processing'
17
+ state 'failed', from: %w(processing available)
18
18
  end
19
19
 
20
20
  scope :unclaimed, -> { where(claimed_at: nil) }
@@ -56,13 +56,15 @@ module DemoMode
56
56
  end
57
57
 
58
58
  def self.claim_for(persona_name:, variant: DEFAULT_VARIANT, **generation_opts)
59
+ persona = DemoMode.personas.find { |p| p.name.to_s == persona_name.to_s && p.variants.key?(variant) }
59
60
  pool_hit = false
60
61
  session = transaction do
61
62
  existing = available_for(persona_name, variant).lock.first
62
63
  pool_hit = existing.present?
63
- (existing || new(persona_name: persona_name, variant: variant)).tap do |s|
64
- s.claim!
65
- AccountGenerationJob.perform_later(s, **generation_opts) if s.signinable.blank?
64
+ if existing
65
+ claim_pool_session(existing, persona, variant)
66
+ else
67
+ new_claimed_session(persona_name, variant, generation_opts)
66
68
  end
67
69
  end
68
70
  ActiveSupport::Notifications.instrument('demo_mode.session.claimed',
@@ -70,6 +72,28 @@ module DemoMode
70
72
  session
71
73
  end
72
74
 
75
+ class << self
76
+ private
77
+
78
+ def claim_pool_session(session, persona, variant)
79
+ transaction(requires_new: true) do
80
+ persona&.effective_at_claim_callback(variant)&.call(session.signinable)
81
+ session.claim!
82
+ rescue StandardError
83
+ raise ActiveRecord::Rollback
84
+ end
85
+ session.update!(status: 'failed') unless session.claimed_at?
86
+ session
87
+ end
88
+
89
+ def new_claimed_session(persona_name, variant, generation_opts)
90
+ new(persona_name: persona_name, variant: variant).tap do |s|
91
+ s.claim!
92
+ AccountGenerationJob.perform_later(s, **generation_opts) if s.signinable.blank?
93
+ end
94
+ end
95
+ end
96
+
73
97
  def claim!
74
98
  if new_record?
75
99
  self.claimed_at = Time.zone.now
@@ -68,13 +68,17 @@ module DemoMode
68
68
  variant = variants[variant]
69
69
  CleverSequence.reset! if defined?(CleverSequence)
70
70
  DemoMode.current_password = password if password
71
- DemoMode.around_persona_generation.call(variant.signinable_generator, **options)
71
+ ActiveRecord::Base.transaction(requires_new: true) do
72
+ DemoMode.around_persona_generation.call(variant.signinable_generator, **options)
73
+ end
72
74
  rescue ActiveRecord::RecordNotUnique, ActiveRecord::RecordInvalid => e
73
75
  raise if retried || !should_retry_with_sequence_adjustment?(e)
74
76
 
75
77
  retried = true
76
78
  CleverSequence.with_sequence_adjustment do
77
- DemoMode.around_persona_generation.call(variant.signinable_generator, **options)
79
+ ActiveRecord::Base.transaction(requires_new: true) do
80
+ DemoMode.around_persona_generation.call(variant.signinable_generator, **options)
81
+ end
78
82
  end
79
83
  ensure
80
84
  DemoMode.current_password = nil
@@ -97,6 +101,16 @@ module DemoMode
97
101
  @enabled_condition ? @enabled_condition.call : true
98
102
  end
99
103
 
104
+ def at_claim(&block)
105
+ @at_claim_callback = block
106
+ end
107
+
108
+ attr_reader :at_claim_callback
109
+
110
+ def effective_at_claim_callback(variant_name)
111
+ variants[variant_name]&.at_claim_callback || @at_claim_callback
112
+ end
113
+
100
114
  def callout(callout = true) # rubocop:disable Style/OptionalBooleanParameter
101
115
  @callout = callout
102
116
  end
@@ -154,10 +168,15 @@ module DemoMode
154
168
  @enabled_condition ? @enabled_condition.call : true
155
169
  end
156
170
 
171
+ def at_claim(&block)
172
+ @at_claim_callback = block
173
+ end
174
+
157
175
  def title
158
176
  name.is_a?(Symbol) ? name.to_s.titleize : name.to_s
159
177
  end
160
178
 
179
+ attr_reader :at_claim_callback
161
180
  attr_reader :signinable_generator
162
181
  end
163
182
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DemoMode
4
- VERSION = '3.7.2'
4
+ VERSION = '3.8.1'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: demo_mode
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.7.2
4
+ version: 3.8.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nathan Griffith
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-04-22 00:00:00.000000000 Z
11
+ date: 2026-05-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionpack