actionview-component-live 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4e37880218a7e67c17a027babfc3814c27120d216e9981b2c3cae9829744312d
4
+ data.tar.gz: 207a44247020ae81c515a02785125cdc64041a821e0d63310faf558521e63fb4
5
+ SHA512:
6
+ metadata.gz: b9170c628f9857eb78fd9937b0b85a083eab0a73bdbb4a61e734240726a7058e4f21fe5c75b2e9282841f8220aecf094028bdedd33ceb07a7bc8557353379bea
7
+ data.tar.gz: b50e47e7d132e82519a336ba305da57c00d2ce610c391382ed02c153501141e45779281233f0feca1c70f25e68ad74412dc6e89924347ddd828fff276fb6c271
@@ -0,0 +1,20 @@
1
+ Copyright 2019 Simon Træls Ravn
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,87 @@
1
+ # Actionview::Component::Live
2
+ This is a small add-on to ActionView::Component to make your components update automatically when data is changed serverside.
3
+
4
+ ## Usage
5
+ Lets say you have a MiniBasketComponent that is renders the small basket in the top of your webshop.
6
+ Wouldn't it be nice if that just updated it self whenever a change happens to the draft order?
7
+
8
+ First lets create a small component for our mini basket.
9
+ This component requires a DraftOrder and will listen to any change to the draft order it renders.
10
+ ```ruby
11
+ class MiniBasketComponent < ActionView::Component::Base
12
+ include ActionView::Component::Live::Subscriber
13
+
14
+ listen_to :draft_order
15
+
16
+ def initialize(draft_order:)
17
+ @order = draft_order
18
+ end
19
+
20
+ def count
21
+ @order.product_count
22
+ end
23
+
24
+ def id
25
+ @order.id
26
+ end
27
+ end
28
+ ```
29
+
30
+ **Important!** There is a strict naming requirement on the `listen_to` part and named argument in initialize!
31
+
32
+ Then the component view. The view that should be auto updated must be wrapped in a element that attaches to a Stimulus controller called LiveController.
33
+ It needs the name of the component and the id of the object that it is attached to.
34
+ ```erb
35
+ <div data-controller="live" data-live-component="MiniBasketComponent" data-live-id="<%= id %>">
36
+ <b><%= count %> products in basket</b>
37
+ </div>
38
+ ```
39
+
40
+ Finally we need to tell our DraftOrder to broadcast changes.
41
+ ```ruby
42
+ class DraftOrder < ApplicationRecord
43
+ include ActionView::Component::Live::Broadcaster
44
+ end
45
+ ```
46
+
47
+ Thats it. Now any update your draft order will be broadcast through ActionCable and your view will update in real time.
48
+
49
+ ## Installation
50
+ Add this line to your application's Gemfile:
51
+
52
+ ```ruby
53
+ gem 'actionview-component-live'
54
+ ```
55
+
56
+ And then execute:
57
+ ```bash
58
+ $ bundle
59
+ ```
60
+
61
+ Add stimulus controller
62
+ ```bash
63
+ $ yarn add stimulus-actionview-live
64
+ ```
65
+
66
+ Add these 2 lines to your `app/javascript/controllers/index.js`
67
+ ```javascript
68
+ import LiveController from "stimulus-actionview-live"
69
+ application.register("live", LiveController)
70
+ ```
71
+
72
+ ## Test
73
+ Running the tests requires a Redis server to have ActionCable running in a productionlike environment. This might seem like a bit overkill but this way one system test will test the entire gem.
74
+
75
+ In the `test/dummy` app there is a complete example of a live updating component.
76
+
77
+ ## License
78
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
79
+
80
+ # TODO
81
+ ## Nice to have
82
+ - install generator that does yarn add and adds lines to stimulus controllers file
83
+ - find a way to do collection updates, a component that lists many items
84
+ - move the component wrapper div out of component (to avoid unsubscribe/subscribe on all updates)
85
+ - could we do something for components that rely on multiple objects?
86
+ - allow the listen to method to accept a list of attributes so we only get updates to relevant things
87
+
@@ -0,0 +1,32 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'Actionview::Component::Live'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path("test/dummy/Rakefile", __dir__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+ load 'rails/tasks/statistics.rake'
21
+
22
+ require 'bundler/gem_tasks'
23
+
24
+ require 'rake/testtask'
25
+
26
+ Rake::TestTask.new(:test) do |t|
27
+ t.libs << 'test'
28
+ t.pattern = 'test/**/*_test.rb'
29
+ t.verbose = false
30
+ end
31
+
32
+ task default: :test
@@ -0,0 +1,18 @@
1
+ module ActionView
2
+ module Component
3
+ module Live
4
+ class LiveChannel < ActionCable::Channel::Base
5
+ def subscribed
6
+ stream_from "action_view:component:live:live:#{params[:component]}:#{params[:id]}"
7
+ end
8
+
9
+ def unsubscribed
10
+ # Any cleanup needed when channel is unsubscribed
11
+ end
12
+
13
+ def notify
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,15 @@
1
+ module ActionView
2
+ module Component
3
+ module Live
4
+ module Broadcaster
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ after_update do
9
+ LiveUpdater.update self
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,29 @@
1
+ module ActionView
2
+ module Component
3
+ module Live
4
+ class LiveUpdater
5
+ class << self
6
+ def update(model)
7
+ model_name = model.class.name
8
+ return unless subscriptions[model_name].present?
9
+
10
+ subscriptions[model_name].each do |component_name|
11
+ locals = {model_name.underscore.to_sym => model}
12
+ body = ApplicationController.render template: 'live', layout: false, locals: {component: component_name, locals: locals}
13
+ LiveChannel.broadcast_to "#{component_name}:#{model.id}", body: body
14
+ end
15
+ end
16
+
17
+ def subscribe(model, component_name)
18
+ subscriptions[model] ||= []
19
+ subscriptions[model] << component_name
20
+ end
21
+
22
+ def subscriptions
23
+ @subscriptions ||= {}
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,15 @@
1
+ module ActionView
2
+ module Component
3
+ module Live
4
+ module Subscriber
5
+ extend ActiveSupport::Concern
6
+
7
+ class_methods do
8
+ def listen_to(name)
9
+ LiveUpdater.subscribe(name.to_s.classify, self)
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1 @@
1
+ <%= render component: component, locals: locals %>
@@ -0,0 +1,2 @@
1
+ Rails.application.routes.draw do
2
+ end
@@ -0,0 +1,17 @@
1
+ require 'action_view/component'
2
+ require "actionview/component/live/engine"
3
+
4
+ require_relative "../../../app/models/action_view/component/live/broadcaster"
5
+ require_relative "../../../app/models/action_view/component/live/subscriber"
6
+ require_relative "../../../app/models/action_view/component/live/live_updater"
7
+
8
+ require_relative "../../../app/channels/action_view/component/live/live_channel"
9
+
10
+
11
+ module Actionview
12
+ module Component
13
+ module Live
14
+ # Your code goes here...
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,8 @@
1
+ module Actionview
2
+ module Component
3
+ module Live
4
+ class Engine < ::Rails::Engine
5
+ end
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,7 @@
1
+ module Actionview
2
+ module Component
3
+ module Live
4
+ VERSION = '0.1.0'
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :actionview_component_live do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,169 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: actionview-component-live
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Simon Træls Ravn
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-12-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 6.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 6.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: actionview-component
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 1.6.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 1.6.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: sqlite3
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 1.4.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 1.4.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: capybara
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 3.29.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 3.29.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: selenium-webdriver
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 3.142.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 3.142.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: puma
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 4.3.0
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 4.3.0
97
+ - !ruby/object:Gem::Dependency
98
+ name: webpacker
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '4.0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '4.0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: redis
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: 4.1.0
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: 4.1.0
125
+ description: Liveupdate from ActiveRecord models to Actionview components
126
+ email:
127
+ - cs2@cs2.dk
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - MIT-LICENSE
133
+ - README.md
134
+ - Rakefile
135
+ - app/assets/config/actionview_component_live_manifest.js
136
+ - app/channels/action_view/component/live/live_channel.rb
137
+ - app/models/action_view/component/live/broadcaster.rb
138
+ - app/models/action_view/component/live/live_updater.rb
139
+ - app/models/action_view/component/live/subscriber.rb
140
+ - app/views/live.html.erb
141
+ - config/routes.rb
142
+ - lib/actionview/component/live.rb
143
+ - lib/actionview/component/live/engine.rb
144
+ - lib/actionview/component/live/version.rb
145
+ - lib/tasks/actionview/component/live_tasks.rake
146
+ homepage: https://bitbucket.org/cs2software/actionview-component-live
147
+ licenses:
148
+ - MIT
149
+ metadata: {}
150
+ post_install_message:
151
+ rdoc_options: []
152
+ require_paths:
153
+ - lib
154
+ required_ruby_version: !ruby/object:Gem::Requirement
155
+ requirements:
156
+ - - ">="
157
+ - !ruby/object:Gem::Version
158
+ version: '0'
159
+ required_rubygems_version: !ruby/object:Gem::Requirement
160
+ requirements:
161
+ - - ">="
162
+ - !ruby/object:Gem::Version
163
+ version: '0'
164
+ requirements: []
165
+ rubygems_version: 3.0.3
166
+ signing_key:
167
+ specification_version: 4
168
+ summary: Liveupdating view components
169
+ test_files: []