motion 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f50ecf7f319478fcd72dca2dd6e55c36a67d803e6235d24c39ac36537528bb88
4
- data.tar.gz: 10c88dabd3104c7351a5d2eeb6623564214171c1b26fdf9b25aca3d8297c2a46
3
+ metadata.gz: 7839c6a006522aecfde98e996d77d5af65d3f50a3472d54a84bb9c59c13b2a88
4
+ data.tar.gz: 3011e0696be6a98232276b35310953939cca6f88d13e85c253f2004b98cc059e
5
5
  SHA512:
6
- metadata.gz: 31a107b75a0e553febfec1f0b93cc878c4c727f5f61ba1d0e39b89c9c7c1a7b45d5ecf188a49b9e1293c2f4056974afb755c1cb2cd4594895e95c3166e38563f
7
- data.tar.gz: 7ad03d82e4a4ca05338368db8cda1b12813662f3f3fe2937f576baeb47b8b7c99b41b5bfc4ec9b11eb42facbce155990b7f278c6171603b0bf972b9adb641c52
6
+ metadata.gz: 7195a8b10a5785ea41b46958b886d06ed8e19cd376879d3c1a1d7c14e5ef7be403df16b55f59d1276c1b7111d4268b6280f8d7326cc3e5f6e31aac555376e6c6
7
+ data.tar.gz: 3f2df4a07139f7ac39fb2733ae3f7ccaf20d9cffd8a8e7c34b1b7e6dd9112fc69de20129e592fe4dacf7dfff5a0c12f15aab896dcea854ab093c795ab69df71d
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails/generators/named_base"
4
+
5
+ module Motion
6
+ module Generators
7
+ class ComponentGenerator < Rails::Generators::NamedBase
8
+ desc "Creates a Motion-enabled component in your application."
9
+
10
+ argument :attributes, type: :array, default: [], banner: "attribute"
11
+
12
+ def generate_component
13
+ generate "component", class_name, *attributes.map(&:name)
14
+ end
15
+
16
+ def include_motion
17
+ inject_into_class component_path, "#{class_name}Component" do
18
+ " include Motion::Component\n\n"
19
+ end
20
+ end
21
+
22
+ private
23
+
24
+ def component_path
25
+ @component_path ||=
26
+ File.join("app/components", class_path, "#{file_name}_component.rb")
27
+ end
28
+
29
+ def file_name
30
+ @_file_name ||= super.sub(/_component\z/i, "")
31
+ end
32
+ end
33
+ end
34
+ end
@@ -16,10 +16,17 @@ module Motion
16
16
  )
17
17
  end
18
18
 
19
- def copy_stimlus_controller
19
+ def copy_client_initializer
20
20
  template(
21
- "motion_controller.js",
22
- "app/javascript/controllers/motion_controller.js"
21
+ "motion.js",
22
+ "app/javascript/motion.js"
23
+ )
24
+ end
25
+
26
+ def add_client_to_application_pack
27
+ append_to_file(
28
+ "app/javascript/packs/application.js",
29
+ 'import "motion"'
23
30
  )
24
31
  end
25
32
  end
@@ -0,0 +1,30 @@
1
+ import { createClient } from '@unabridged/motion';
2
+ import consumer from './channels/consumer';
3
+
4
+ export default createClient({
5
+
6
+ // To avoid creating a second websocket, make sure to reuse the application's
7
+ // ActionCable consumer. If you are not otherwise using ActionCable, you can
8
+ // remove this line and the corresponding import.
9
+ consumer,
10
+
11
+ // Motion can log information about the lifecycle of components to the
12
+ // browser's console. It is recommended to turn this feature off outside of
13
+ // development.
14
+ logging: process.env["RAILS_ENV"] === "development",
15
+
16
+ // This function will be called for every motion, and the return value will be
17
+ // made available at `Motion::Event#extra_data`:
18
+ //
19
+ // getExtraDataForEvent(event) {},
20
+ //
21
+
22
+ // The data attributes used by Motion can be customized, but these values must
23
+ // also be updated in the Ruby initializer:
24
+ //
25
+ // keyAttribute: "data-motion-key",
26
+ // stateAttribute: "data-motion-state",
27
+ // motionAttribute: "data-motion",
28
+ //
29
+
30
+ });
@@ -1,22 +1,43 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # TODO: Explain all the options.
4
3
  Motion.configure do |config|
