stimulus_reflex 3.4.0.pre0 → 3.4.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.

checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 81b721f2742cc09bd39632481e120723205409806d86a0e98e99954fc10b4974
4
- data.tar.gz: ca698e3f7b6f3c2fe740429313b166605ad3bab37a74dae4b4fadc4f7e983a21
3
+ metadata.gz: 96bccb8addb37f4a5096017acee725da0b9b85316216adf56e5591b582b337c5
4
+ data.tar.gz: b7e3559981c86136e2db6f0a91eb7996cada8606c12eb23ab339220729bebb06
5
5
  SHA512:
6
- metadata.gz: 88c0a588d1e9e538bf53ad2a40cfcd504d4715ee9cef23c0aaf8a41909c8cd28243030e02ea9b9daf9c4dc56874e1c7f34decf082e21ea18195cf9523c830dc6
7
- data.tar.gz: c021b8e46203b36abb2386ad50f999bd05c396e0bec0ba943a667ceccc213ea24d6f3ac310c0eff922c518165b3879dcbd3ddae320c78b78821529514ee5d021
6
+ metadata.gz: fc0767685fc9cc55fce312f2112ec0b42682aeaae5200ca2f8b1371539e458cdfd55cf46335a22682a4fce50c851a9a996b7e12cf3caa4b12d3f309e3fb6ee4d
7
+ data.tar.gz: f28b5af48c24bd07542e37ee5f940df373cc7f74a068c67dddfd8eda0f31a1131802bceaa69713f2f69235955e0b33a79fda1fed33cad66653158eb0e84c6654
@@ -1,8 +1,8 @@
1
1
  # Changelog
2
2
 
3
- ## [Unreleased](https://github.com/hopsoft/stimulus_reflex/tree/HEAD)
3
+ ## [v3.4.0.pre0](https://github.com/hopsoft/stimulus_reflex/tree/v3.4.0.pre0) (2020-11-02)
4
4
 
5
- [Full Changelog](https://github.com/hopsoft/stimulus_reflex/compare/v3.3.0...HEAD)
5
+ [Full Changelog](https://github.com/hopsoft/stimulus_reflex/compare/v3.3.0...v3.4.0.pre0)
6
6
 
7
7
  **Implemented enhancements:**
8
8
 
@@ -335,7 +335,6 @@
335
335
  **Fixed bugs:**
336
336
 
337
337
  - 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))
