stimulus_reflex 3.4.1 → 3.5.0.pre0
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.
Potentially problematic release.
This version of stimulus_reflex might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/CHANGELOG.md +587 -495
- data/Gemfile.lock +82 -86
- data/LATEST +1 -0
- data/README.md +10 -10
- data/app/channels/stimulus_reflex/channel.rb +40 -67
- data/lib/generators/USAGE +1 -1
- data/lib/generators/stimulus_reflex/{config_generator.rb → initializer_generator.rb} +3 -3
- data/lib/generators/stimulus_reflex/templates/app/reflexes/%file_name%_reflex.rb.tt +3 -2
- data/lib/generators/stimulus_reflex/templates/app/reflexes/application_reflex.rb.tt +1 -1
- data/lib/generators/stimulus_reflex/templates/config/initializers/stimulus_reflex.rb +6 -1
- data/lib/stimulus_reflex.rb +9 -2
- data/lib/stimulus_reflex/broadcasters/broadcaster.rb +7 -4
- data/lib/stimulus_reflex/broadcasters/page_broadcaster.rb +2 -2
- data/lib/stimulus_reflex/broadcasters/selector_broadcaster.rb +12 -5
- data/lib/stimulus_reflex/cable_ready_channels.rb +6 -2
- data/lib/stimulus_reflex/callbacks.rb +55 -5
- data/lib/stimulus_reflex/concern_enhancer.rb +37 -0
- data/lib/stimulus_reflex/configuration.rb +2 -1
- data/lib/stimulus_reflex/element.rb +31 -7
- data/lib/stimulus_reflex/policies/reflex_invocation_policy.rb +28 -0
- data/lib/stimulus_reflex/reflex.rb +35 -20
- data/lib/stimulus_reflex/reflex_data.rb +79 -0
- data/lib/stimulus_reflex/reflex_factory.rb +31 -0
- data/lib/stimulus_reflex/request_parameters.rb +19 -0
- data/lib/stimulus_reflex/{logger.rb → utils/logger.rb} +0 -2
- data/lib/stimulus_reflex/{sanity_checker.rb → utils/sanity_checker.rb} +58 -10
- data/lib/stimulus_reflex/version.rb +1 -1
- data/lib/tasks/stimulus_reflex/install.rake +6 -4
- data/package.json +6 -5
- data/stimulus_reflex.gemspec +5 -5
- data/test/broadcasters/broadcaster_test_case.rb +1 -1
- data/test/broadcasters/nothing_broadcaster_test.rb +5 -3
- data/test/broadcasters/page_broadcaster_test.rb +8 -4
- data/test/broadcasters/selector_broadcaster_test.rb +171 -55
- data/test/callbacks_test.rb +652 -0
- data/test/concern_enhancer_test.rb +54 -0
- data/test/element_test.rb +181 -0
- data/test/reflex_test.rb +1 -1
- data/test/test_helper.rb +4 -0
- data/test/tmp/app/reflexes/application_reflex.rb +2 -2
- data/test/tmp/app/reflexes/user_reflex.rb +3 -2
- data/yarn.lock +1138 -919
- metadata +39 -28
- data/tags +0 -156
data/stimulus_reflex.gemspec
CHANGED
@@ -8,7 +8,7 @@ Gem::Specification.new do |gem|
|
|
8
8
|
gem.version = StimulusReflex::VERSION
|
9
9
|
gem.authors = ["Nathan Hopkins"]
|
10
10
|
gem.email = ["natehop@gmail.com"]
|
11
|
-
gem.homepage = "https://github.com/
|
11
|
+
gem.homepage = "https://github.com/stimulusreflex/stimulus_reflex"
|
12
12
|
gem.summary = "Build reactive applications with the Rails tooling you already know and love."
|
13
13
|
gem.post_install_message = <<~MESSAGE
|
14
14
|
Friendly reminder: When updating the stimulus_reflex gem,
|
@@ -18,8 +18,8 @@ Gem::Specification.new do |gem|
|
|
18
18
|
MESSAGE
|
19
19
|
|
20
20
|
gem.metadata = {
|
21
|
-
"bug_tracker_uri" => "https://github.com/
|
22
|
-
"changelog_uri" => "https://github.com/
|
21
|
+
"bug_tracker_uri" => "https://github.com/stimulusreflex/stimulus_reflex/issues",
|
22
|
+
"changelog_uri" => "https://github.com/stimulusreflex/stimulus_reflex/CHANGELOG.md",
|
23
23
|
"documentation_uri" => "https://docs.stimulusreflex.com",
|
24
24
|
"homepage_uri" => gem.homepage,
|
25
25
|
"source_code_uri" => gem.homepage
|
@@ -32,11 +32,11 @@ Gem::Specification.new do |gem|
|
|
32
32
|
gem.add_dependency "nokogiri"
|
33
33
|
gem.add_dependency "rails", ">= 5.2"
|
34
34
|
gem.add_dependency "redis"
|
35
|
-
gem.add_dependency "cable_ready", "
|
35
|
+
gem.add_dependency "cable_ready", "5.0.0.pre0"
|
36
36
|
|
37
37
|
gem.add_development_dependency "bundler", "~> 2.0"
|
38
38
|
gem.add_development_dependency "pry-nav"
|
39
39
|
gem.add_development_dependency "pry"
|
40
40
|
gem.add_development_dependency "rake"
|
41
|
-
gem.add_development_dependency "standardrb"
|
41
|
+
gem.add_development_dependency "standardrb", "~> 1.0"
|
42
42
|
end
|
@@ -10,6 +10,6 @@ class StimulusReflex::BroadcasterTestCase < ActionCable::Channel::TestCase
|
|
10
10
|
def connection.env
|
11
11
|
@env ||= {}
|
12
12
|
end
|
13
|
-
@reflex = StimulusReflex::Reflex.new(subscribe, url: "https://test.stimulusreflex.com")
|
13
|
+
@reflex = StimulusReflex::Reflex.new(subscribe, url: "https://test.stimulusreflex.com", client_attributes: {reflex_id: "666"})
|
14
14
|
end
|
15
15
|
end
|
@@ -13,7 +13,8 @@ class StimulusReflex::NothingBroadcasterTest < StimulusReflex::BroadcasterTestCa
|
|
13
13
|
{
|
14
14
|
"name" => "stimulus-reflex:server-message",
|
15
15
|
"detail" => {
|
16
|
-
"reflexId" =>
|
16
|
+
"reflexId" => "666",
|
17
|
+
"payload" => {},
|
17
18
|
"stimulusReflex" => {
|
18
19
|
"some" => :data,
|
19
20
|
"morph" => :nothing,
|
@@ -22,14 +23,15 @@ class StimulusReflex::NothingBroadcasterTest < StimulusReflex::BroadcasterTestCa
|
|
22
23
|
"body" => nil
|
23
24
|
}
|
24
25
|
}
|
25
|
-
}
|
26
|
+
},
|
27
|
+
"reflexId" => "666"
|
26
28
|
}
|
27
29
|
]
|
28
30
|
}
|
29
31
|
}
|
30
32
|
|
31
33
|
assert_broadcast_on @reflex.stream_name, expected do
|
32
|
-
broadcaster.broadcast nil, some
|
34
|
+
broadcaster.broadcast nil, {:some => :data, "reflexId" => "666"}
|
33
35
|
end
|
34
36
|
end
|
35
37
|
end
|
@@ -12,7 +12,7 @@ class StimulusReflex::PageBroadcasterTest < StimulusReflex::BroadcasterTestCase
|
|
12
12
|
test "performs a page morph on body" do
|
13
13
|
class << @reflex.controller.response
|
14
14
|
def body
|
15
|
-
"<html><head></head><body>New Content</body></html>"
|
15
|
+
"<html><head></head><body><div>New Content</div><div>Another Content</div></body></html>"
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
@@ -24,13 +24,15 @@ class StimulusReflex::PageBroadcasterTest < StimulusReflex::BroadcasterTestCase
|
|
24
24
|
"morph" => [
|
25
25
|
{
|
26
26
|
"selector" => "body",
|
27
|
-
"html" => "New Content",
|
27
|
+
"html" => "<div>New Content</div><div>Another Content</div>",
|
28
|
+
"payload" => {},
|
28
29
|
"childrenOnly" => true,
|
29
30
|
"permanentAttributeName" => nil,
|
30
31
|
"stimulusReflex" => {
|
31
32
|
"some" => :data,
|
32
33
|
"morph" => :page
|
33
|
-
}
|
34
|
+
},
|
35
|
+
"reflexId" => "666"
|
34
36
|
}
|
35
37
|
]
|
36
38
|
}
|
@@ -57,12 +59,14 @@ class StimulusReflex::PageBroadcasterTest < StimulusReflex::BroadcasterTestCase
|
|
57
59
|
{
|
58
60
|
"selector" => "#foo",
|
59
61
|
"html" => "New Content",
|
62
|
+
"payload" => {},
|
60
63
|
"childrenOnly" => true,
|
61
64
|
"permanentAttributeName" => nil,
|
62
65
|
"stimulusReflex" => {
|
63
66
|
"some" => :data,
|
64
67
|
"morph" => :page
|
65
|
-
}
|
68
|
+
},
|
69
|
+
"reflexId" => "666"
|
66
70
|
}
|
67
71
|
]
|
68
72
|
}
|
@@ -1,57 +1,173 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
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
|
-
|
56
|
-
|
57
|
-
|
3
|
+
require_relative "broadcaster_test_case"
|
4
|
+
|
5
|
+
module StimulusReflex
|
6
|
+
class SelectorBroadcasterTest < StimulusReflex::BroadcasterTestCase
|
7
|
+
test "morphs the contents of an element if the selector(s) are present in both original and morphed html fragments" do
|
8
|
+
broadcaster = StimulusReflex::SelectorBroadcaster.new(@reflex)
|
9
|
+
broadcaster.append_morph("#foo", '<div id="foo"><div>bar</div><div>baz</div></div>')
|
10
|
+
|
11
|
+
expected = {
|
12
|
+
"cableReady" => true,
|
13
|
+
"operations" => {
|
14
|
+
"morph" => [
|
15
|
+
{
|
16
|
+
"selector" => "#foo",
|
17
|
+
"html" => "<div>bar</div><div>baz</div>",
|
18
|
+
"payload" => {},
|
19
|
+
"childrenOnly" => true,
|
20
|
+
"permanentAttributeName" => nil,
|
21
|
+
"stimulusReflex" => {
|
22
|
+
"some" => "data",
|
23
|
+
"morph" => "selector"
|
24
|
+
},
|
25
|
+
"reflexId" => "666"
|
26
|
+
}
|
27
|
+
]
|
28
|
+
}
|
29
|
+
}
|
30
|
+
|
31
|
+
assert_broadcast_on @reflex.stream_name, expected do
|
32
|
+
broadcaster.broadcast nil, some: :data
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
test "replaces the contents of an element and ignores permanent-attributes if the selector(s) aren't present in the replacing html fragment" do
|
37
|
+
broadcaster = StimulusReflex::SelectorBroadcaster.new(@reflex)
|
38
|
+
broadcaster.append_morph("#foo", '<div id="baz"><span>bar</span></div>')
|
39
|
+
|
40
|
+
expected = {
|
41
|
+
"cableReady" => true,
|
42
|
+
"operations" => {
|
43
|
+
"innerHtml" => [
|
44
|
+
{
|
45
|
+
"selector" => "#foo",
|
46
|
+
"html" => '<div id="baz"><span>bar</span></div>',
|
47
|
+
"payload" => {},
|
48
|
+
"stimulusReflex" => {
|
49
|
+
"some" => "data",
|
50
|
+
"morph" => "selector"
|
51
|
+
},
|
52
|
+
"reflexId" => "666"
|
53
|
+
}
|
54
|
+
]
|
55
|
+
}
|
56
|
+
}
|
57
|
+
|
58
|
+
assert_broadcast_on @reflex.stream_name, expected do
|
59
|
+
broadcaster.broadcast nil, some: :data
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
test "morphs the contents of an element to an empty string if no content specified" do
|
64
|
+
broadcaster = StimulusReflex::SelectorBroadcaster.new(@reflex)
|
65
|
+
broadcaster.append_morph("#foo", nil)
|
66
|
+
|
67
|
+
expected = {
|
68
|
+
"cableReady" => true,
|
69
|
+
"operations" => {
|
70
|
+
"innerHtml" => [
|
71
|
+
{
|
72
|
+
"selector" => "#foo",
|
73
|
+
"html" => "",
|
74
|
+
"payload" => {},
|
75
|
+
"stimulusReflex" => {
|
76
|
+
"some" => "data",
|
77
|
+
"morph" => "selector"
|
78
|
+
},
|
79
|
+
"reflexId" => "666"
|
80
|
+
}
|
81
|
+
]
|
82
|
+
}
|
83
|
+
}
|
84
|
+
|
85
|
+
assert_broadcast_on @reflex.stream_name, expected do
|
86
|
+
broadcaster.broadcast nil, some: :data
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
test "morphs the contents of an element to an empty string if empty specified" do
|
91
|
+
broadcaster = StimulusReflex::SelectorBroadcaster.new(@reflex)
|
92
|
+
broadcaster.append_morph("#foo", "")
|
93
|
+
|
94
|
+
expected = {
|
95
|
+
"cableReady" => true,
|
96
|
+
"operations" => {
|
97
|
+
"innerHtml" => [
|
98
|
+
{
|
99
|
+
"selector" => "#foo",
|
100
|
+
"html" => "",
|
101
|
+
"payload" => {},
|
102
|
+
"stimulusReflex" => {
|
103
|
+
"some" => "data",
|
104
|
+
"morph" => "selector"
|
105
|
+
},
|
106
|
+
"reflexId" => "666"
|
107
|
+
}
|
108
|
+
]
|
109
|
+
}
|
110
|
+
}
|
111
|
+
|
112
|
+
assert_broadcast_on @reflex.stream_name, expected do
|
113
|
+
broadcaster.broadcast nil, some: :data
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
test "morphs the contents of an element to an empty string if no content specified, hash form" do
|
118
|
+
broadcaster = StimulusReflex::SelectorBroadcaster.new(@reflex)
|
119
|
+
broadcaster.append_morph({"#foo": nil}, nil)
|
120
|
+
|
121
|
+
expected = {
|
122
|
+
"cableReady" => true,
|
123
|
+
"operations" => {
|
124
|
+
"innerHtml" => [
|
125
|
+
{
|
126
|
+
"selector" => "#foo",
|
127
|
+
"html" => "",
|
128
|
+
"payload" => {},
|
129
|
+
"stimulusReflex" => {
|
130
|
+
"some" => "data",
|
131
|
+
"morph" => "selector"
|
132
|
+
},
|
133
|
+
"reflexId" => "666"
|
134
|
+
}
|
135
|
+
]
|
136
|
+
}
|
137
|
+
}
|
138
|
+
|
139
|
+
assert_broadcast_on @reflex.stream_name, expected do
|
140
|
+
broadcaster.broadcast nil, some: :data
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
test "morphs the contents of an element to specified HTML, hash form" do
|
145
|
+
broadcaster = StimulusReflex::SelectorBroadcaster.new(@reflex)
|
146
|
+
broadcaster.append_morph({"#foo": '<div id="foo"><div>bar</div><div>baz</div></div>'}, nil)
|
147
|
+
|
148
|
+
expected = {
|
149
|
+
"cableReady" => true,
|
150
|
+
"operations" => {
|
151
|
+
"morph" => [
|
152
|
+
{
|
153
|
+
"selector" => "#foo",
|
154
|
+
"html" => "<div>bar</div><div>baz</div>",
|
155
|
+
"payload" => {},
|
156
|
+
"childrenOnly" => true,
|
157
|
+
"permanentAttributeName" => nil,
|
158
|
+
"stimulusReflex" => {
|
159
|
+
"some" => "data",
|
160
|
+
"morph" => "selector"
|
161
|
+
},
|
162
|
+
"reflexId" => "666"
|
163
|
+
}
|
164
|
+
]
|
165
|
+
}
|
166
|
+
}
|
167
|
+
|
168
|
+
assert_broadcast_on @reflex.stream_name, expected do
|
169
|
+
broadcaster.broadcast nil, some: :data
|
170
|
+
end
|
171
|
+
end
|
172
|
+
end
|
173
|
+
end
|
@@ -0,0 +1,652 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "test_helper"
|
4
|
+
|
5
|
+
# standard:disable Lint/ConstantDefinitionInBlock
|
6
|
+
|
7
|
+
class CallbacksTest < ActionCable::Channel::TestCase
|
8
|
+
tests StimulusReflex::Channel
|
9
|
+
|
10
|
+
setup do
|
11
|
+
stub_connection(session_id: SecureRandom.uuid)
|
12
|
+
def connection.env
|
13
|
+
@env ||= {}
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
test "basic before_reflex works" do
|
18
|
+
class BeforeCallbackReflex < StimulusReflex::Reflex
|
19
|
+
before_reflex :init_counter
|
20
|
+
|
21
|
+
def increment
|
22
|
+
@count += 1
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def init_counter
|
28
|
+
@count = 5
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
reflex = BeforeCallbackReflex.new(subscribe, url: "https://test.stimulusreflex.com", method_name: :increment)
|
33
|
+
reflex.process(:increment)
|
34
|
+
assert_equal 6, reflex.instance_variable_get("@count")
|
35
|
+
end
|
36
|
+
|
37
|
+
test "before_reflex with block works" do
|
38
|
+
class BeforeBlockCallbackReflex < StimulusReflex::Reflex
|
39
|
+
before_reflex do
|
40
|
+
@count = 5
|
41
|
+
end
|
42
|
+
|
43
|
+
def increment
|
44
|
+
@count += 1
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
reflex = BeforeBlockCallbackReflex.new(subscribe, url: "https://test.stimulusreflex.com", method_name: :increment)
|
49
|
+
reflex.process(:increment)
|
50
|
+
assert_equal 6, reflex.instance_variable_get("@count")
|
51
|
+
end
|
52
|
+
|
53
|
+
test "basic after_reflex works" do
|
54
|
+
class AfterCallbackReflex < StimulusReflex::Reflex
|
55
|
+
after_reflex :reset
|
56
|
+
|
57
|
+
def increment
|
58
|
+
@count = 5
|
59
|
+
end
|
60
|
+
|
61
|
+
private
|
62
|
+
|
63
|
+
def reset
|
64
|
+
@count = 1
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
reflex = AfterCallbackReflex.new(subscribe, url: "https://test.stimulusreflex.com", method_name: :increment)
|
69
|
+
reflex.process(:increment)
|
70
|
+
assert_equal 1, reflex.instance_variable_get("@count")
|
71
|
+
end
|
72
|
+
|
73
|
+
test "after_reflex with block works" do
|
74
|
+
class AfterBlockCallbackReflex < StimulusReflex::Reflex
|
75
|
+
after_reflex do
|
76
|
+
@count = 1
|
77
|
+
end
|
78
|
+
|
79
|
+
def increment
|
80
|
+
@count = 5
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
reflex = AfterBlockCallbackReflex.new(subscribe, url: "https://test.stimulusreflex.com", method_name: :increment)
|
85
|
+
reflex.process(:increment)
|
86
|
+
assert_equal 1, reflex.instance_variable_get("@count")
|
87
|
+
end
|
88
|
+
|
89
|
+
test "basic around_reflex works" do
|
90
|
+
class AroundCallbackReflex < StimulusReflex::Reflex
|
91
|
+
around_reflex :around
|
92
|
+
|
93
|
+
def increment
|
94
|
+
@count += 8
|
95
|
+
end
|
96
|
+
|
97
|
+
private
|
98
|
+
|
99
|
+
def around
|
100
|
+
@count = 2
|
101
|
+
yield
|
102
|
+
@count += 4
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
reflex = AroundCallbackReflex.new(subscribe, url: "https://test.stimulusreflex.com", method_name: :increment)
|
107
|
+
reflex.process(:increment)
|
108
|
+
assert_equal 14, reflex.instance_variable_get("@count")
|
109
|
+
end
|
110
|
+
|
111
|
+
test "execute methods in order" do
|
112
|
+
class CallbackOrderReflex < StimulusReflex::Reflex
|
113
|
+
before_reflex :one
|
114
|
+
before_reflex :two, :three
|
115
|
+
|
116
|
+
def increment
|
117
|
+
end
|
118
|
+
|
119
|
+
private
|
120
|
+
|
121
|
+
def one
|
122
|
+
@count = 1
|
123
|
+
end
|
124
|
+
|
125
|
+
def two
|
126
|
+
@count = 2 if @count == 1
|
127
|
+
end
|
128
|
+
|
129
|
+
def three
|
130
|
+
@count = 3 if @count == 2
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
reflex = CallbackOrderReflex.new(subscribe, url: "https://test.stimulusreflex.com", method_name: :increment)
|
135
|
+
reflex.process(:increment)
|
136
|
+
assert_equal 3, reflex.instance_variable_get("@count")
|
137
|
+
end
|
138
|
+
|
139
|
+
test "basic if option works" do
|
140
|
+
class IfCallbackReflex < StimulusReflex::Reflex
|
141
|
+
before_reflex :init, if: :present
|
142
|
+
around_reflex :around, if: :blank
|
143
|
+
after_reflex :after, if: :present
|
144
|
+
|
145
|
+
def increment
|
146
|
+
@count += 8
|
147
|
+
end
|
148
|
+
|
149
|
+
private
|
150
|
+
|
151
|
+
def present
|
152
|
+
true
|
153
|
+
end
|
154
|
+
|
155
|
+
def blank
|
156
|
+
false
|
157
|
+
end
|
158
|
+
|
159
|
+
def init
|
160
|
+
@count = 5
|
161
|
+
end
|
162
|
+
|
163
|
+
def around
|
164
|
+
@count += 2
|
165
|
+
yield
|
166
|
+
@count += 4
|
167
|
+
end
|
168
|
+
|
169
|
+
def after
|
170
|
+
@count += 10
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
reflex = IfCallbackReflex.new(subscribe, url: "https://test.stimulusreflex.com", method_name: :increment)
|
175
|
+
reflex.process(:increment)
|
176
|
+
assert_equal 23, reflex.instance_variable_get("@count")
|
177
|
+
end
|
178
|
+
|
179
|
+
test "if option with proc/lambda works" do
|
180
|
+
class IfProcCallbackReflex < StimulusReflex::Reflex
|
181
|
+
before_reflex :init, if: -> { true }
|
182
|
+
around_reflex :around, if: lambda { false }
|
183
|
+
after_reflex :after, if: proc { true }
|
184
|
+
|
185
|
+
def increment
|
186
|
+
@count += 8
|
187
|
+
end
|
188
|
+
|
189
|
+
private
|
190
|
+
|
191
|
+
def init
|
192
|
+
@count = 5
|
193
|
+
end
|
194
|
+
|
195
|
+
def around
|
196
|
+
@count += 2
|
197
|
+
yield
|
198
|
+
@count += 4
|
199
|
+
end
|
200
|
+
|
201
|
+
def after
|
202
|
+
@count += 10
|
203
|
+
end
|
204
|
+
end
|
205
|
+
|
206
|
+
reflex = IfProcCallbackReflex.new(subscribe, url: "https://test.stimulusreflex.com", method_name: :increment)
|
207
|
+
reflex.process(:increment)
|
208
|
+
assert_equal 23, reflex.instance_variable_get("@count")
|
209
|
+
end
|
210
|
+
|
211
|
+
test "basic unless option works" do
|
212
|
+
class UnlessCallbackReflex < StimulusReflex::Reflex
|
213
|
+
before_reflex :init, unless: :blank
|
214
|
+
around_reflex :around, unless: :present
|
215
|
+
after_reflex :after, unless: :blank
|
216
|
+
|
217
|
+
def increment
|
218
|
+
@count += 8
|
219
|
+
end
|
220
|
+
|
221
|
+
private
|
222
|
+
|
223
|
+
def present
|
224
|
+
true
|
225
|
+
end
|
226
|
+
|
227
|
+
def blank
|
228
|
+
false
|
229
|
+
end
|
230
|
+
|
231
|
+
def init
|
232
|
+
@count = 5
|
233
|
+
end
|
234
|
+
|
235
|
+
def around
|
236
|
+
@count += 2
|
237
|
+
yield
|
238
|
+
@count += 4
|
239
|
+
end
|
240
|
+
|
241
|
+
def after
|
242
|
+
@count += 10
|
243
|
+
end
|
244
|
+
end
|
245
|
+
|
246
|
+
reflex = UnlessCallbackReflex.new(subscribe, url: "https://test.stimulusreflex.com", method_name: :increment)
|
247
|
+
reflex.process(:increment)
|
248
|
+
assert_equal 23, reflex.instance_variable_get("@count")
|
249
|
+
end
|
250
|
+
|
251
|
+
test "unless option with proc/lambda works" do
|
252
|
+
class UnlessProcCallbackReflex < StimulusReflex::Reflex
|
253
|
+
before_reflex :init, unless: -> { false }
|
254
|
+
around_reflex :around, unless: lambda { true }
|
255
|
+
after_reflex :after, unless: proc { false }
|
256
|
+
|
257
|
+
def increment
|
258
|
+
@count += 8
|
259
|
+
end
|
260
|
+
|
261
|
+
private
|
262
|
+
|
263
|
+
def init
|
264
|
+
@count = 5
|
265
|
+
end
|
266
|
+
|
267
|
+
def around
|
268
|
+
@count += 2
|
269
|
+
yield
|
270
|
+
@count += 4
|
271
|
+
end
|
272
|
+
|
273
|
+
def after
|
274
|
+
@count += 10
|
275
|
+
end
|
276
|
+
end
|
277
|
+
|
278
|
+
reflex = UnlessProcCallbackReflex.new(subscribe, url: "https://test.stimulusreflex.com", method_name: :increment)
|
279
|
+
reflex.process(:increment)
|
280
|
+
assert_equal 23, reflex.instance_variable_get("@count")
|
281
|
+
end
|
282
|
+
|
283
|
+
test "only option works" do
|
284
|
+
class OnlyCallbackReflex < StimulusReflex::Reflex
|
285
|
+
before_reflex :init
|
286
|
+
before_reflex :increment_bonus, only: :increment
|
287
|
+
before_reflex :decrement_bonus, only: [:decrement]
|
288
|
+
|
289
|
+
def increment
|
290
|
+
@count += 1
|
291
|
+
end
|
292
|
+
|
293
|
+
def decrement
|
294
|
+
@count -= 1
|
295
|
+
end
|
296
|
+
|
297
|
+
private
|
298
|
+
|
299
|
+
def init
|
300
|
+
@count = 0
|
301
|
+
end
|
302
|
+
|
303
|
+
def increment_bonus
|
304
|
+
@count += 5
|
305
|
+
end
|
306
|
+
|
307
|
+
def decrement_bonus
|
308
|
+
@count -= 7
|
309
|
+
end
|
310
|
+
end
|
311
|
+
|
312
|
+
reflex = OnlyCallbackReflex.new(subscribe, url: "https://test.stimulusreflex.com", method_name: :decrement)
|
313
|
+
reflex.process(:decrement)
|
314
|
+
assert_equal(-8, reflex.instance_variable_get("@count"))
|
315
|
+
end
|
316
|
+
|
317
|
+
test "except option works" do
|
318
|
+
class ExceptCallbackReflex < StimulusReflex::Reflex
|
319
|
+
before_reflex :init
|
320
|
+
before_reflex :increment_bonus, except: [:decrement]
|
321
|
+
before_reflex :decrement_bonus, except: :increment
|
322
|
+
|
323
|
+
def increment
|
324
|
+
@count += 1
|
325
|
+
end
|
326
|
+
|
327
|
+
def decrement
|
328
|
+
@count -= 1
|
329
|
+
end
|
330
|
+
|
331
|
+
private
|
332
|
+
|
333
|
+
def init
|
334
|
+
@count = 0
|
335
|
+
end
|
336
|
+
|
337
|
+
def increment_bonus
|
338
|
+
@count += 5
|
339
|
+
end
|
340
|
+
|
341
|
+
def decrement_bonus
|
342
|
+
@count -= 7
|
343
|
+
end
|
344
|
+
end
|
345
|
+
|
346
|
+
reflex = ExceptCallbackReflex.new(subscribe, url: "https://test.stimulusreflex.com", method_name: :increment)
|
347
|
+
reflex.process(:increment)
|
348
|
+
assert_equal 6, reflex.instance_variable_get("@count")
|
349
|
+
|
350
|
+
reflex = ExceptCallbackReflex.new(subscribe, url: "https://test.stimulusreflex.com", method_name: :decrement)
|
351
|
+
reflex.process(:decrement)
|
352
|
+
assert_equal(-8, reflex.instance_variable_get("@count"))
|
353
|
+
end
|
354
|
+
|
355
|
+
test "skip_before_reflex works" do
|
356
|
+
class SkipBeforeCallbackReflex < StimulusReflex::Reflex
|
357
|
+
before_reflex :blowup
|
358
|
+
before_reflex :init_counter
|
359
|
+
before_reflex :bonus
|
360
|
+
|
361
|
+
skip_before_reflex :blowup
|
362
|
+
skip_before_reflex :init_counter, if: -> { false }
|
363
|
+
skip_before_reflex :bonus, if: -> { true }
|
364
|
+
|
365
|
+
def increment
|
366
|
+
@count += 1
|
367
|
+
end
|
368
|
+
|
369
|
+
private
|
370
|
+
|
371
|
+
def blowup
|
372
|
+
raise StandardError
|
373
|
+
end
|
374
|
+
|
375
|
+
def init_counter
|
376
|
+
@count = 5
|
377
|
+
end
|
378
|
+
|
379
|
+
def bonus
|
380
|
+
@count += 100
|
381
|
+
end
|
382
|
+
end
|
383
|
+
|
384
|
+
reflex = SkipBeforeCallbackReflex.new(subscribe, url: "https://test.stimulusreflex.com", method_name: :increment)
|
385
|
+
reflex.process(:increment)
|
386
|
+
assert_equal 6, reflex.instance_variable_get("@count")
|
387
|
+
end
|
388
|
+
|
389
|
+
test "skip_after_reflex works" do
|
390
|
+
class SkipAfterCallbackReflex < StimulusReflex::Reflex
|
391
|
+
after_reflex :blowup
|
392
|
+
after_reflex :reset
|
393
|
+
after_reflex :clear
|
394
|
+
|
395
|
+
skip_after_reflex :blowup
|
396
|
+
skip_after_reflex :reset, if: -> { false }
|
397
|
+
skip_after_reflex :clear, if: -> { true }
|
398
|
+
|
399
|
+
def increment
|
400
|
+
@count = 0
|
401
|
+
end
|
402
|
+
|
403
|
+
private
|
404
|
+
|
405
|
+
def blowup
|
406
|
+
raise StandardError
|
407
|
+
end
|
408
|
+
|
409
|
+
def reset
|
410
|
+
@count += 1
|
411
|
+
end
|
412
|
+
|
413
|
+
def clear
|
414
|
+
@count += 10
|
415
|
+
end
|
416
|
+
end
|
417
|
+
|
418
|
+
reflex = SkipAfterCallbackReflex.new(subscribe, url: "https://test.stimulusreflex.com", method_name: :increment)
|
419
|
+
reflex.process(:increment)
|
420
|
+
assert_equal 1, reflex.instance_variable_get("@count")
|
421
|
+
end
|
422
|
+
|
423
|
+
test "skip_around_reflex works" do
|
424
|
+
class SkipAroundCallbackReflex < StimulusReflex::Reflex
|
425
|
+
around_reflex :blowup
|
426
|
+
around_reflex :around
|
427
|
+
around_reflex :bonus
|
428
|
+
|
429
|
+
skip_around_reflex :blowup
|
430
|
+
skip_around_reflex :around, if: -> { false }
|
431
|
+
skip_around_reflex :bonus, if: -> { true }
|
432
|
+
|
433
|
+
def increment
|
434
|
+
@count += 2
|
435
|
+
end
|
436
|
+
|
437
|
+
private
|
438
|
+
|
439
|
+
def blowup
|
440
|
+
raise StandardError
|
441
|
+
end
|
442
|
+
|
443
|
+
def around
|
444
|
+
@count = 1
|
445
|
+
yield
|
446
|
+
@count += 4
|
447
|
+
end
|
448
|
+
|
449
|
+
def bonus
|
450
|
+
@count += 100
|
451
|
+
yield
|
452
|
+
@count += 1000
|
453
|
+
end
|
454
|
+
end
|
455
|
+
|
456
|
+
reflex = SkipAroundCallbackReflex.new(subscribe, url: "https://test.stimulusreflex.com", method_name: :increment)
|
457
|
+
reflex.process(:increment)
|
458
|
+
assert_equal 7, reflex.instance_variable_get("@count")
|
459
|
+
end
|
460
|
+
|
461
|
+
test "skip_before_reflex works in inherited reflex" do
|
462
|
+
class SkipApplicationReflex < StimulusReflex::Reflex
|
463
|
+
before_reflex :blowup
|
464
|
+
before_reflex :init_counter
|
465
|
+
|
466
|
+
private
|
467
|
+
|
468
|
+
def blowup
|
469
|
+
raise StandardError
|
470
|
+
end
|
471
|
+
|
472
|
+
def init_counter
|
473
|
+
@count = 5
|
474
|
+
end
|
475
|
+
end
|
476
|
+
|
477
|
+
class InheritedSkipApplicationReflex < SkipApplicationReflex
|
478
|
+
skip_before_reflex :blowup
|
479
|
+
|
480
|
+
def increment
|
481
|
+
@count += 1
|
482
|
+
end
|
483
|
+
end
|
484
|
+
|
485
|
+
reflex = InheritedSkipApplicationReflex.new(subscribe, url: "https://test.stimulusreflex.com", method_name: :increment)
|
486
|
+
reflex.process(:increment)
|
487
|
+
assert_equal 6, reflex.instance_variable_get("@count")
|
488
|
+
end
|
489
|
+
|
490
|
+
test "basic prepend_before_reflex works" do
|
491
|
+
class SimplePrependBeforeCallbackReflex < StimulusReflex::Reflex
|
492
|
+
before_reflex :two
|
493
|
+
prepend_before_reflex :one
|
494
|
+
|
495
|
+
def increment
|
496
|
+
@count += 1 if @count == 2
|
497
|
+
end
|
498
|
+
|
499
|
+
private
|
500
|
+
|
501
|
+
def one
|
502
|
+
@count = 1
|
503
|
+
end
|
504
|
+
|
505
|
+
def two
|
506
|
+
@count += 1 if @count == 1
|
507
|
+
end
|
508
|
+
end
|
509
|
+
|
510
|
+
reflex = SimplePrependBeforeCallbackReflex.new(subscribe, url: "https://test.stimulusreflex.com", method_name: :increment)
|
511
|
+
reflex.process(:increment)
|
512
|
+
assert_equal 3, reflex.instance_variable_get("@count")
|
513
|
+
end
|
514
|
+
|
515
|
+
test "prepend_before_reflex with block works" do
|
516
|
+
class BlockPrependBeforeCallbackReflex < StimulusReflex::Reflex
|
517
|
+
before_reflex do
|
518
|
+
@count += 1 if @count == 1
|
519
|
+
end
|
520
|
+
|
521
|
+
prepend_before_reflex do
|
522
|
+
@count = 1
|
523
|
+
end
|
524
|
+
|
525
|
+
def increment
|
526
|
+
@count += 1 if @count == 2
|
527
|
+
end
|
528
|
+
end
|
529
|
+
|
530
|
+
reflex = BlockPrependBeforeCallbackReflex.new(subscribe, url: "https://test.stimulusreflex.com", method_name: :increment)
|
531
|
+
reflex.process(:increment)
|
532
|
+
assert_equal 3, reflex.instance_variable_get("@count")
|
533
|
+
end
|
534
|
+
|
535
|
+
test "basic prepend_before_reflex works in inherited reflex" do
|
536
|
+
class PrependBeforeCallbackReflex < StimulusReflex::Reflex
|
537
|
+
before_reflex :two
|
538
|
+
|
539
|
+
def increment
|
540
|
+
@count += 5 if @count == 4
|
541
|
+
end
|
542
|
+
|
543
|
+
private
|
544
|
+
|
545
|
+
def two
|
546
|
+
@count += 3 if @count == 1
|
547
|
+
end
|
548
|
+
end
|
549
|
+
|
550
|
+
class InheritedPrependBeforeCallbackReflex < PrependBeforeCallbackReflex
|
551
|
+
prepend_before_reflex :one
|
552
|
+
|
553
|
+
private
|
554
|
+
|
555
|
+
def one
|
556
|
+
@count = 1
|
557
|
+
end
|
558
|
+
end
|
559
|
+
|
560
|
+
reflex = InheritedPrependBeforeCallbackReflex.new(subscribe, url: "https://test.stimulusreflex.com", method_name: :increment)
|
561
|
+
reflex.process(:increment)
|
562
|
+
assert_equal 9, reflex.instance_variable_get("@count")
|
563
|
+
end
|
564
|
+
|
565
|
+
test "basic prepend_around_reflex works" do
|
566
|
+
class SimplePrependAroundCallbackReflex < StimulusReflex::Reflex
|
567
|
+
around_reflex :two
|
568
|
+
prepend_around_reflex :one
|
569
|
+
|
570
|
+
def increment
|
571
|
+
@count += 10
|
572
|
+
end
|
573
|
+
|
574
|
+
private
|
575
|
+
|
576
|
+
def one
|
577
|
+
@count = 1
|
578
|
+
yield
|
579
|
+
@count += 3 if @count == 23
|
580
|
+
end
|
581
|
+
|
582
|
+
def two
|
583
|
+
@count += 5 if @count == 1
|
584
|
+
yield
|
585
|
+
@count += 7 if @count == 16
|
586
|
+
end
|
587
|
+
end
|
588
|
+
|
589
|
+
reflex = SimplePrependAroundCallbackReflex.new(subscribe, url: "https://test.stimulusreflex.com", method_name: :increment)
|
590
|
+
reflex.process(:increment)
|
591
|
+
assert_equal 26, reflex.instance_variable_get("@count")
|
592
|
+
end
|
593
|
+
|
594
|
+
test "basic prepend_after_reflex works" do
|
595
|
+
class SimplePrependAfterCallbackReflex < StimulusReflex::Reflex
|
596
|
+
after_reflex :two
|
597
|
+
prepend_after_reflex :one
|
598
|
+
|
599
|
+
def increment
|
600
|
+
@count = 1
|
601
|
+
end
|
602
|
+
|
603
|
+
private
|
604
|
+
|
605
|
+
def one
|
606
|
+
@count += 3 if @count == 6
|
607
|
+
end
|
608
|
+
|
609
|
+
def two
|
610
|
+
@count += 5 if @count == 1
|
611
|
+
end
|
612
|
+
end
|
613
|
+
|
614
|
+
reflex = SimplePrependAfterCallbackReflex.new(subscribe, url: "https://test.stimulusreflex.com", method_name: :increment)
|
615
|
+
reflex.process(:increment)
|
616
|
+
assert_equal 9, reflex.instance_variable_get("@count")
|
617
|
+
end
|
618
|
+
|
619
|
+
test "append before_, around_ and after_reflex works" do
|
620
|
+
class AppendCallbackReflex < StimulusReflex::Reflex
|
621
|
+
append_before_reflex :before
|
622
|
+
append_around_reflex :around
|
623
|
+
append_after_reflex :after
|
624
|
+
|
625
|
+
def increment
|
626
|
+
@count += 5 if @count == 4
|
627
|
+
end
|
628
|
+
|
629
|
+
private
|
630
|
+
|
631
|
+
def before
|
632
|
+
@count = 1 unless @counts
|
633
|
+
end
|
634
|
+
|
635
|
+
def around
|
636
|
+
@count += 3 if @count == 1
|
637
|
+
yield
|
638
|
+
@count += 9 if @count == 16
|
639
|
+
end
|
640
|
+
|
641
|
+
def after
|
642
|
+
@count += 7 if @count == 9
|
643
|
+
end
|
644
|
+
end
|
645
|
+
|
646
|
+
reflex = AppendCallbackReflex.new(subscribe, url: "https://test.stimulusreflex.com", method_name: :increment)
|
647
|
+
reflex.process(:increment)
|
648
|
+
assert_equal 25, reflex.instance_variable_get("@count")
|
649
|
+
end
|
650
|
+
end
|
651
|
+
|
652
|
+
# standard:enable Lint/ConstantDefinitionInBlock
|