5
- # config.secret = Rails.application.key_generator.generate_key "motion:secret"
4
+ # Motion needs to be able to uniquely identify the version of the running
5
+ # version of your application. By default, the commit hash from git is used,
6
+ # but depending on your deployment, this may not be available in production.
7
+ #
8
+ # If you are sure that git is available in your production enviorment, you can
9
+ # uncomment this line:
10
+ #
11
+ # config.revision = `git rev-parse HEAD`.chomp
12
+ #
13
+ # If git is not available in your production enviorment, you must identify
14
+ # your application version some other way:
15
+ #
16
+ # config.revision =
17
+ # ENV.fetch("MY_DEPLOYMENT_NUMBER") { `git rev-parse HEAD`.chomp }
18
+ #
19
+ # Using a value that does not change on every deployment will likely lead to
20
+ # confusing errors if components are connected during a deployment.
6
21
 
7
- # config.revision = `git rev-parse HEAD`.chomp
22
+ # This proc will be invoked by Motion in order to create a renderer for each
23
+ # websocket connection. By default, your `ApplicationController` will be used
24
+ # and the session/cookies **as they were when the websocket was first open**
25
+ # will be available:
26
+ #
27
+ # config.renderer_for_connection_proc = ->(websocket_connection) do
28
+ # ApplicationController.renderer.new(
29
+ # websocket_connection.env.slice(
30
+ # Rack::HTTP_COOKIE,
31
+ # Rack::RACK_SESSION,
32
+ # )
33
+ # )
34
+ # end
8
35
 
9
- # config.renderer_for_connection_proc = ->(websocket_connection) do
10
- # ApplicationController.renderer.new(
11
- # websocket_connection.env.slice(
12
- # Rack::HTTP_COOKIE,
13
- # Rack::RACK_SESSION,
14
- # )
15
- # )
16
- # end
17
-
18
- # config.stimulus_controller_identifier = "motion"
19
- # config.key_attribute = "data-motion-key"
20
- # config.state_attribute = "data-motion-state"
21
- # config.motion_attribute = "data-motion"
36
+ # The data attributes used by Motion can be customized, but these values must
37
+ # also be updated in the Ruby initializer:
38
+ #
39
+ # config.key_attribute = "data-motion-key"
40
+ # config.state_attribute = "data-motion-state"
41
+ # config.motion_attribute = "data-motion"
42
+ #
22
43
  end
@@ -24,9 +24,7 @@ module Motion
24
24
  def subscribed
25
25
  state, client_version = params.values_at("state", "version")
26
26
 
27
- # TODO: This is too restrictive. Introduce a protocol version and support
28
- # older versions of the client that have a compatible protocol.
29
- unless Motion::VERSION == client_version
27
+ if Gem::Version.new(Motion::VERSION) < Gem::Version.new(client_version)
30
28
  raise IncompatibleClientError.new(Motion::VERSION, client_version)
31
29
  end
32
30
 
@@ -95,7 +95,6 @@ module Motion
95
95
  end
96
96
  end
97
97
 
98
- option(:stimulus_controller_identifier) { "motion" }
99
98
  option(:key_attribute) { "data-motion-key" }
100
99
  option(:state_attribute) { "data-motion-state" }
101
100
 
@@ -117,15 +117,18 @@ module Motion
117
117
  end
118
118
 
119
119
  class IncompatibleClientError < Error
120
- attr_reader :expected_version,
121
- :actual_version
120
+ attr_reader :server_version, :client_version
122
121
 
123
- def initialize(expected_version, actual_version)
122
+ def initialize(server_version, client_version)
124
123
  super(<<~MSG)
