futurism 0.1.4 → 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: 92467463ea6f80379a1dc26c85afe18564cd5cf2c6b92be207fe68d74f50b674
4
- data.tar.gz: 7e5934d6194107a7d3b7ba39af4f38f0731ba8caa67fb706afeac5220ae46d85
3
+ metadata.gz: 490e77869b91685fe4335badd8006842871eaba8df9cab7998038c531f11e607
4
+ data.tar.gz: 9bc29e824f7a1e8298942842d543e19eef1e2cf6e22d1b058109675b2d38ad15
5
5
  SHA512:
6
- metadata.gz: a6514295bff471bc75f02186eb0446166a14d4dfdf1993b481ce6f0df737b863a90c33d4eef5ae09b0ad5007d66fb4dda9639debdedfc3a6ce6cc0613a9d0c29
7
- data.tar.gz: 5aedb6c88925314d27a46d933b243113006be0b49d0f20fb91d4b767426deecbc73ce78d99e3148e09ca2a8219bf61a510f12dc57d0c6d846e6149a85301e852
6
+ metadata.gz: 55449d0339a7731cb8725459aca47b66ac8039600c4e8e24a5dbf3f7c9986466f949fc91042f1882dbc2332a61009f3cabbd3c76dda98d1a07bd7a4c9abdb111
7
+ data.tar.gz: 315e1274d56647b26bd0a703759160f93fa75e8d302bf5cc6304835e74fabc727e68395cba3554bf7a2fa5174acfb11c481fdbe536df0af34870560a1aa54b54
data/README.md CHANGED
@@ -1,10 +1,12 @@
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-5-orange.svg?style=flat-square)](#contributors-)
4
+ [![All Contributors](https://img.shields.io/badge/all_contributors-6-orange.svg?style=flat-square)](#contributors-)
5
5
  <!-- ALL-CONTRIBUTORS-BADGE:END -->
6
6
  Lazy-load Rails partials via CableReady
7
7
 
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
+
8
10
  ## Facts
9
11
  - only one dependency: CableReady
10
12
  - bundle size (without CableReady) is around [~1.04kB](https://bundlephobia.com/result?p=@minthesize/futurism@0.1.3)
@@ -23,7 +25,9 @@ Lazy-load Rails partials via CableReady
23
25
  with a helper in your template
24
26
 
25
27
  ```erb
26
- <%= futurize @posts %>
28
+ <%= futurize @posts, extends: :div do %>
29
+ <!-- placeholder -->
30
+ <% end %>
27
31
  ```
28
32
 
29
33
  custom `<futurism-element>`s (in the form of a `<div>` or a `<tr is="futurism-table-row">` are rendered. Those custom elements have an `IntersectionObserver` attached that will send a signed global id to an ActionCable channel (`FuturismChannel`) which will then replace the placeholders with the actual resource partial.
@@ -40,7 +44,21 @@ You can pass the placeholder as a block:
40
44
 
41
45
  ![aa601dec1930151f71dbf0d6b02b61c9](https://user-images.githubusercontent.com/4352208/87131629-f768a480-c294-11ea-89a9-ea0a76ee06ef.gif)
42
46
 
43
- ### Partial Path
47
+ ## API
48
+
49
+ Currently there are two ways to call `futurize`, designed to wrap `render`'s behavior:
50
+
51
+ ### Resource
52
+
53
+ You can pass a single `ActiveRecord` or an `ActiveRecord::Relation` to `futurize`, just as you would call `render`:
54
+
55
+ ```erb
56
+ <%= futurize @posts, extends: :tr do %>
57
+ <td class="placeholder"></td>
58
+ <% end %>
59
+ ```
60
+
61
+ #### Partial Path
44
62
 
45
63
  Remember that you can override the partial path in you models, like so:
46
64
 
@@ -54,6 +72,23 @@ end
54
72
 
55
73
  That way you get maximal flexibility when just specifying a single resource.
56
74
 
75
+ ### Explicit Partial
76
+
77
+ Call `futurize` with a `partial` keyword:
78
+
79
+ ```erb
80
+ <%= futurize partial: "items/card", locals: {card: @card}, extends: :div %>
81
+ <div class="spinner"></div>
82
+ <% end %>
83
+ ```
84
+
85
+ You can also use the shorthand syntax:
86
+
87
+ ```erb
88
+ <%= futurize "items/card", card: @card, extends: :div %>
89
+ <div class="spinner"></div>
90
+ <% end %>
91
+
57
92
  ## Installation
58
93
  Add this line to your application's Gemfile:
59
94
 
@@ -72,13 +107,13 @@ To copy over the javascript files to your application, run
72
107
  $ bin/rails futurism:install
73
108
  ```
74
109
 
75
- ** ! Note that the installer will run `yarn install @minthesize/futurism` for you ! **
110
+ **! Note that the installer will run `yarn add @minthesize/futurism` for you !**
76
111
 
77
112
  ### Manual Installation
78
113
  After `bundle`, install the Javascript library:
79
114
 
80
115
  ```bash
81
- $ bin/yarn install @minthesize/futurism
116
+ $ bin/yarn add @minthesize/futurism
82
117
  ```
83
118
 
84
119
  In your `app/javascript/channels/index.js`, add the following
@@ -111,6 +146,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
111
146
  <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>
112
147
  <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>
113
148
  <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>
114
150
  </tr>
115
151
  </table>
116
152
 
@@ -6,6 +6,10 @@ require "futurism/channel"
6
6
  require "futurism/helpers"
7
7
 
8
8
  module Futurism
9
+ extend ActiveSupport::Autoload
10
+
11
+ autoload :Helpers, "futurism/helpers"
12
+
9
13
  ActiveSupport.on_load(:action_view) {
10
14
  include Futurism::Helpers
11
15
  }
@@ -7,13 +7,15 @@ module Futurism
7
7
  end
8
8
 
9
9
  def receive(data)
10
- resources = data["sgids"].map { |sgid|
11
- [sgid, GlobalID::Locator.locate_signed(sgid)]
10
+ resources = data["signed_params"].map { |signed_params|
11
+ [signed_params, Rails.application.message_verifier("futurism").verify(signed_params)]
12
12
  }
13
13
 
14
- resources.each do |sgid, resource|
14
+ ApplicationController.renderer.instance_variable_set(:@env, connection.env)
15
+
16
+ resources.each do |signed_params, resource|
15
17
  cable_ready["Futurism::Channel"].outer_html(
16
- selector: "[data-sgid='#{sgid}']",
18
+ selector: "[data-signed-params='#{signed_params}']",
17
19
  html: ApplicationController.render(resource)
18
20
  )
19
21
  end
@@ -1,15 +1,39 @@
1
1
  module Futurism
2
2
  module Helpers
3
- def futurize(records, extends:, &block)
3
+ def futurize(records_or_string = nil, extends:, **options, &block)
4
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)
8
+ elsif records_or_string.is_a?(String)
9
+ futurize_with_options(extends: extends, partial: records_or_string, locals: options, placeholder: placeholder)
10
+ else
11
+ futurize_with_options(extends: extends, placeholder: placeholder, **options)
12
+ end
13
+ end
14
+
15
+ 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"
19
+ else
20
+ content_tag :"futurism-element", placeholder, data: {signed_params: futurism_signed_params(options)}
21
+ end
22
+ end
23
+
24
+ def futurize_active_record(records, extends:, placeholder:)
5
25
  Array(records).map { |record|
6
26
  case extends
7
27
  when :tr
8
- content_tag :tr, placeholder, data: {sgid: record.to_sgid.to_s}, is: "futurism-table-row"
28
+ content_tag :tr, placeholder, data: {signed_params: futurism_signed_params(record)}, is: "futurism-table-row"
9
29
  else
10
- content_tag :"futurism-element", placeholder, data: {sgid: record.to_sgid.to_s}
30
+ content_tag :"futurism-element", placeholder, data: {signed_params: futurism_signed_params(record)}
11
31
  end
12
32
  }.join.html_safe
13
33
  end
34
+
35
+ def futurism_signed_params(params)
36
+ Rails.application.message_verifier("futurism").generate(params)
37
+ end
14
38
  end
15
39
  end
@@ -1,3 +1,3 @@
1
1
  module Futurism
2
- VERSION = "0.1.4"
2
+ VERSION = "0.2.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.1.4
4
+ version: 0.2.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-18 00:00:00.000000000 Z
11
+ date: 2020-07-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: 0.12.2
55
+ - !ruby/object:Gem::Dependency
56
+ name: nokogiri
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: standardrb
57
71
  requirement: !ruby/object:Gem::Requirement