stimulus_reflex 3.1.4 → 3.2.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a559b60e0f9fa9519b36dbdec1a217f15eecbf2a3bff051c20faf91d046c9e89
4
- data.tar.gz: 04af4bf702cd97e692d2f0c0b6396e8c1d86821a53d7352854753b524aa7ba92
3
+ metadata.gz: ea3e0dabfb3b7b82b035f2b63db76ea575452a465e07bd992dd3d7a270e89dab
4
+ data.tar.gz: 1f26c53eaa26520dd2697c50996b690f6116476eb6235bf126c2fc31991b677c
5
5
  SHA512:
6
- metadata.gz: 8b5f428fe17bddfeff38c20d400b0b7a25f211b000acd5a0c0578a46379762371ff11f67e81243e20d08fdbeeae585e48c65b2aeb17f7bb6bfc228da7ecd62ce
7
- data.tar.gz: 22df1e093ae82f7454fbd60501622d6d083e69e5c00732dbf953221b9a113d901aa7d347ee82b19a74b9bed75e918c60749bc71caf0f8e9f038619e0464dcf6d
6
+ metadata.gz: ac488c852fa0af0083522553addc0423b99914981719ef988f03101ded5a7be2dfe448959ac0640a5082a0e40f12e303788ddde0fcd4086a4dcd831ba408a6d7
7
+ data.tar.gz: 8c80b5cae19be15c6dd3519565872b92de7729b19a7a768eebcfe342ebe66916231a11ddf0bed224da574f7ea4695fad8cf8f4fc98ecfbdc837eb620215ea78f
data/CHANGELOG.md CHANGED
@@ -2,7 +2,37 @@
2
2
 
3
3
  ## [Unreleased](https://github.com/hopsoft/stimulus_reflex/tree/HEAD)
4
4
 
