stimulus_reflex 3.4.0.pre8 → 3.5.0.pre1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of stimulus_reflex might be problematic. Click here for more details.

Files changed (47) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +639 -463
  3. data/CODE_OF_CONDUCT.md +6 -0
  4. data/Gemfile.lock +103 -100
  5. data/LATEST +1 -0
  6. data/README.md +14 -13
  7. data/app/channels/stimulus_reflex/channel.rb +62 -67
  8. data/lib/generators/USAGE +1 -1
  9. data/lib/generators/stimulus_reflex/{config_generator.rb → initializer_generator.rb} +3 -3
  10. data/lib/generators/stimulus_reflex/templates/app/reflexes/%file_name%_reflex.rb.tt +3 -2
  11. data/lib/generators/stimulus_reflex/templates/app/reflexes/application_reflex.rb.tt +1 -1
  12. data/lib/generators/stimulus_reflex/templates/config/initializers/stimulus_reflex.rb +14 -0
  13. data/lib/stimulus_reflex.rb +11 -3
  14. data/lib/stimulus_reflex/broadcasters/broadcaster.rb +7 -4
  15. data/lib/stimulus_reflex/broadcasters/page_broadcaster.rb +2 -2
  16. data/lib/stimulus_reflex/broadcasters/selector_broadcaster.rb +12 -5
  17. data/lib/stimulus_reflex/cable_ready_channels.rb +30 -6
  18. data/lib/stimulus_reflex/callbacks.rb +98 -0
  19. data/lib/stimulus_reflex/concern_enhancer.rb +37 -0
  20. data/lib/stimulus_reflex/configuration.rb +3 -1
  21. data/lib/stimulus_reflex/element.rb +31 -7
  22. data/lib/stimulus_reflex/policies/reflex_invocation_policy.rb +28 -0
  23. data/lib/stimulus_reflex/reflex.rb +57 -57
  24. data/lib/stimulus_reflex/reflex_data.rb +79 -0
  25. data/lib/stimulus_reflex/reflex_factory.rb +31 -0
  26. data/lib/stimulus_reflex/request_parameters.rb +19 -0
  27. data/lib/stimulus_reflex/{logger.rb → utils/logger.rb} +6 -8
  28. data/lib/stimulus_reflex/{sanity_checker.rb → utils/sanity_checker.rb} +65 -10
  29. data/lib/stimulus_reflex/version.rb +1 -1
  30. data/lib/tasks/stimulus_reflex/install.rake +14 -7
  31. data/test/broadcasters/broadcaster_test.rb +2 -0
  32. data/test/broadcasters/broadcaster_test_case.rb +3 -1
  33. data/test/broadcasters/nothing_broadcaster_test.rb +7 -3
  34. data/test/broadcasters/page_broadcaster_test.rb +10 -4
  35. data/test/broadcasters/selector_broadcaster_test.rb +173 -55
  36. data/test/callbacks_test.rb +652 -0
  37. data/test/concern_enhancer_test.rb +54 -0
  38. data/test/element_test.rb +181 -0
  39. data/test/reflex_test.rb +7 -1
  40. data/test/test_helper.rb +10 -0
  41. data/test/tmp/app/reflexes/application_reflex.rb +1 -1
  42. data/test/tmp/app/reflexes/demo_reflex.rb +3 -2
  43. metadata +45 -36
  44. data/package.json +0 -57
  45. data/stimulus_reflex.gemspec +0 -40
  46. data/tags +0 -139
  47. data/yarn.lock +0 -4711
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module StimulusReflex
4
- VERSION = "3.4.0.pre8"
4
+ VERSION = "3.5.0.pre1"
5
5
  end
@@ -6,9 +6,10 @@ require "stimulus_reflex/version"
6
6
  namespace :stimulus_reflex do
7
7
  desc "Install StimulusReflex in this application"
8
8
  task install: :environment do
9
- system "bundle exec rails webpacker:install:stimulus"
9
+ system "rails dev:cache" unless Rails.root.join("tmp", "caching-dev.txt").exist?
10
10
  gem_version = StimulusReflex::VERSION.gsub(".pre", "-pre")
11
11
  system "yarn add stimulus_reflex@#{gem_version}"
12
+ system "bundle exec rails webpacker:install:stimulus"
12
13
  main_folder = defined?(Webpacker) ? Webpacker.config.source_path.to_s.gsub("#{Rails.root}/", "") : "app/javascript"
13
14
 
