futurism 0.2.0 → 0.4.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: 490e77869b91685fe4335badd8006842871eaba8df9cab7998038c531f11e607
4
- data.tar.gz: 9bc29e824f7a1e8298942842d543e19eef1e2cf6e22d1b058109675b2d38ad15
3
+ metadata.gz: ba66b2cb1a12fb863e2e0fc34798411d7f95947a197496150e1a499582fc4e28
4
+ data.tar.gz: 3f7f290ef2b0b79f9c9e5100e7eaed2520dcb8e481e391bd537649d0023d379b
5
5
  SHA512:
6
- metadata.gz: 55449d0339a7731cb8725459aca47b66ac8039600c4e8e24a5dbf3f7c9986466f949fc91042f1882dbc2332a61009f3cabbd3c76dda98d1a07bd7a4c9abdb111
7
- data.tar.gz: 315e1274d56647b26bd0a703759160f93fa75e8d302bf5cc6304835e74fabc727e68395cba3554bf7a2fa5174acfb11c481fdbe536df0af34870560a1aa54b54
6
+ metadata.gz: e772253cf0ed802d14c6836c0fb7285f7011f79f516fa88cc85e13191b7de0f29ebbc0edbd5c7996f4f858ec4efe4890613605587a2cbd6c17c4ecbad05b4985
7
+ data.tar.gz: cf4db9dd38a545a7fe43381dc22597a4c5e77a4023698b19017740fa996e6aa0433e1927c115a1187bb9028ad26e2b36aa3ecd9f66fb19000380a6b6467efffb
data/README.md CHANGED
@@ -1,12 +1,15 @@
1
1
  # Futurism
