Class: DemoMode::Persona

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model
Defined in:
lib/demo_mode/persona.rb

Defined Under Namespace

Classes: Variant

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#file_checksumObject

Returns the value of attribute file_checksum.



9
10
11
# File 'lib/demo_mode/persona.rb', line 9

def file_checksum
  @file_checksum
end

#nameObject

Returns the value of attribute name.



9
10
11
# File 'lib/demo_mode/persona.rb', line 9

def name
  @name
end

Instance Method Details

#begin_demo(&block) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/demo_mode/persona.rb', line 30

def begin_demo(&block)
  if block
    @begin_demo = block
  else
    @begin_demo || proc do
       @session.signinable
      redirect_to main_app.root_path
    end
  end
end

#callout(callout = true) ⇒ Object

rubocop:disable Style/OptionalBooleanParameter



100
101
102
# File 'lib/demo_mode/persona.rb', line 100

def callout(callout = true) # rubocop:disable Style/OptionalBooleanParameter
  @callout = callout
end

#callout?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/demo_mode/persona.rb', line 104

def callout?
  instance_variable_defined?(:@callout) && @callout
end

#css_classObject



124
125
126
# File 'lib/demo_mode/persona.rb', line 124

def css_class
  "dm-Persona--#{name.to_s.camelize(:lower)}"
end

#custom_sign_in?Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/demo_mode/persona.rb', line 120

def custom_sign_in?
  display_credentials? || @begin_demo.present?
end

#display_credentials(display_credentials = true) ⇒ Object

rubocop:disable Style/OptionalBooleanParameter



108
109
110
# File 'lib/demo_mode/persona.rb', line 108

def display_credentials(display_credentials = true) # rubocop:disable Style/OptionalBooleanParameter
  @display_credentials = display_credentials
end

#display_credentials?Boolean

Returns:

  • (Boolean)


112
113
114
115
116
117
118
# File 'lib/demo_mode/persona.rb', line 112

def display_credentials?
  if instance_variable_defined?(:@display_credentials)
    @display_credentials
  else
    DemoMode.display_credentials?
  end
end

#enabled(&block) ⇒ Object



92
93
94
# File 'lib/demo_mode/persona.rb', line 92

def enabled(&block)
  @enabled_condition = block
end

#enabled?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/demo_mode/persona.rb', line 96

def enabled?
  @enabled_condition ? @enabled_condition.call : true
end

#featuresObject



26
27
28
# File 'lib/demo_mode/persona.rb', line 26

def features
  @features ||= []
end

#generate!(variant: :default, password: nil, options: {}) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/demo_mode/persona.rb', line 65

def generate!(variant: :default, password: nil, options: {})
  retried = false
  ActiveSupport::Notifications.instrument('demo_mode.persona.generate', name: name, variant: variant) do
    variant = variants[variant]
    CleverSequence.reset! if defined?(CleverSequence)
    DemoMode.current_password = password if password
    DemoMode.around_persona_generation.call(variant.signinable_generator, **options)
  rescue ActiveRecord::RecordNotUnique, ActiveRecord::RecordInvalid => e
    raise if retried || !should_retry_with_sequence_adjustment?(e)

    retried = true
    CleverSequence.with_sequence_adjustment do
      DemoMode.around_persona_generation.call(variant.signinable_generator, **options)
    end
  ensure
    DemoMode.current_password = nil
  end
end

#icon(name_or_path = nil, &block) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/demo_mode/persona.rb', line 14

def icon(name_or_path = nil, &block)
  if block
    @icon = block
  elsif name_or_path
    @path = ICONS.fetch(name_or_path, name_or_path)
    path = @path
    @icon = ->(_) { image_tag path }
  else
    @icon ||= DemoMode.icon
  end
end

#metadata(&block) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/demo_mode/persona.rb', line 41

def (&block)
  if block
    @metadata = block
  else
    @metadata ||= ->(_) { {} }
  end
end

#should_retry_with_sequence_adjustment?(error) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
87
88
89
90
# File 'lib/demo_mode/persona.rb', line 84

def should_retry_with_sequence_adjustment?(error)
  return false unless defined?(CleverSequence)
  return false unless CleverSequence.retry_on_uniqueness_violation?

  Rails.logger.warn("[DemoMode] Uniqueness violation during persona generation, retrying with sequence adjustment: #{error.message}")
  true
end

#sign_in_as(&signinable_generator) ⇒ Object



49
50
51
52
53
# File 'lib/demo_mode/persona.rb', line 49

def (&signinable_generator)
  variant(:default) do
    (&signinable_generator)
  end
end

#variant(name, &block) ⇒ Object



55
56
57
58
59
# File 'lib/demo_mode/persona.rb', line 55

def variant(name, &block)
  (@variants ||= {}.with_indifferent_access)[name] = Variant.new(name: name).tap do |v|
    v.instance_eval(&block)
  end
end

#variantsObject



61
62
63
# File 'lib/demo_mode/persona.rb', line 61

def variants
  (@variants ||= {}.with_indifferent_access).select { |_, v| v.enabled? }
end