futurism 0.3.3 → 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: '09ba45b906e8c0f223e8da6793131f6eb96acad018116c32584d033392f1fe3b'
4
- data.tar.gz: 178166e14d83fab48dcb6296d7dc9b412762ca228e201d5d97e7c70c08d1d1e1
3
+ metadata.gz: ba66b2cb1a12fb863e2e0fc34798411d7f95947a197496150e1a499582fc4e28
4
+ data.tar.gz: 3f7f290ef2b0b79f9c9e5100e7eaed2520dcb8e481e391bd537649d0023d379b
5
5
  SHA512:
6
- metadata.gz: 29c7a409b86e1e29eac2feadc4e01d2042071e8daa196849bc676d400b2d59f4b4b7548fd4c3c76d45a262cb0f57b80adc0bde56812a2bcbc394eefd56f50c1c
7
- data.tar.gz: f85ea36325a9a4aecb63a0c8a47636db1cf4e7573907e558cb46203bfbcd87b583d52efd78b2c6bffe20040a44e4cbad9cd4bf269741a7d011966d427518ec55
6
+ metadata.gz: e772253cf0ed802d14c6836c0fb7285f7011f79f516fa88cc85e13191b7de0f29ebbc0edbd5c7996f4f858ec4efe4890613605587a2cbd6c17c4ecbad05b4985
7
+ data.tar.gz: cf4db9dd38a545a7fe43381dc22597a4c5e77a4023698b19017740fa996e6aa0433e1927c115a1187bb9028ad26e2b36aa3ecd9f66fb19000380a6b6467efffb
data/README.md CHANGED
@@ -182,7 +182,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
182
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>
183
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>
184
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>
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></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
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
187
  </tr>
188
188
  <tr>
@@ -7,21 +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
12
  new_env = connection.env.merge(ApplicationController.renderer.instance_variable_get(:@env))
15
13
  ApplicationController.renderer.instance_variable_set(:@env, new_env)
16
14
 
17
- 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?
18
18
  cable_ready["Futurism::Channel"].outer_html(
19
- selector: "[data-signed-params='#{signed_params}']",
20
- html: ApplicationController.render(resource)
19
+ selector: selector,
20
+ html: ApplicationController.render(resource(signed_params: signed_params, sgid: sgid))
21
21
  )
22
22
  end
23
23
 
24
24
  cable_ready.broadcast
25
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
26
34
  end
27
35
  end
@@ -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
@@ -6,7 +6,8 @@ module Futurism
6
6
  if records_or_string.is_a?(ActiveRecord::Base) || records_or_string.is_a?(ActiveRecord::Relation)
7
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
@@ -15,37 +16,59 @@ module Futurism
15
16
  def futurize_with_options(extends:, placeholder:, **options)
16
17
  collection = options.delete(:collection)
17
18
  if collection.nil?
18
- render_element(extends: extends, placeholder: placeholder, options: options)
19
+ Element.new(extends: extends, placeholder: placeholder, options: options).render
19
20
  else
20
21
  collection_class_name = collection.klass.name
21
22
  as = options.delete(:as) || collection_class_name.downcase
22
23
  collection.map { |record|
23
- render_element(extends: extends, placeholder: placeholder, options: options.deep_merge(locals: {as.to_sym => record}))
24
+ Element.new(extends: extends, placeholder: placeholder, options: options.deep_merge(locals: {as.to_sym => record})).render
24
25
  }.join.html_safe
25
26
  end
26
27
  end
27
28
 
28
29
  def futurize_active_record(records, extends:, placeholder:, **options)
29
30
  Array(records).map { |record|
30
- render_element(extends: extends, placeholder: placeholder, options: options.merge(model: record))
31
+ Element.new(extends: extends, options: options.merge(model: record), placeholder: placeholder).render
31
32
  }.join.html_safe
32
33
  end
33
34
 
34
- def render_element(extends:, options:, placeholder:)
35
- html_options = options.delete(:html_options) || {}
36
- model_or_options = options.delete(:model) || options
37
- case extends
38
- when :li
39
- content_tag :li, placeholder, {data: {signed_params: futurism_signed_params(model_or_options)}, is: "futurism-li"}.merge(html_options)
40
- when :tr
41
- content_tag :tr, placeholder, {data: {signed_params: futurism_signed_params(model_or_options)}, is: "futurism-table-row"}.merge(html_options)
42
- else
43
- content_tag :"futurism-element", placeholder, {data: {signed_params: futurism_signed_params(model_or_options)}}.merge(html_options)
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
57
+ case extends
58
+ when :li
59
+ content_tag :li, placeholder, {data: dataset, is: "futurism-li"}.merge(html_options)
60
+ when :tr
61
+ content_tag :tr, placeholder, {data: dataset, is: "futurism-table-row"}.merge(html_options)
62
+ else
63
+ content_tag :"futurism-element", placeholder, {data: dataset}.merge(html_options)
64
+ end
44
65
  end
45
- end
46
66
 
47
- def futurism_signed_params(params)
48
- Rails.application.message_verifier("futurism").generate(params)
67
+ private
68
+
69
+ def signed_params
70
+ Rails.application.message_verifier("futurism").generate(options)
71
+ end
49
72
  end
50
73
  end
51
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.3.3"
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.3.3
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-30 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