14
15
  FileUtils.mkdir_p Rails.root.join("#{main_folder}/controllers"), verbose: true
@@ -39,18 +40,20 @@ namespace :stimulus_reflex do
39
40
 
40
41
  unless lines.find { |line| line.start_with?("import controller") }
41
42
  matches = lines.select { |line| line =~ /\A(require|import)/ }
42
- lines.insert lines.index(matches.last).to_i + 1, "import controller from './application_controller'\n"
43
+ lines.insert lines.index(matches.last).to_i + 1, "import controller from '../controllers/application_controller'\n"
43
44
  end
44
45
 
45
46
  initialize_line = lines.find { |line| line.start_with?("StimulusReflex.initialize") }
46
- lines << "StimulusReflex.initialize(application, { consumer, controller, isolate: true })\n" unless initialize_line
47
+ lines << "application.consumer = consumer\n"
48
+ lines << "StimulusReflex.initialize(application, { controller, isolate: true })\n" unless initialize_line
47
49
  lines << "StimulusReflex.debug = process.env.RAILS_ENV === 'development'\n" unless initialize_line
48
50
  File.open(filepath, "w") { |f| f.write lines.join }
49
51
 
50
52
  filepath = Rails.root.join("config/environments/development.rb")
51
53
  lines = File.open(filepath, "r") { |f| f.readlines }
52
54
  unless lines.find { |line| line.include?("config.session_store") }
53
- lines.insert 3, " config.session_store :cache_store\n\n"
55
+ matches = lines.select { |line| line =~ /\A(Rails.application.configure do)/ }
56
+ lines.insert lines.index(matches.last).to_i + 1, " config.session_store :cache_store\n\n"
54
57
  File.open(filepath, "w") { |f| f.write lines.join }
55
58
  end
56
59
 
@@ -60,13 +63,17 @@ namespace :stimulus_reflex do
60
63
  lines.delete_at 1
61
64
  lines.insert 1, " adapter: redis\n"
62
65
  lines.insert 2, " url: <%= ENV.fetch(\"REDIS_URL\") { \"redis://localhost:6379/1\" } %>\n"
63
- lines.insert 3, " channel_prefix: " + File.basename(Rails.root.to_s).underscore + "_development\n"
66
+ lines.insert 3, " channel_prefix: " + File.basename(Rails.root.to_s).tr("\\", "").tr("-. ", "_").underscore + "_development\n"
64
67
  File.open(filepath, "w") { |f| f.write lines.join }
65
68
  end
66
69
 
67
70
  system "bundle exec rails generate stimulus_reflex example"
68
71
  puts "Generating default StimulusReflex configuration file into your application config/initializers directory"
69
- system "bundle exec rails generate stimulus_reflex:config"
70
- system "rails dev:cache" unless Rails.root.join("tmp", "caching-dev.txt").exist?
72
+ system "bundle exec rails generate stimulus_reflex:initializer"
73
+
74
+ puts
75
+ puts "StimulusReflex and CableReady have been successfully installed!"
76
+ puts "Go to https://docs.stimulusreflex.com/hello-world/quickstart if you need help getting started."
77
+ puts
71
78
  end
72
79
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative "broadcaster_test_case"
2
4
 
3
5
  class StimulusReflex::BroadcasterTest < StimulusReflex::BroadcasterTestCase
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative "../test_helper"
2
4
 
3
5
  class StimulusReflex::BroadcasterTestCase < ActionCable::Channel::TestCase
@@ -8,6 +10,6 @@ class StimulusReflex::BroadcasterTestCase < ActionCable::Channel::TestCase
8
10
  def connection.env
9
11
  @env ||= {}
10
12
  end
11
- @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"})
12
14
  end
13
15
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative "broadcaster_test_case"
2
4
 
3
5
  class StimulusReflex::NothingBroadcasterTest < StimulusReflex::BroadcasterTestCase
@@ -11,7 +13,8 @@ class StimulusReflex::NothingBroadcasterTest < StimulusReflex::BroadcasterTestCa
11
13
  {
12
14
  "name" => "stimulus-reflex:server-message",
13
15
  "detail" => {
14
- "reflexId" => nil,
16
+ "reflexId" => "666",
17
+ "payload" => {},
15
18
  "stimulusReflex" => {
16
19
  "some" => :data,
17
20
  "morph" => :nothing,
@@ -20,14 +23,15 @@ class StimulusReflex::NothingBroadcasterTest < StimulusReflex::BroadcasterTestCa
20
23
  "body" => nil
21
24
  }
22
25
  }
23
- }
26
+ },
27
+ "reflexId" => "666"
24
28
  }