2
2
  [![Twitter follow](https://img.shields.io/twitter/follow/julian_rubisch?style=social)](https://twitter.com/julian_rubisch)
3
3
  <!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
4
- [![All Contributors](https://img.shields.io/badge/all_contributors-6-orange.svg?style=flat-square)](#contributors-)
4
+ [![All Contributors](https://img.shields.io/badge/all_contributors-10-orange.svg?style=flat-square)](#contributors-)
5
5
  <!-- ALL-CONTRIBUTORS-BADGE:END -->
6
6
  Lazy-load Rails partials via CableReady
7
7
 
8
8
  :rotating_light: *Futurism is still in pre-1.0 state. As much as I hope to keep the API backwards-compatible, I cannot guarantee it* :rotating_light:
9
9
 
10
+ <img src="https://user-images.githubusercontent.com/4352208/88374198-9e6f3500-cd99-11ea-804b-0216ed320eff.jpg" alt="birmingham-museums-trust-GrvC6MI-z4w-unsplash" width="50%" align="center"/>
11
+ <span>Photo by <a href="https://unsplash.com/@birminghammuseumstrust?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Birmingham Museums Trust</a> on <a href="https://unsplash.com/s/photos/futurism?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></span>
12
+
10
13
  ## Facts
11
14
  - only one dependency: CableReady
12
15
  - bundle size (without CableReady) is around [~1.04kB](https://bundlephobia.com/result?p=@minthesize/futurism@0.1.3)
@@ -77,7 +80,7 @@ That way you get maximal flexibility when just specifying a single resource.
77
80
  Call `futurize` with a `partial` keyword:
78
81
 
79
82
  ```erb
80
- <%= futurize partial: "items/card", locals: {card: @card}, extends: :div %>
83
+ <%= futurize partial: "items/card", locals: {card: @card}, extends: :div do %>
81
84
  <div class="spinner"></div>
82
85
  <% end %>
83
86
  ```
@@ -85,9 +88,42 @@ Call `futurize` with a `partial` keyword:
85
88
  You can also use the shorthand syntax:
86
89
 
87
90
  ```erb
88
- <%= futurize "items/card", card: @card, extends: :div %>
91
+ <%= futurize "items/card", card: @card, extends: :div do %>
92
+ <div class="spinner"></div>
93
+ <% end %>
94
+ ```
95
+
96
+ #### Collections
97
+
98
+ Collection rendering is also possible:
99
+
100
+ ```erb
101
+ <%= futurize partial: "items/card", collection: @cards, extends: :div do %>
89
102
  <div class="spinner"></div>
90
103
  <% end %>
104
+ ```
105
+
106
+ ### HTML Options
107
+
108
+ You can pass a hash of attribute/value pairs which will be mixed into the HTML markup for the placeholder element. This is important for layouts that require elements to have dimensionality. For example, many scripts calculate size based on element height and width. This option ensures that your elements have integrity, even if they are gone before you see them.
109
+
110
+ ```erb
111
+ <%= futurize @posts, extends: :tr, html_options: {style: "width: 50px; height: 50px;"} do %>
112
+ <td class="placeholder"></td>
113
+ <% end %>
114
+ ```
115
+
116
+ This will output the following:
117
+
118
+ ```html
119
+ <tr style="width: 50px; height: 50px;">
120
+ <td class="placeholder"></td>
121
+ </tr>
122
+ ```
123
+
124
+ ## Events
125
+
126
+ Once your futurize element has been rendered, the `futurize:appeared` custom event will be called.
91
127
 
92
128
  ## Installation
93
129
  Add this line to your application's Gemfile:
@@ -119,7 +155,7 @@ $ bin/yarn add @minthesize/futurism
119
155
  In your `app/javascript/channels/index.js`, add the following
120
156
 
121
157
  ```js
122
- import * as Futurism
158
+ import * as Futurism from '@minthesize/futurism''
123
159
 
124
160
  import consumer from './consumer'
125
161
 
@@ -146,7 +182,13 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
146
182
  <td align="center"><a href="https://ParamagicDev.github.io/portfolio"><img src="https://avatars2.githubusercontent.com/u/26425882?v=4" width="100px;" alt=""/><br /><sub><b>Konnor Rogers</b></sub></a><br /><a href="https://github.com/julianrubisch/futurism/commits?author=ParamagicDev" title="Code">💻</a></td>
147
183
  <td align="center"><a href="https://www.andrewm.codes"><img src="https://avatars1.githubusercontent.com/u/18423853?v=4" width="100px;" alt=""/><br /><sub><b>Andrew Mason</b></sub></a><br /><a href="#maintenance-andrewmcodes" title="Maintenance">🚧</a></td>
148
184
  <td align="center"><a href="http://gorails.com"><img src="https://avatars1.githubusercontent.com/u/67093?v=4" width="100px;" alt=""/><br /><sub><b>Chris Oliver</b></sub></a><br /><a href="https://github.com/julianrubisch/futurism/commits?author=excid3" title="Code">💻</a> <a href="https://github.com/julianrubisch/futurism/pulls?q=is%3Apr+reviewed-by%3Aexcid3" title="Reviewed Pull Requests">👀</a></td>
149
- <td align="center"><a href="https://github.com/leastbad"><img src="https://avatars2.githubusercontent.com/u/38150464?v=4" width="100px;" alt=""/><br /><sub><b>leastbad</b></sub></a><br /><a href="https://github.com/julianrubisch/futurism/commits?author=leastbad" title="Code">💻</a></td>
185
+ <td align="center"><a href="https://github.com/leastbad"><img src="https://avatars2.githubusercontent.com/u/38150464?v=4" width="100px;" alt=""/><br /><sub><b>leastbad</b></sub></a><br /><a href="https://github.com/julianrubisch/futurism/commits?author=leastbad" title="Code">💻</a> <a href="https://github.com/julianrubisch/futurism/pulls?q=is%3Apr+reviewed-by%3Aleastbad" title="Reviewed Pull Requests">👀</a></td>
186
+ <td align="center"><a href="http://code.digimonkey.com"><img src="https://avatars0.githubusercontent.com/u/74207?v=4" width="100px;" alt=""/><br /><sub><b>M. E. Patterson</b></sub></a><br /><a href="https://github.com/julianrubisch/futurism/issues?q=author%3Amepatterson" title="Bug reports">🐛</a></td>
187
+ </tr>
188
+ <tr>
189
+ <td align="center"><a href="http://fractaledmind.com"><img src="https://avatars3.githubusercontent.com/u/5077225?v=4" width="100px;" alt=""/><br /><sub><b>Stephen Margheim</b></sub></a><br /><a href="https://github.com/julianrubisch/futurism/commits?author=fractaledmind" title="Code">💻</a></td>
190
+ <td align="center"><a href="http://hass.codes"><img src="https://avatars2.githubusercontent.com/u/1064205?v=4" width="100px;" alt=""/><br /><sub><b>Hassanin Ahmed</b></sub></a><br /><a href="https://github.com/julianrubisch/futurism/commits?author=sas1ni69" title="Code">💻</a></td>
191
+ <td align="center"><a href="https://marcoroth.dev"><img src="https://avatars2.githubusercontent.com/u/6411752?v=4" width="100px;" alt=""/><br /><sub><b>Marco Roth</b></sub></a><br /><a href="https://github.com/julianrubisch/futurism/commits?author=marcoroth" title="Code">💻</a></td>
150
192
  </tr>
151
193
  </table>
152
194
 
@@ -7,20 +7,29 @@ module Futurism
7
7
  end
8
8
 
9
9
  def receive(data)
10
- resources = data["signed_params"].map { |signed_params|
11
- [signed_params, Rails.application.message_verifier("futurism").verify(signed_params)]
12
- }
10
+ resources = data.fetch_values("signed_params", "sgids") { |key| [nil] }.transpose
13
11
 
14
- ApplicationController.renderer.instance_variable_set(:@env, connection.env)
12
+ new_env = connection.env.merge(ApplicationController.renderer.instance_variable_get(:@env))
13
+ ApplicationController.renderer.instance_variable_set(:@env, new_env)
15
14
 
16
- resources.each do |signed_params, resource|
15
+ resources.each do |signed_params, sgid|
16
+ selector = "[data-signed-params='#{signed_params}']"
17
+ selector << "[data-sgid='#{sgid}']" if sgid.present?
17
18
  cable_ready["Futurism::Channel"].outer_html(
18
- selector: "[data-signed-params='#{signed_params}']",
19
- html: ApplicationController.render(resource)
19
+ selector: selector,
20
+ html: ApplicationController.render(resource(signed_params: signed_params, sgid: sgid))
20
21
  )
21
22
  end
22
23
 
23
24
  cable_ready.broadcast
24
25
  end
26
+
27
+ private
28
+
29
+ def resource(signed_params:, sgid:)
30
+ return GlobalID::Locator.locate_signed(sgid) if sgid.present?
31
+
32
+ Rails.application.message_verifier("futurism").verify(signed_params)
33
+ end
25
34
  end
26
35
  end
@@ -1,22 +1,35 @@
1
- class FuturismChannel < ApplicationCable::Channel
2
- include CableReady::Broadcaster
1
+ module Futurism
2
+ class Channel < ActionCable::Channel::Base
3
+ include CableReady::Broadcaster
3
4
 
4
- def subscribed
5
- stream_from "FuturismChannel"
6
- end
5
+ def subscribed
6
+ stream_from "Futurism::Channel"
7
+ end
8
+
9
+ def receive(data)
10
+ resources = data.fetch_values("signed_params", "sgids") { |key| [nil] }.transpose
11
+
12
+ new_env = connection.env.merge(ApplicationController.renderer.instance_variable_get(:@env))
13
+ ApplicationController.renderer.instance_variable_set(:@env, new_env)
7
14
 
8
- def receive(data)
9
- resources = data["sgids"].map { |sgid|
10
- [sgid, GlobalID::Locator.locate_signed(sgid)]
11
- }
15
+ resources.each do |signed_params, sgid|
16
+ selector = "[data-signed-params='#{signed_params}']"
17
+ selector << "[data-sgid='#{sgid}']" if sgid.present?
18
+ cable_ready["Futurism::Channel"].outer_html(
19
+ selector: selector,
20
+ html: ApplicationController.render(resource(signed_params: signed_params, sgid: sgid))
21
+ )
22
+ end
12
23
 
13
- resources.each do |sgid, resource|
14
- cable_ready["FuturismChannel"].outer_html(
15
- selector: "[data-sgid='#{sgid}']",
16
- html: ApplicationController.render(resource)
17
- )
24
+ cable_ready.broadcast
18
25
  end
19
26
 
20
- cable_ready.broadcast
27
+ private
28
+
29
+ def resource(signed_params:, sgid:)
30
+ return GlobalID::Locator.locate_signed(sgid) if sgid.present?
31
+
32
+ Rails.application.message_verifier("futurism").verify(signed_params)
33
+ end
21
34
  end
22
35
  end
@@ -4,36 +4,71 @@ module Futurism
4
4
  placeholder = capture(&block)
5
5
 
6
6
  if records_or_string.is_a?(ActiveRecord::Base) || records_or_string.is_a?(ActiveRecord::Relation)
7
- futurize_active_record(records_or_string, extends: extends, placeholder: placeholder)
7
+ futurize_active_record(records_or_string, extends: extends, placeholder: placeholder, **options)
8
8
  elsif records_or_string.is_a?(String)
9
- futurize_with_options(extends: extends, partial: records_or_string, locals: options, placeholder: placeholder)
9
+ html_options = options.delete(:html_options)
10
+ futurize_with_options(extends: extends, placeholder: placeholder, partial: records_or_string, locals: options, html_options: html_options)
10
11
  else
11
12
  futurize_with_options(extends: extends, placeholder: placeholder, **options)
12
13
  end
13
14
  end
14
15
 
15
16
  def futurize_with_options(extends:, placeholder:, **options)
16
- case extends
17
- when :tr
18
- content_tag :tr, placeholder, data: {signed_params: futurism_signed_params(options)}, is: "futurism-table-row"
17
+ collection = options.delete(:collection)
18
+ if collection.nil?
19
+ Element.new(extends: extends, placeholder: placeholder, options: options).render
19
20
  else
20
- content_tag :"futurism-element", placeholder, data: {signed_params: futurism_signed_params(options)}
21
+ collection_class_name = collection.klass.name
22
+ as = options.delete(:as) || collection_class_name.downcase
23
+ collection.map { |record|
24
+ Element.new(extends: extends, placeholder: placeholder, options: options.deep_merge(locals: {as.to_sym => record})).render
25
+ }.join.html_safe
21
26
  end
22
27
  end
23
28
 
24
- def futurize_active_record(records, extends:, placeholder:)
29
+ def futurize_active_record(records, extends:, placeholder:, **options)
25
30
  Array(records).map { |record|
31
+ Element.new(extends: extends, options: options.merge(model: record), placeholder: placeholder).render
32
+ }.join.html_safe
33
+ end
34
+
35
+ # wraps functionality for rendering a futurism element
36
+ class Element
37
+ include ActionView::Helpers
38
+
39
+ attr_reader :extends, :placeholder, :html_options, :model, :options
40
+
41
+ def initialize(extends:, placeholder:, options:)
42
+ @extends = extends
43
+ @placeholder = placeholder
44
+ @html_options = options.delete(:html_options) || {}
45
+ @model = options.delete(:model)
46
+ @options = options
47
+ end
48
+
49
+ def dataset
50
+ {
51
+ signed_params: signed_params,
52
+ sgid: model && model.to_sgid.to_s
53
+ }
54
+ end
55
+
56
+ def render
26
57
  case extends
58
+ when :li
59
+ content_tag :li, placeholder, {data: dataset, is: "futurism-li"}.merge(html_options)
27
60
  when :tr
28
- content_tag :tr, placeholder, data: {signed_params: futurism_signed_params(record)}, is: "futurism-table-row"
61
+ content_tag :tr, placeholder, {data: dataset, is: "futurism-table-row"}.merge(html_options)
29
62
  else
30
- content_tag :"futurism-element", placeholder, data: {signed_params: futurism_signed_params(record)}
63
+ content_tag :"futurism-element", placeholder, {data: dataset}.merge(html_options)
31
64
  end
32
- }.join.html_safe
33
- end
65
+ end
66
+
67
+ private
34
68
 
35
- def futurism_signed_params(params)
36
- Rails.application.message_verifier("futurism").generate(params)
69
+ def signed_params
70
+ Rails.application.message_verifier("futurism").generate(options)
71
+ end
37
72
  end
38
73
  end
39
74
  end
@@ -1,13 +1,73 @@
1
- module FuturismHelper
2
- def futurize(records, extends: :tr, &block)
3
- placeholder = capture(&block)
4
- Array(records).map { |record|
5
- case extends
6
- when :tr
7
- content_tag :tr, placeholder, data: { sgid: record.to_sgid.to_s }, is: "futurism-table-row"
1
+ module Futurism
2
+ module Helpers
3
+ def futurize(records_or_string = nil, extends:, **options, &block)
4
+ placeholder = capture(&block)
5
+
6
+ if records_or_string.is_a?(ActiveRecord::Base) || records_or_string.is_a?(ActiveRecord::Relation)
7
+ futurize_active_record(records_or_string, extends: extends, placeholder: placeholder, **options)
8
+ elsif records_or_string.is_a?(String)
9
+ futurize_with_options(extends: extends, placeholder: placeholder, partial: records_or_string, **options)
8
10
  else
9
- content_tag :"futurism-element", placeholder, data: { sgid: record.to_sgid.to_s }
11
+ futurize_with_options(extends: extends, placeholder: placeholder, **options)
10
12
  end
11
- }.join.html_safe
13
+ end
14
+
15
+ def futurize_with_options(extends:, placeholder:, **options)
16
+ collection = options.delete(:collection)
17
+ if collection.nil?
18
+ Element.new(extends: extends, placeholder: placeholder, options: options).render
19
+ else
20
+ collection_class_name = collection.klass.name
21
+ as = options.delete(:as) || collection_class_name.downcase
22
+ collection.map { |record|
23
+ Element.new(extends: extends, placeholder: placeholder, options: options.deep_merge(locals: {as.to_sym => record})).render
24
+ }.join.html_safe
25
+ end
26
+ end
27
+
28
+ def futurize_active_record(records, extends:, placeholder:, **options)
29
+ Array(records).map { |record|
30
+ Element.new(extends: extends, options: options.merge(model: record), placeholder: placeholder).render
31
+ }.join.html_safe
32
+ end
33
+
34
+ # wraps functionality for rendering a futurism element
35
+ class Element
36
+ include ActionView::Helpers
37
+
38
+ attr_reader :extends, :placeholder, :html_options, :model, :options
39
+
40
+ def initialize(extends:, placeholder:, options:)
41
+ @extends = extends
42
+ @placeholder = placeholder
43
+ @html_options = options.delete(:html_options) || {}
44
+ @model = options.delete(:model)
45
+ @options = options
46
+ end
47
+
48
+ def dataset
49
+ {
50
+ signed_params: signed_params,
51
+ sgid: model && model.to_sgid.to_s
52
+ }
53
+ end
54
+
55
+ def render
56
+ case extends
57
+ when :li
58
+ content_tag :li, placeholder, {data: dataset, is: "futurism-li"}.merge(html_options)
59
+ when :tr
60
+ content_tag :tr, placeholder, {data: dataset, is: "futurism-table-row"}.merge(html_options)
61
+ else
62
+ content_tag :"futurism-element", placeholder, {data: dataset}.merge(html_options)
63
+ end
64
+ end
65
+
66
+ private
67
+
68
+ def signed_params
69
+ Rails.application.message_verifier("futurism").generate(options)
70
+ end
71
+ end
12
72
  end
13
73
  end
@@ -1,3 +1,3 @@
1
1
  module Futurism
2
- VERSION = "0.2.0"
2
+ VERSION = "0.4.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: futurism
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Julian Rubisch
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-07-21 00:00:00.000000000 Z
11
+ date: 2020-08-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler