demo_mode 1.2.3 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3032c631c3d2308b21588fe6d8598635c3a90e0be15edaae81fe9d869bd9b089
4
- data.tar.gz: 8af75b0ceeb37fcdd98120980c3ab2ac14a18e6b503c04fbf8e3fd9eb6b47c48
3
+ metadata.gz: f46c5ac419af565102df4efa36cc8383f847cbe680a48c12b362bbbde6c83417
4
+ data.tar.gz: b202d52d608dfbcf1c09fc880d34597b99c457bba786db624d414c671ffe1291
5
5
  SHA512:
6
- metadata.gz: 7d5352ef735cc354a3da6260c690a1862c4e24f1adca8e91b7a8ba13944e0a1000339d3f184a8ea12b97e6578f535a040c24b17254f0c8d3cfe9131182a8d1fa
7
- data.tar.gz: 810e15296241a0913d4c2a194eeae7577c7c6cbad58f5bc28c188fff2279eefc134368aa17e2fe14813db925192c8e1f2f1ddd88ddc9044cee92136429d93345
6
+ metadata.gz: 1da08b4e2d624471ac68b6417b16bd2217594d037d988906a80f6b6c760ddce5c576040c14fc97448efd766bf83da5a5d147fe35b0a216574cc642146bfa4852
7
+ data.tar.gz: a6c9edbbc801d7976df372fbd2ecf6544bc9c64f89083a483bd8ea20f828b3c0f43177c899b618079ebc46de91b35e3e169616ff95cb72d270e6c7ce7669cf98
@@ -25,7 +25,7 @@ module DemoMode
25
25
 
26
26
  def create
27
27
  @session = Session.new(create_params)
28
- @session.save!
28
+ @session.save_and_generate_account_later!
29
29
  session[:demo_session] = { 'id' => @session.id, 'last_request_at' => Time.zone.now }
30
30
  respond_to do |f|
31
31
  f.html { redirect_to @session }
@@ -4,7 +4,7 @@ module DemoMode
4
4
  class AccountGenerationJob < DemoMode.base_job_name.constantize
5
5
  def perform(session)
6
6
  session.with_lock do
7
- persona = persona(session)
7
+ persona = session.persona
8
8
  raise "Unknown persona: #{session.persona_name}" if persona.blank?
9
9
 
10
10
  signinable = persona.generate!(variant: session.variant, password: session.signinable_password)
@@ -12,11 +12,5 @@ module DemoMode
12
12
  end
13
13
  raise "Failed to create signinable persona!" if session.signinable.blank?
14
14
  end
15
-
16
- private
17
-
18
- def persona(session)
19
- DemoMode.personas.find { |p| p.name.to_s == session.persona_name.to_s }
20
- end
21
15
  end
22
16
  end
@@ -5,10 +5,10 @@ module DemoMode
5
5
  attribute :variant, default: :default
6
6
 
7
7
  validates :persona_name, :variant, presence: true
8
+ validates :persona, presence: { message: :required }, on: :create, if: :persona_name?
8
9
  belongs_to :signinable, polymorphic: true, optional: true
9
10
 
10
11
  before_create :set_password!
11
- after_create -> { AccountGenerationJob.perform_later(self) }
12
12
 
13
13
  delegate :begin_demo,
14
14
  :custom_sign_in?,
@@ -20,15 +20,29 @@ module DemoMode
20
20
  signinable.public_send(DemoMode.signinable_username_method)
21
21
  end
22
22
 
23
+ # Heads up: finding a persona is not guaranteed (e.g. past sessions)
24
+ def persona
25
+ DemoMode.personas.find { |p| p.name.to_s == persona_name.to_s }
26
+ end
27
+
28
+ def save_and_generate_account!
29
+ transaction do
30
+ save!
31
+ AccountGenerationJob.perform_now(self)
32
+ end
33
+ end
34
+
35
+ def save_and_generate_account_later!
36
+ transaction do
37
+ save!
38
+ AccountGenerationJob.perform_later(self)
39
+ end
40
+ end
41
+
23
42
  private
24
43
 
25
44
  def set_password!
26
45
  self.signinable_password ||= DemoMode.current_password
27
46
  end
28
-
29
- # Heads up: finding a persona is not guaranteed (e.g. past sessions)
30
- def persona
31
- DemoMode.personas.find { |p| p.name.to_s == persona_name.to_s }
32
- end
33
47
  end
34
48
  end
data/lib/demo_mode/cli.rb CHANGED
@@ -20,8 +20,8 @@ class DemoMode::Cli
20
20
  prompt_persona
21
21
  end
22
22
 
23
- def created_personas
24
- @created_personas ||= []
23
+ def created_sessions
24
+ @created_sessions ||= []
25
25
  end
26
26
 
27
27
  private
@@ -41,23 +41,24 @@ class DemoMode::Cli
41
41
  CLI::UI::Frame.open("{{*}} Generate an Account! {{*}}") do
42
42
  CLI::UI::Prompt.ask('Which persona should we use?') do |handler|
43
43
  DemoMode.personas.sort_by { |p| p.name.to_s }.each do |persona|
44
- persona_name = persona.name.to_s.titleize
44
+ persona_label = persona.name.to_s.titleize
45
45
 
46
- handler.option(persona_name) do
46
+ handler.option(persona_label) do
47
47
  persona.features.each do |feature|
48
48
  puts "👉 #{feature}"
49
49
  end
50
50
 
51
- named_tags = SemanticLogger.named_tags
51
+ named_tags = SemanticLogger.named_tags if defined?(SemanticLogger)
52
52
 
53
- variant = variant_for(persona, persona_name)
53
+ variant = variant_for(persona, persona_label)
54
54
 
55
55
  CLI::UI::Spinner.spin("generating account...") do |spinner|
56
- SemanticLogger.push_named_tags(named_tags)
57
- password = DemoMode.current_password
58
- signinable = persona.generate!(variant: variant)
56
+ SemanticLogger.push_named_tags(named_tags) if defined?(SemanticLogger)
57
+
58
+ session = DemoMode::Session.new(persona_name: persona.name, variant: variant)
59
+ session.save_and_generate_account!
59
60
  spinner.update_title('done!')
60
- created_personas << { name: persona_name, email: signinable.email, password: password }
61
+ created_sessions << session
61
62
  end
62
63
  end
63
64
  end
@@ -67,22 +68,23 @@ class DemoMode::Cli
67
68
  ask_next_step
68
69
  end
69
70
 
70
- def variant_for(persona, persona_name)
71
+ def variant_for(persona, persona_label)
71
72
  if persona.variants.keys == ['default']
72
73
  :default
73
74
  else
74
75
  CLI::UI::Prompt.ask(
75
- "Which variant should we use for #{persona_name}?",
76
+ "Which variant should we use for #{persona_label}?",
76
77
  options: persona.variants.keys,
77
78
  )
78
79
  end
79
80
  end
80
81
 
81
82
  def display_personas
82
- created_personas.each do |persona|
83
- CLI::UI::Frame.open("{{*}} #{persona[:name]} {{*}}") do
84
- puts "👤 :: #{persona[:email]}"
85
- puts "🔑 :: #{persona[:password]}"
83
+ created_sessions.each do |session|
84
+ CLI::UI::Frame.open("{{*}} #{session.persona_name} {{*}}") do
85
+ puts "👤 :: #{session.signinable.email}"
86
+ puts "🔑 :: #{session.signinable_password}"
87
+ puts "🌐 :: #{DemoMode.session_url(session)}"
86
88
  end
87
89
  end
88
90
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DemoMode
4
- VERSION = '1.2.3'
4
+ VERSION = '1.4.0'
5
5
  end
data/lib/demo_mode.rb CHANGED
@@ -46,6 +46,16 @@ module DemoMode
46
46
  Thread.current[:_demo_mode_password] = value
47
47
  end
48
48
 
49
+ def session_url(session)
50
+ routes = DemoMode::Engine.routes
51
+
52
+ options = routes.default_url_options.presence
53
+ options ||= ActionMailer::Base.default_url_options.presence if defined?(ActionMailer::Base)
54
+ options ||= { only_path: true }
55
+
56
+ routes.url_for(controller: 'demo_mode/sessions', action: 'show', id: session, **options)
57
+ end
58
+
49
59
  private
50
60
 
51
61
  def configuration
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: 1.2.3
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nathan Griffith
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-07-10 00:00:00.000000000 Z
11
+ date: 2024-07-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: cli-ui