view_component-live 0.2.1
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 +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +89 -0
- data/Rakefile +32 -0
- data/app/assets/config/actionview_component_live_manifest.js +0 -0
- data/app/channels/view_component/live/live_channel.rb +16 -0
- data/app/models/view_component/live/broadcaster.rb +13 -0
- data/app/models/view_component/live/live_updater.rb +27 -0
- data/app/models/view_component/live/subscriber.rb +30 -0
- data/app/views/live.html.erb +1 -0
- data/config/routes.rb +2 -0
- data/lib/tasks/actionview/component/live_tasks.rake +4 -0
- data/lib/view_component/live.rb +15 -0
- data/lib/view_component/live/engine.rb +6 -0
- data/lib/view_component/live/version.rb +5 -0
- metadata +169 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 15f0780daa710f06f03b185800452e61c950a2c1c24829483df627a5ad592bc0
|
4
|
+
data.tar.gz: f48fe82437ae5f18a74bfc27734b798d9914ff0d3374667abca0c1f831b00766
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6da365cd6e73595f444d2388eeb08bc4cc54ccd781a8b2260f86faf953b3c480c4db65c46e645677d768aa85c70b6e6c7259fb62596b808793cbe6be0cd706bb
|
7
|
+
data.tar.gz: cf951d287fa7505e96034f19565a5bc744648b5e9b4601834944841a3baf9ae0ae7cecb797bf59f09d1403b13ffb533361ddd4423021f1ffeec4981a1cdd12ab
|
data/MIT-LICENSE
ADDED
@@ -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.
|
data/README.md
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
# ViewComponent::Live
|
2
|
+
This is a small add-on to the ViewComponent gem to make your components update automatically when data is changed serverside.
|
3
|
+
|
4
|
+
**Renamed to ViewComponent::Live to follow naming convention of ViewComponent gem**
|
5
|
+
|
6
|
+
## Usage
|
7
|
+
Lets say you have a MiniBasketComponent that is renders the small basket in the top of your webshop.
|
8
|
+
Wouldn't it be nice if that just updated it self whenever a change happens to the draft order?
|
9
|
+
|
10
|
+
First lets create a small component for our mini basket.
|
11
|
+
This component requires a DraftOrder and will listen to any change to the draft order it renders.
|
12
|
+
```ruby
|
13
|
+
class MiniBasketComponent < ViewComponent::Base
|
14
|
+
include ViewComponent::Live::Subscriber
|
15
|
+
|
16
|
+
listen_to :draft_order
|
17
|
+
|
18
|
+
def initialize(draft_order:)
|
19
|
+
@order = draft_order
|
20
|
+
end
|
21
|
+
|
22
|
+
def count
|
23
|
+
@order.product_count
|
24
|
+
end
|
25
|
+
|
26
|
+
def id
|
27
|
+
@order.id
|
28
|
+
end
|
29
|
+
end
|
30
|
+
```
|
31
|
+
|
32
|
+
**Important!** There is a strict naming requirement on the `listen_to` part and named argument in initialize!
|
33
|
+
|
34
|
+
Then the component view.
|
35
|
+
Notice the **=** on the live block. It wraps the view in a **div** tag so we know what part of the page to update.
|
36
|
+
```erb
|
37
|
+
<%= live do %>
|
38
|
+
<b><%= count %> products in basket</b>
|
39
|
+
<% end %>
|
40
|
+
```
|
41
|
+
|
42
|
+
Finally we need to tell our DraftOrder to broadcast changes.
|
43
|
+
```ruby
|
44
|
+
class DraftOrder < ApplicationRecord
|
45
|
+
include ViewComponent::Live::Broadcaster
|
46
|
+
end
|
47
|
+
```
|
48
|
+
|
49
|
+
Thats it. Now any update your draft order will be broadcast through ActionCable and your view will update in real time.
|
50
|
+
|
51
|
+
## Installation
|
52
|
+
Add this line to your application's Gemfile:
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
gem 'view_component-live'
|
56
|
+
```
|
57
|
+
|
58
|
+
And then execute:
|
59
|
+
```bash
|
60
|
+
$ bundle
|
61
|
+
```
|
62
|
+
|
63
|
+
Add stimulus controller
|
64
|
+
```bash
|
65
|
+
$ yarn add stimulus-actionview-live, '1.0.0'
|
66
|
+
```
|
67
|
+
|
68
|
+
Add these 2 lines to your `app/javascript/controllers/index.js`
|
69
|
+
```javascript
|
70
|
+
import LiveController from "stimulus-actionview-live"
|
71
|
+
application.register("live", LiveController)
|
72
|
+
```
|
73
|
+
|
74
|
+
## Test
|
75
|
+
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.
|
76
|
+
|
77
|
+
In the `test/dummy` app there is a complete example of a live updating component.
|
78
|
+
_Note to self, remember to compile webpacker when making changes to dummy app assets_
|
79
|
+
|
80
|
+
## License
|
81
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
82
|
+
|
83
|
+
# TODO
|
84
|
+
## Nice to have
|
85
|
+
- install generator that does yarn add and adds lines to stimulus controllers file
|
86
|
+
- find a way to do collection updates, a component that lists many items
|
87
|
+
- could we do something for components that rely on multiple objects?
|
88
|
+
- allow the listen to method to accept a list of attributes so we only get updates to relevant things
|
89
|
+
|
data/Rakefile
ADDED
@@ -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
|
File without changes
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module ViewComponent
|
2
|
+
module Live
|
3
|
+
class LiveChannel < ActionCable::Channel::Base
|
4
|
+
def subscribed
|
5
|
+
stream_from "view_component:live:live:#{params[:component]}:#{params[:id]}"
|
6
|
+
end
|
7
|
+
|
8
|
+
def unsubscribed
|
9
|
+
# Any cleanup needed when channel is unsubscribed
|
10
|
+
end
|
11
|
+
|
12
|
+
def notify
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module ViewComponent
|
2
|
+
module Live
|
3
|
+
class LiveUpdater
|
4
|
+
class << self
|
5
|
+
def update(model)
|
6
|
+
model_name = model.class.name
|
7
|
+
return unless subscriptions[model_name].present?
|
8
|
+
|
9
|
+
subscriptions[model_name].each do |component_name|
|
10
|
+
locals = {model_name.underscore.to_sym => model}
|
11
|
+
body = ApplicationController.render template: 'live', layout: false, locals: {component: component_name, locals: locals}
|
12
|
+
LiveChannel.broadcast_to "#{component_name}:#{model.id}", body: body
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def subscribe(model, component_name)
|
17
|
+
subscriptions[model] ||= []
|
18
|
+
subscriptions[model] << component_name
|
19
|
+
end
|
20
|
+
|
21
|
+
def subscriptions
|
22
|
+
@subscriptions ||= {}
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module ViewComponent
|
2
|
+
module Live
|
3
|
+
module Subscriber
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
def live_render!
|
7
|
+
@live_render = true
|
8
|
+
end
|
9
|
+
|
10
|
+
def live_render?
|
11
|
+
@live_render
|
12
|
+
end
|
13
|
+
|
14
|
+
class_methods do
|
15
|
+
def listen_to(name)
|
16
|
+
LiveUpdater.subscribe(name.to_s.classify, self)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
# Used as view helper to wrap view in div that will connect to auto-updating stimulus controller
|
21
|
+
# If we render the component in live update on cable we will not wrap it - we just need the component content.
|
22
|
+
#
|
23
|
+
def live
|
24
|
+
return yield if live_render?
|
25
|
+
# <div data-controller="live" data-live-component="UserCardComponent" data-live-id="<%= id %>">
|
26
|
+
content_tag(:div, nil, data: {controller: :live, live_component: self.class.name, live_id: id}) { yield }
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
<%= render component.new(locals).tap(&:live_render!) %>
|
data/config/routes.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require "view_component/engine"
|
2
|
+
require "view_component/live/engine"
|
3
|
+
|
4
|
+
require_relative "../../app/models/view_component/live/broadcaster"
|
5
|
+
require_relative "../../app/models/view_component/live/subscriber"
|
6
|
+
require_relative "../../app/models/view_component/live/live_updater"
|
7
|
+
|
8
|
+
require_relative "../../app/channels/view_component/live/live_channel"
|
9
|
+
|
10
|
+
|
11
|
+
module ViewComponent
|
12
|
+
module Live
|
13
|
+
# Your code goes here...
|
14
|
+
end
|
15
|
+
end
|
metadata
ADDED
@@ -0,0 +1,169 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: view_component-live
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Simon Træls Ravn
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-03-27 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: view_component
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 2.1.0
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 2.1.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 ActiveModel models to view 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/view_component/live/live_channel.rb
|
137
|
+
- app/models/view_component/live/broadcaster.rb
|
138
|
+
- app/models/view_component/live/live_updater.rb
|
139
|
+
- app/models/view_component/live/subscriber.rb
|
140
|
+
- app/views/live.html.erb
|
141
|
+
- config/routes.rb
|
142
|
+
- lib/tasks/actionview/component/live_tasks.rake
|
143
|
+
- lib/view_component/live.rb
|
144
|
+
- lib/view_component/live/engine.rb
|
145
|
+
- lib/view_component/live/version.rb
|
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: []
|