125
- Expected client version #{expected_version}, but got #{actual_version}.
124
+ The client version (#{client_version}) is newer than the server version
125
+ (#{server_version}). Please upgrade the Motion gem.
126
126
 
127
- Fix: Run `bin/yarn add @unabridged/motion@#{expected_version}`
127
+ Fix: Run `bundle update motion`
128
128
  MSG
129
+
130
+ @server_version = server_version
131
+ @client_version = client_version
129
132
  end
130
133
  end
131
134
 
@@ -6,22 +6,16 @@ require "motion"
6
6
 
7
7
  module Motion
8
8
  class MarkupTransformer
9
- STIMULUS_CONTROLLER_ATTRIBUTE = "data-controller"
10
-
11
9
  attr_reader :serializer,
12
- :stimulus_controller_identifier,
13
10
  :key_attribute,
14
11
  :state_attribute
15
12
 
16
13
  def initialize(
17
14
  serializer: Motion.serializer,
18
- stimulus_controller_identifier:
19
- Motion.config.stimulus_controller_identifier,
20
15
  key_attribute: Motion.config.key_attribute,
21
16
  state_attribute: Motion.config.state_attribute
22
17
  )
23
18
  @serializer = serializer
24
- @stimulus_controller_identifier = stimulus_controller_identifier
25
19
  @key_attribute = key_attribute
26
20
  @state_attribute = state_attribute
27
21
  end
@@ -30,12 +24,6 @@ module Motion
30
24
  key, state = serializer.serialize(component)
31
25
 
32
26
  transform_root(component, html) do |root|
33
- root[STIMULUS_CONTROLLER_ATTRIBUTE] =
34
- values(
35
- stimulus_controller_identifier,
36
- root[STIMULUS_CONTROLLER_ATTRIBUTE]
37
- )
38
-
39
27
  root[key_attribute] = key
40
28
  root[state_attribute] = state
41
29
  end
@@ -53,13 +41,5 @@ module Motion
53
41
 
54
42
  fragment.to_html.html_safe
55
43
  end
56
-
57
- def values(*values, delimiter: " ")
58
- values
59
- .compact
60
- .flat_map { |value| value.split(delimiter) }
61
- .uniq
62
- .join(delimiter)
63
- end
64
44
  end
65
45
  end
@@ -6,6 +6,7 @@ module Motion
6
6
  class MyRailtie < Rails::Railtie
7
7
  generators do
8
8
  require "generators/motion/install_generator"
9
+ require "generators/motion/component_generator"
9
10
  end
10
11
  end
11
12
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Motion
4
- VERSION = "0.1.2"
4
+ VERSION = "0.2.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: motion
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alec Larsen
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2020-06-26 00:00:00.000000000 Z
12
+ date: 2020-06-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: nokogiri
@@ -49,9 +49,10 @@ executables: []
49
49
  extensions: []
50
50
  extra_rdoc_files: []
51
51
  files:
52
+ - lib/generators/motion/component_generator.rb
52
53
  - lib/generators/motion/install_generator.rb
54
+ - lib/generators/motion/templates/motion.js
53
55
  - lib/generators/motion/templates/motion.rb
54
- - lib/generators/motion/templates/motion_controller.js
55
56
  - lib/motion.rb
56
57
  - lib/motion/action_cable_extentions.rb
57
58
  - lib/motion/action_cable_extentions/declarative_streams.rb
@@ -81,7 +82,7 @@ metadata:
81
82
  source_code_uri: https://github.com/unabridged/motion
82
83
  post_install_message: |
83
84
  Friendly reminder: When updating the motion gem, don't forget to update the
84
- NPM package as well (`bin/yarn add '@unabridged/motion@0.1.2'`).
85
+ NPM package as well (`bin/yarn add '@unabridged/motion@0.2.0'`).
85
86
  rdoc_options: []
86
87
  require_paths:
87
88
  - lib
@@ -1,28 +0,0 @@
1
- import { Controller } from "@unabridged/motion";
2
- import consumer from "../channels/consumer";
3
-
4
- // If you change the name of this controller (determined by the file name),
5
- // make sure to update `Motion.config.stimulus_controller_identifier`.
6
- export default class extends Controller {
7
- // To avoid creating a second websocket, make sure to reuse the application's
8
- // ActionCable consumer.
9
- getConsumer() {
10
- return consumer;
11
- }
12
-
13
- // It is possible to additionally customize the behavior of the client by
14
- // overriding these properties and methods:
15
-
16
- // getExtraDataForEvent(event) {} // `Motion::Event#extra_data`
17
-
18
- // keyAttribute = "data-motion-key"; // `Motion.config.key_attribute`
19
- // stateAttribute = "data-motion-state"; // `Motion.config.state_attribute`
20
- // motionAttribute = "data-motion"; // `Motion.config.motion_attribute`
21
-
22
- // beforeConnect() { /* by default, dispatches `motion:before-connect` */ }
23
- // connected() { /* by default, dispatches `motion:connected` */ }
24
- // connectFailed() { /* by default, dispatches `motion:connect-failed` */ }
25
- // disconnected() { /* by default, dispatches `motion:disconnected` */ }
26
- // beforeRender() { /* by default, dispatches `motion:before-render` */ }
27
- // rendered() { /* by default, dispatches `motion:rendered` */ }
28
- }