25
29
  ]
26
30
  }
27
31
  }
28
32
 
29
33
  assert_broadcast_on @reflex.stream_name, expected do
30
- broadcaster.broadcast nil, some: :data
34
+ broadcaster.broadcast nil, {:some => :data, "reflexId" => "666"}
31
35
  end
32
36
  end
33
37
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative "broadcaster_test_case"
2
4
 
3
5
  class StimulusReflex::PageBroadcasterTest < StimulusReflex::BroadcasterTestCase
@@ -10,7 +12,7 @@ class StimulusReflex::PageBroadcasterTest < StimulusReflex::BroadcasterTestCase
10
12
  test "performs a page morph on body" do
11
13
  class << @reflex.controller.response
12
14
  def body
13
- "<html><head></head><body>New Content</body></html>"
15
+ "<html><head></head><body><div>New Content</div><div>Another Content</div></body></html>"
14
16
  end
15
17
  end
16
18
 
@@ -22,13 +24,15 @@ class StimulusReflex::PageBroadcasterTest < StimulusReflex::BroadcasterTestCase
22
24
  "morph" => [
23
25
  {
24
26
  "selector" => "body",
25
- "html" => "New Content",
27
+ "html" => "<div>New Content</div><div>Another Content</div>",
28
+ "payload" => {},
26
29
  "childrenOnly" => true,
27
30
  "permanentAttributeName" => nil,
28
31
  "stimulusReflex" => {
29
32
  "some" => :data,
30
33
  "morph" => :page
31
- }
34
+ },
35
+ "reflexId" => "666"
32
36
  }
33
37
  ]
34
38
  }
@@ -55,12 +59,14 @@ class StimulusReflex::PageBroadcasterTest < StimulusReflex::BroadcasterTestCase
55
59
  {
56
60
  "selector" => "#foo",
57
61
  "html" => "New Content",
62
+ "payload" => {},
58
63
  "childrenOnly" => true,
59
64
  "permanentAttributeName" => nil,
60
65
  "stimulusReflex" => {
61
66
  "some" => :data,
62
67
  "morph" => :page
63
- }
68
+ },
69
+ "reflexId" => "666"
64
70
  }
65
71
  ]
66
72
  }
@@ -1,55 +1,173 @@
1
- # require_relative "broadcaster_test_case"
2
-
3
- # class StimulusReflex::SelectorBroadcasterTest < StimulusReflex::BroadcasterTestCase
4
- # test "morphs the contents of an element if the selector(s) are present in both original and morphed html fragments" do
5
- # broadcaster = StimulusReflex::SelectorBroadcaster.new(@reflex)
6
- # broadcaster.append_morph("#foo", "<div id=\"foo\"><span>bar</span></div>")
7
-
8
- # expected = {
9
- # "cableReady" => true,
10
- # "operations" => {
11
- # "morph" => [
12
- # {
13
- # "selector" => "#foo",
14
- # "html" => "<span>bar</span>",
15
- # "childrenOnly" => true,
16
- # "permanentAttributeName" => nil,
17
- # "stimulusReflex" => {
18
- # "some" => :data,
19
- # "morph" => :selector
20
- # }
21
- # }
22
- # ]
23
- # }
24
- # }
25
-
26
- # assert_broadcast_on @reflex.stream_name, expected do
27
- # broadcaster.broadcast nil, some: :data
28
- # end
29
- # end
30
-
31
- # test "replaces the contents of an element and ignores permanent-attributes if the selector(s) aren't present in the replacing html fragment" do
32
- # broadcaster = StimulusReflex::SelectorBroadcaster.new(@reflex)
33
- # broadcaster.append_morph("#foo", "<div id=\"baz\"><span>bar</span></div>")
34
-
35
- # expected = {
36
- # "cableReady" => true,
37
- # "operations" => {
38
- # "innerHtml" => [
39
- # {
40
- # "selector" => "#foo",
41
- # "html" => "<div id=\"baz\"><span>bar</span></div>",
42
- # "stimulusReflex" => {
43
- # "some" => :data,
44
- # "morph" => :selector
45
- # }
46
- # }
47
- # ]
48
- # }
49
- # }
50
-
51
- # assert_broadcast_on @reflex.stream_name, expected do
52
- # broadcaster.broadcast nil, some: :data
53
- # end
54
- # end
55
- # end
1
+ # frozen_string_literal: true
2
+
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