338
- - Remove unneeded registerConsumer logic [\#158](https://github.com/hopsoft/stimulus_reflex/pull/158) ([hopsoft](https://github.com/hopsoft))
339
338
 
340
339
  **Closed issues:**
341
340
 
@@ -389,6 +388,7 @@
389
388
  **Fixed bugs:**
390
389
 
391
390
  - Unable to register the ActionCable Consumer [\#156](https://github.com/hopsoft/stimulus_reflex/issues/156)
391
+ - Remove unneeded registerConsumer logic [\#158](https://github.com/hopsoft/stimulus_reflex/pull/158) ([hopsoft](https://github.com/hopsoft))
392
392
 
393
393
  **Closed issues:**
394
394
 
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- stimulus_reflex (3.4.0.pre0)
4
+ stimulus_reflex (3.4.0.pre1)
5
5
  cable_ready (>= 4.3.0)
6
6
  nokogiri
7
7
  rack
@@ -0,0 +1,109 @@
1
+ # frozen_string_literal: true
2
+
3
+ class StimulusReflex::Channel < StimulusReflex.configuration.parent_channel.constantize
4
+ def stream_name
5
+ ids = connection.identifiers.map { |identifier| send(identifier).try(:id) || send(identifier) }
6
+ [
7
+ params[:channel],
8
+ ids.select(&:present?).join(";")
9
+ ].select(&:present?).join(":")
10
+ end
11
+
12
+ def subscribed
13
+ super
14
+ fix_environment!
15
+ stream_from stream_name
16
+ end
17
+
18
+ def receive(data)
19
+ url = data["url"].to_s
20
+ selectors = (data["selectors"] || []).select(&:present?)
21
+ selectors = data["selectors"] = ["body"] if selectors.blank?
22
+ target = data["target"].to_s
23
+ reflex_name, method_name = target.split("#")
24
+ reflex_name = reflex_name.camelize
25
+ reflex_name = reflex_name.end_with?("Reflex") ? reflex_name : "#{reflex_name}Reflex"
26
+ arguments = (data["args"] || []).map { |arg| object_with_indifferent_access arg }
27
+ element = StimulusReflex::Element.new(data)
28
+ permanent_attribute_name = data["permanentAttributeName"]
29
+ form_data = Rack::Utils.parse_nested_query(data["formData"])
30
+ params = form_data.deep_merge(data["params"] || {})
31
+
32
+ begin
33
+ begin
34
+ reflex_class = reflex_name.constantize.tap { |klass| raise ArgumentError.new("#{reflex_name} is not a StimulusReflex::Reflex") unless is_reflex?(klass) }
35
+ reflex = reflex_class.new(self, url: url, element: element, selectors: selectors, method_name: method_name, permanent_attribute_name: permanent_attribute_name, params: params)
36
+ delegate_call_to_reflex reflex, method_name, arguments
37
+ rescue => invoke_error
38
+ message = exception_message_with_backtrace(invoke_error)
39
+ body = "Reflex #{target} failed: #{message} [#{url}]"
40
+ if reflex
41
+ reflex.rescue_with_handler(invoke_error)
42
+ reflex.broadcast_message subject: "error", body: body, data: data, error: invoke_error
43
+ else
44
+ logger.error "\e[31m#{body}\e[0m"
45
+ end
46
+ return
47
+ end
48
+
49
+ if reflex.halted?
50
+ reflex.broadcast_message subject: "halted", data: data
51
+ else
52
+ begin
53
+ reflex.broadcast(selectors, data)
54
+ rescue => render_error
55
+ reflex.rescue_with_handler(render_error)
56
+ message = exception_message_with_backtrace(render_error)
57
+ body = "Reflex failed to re-render: #{message} [#{url}]"
58
+ reflex.broadcast_message subject: "error", body: body, data: data, error: render_error
59
+ end
60
+ end
61
+ ensure
62
+ commit_session reflex if reflex
63
+ end
64
+ end
65
+
66
+ private
67
+
68
+ def object_with_indifferent_access(object)
69
+ return object.with_indifferent_access if object.respond_to?(:with_indifferent_access)
70
+ object.map! { |obj| object_with_indifferent_access obj } if object.is_a?(Array)
71
+ object
72
+ end
73
+
74
+ def is_reflex?(reflex_class)
75
+ reflex_class.ancestors.include? StimulusReflex::Reflex
76
+ end
77
+
78
+ def delegate_call_to_reflex(reflex, method_name, arguments = [])
79
+ method = reflex.method(method_name)
80
+ required_params = method.parameters.select { |(kind, _)| kind == :req }
81
+ optional_params = method.parameters.select { |(kind, _)| kind == :opt }
82
+
83
+ if arguments.size == 0 && required_params.size == 0
84
+ reflex.process(method_name)
85
+ elsif arguments.size >= required_params.size && arguments.size <= required_params.size + optional_params.size
86
+ reflex.process(method_name, *arguments)
87
+ else
88
+ raise ArgumentError.new("wrong number of arguments (given #{arguments.inspect}, expected #{required_params.inspect}, optional #{optional_params.inspect})")
89
+ end
90
+ end
91
+
92
+ def commit_session(reflex)
93
+ store = reflex.request.session.instance_variable_get("@by")
94
+ store.commit_session reflex.request, reflex.controller.response
95
+ rescue => e
96
+ message = "Failed to commit session! #{exception_message_with_backtrace(e)}"
97
+ logger.error "\e[31m#{message}\e[0m"
98
+ end
99
+
100
+ def exception_message_with_backtrace(exception)
101
+ "#{exception}\n#{exception.backtrace.first}"
102
+ end
103
+
104
+ def fix_environment!
105
+ ([ApplicationController] + ApplicationController.descendants).each do |controller|
106
+ controller.renderer.instance_variable_set(:@env, connection.env.merge(controller.renderer.instance_variable_get(:@env)))
107
+ end
108
+ end
109
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module StimulusReflex
4
- VERSION = "3.4.0.pre0"
4
+ VERSION = "3.4.0.pre1"
5
5
  end
@@ -23,7 +23,7 @@ Gem::Specification.new do |gem|
23
23
  "source_code_uri" => gem.homepage
24
24
  }
25
25
 
26
- gem.files = Dir["lib/**/*", "bin/*", "[A-Z]*"]
26
+ gem.files = Dir["app/**/*", "lib/**/*", "bin/*", "[A-Z]*"]
27
27
  gem.test_files = Dir["test/**/*.rb"]
28
28
 
29
29
  gem.add_dependency "rack"
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.4.0.pre0
4
+ version: 3.4.0.pre1
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-11-02 00:00:00.000000000 Z
11
+ date: 2020-11-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -164,6 +164,7 @@ files:
164
164
  - LICENSE.txt
165
165
  - README.md
166
166
  - Rakefile
167
+ - app/channels/stimulus_reflex/channel.rb
167
168
  - bin/console
168
169
  - bin/loc
169
170
  - bin/rake