Class: Wizard
- Inherits:
-
Object
show all
- Defined in:
- lib/wizard.rb,
lib/wizard/step.rb,
lib/wizard/field.rb,
lib/wizard/builder.rb,
lib/wizard/step_updater.rb
Defined Under Namespace
Classes: Builder, Choice, Field, Step, StepUpdater
Constant Summary
collapse
- @@excluded_steps =
[]
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(user) ⇒ Wizard
Returns a new instance of Wizard.
9
10
11
12
13
14
|
# File 'lib/wizard.rb', line 9
def initialize(user)
@steps = []
@user = user
@first_step = nil
@max_topics_to_require_completion = 15
end
|
Instance Attribute Details
#max_topics_to_require_completion ⇒ Object
Returns the value of attribute max_topics_to_require_completion.
5
6
7
|
# File 'lib/wizard.rb', line 5
def max_topics_to_require_completion
@max_topics_to_require_completion
end
|
#steps ⇒ Object
Returns the value of attribute steps.
4
5
6
|
# File 'lib/wizard.rb', line 4
def steps
@steps
end
|
#user ⇒ Object
Returns the value of attribute user.
4
5
6
|
# File 'lib/wizard.rb', line 4
def user
@user
end
|
Class Method Details
.exclude_step(step) ⇒ Object
76
77
78
|
# File 'lib/wizard.rb', line 76
def self.exclude_step(step)
@@excluded_steps << step
end
|
.user_requires_completion?(user) ⇒ Boolean
135
136
137
|
# File 'lib/wizard.rb', line 135
def self.user_requires_completion?(user)
self.new(user).requires_completion?
end
|
Instance Method Details
#append_step(step, after: nil) {|step| ... } ⇒ Object
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/wizard.rb', line 20
def append_step(step, after: nil)
return if @@excluded_steps.include?(step)
step = create_step(step) if step.is_a?(String)
yield step if block_given?
if after
before_step = @steps.detect { |s| s.id == after }
if before_step
step.previous = before_step
step.index = before_step.index + 1
if before_step.next
step.next = before_step.next
before_step.next.previous = step
end
before_step.next = step
@steps.insert(before_step.index + 1, step)
step.index += 1 while (step = step.next)
return
end
end
last_step = @steps.last
@steps << step
if @steps.size == 1
@first_step = step
step.index = 0
elsif last_step.present?
last_step.next = step
step.previous = last_step
step.index = last_step.index + 1
end
end
|
#completed? ⇒ Boolean
102
103
104
|
# File 'lib/wizard.rb', line 102
def completed?
completed_steps?(steps_with_fields.map(&:id))
end
|
#completed_steps?(steps) ⇒ Boolean
106
107
108
109
110
111
112
113
114
115
116
117
|
# File 'lib/wizard.rb', line 106
def completed_steps?(steps)
steps = [steps].flatten.uniq
completed =
UserHistory
.where(action: UserHistory.actions[:wizard_step], context: steps)
.distinct
.order(:context)
.pluck(:context)
steps.sort == completed
end
|
#create_step(step_name) ⇒ Object
16
17
18
|
# File 'lib/wizard.rb', line 16
def create_step(step_name)
Step.new(step_name)
end
|
#create_updater(step_id, fields) ⇒ Object
97
98
99
100
|
# File 'lib/wizard.rb', line 97
def create_updater(step_id, fields)
step = @steps.find { |s| s.id == step_id.dasherize }
Wizard::StepUpdater.new(@user, step, fields)
end
|
#remove_step(step_id) ⇒ Object
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
# File 'lib/wizard.rb', line 57
def remove_step(step_id)
i = @steps.index { |step| step.id == step_id }
return if i.nil?
step = @steps.delete_at(i)
step.previous.next = step.next if step.previous
while step = step.next
step.index -= 1
if step.index == 0
step.previous = nil
@first_step = step
else
step.previous = @steps[step.index - 1]
end
end
end
|
#requires_completion? ⇒ Boolean
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
# File 'lib/wizard.rb', line 119
def requires_completion?
return false unless SiteSetting.wizard_enabled?
return false if SiteSetting.bypass_wizard_check?
if Topic.limit(@max_topics_to_require_completion + 1).count > @max_topics_to_require_completion
SiteSetting.bypass_wizard_check = true
return false
end
if @user&.id && User.first_login_admin_id == @user.id
!Wizard::Builder.new(@user).build.completed?
else
false
end
end
|
#start ⇒ Object
84
85
86
87
88
89
90
91
92
93
94
95
|
# File 'lib/wizard.rb', line 84
def start
completed =
UserHistory
.where(action: UserHistory.actions[:wizard_step], context: steps_with_fields.map(&:id))
.uniq
.pluck(:context)
steps_with_fields.each { |s| return s if completed.exclude?(s.id) }
@first_step
end
|
#steps_with_fields ⇒ Object
80
81
82
|
# File 'lib/wizard.rb', line 80
def steps_with_fields
@steps_with_fields ||= @steps.select(&:has_fields?)
end
|