5
- [Full Changelog](https://github.com/hopsoft/stimulus_reflex/compare/v3.1.3...HEAD)
5
+ [Full Changelog](https://github.com/hopsoft/stimulus_reflex/compare/v3.1.4...HEAD)
6
+
7
+ **Implemented enhancements:**
8
+
9
+ - Stimulate without a reflex target [\#179](https://github.com/hopsoft/stimulus_reflex/issues/179)
10
+ - Reflex callbacks [\#155](https://github.com/hopsoft/stimulus_reflex/issues/155)
11
+ - Replace camelize with homegrown version [\#184](https://github.com/hopsoft/stimulus_reflex/pull/184) ([jonathan-s](https://github.com/jonathan-s))
12
+ - Replace uuid4 dependency with function in repo [\#181](https://github.com/hopsoft/stimulus_reflex/pull/181) ([jonathan-s](https://github.com/jonathan-s))
13
+ - Allow channel exceptions to be rescuable [\#180](https://github.com/hopsoft/stimulus_reflex/pull/180) ([dark-panda](https://github.com/dark-panda))
14
+ - add console log messages for every reflex call [\#163](https://github.com/hopsoft/stimulus_reflex/pull/163) ([marcoroth](https://github.com/marcoroth))
15
+ - add reflex callbacks [\#160](https://github.com/hopsoft/stimulus_reflex/pull/160) ([seb1441](https://github.com/seb1441))
16
+
17
+ **Fixed bugs:**
18
+
19
+ - Pluralize the generated class name, so that will match with the file name [\#178](https://github.com/hopsoft/stimulus_reflex/pull/178) ([darkrubyist](https://github.com/darkrubyist))
20
+ - remove changelog rake task [\#150](https://github.com/hopsoft/stimulus_reflex/pull/150) ([andrewmcodes](https://github.com/andrewmcodes))
21
+
22
+ **Closed issues:**
23
+
24
+ - The ActionCable connection is not open! `this.isActionCableConnectionOpen\(\)` must return true before calling `this.stimulate\(\)` [\#187](https://github.com/hopsoft/stimulus_reflex/issues/187)
25
+ - Promises just resolve with last Partial DOM update [\#171](https://github.com/hopsoft/stimulus_reflex/issues/171)
26
+
27
+ **Merged pull requests:**
28
+
29
+ - Some housekeeping [\#189](https://github.com/hopsoft/stimulus_reflex/pull/189) ([hopsoft](https://github.com/hopsoft))
30
+ - Allow to call stimulate without a reflex target [\#188](https://github.com/hopsoft/stimulus_reflex/pull/188) ([marcoroth](https://github.com/marcoroth))
31
+ - Fix bug in super documentation [\#174](https://github.com/hopsoft/stimulus_reflex/pull/174) ([silva96](https://github.com/silva96))
32
+
33
+ ## [v3.1.4](https://github.com/hopsoft/stimulus_reflex/tree/v3.1.4) (2020-04-27)
34
+
35
+ [Full Changelog](https://github.com/hopsoft/stimulus_reflex/compare/v3.1.3...v3.1.4)
6
36
 
7
37
  **Implemented enhancements:**
8
38
 
@@ -62,7 +92,6 @@
62
92
  **Fixed bugs:**
63
93
 
64
94
  - Cannot read property 'removeEventListener' of undefined after updating to 3.1.0 [\#151](https://github.com/hopsoft/stimulus_reflex/issues/151)
65
- - remove changelog rake task [\#150](https://github.com/hopsoft/stimulus_reflex/pull/150) ([andrewmcodes](https://github.com/andrewmcodes))
66
95
 
67
96
  **Closed issues:**
68
97
 
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- stimulus_reflex (3.1.4)
4
+ stimulus_reflex (3.2.0.pre0)
5
5
  cable_ready (>= 4.1.2)
6
6
  nokogiri
7
7
  rack
@@ -86,7 +86,7 @@ GEM
86
86
  marcel (0.3.3)
87
87
  mimemagic (~> 0.3.2)
88
88
  method_source (0.9.2)
89
- mimemagic (0.3.4)
89
+ mimemagic (0.3.5)
90
90
  mini_mime (1.0.2)
91
91
  mini_portile2 (2.4.0)
92
92
  minitest (5.14.0)
@@ -94,7 +94,7 @@ GEM
94
94
  nokogiri (1.10.9)
95
95
  mini_portile2 (~> 2.4.0)
96
96
  parallel (1.19.1)
97
- parser (2.7.1.1)
97
+ parser (2.7.1.2)
98
98
  ast (~> 2.4.0)
99
99
  pry (0.12.2)
100
100
  coderay (~> 1.1.0)
@@ -133,14 +133,14 @@ GEM
133
133
  rainbow (3.0.0)
134
134
  rake (13.0.1)
135
135
  rexml (3.2.4)
136
- rubocop (0.80.1)
136
+ rubocop (0.82.0)
137
137
  jaro_winkler (~> 1.5.1)
138
138
  parallel (~> 1.10)
139
139
  parser (>= 2.7.0.1)
140
140
  rainbow (>= 2.2.2, < 4.0)
141
141
  rexml
142
142
  ruby-progressbar (~> 1.7)
143
- unicode-display_width (>= 1.4.0, < 1.7)
143
+ unicode-display_width (>= 1.4.0, < 2.0)
144
144
  rubocop-performance (1.5.2)
145
145
  rubocop (>= 0.71.0)
146
146
  ruby-progressbar (1.10.1)
@@ -151,8 +151,8 @@ GEM
151
151
  actionpack (>= 4.0)
152
152
  activesupport (>= 4.0)
153
153
  sprockets (>= 3.0.0)
154
- standard (0.2.4)
155
- rubocop (~> 0.80.1)
154
+ standard (0.3.0)
155
+ rubocop (~> 0.82.0)
156
156
  rubocop-performance (~> 1.5.2)
157
157
  standardrb (1.0.0)
158
158
  standard
@@ -160,7 +160,7 @@ GEM
160
160
  thread_safe (0.3.6)
161
161
  tzinfo (1.2.7)
162
162
  thread_safe (~> 0.1)
163
- unicode-display_width (1.6.1)
163
+ unicode-display_width (1.7.0)
164
164
  websocket-driver (0.7.1)
165
165
  websocket-extensions (>= 0.1.0)
166
166
  websocket-extensions (0.1.4)
data/README.md CHANGED
@@ -11,7 +11,7 @@
11
11
  <img alt="License: MIT" src="https://img.shields.io/badge/license-MIT-brightgreen.svg" target="_blank" />
12
12
  </a>
13
13
  <a href="http://blog.codinghorror.com/the-best-code-is-no-code-at-all/" target="_blank">
14
- <img alt="Lines of Code" src="https://img.shields.io/badge/lines_of_code-684-brightgreen.svg?style=flat" />
14
+ <img alt="Lines of Code" src="https://img.shields.io/badge/lines_of_code-805-brightgreen.svg?style=flat" />
15
15
  </a>
16
16
  <a href="https://docs.stimulusreflex.com/" target="_blank">
17
17
  <img alt="Documentation" src="https://img.shields.io/badge/documentation-yes-brightgreen.svg" />
@@ -68,6 +68,7 @@ This project strives to live up to the vision outlined in [The Rails Doctrine](h
68
68
 
69
69
  ## ✨ Demos
70
70
 
71
+ - https://youtu.be/F5hA79vKE_E
71
72
  - http://expo.stimulusreflex.com
72
73
 
73
74
  ## 💙 Community
@@ -5,6 +5,10 @@ require "rails/generators"
5
5
  class StimulusReflexGenerator < Rails::Generators::NamedBase
6
6
  source_root File.expand_path("templates", __dir__)
7
7
 
8
+ def initialize_application_reflexes
9
+ copy_application_files if behavior == :invoke
10
+ end
11
+
8
12
  def initialize_reflexes
9
13
  copy_reflex_files
10
14
  end
@@ -19,12 +23,15 @@ class StimulusReflexGenerator < Rails::Generators::NamedBase
19
23
  REFLEX_BASE_PATH = "app/reflexes"
20
24
 
21
25
  def copy_reflex_files
22
- template "application_reflex.rb", File.join(REFLEX_BASE_PATH, "application_reflex.rb")
23
26
  template "custom_reflex.rb", File.join(REFLEX_BASE_PATH, "#{name.underscore}_reflex.rb")
24
27
  end
25
28
 
26
29
  def copy_controller_files
27
- template "application_controller.js", File.join(CONTROLLER_BASE_PATH, "application_controller.js")
28
30
  template "custom_controller.js", File.join(CONTROLLER_BASE_PATH, "#{name.underscore}_controller.js")
29
31
  end
32
+
33
+ def copy_application_files
34
+ template "application_reflex.rb", File.join(REFLEX_BASE_PATH, "application_reflex.rb")
35
+ template "application_controller.js", File.join(CONTROLLER_BASE_PATH, "application_controller.js")
36
+ end
30
37
  end
@@ -1,6 +1,6 @@
1
1
  import ApplicationController from './application_controller'
2
2
 
3
- /* This is the custom StimulusReflex controller for <%= @name.classify %>Reflex.
3
+ /* This is the custom StimulusReflex controller for <%= class_name %>Reflex.
4
4
  * Learn more at: https://docs.stimulusreflex.com
5
5
  */
6
6
  export default class extends ApplicationController {
@@ -10,14 +10,14 @@ export default class extends ApplicationController {
10
10
  *
11
11
  * Example:
12
12
  *
13
- * <a href="#" data-reflex="<%= @name.classify %>Reflex#example">Example</a>
13
+ * <a href="#" data-reflex="<%= class_name %>Reflex#example">Example</a>
14
14
  *
15
15
  * Arguments:
16
16
  *
17
17
  * element - the element that triggered the reflex
18
18
  * may be different than the Stimulus controller's this.element
19
19
  *
20
- * reflex - the name of the reflex e.g. "<%= @name.classify %>Reflex#example"
20
+ * reflex - the name of the reflex e.g. "<%= class_name %>Reflex#example"
21
21
  *
22
22
  * error - error message from the server
23
23
  */
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- class <%= @name.classify %>Reflex < ApplicationReflex
3
+ class <%= class_name %>Reflex < ApplicationReflex
4
4
  # Add Reflex methods in this file.
5
5
  #
6
6
  # All Reflex instances expose the following properties:
@@ -28,16 +28,18 @@ class StimulusReflex::Channel < ActionCable::Channel::Base
28
28
  begin
29
29
  reflex_class = reflex_name.constantize
30
30
  raise ArgumentError.new("#{reflex_name} is not a StimulusReflex::Reflex") unless is_reflex?(reflex_class)
31
- reflex = reflex_class.new(self, url: url, element: element, selectors: selectors)
31
+ reflex = reflex_class.new(self, url: url, element: element, selectors: selectors, method_name: method_name)
32
32
  delegate_call_to_reflex reflex, method_name, arguments
33
33
  rescue => invoke_error
34
+ reflex.rescue_with_handler(invoke_error)
34
35
  message = exception_message_with_backtrace(invoke_error)
35
36
  return broadcast_error("StimulusReflex::Channel Failed to invoke #{target}! #{url} #{message}", data)
36
37
  end
37
38
 
38
39
  begin
39
- render_page_and_broadcast_morph reflex, selectors, data
40
+ render_page_and_broadcast_morph reflex, selectors, data unless reflex.halted?
40
41
  rescue => render_error
42
+ reflex.rescue_with_handler(render_error)
41
43
  message = exception_message_with_backtrace(render_error)
42
44
  broadcast_error "StimulusReflex::Channel Failed to re-render #{url} #{message}", data
43
45
  end
@@ -55,9 +57,9 @@ class StimulusReflex::Channel < ActionCable::Channel::Base
55
57
  optional_params = method.parameters.select { |(kind, _)| kind == :opt }
56
58
 
57
59
  if arguments.size == 0 && required_params.size == 0
58
- reflex.public_send method_name
60
+ reflex.process(method_name)
59
61
  elsif arguments.size >= required_params.size && arguments.size <= required_params.size + optional_params.size
60
- reflex.public_send method_name, *arguments
62
+ reflex.process(method_name, *arguments)
61
63
  else
62
64
  raise ArgumentError.new("wrong number of arguments (given #{arguments.inspect}, expected #{required_params.inspect}, optional #{optional_params.inspect})")
63
65
  end
@@ -1,16 +1,58 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class StimulusReflex::Reflex
4
- attr_reader :channel, :url, :element, :selectors
4
+ include ActiveSupport::Rescuable
5
+ include ActiveSupport::Callbacks
6
+
7
+ define_callbacks :process, skip_after_callbacks_if_terminated: true
8
+
9
+ class << self
10
+ def before_reflex(*args, &block)
11
+ add_callback(:before, *args, &block)
12
+ end
13
+
14
+ def after_reflex(*args, &block)
15
+ add_callback(:after, *args, &block)
16
+ end
17
+
18
+ def around_reflex(*args, &block)
19
+ add_callback(:around, *args, &block)
20
+ end
21
+
22
+ private
23
+
24
+ def add_callback(kind, *args, &block)
25
+ options = args.extract_options!
26
+ options.assert_valid_keys :if, :unless, :only, :except
27
+ set_callback(*[:process, kind, args, normalize_callback_options!(options)].flatten, &block)
28
+ end
29
+
30
+ def normalize_callback_options!(options)
31
+ normalize_callback_option! options, :only, :if
32
+ normalize_callback_option! options, :except, :unless
33
+ options
34
+ end
35
+
36
+ def normalize_callback_option!(options, from, to)
37
+ if (from = options.delete(from))
38
+ from_set = Array(from).map(&:to_s).to_set
39
+ from = proc { |reflex| from_set.include? reflex.method_name }
40
+ options[to] = Array(options[to]).unshift(from)
41
+ end
42
+ end
43
+ end
44
+
45
+ attr_reader :channel, :url, :element, :selectors, :method_name
5
46
 
6
47
  delegate :connection, to: :channel
7
48
  delegate :session, to: :request
8
49
 
9
- def initialize(channel, url: nil, element: nil, selectors: [])
50
+ def initialize(channel, url: nil, element: nil, selectors: [], method_name: nil)
10
51
  @channel = channel
11
52
  @url = url
12
53
  @element = element
13
54
  @selectors = selectors
55
+ @method_name = method_name
14
56
  end
15
57
 
16
58
  def request
@@ -41,4 +83,21 @@ class StimulusReflex::Reflex
41
83
  def url_params
42
84
  @url_params ||= Rails.application.routes.recognize_path_with_request(request, request.path, request.env[:extras] || {})
43
85
  end
86
+
87
+ def process(name, *args)
88
+ result = run_callbacks(:process) { public_send(name, *args) }
89
+ @halted ||= result == false
90
+ result
91
+ end
92
+
93
+ # Indicates if the callback chain was halted via a throw(:abort) in a before_reflex callback.
94
+ # SEE: https://api.rubyonrails.org/classes/ActiveSupport/Callbacks.html
95
+ # IMPORTANT: The reflex will not re-render the page if the callback chain is halted
96
+ def halted?
97
+ !!@halted
98
+ end
99
+
100
+ def default_reflex
101
+ # noop default reflex to force page reloads
102
+ end
44
103
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module StimulusReflex
4
- VERSION = "3.1.4"
4
+ VERSION = "3.2.0.pre0"
5
5
  end
@@ -18,13 +18,24 @@ namespace :stimulus_reflex do
18
18
  end
19
19
  puts "Updating #{filepath}"
20
20
  lines = File.open(filepath, "r") { |f| f.readlines }
21
- import_line = lines.find { |line| line.start_with?("import StimulusReflex") }
22
- initialize_line = lines.find { |line| line.start_with?("StimulusReflex.initialize") }
23
- unless import_line
21
+
22
+ unless lines.find { |line| line.start_with?("import StimulusReflex") }
24
23
  matches = lines.select { |line| line =~ /\A(require|import)/ }
25
24
  lines.insert lines.index(matches.last).to_i + 1, "import StimulusReflex from 'stimulus_reflex'\n"
26
25
  end
27
- lines << "StimulusReflex.initialize(application)\n" unless initialize_line
26
+
27
+ unless lines.find { |line| line.start_with?("import consumer") }
28
+ matches = lines.select { |line| line =~ /\A(require|import)/ }
29
+ lines.insert lines.index(matches.last).to_i + 1, "import consumer from '../channels/consumer'\n"
30
+ end
31
+
32
+ unless lines.find { |line| line.start_with?("import controller") }
33
+ matches = lines.select { |line| line =~ /\A(require|import)/ }
34
+ lines.insert lines.index(matches.last).to_i + 1, "import controller from './application_controller'\n"
35
+ end
36
+
37
+ initialize_line = lines.find { |line| line.start_with?("StimulusReflex.initialize") }
38
+ lines << "StimulusReflex.initialize(application, { consumer, controller, debug: false })\n" unless initialize_line
28
39
  File.open(filepath, "w") { |f| f.write lines.join }
29
40
 
30
41
  filepath = Rails.root.join("config/environments/development.rb")
@@ -8,11 +8,19 @@ class StimulusReflexGeneratorTest < Rails::Generators::TestCase
8
8
  destination File.expand_path("../../tmp", __FILE__)
9
9
  setup :prepare_destination
10
10
 
11
- test "creates named controller and reflex files" do
11
+ test "creates singular named controller and reflex files" do
12
12
  run_generator %w[demo]
13
13
  assert_file "app/javascript/controllers/application_controller.js"
14
14
  assert_file "app/javascript/controllers/demo_controller.js", /DemoReflex/
15
15
  assert_file "app/reflexes/application_reflex.rb"
16
16
  assert_file "app/reflexes/demo_reflex.rb", /DemoReflex/
17
17
  end
18
+
19
+ test "creates plural named controller and reflex files" do
20
+ run_generator %w[posts]
21
+ assert_file "app/javascript/controllers/application_controller.js"
22
+ assert_file "app/javascript/controllers/posts_controller.js", /PostsReflex/
23
+ assert_file "app/reflexes/application_reflex.rb"
24
+ assert_file "app/reflexes/posts_reflex.rb", /PostsReflex/
25
+ end
18
26
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stimulus_reflex
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.1.4
4
+ version: 3.2.0.pre0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nathan Hopkins
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-04-27 00:00:00.000000000 Z
11
+ date: 2020-05-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -197,11 +197,11 @@ required_ruby_version: !ruby/object:Gem::Requirement
197
197
  version: '0'
198
198
  required_rubygems_version: !ruby/object:Gem::Requirement
199
199
  requirements:
200
- - - ">="
200
+ - - ">"
201
201
  - !ruby/object:Gem::Version
202
- version: '0'
202
+ version: 1.3.1
203
203
  requirements: []
204
- rubygems_version: 3.1.2
204
+ rubygems_version: 3.0.3
205
205
  signing_key:
206
206
  specification_version: 4
207
207
  summary: Build reactive applications with the Rails tooling you already know and love.