futurism 1.0.1 → 1.1.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d03b4cdaed591ec0a7f0031ddc637ce51030b65e7394031c0f6621b46dbf50ef
4
- data.tar.gz: fd47bcaefebc50d2e5c3fdafcbdd9908505a3fa8b6f5a3c5c96838290c814649
3
+ metadata.gz: 591fcd6ac1487dba8088003e9890f285336699ca85e4c54a42d8cc870c75a094
4
+ data.tar.gz: 1acc85f2deb6f879104a8010581a42eeed05692154ef0fba7fc0f0858d88afcd
5
5
  SHA512:
6
- metadata.gz: 3942643f2fcadb5be781030aaf2eebd806da1d672e034b799c5ca43b3ed3d3aed3708d97ec70d4400f843dc134d0499f236a2e8e41a3d201e7cb2ba05c6b7ec0
7
- data.tar.gz: 71d2546809de6dc4355e57e4f54e7bf783098175f4cc221deb4c874b508b35b33796990ae7d633382b80a28c753b4e9f9d34dfb3661f3535e6f05655b0d10be9
6
+ metadata.gz: f037cca43c70ba733a81fe366b6af3885e262318cf3333194e756a7593d9e7df729bc651e6b37071607fe954f2baf77fac2eb7c6f027cd94daa2a29dc5d99df8
7
+ data.tar.gz: cd1ae23d900d3e594cbb4f351afda32e96847eb3be9d263559f6540c1b306b24675315d87c4575cf17d60c4ed72675786a19e150ed5534ad18e98b29754c3b58
data/README.md CHANGED
@@ -22,6 +22,7 @@ Lazy-load Rails partials via CableReady
22
22
  - [HTML Options](#html-options)
23
23
  - [Eager Loading](#eager-loading)
24
24
  - [Broadcast Partials Individually](#broadcast-partials-individually)
25
+ - [Contextual Placeholder Arguments](#contextual-placeholder-arguments)
25
26
  - [Events](#events)
26
27
  - [Installation](#installation)
27
28
  - [Manual Installation](#manual-installation)
@@ -187,6 +188,28 @@ For collections, however, you can opt into individual broadcasts by specifying `
187
188
  <% end %>
188
189
  ```
189
190
 
191
+ ## Contextual Placeholder Arguments
192
+
193
+ For individual models or arbitrary collections, you can pass `record` and `index` to the placeholder block as arguments:
194
+
195
+ ```erb
196
+ <%= futurize @post, extends: :div do |post| %>
197
+ <div><%= post.title %></div>
198
+ <% end %>
199
+ ```
200
+
201
+ ```erb
202
+ <%= futurize @posts, extends: :tr do |post, index| %>
203
+ <td><%= index + 1 %></td><td><%= post.title %></td>
204
+ <% end %>
205
+ ```
206
+
207
+ ```erb
208
+ <%= futurize partial: "users/user", collection: users, extends: "tr" do |user, index| %>
209
+ <td><%= index + 1 %></td><td><%= user.name %></td>
210
+ <% end >
211
+ ```
212
+
190
213
  ## Events
191
214
 
192
215
  Once your futurize element has been rendered, the `futurize:appeared` custom event will be called.
@@ -9,31 +9,32 @@ module Futurism
9
9
  end
10
10
  end
11
11
 
12
- if block_given?
13
- placeholder = capture(&block)
14
- else
15
- options[:eager] = true
16
- end
12
+ options[:eager] = true unless block_given?
17
13
 
18
14
  if records_or_string.is_a?(ActiveRecord::Base) || records_or_string.is_a?(ActiveRecord::Relation)
19
- futurize_active_record(records_or_string, extends: extends, placeholder: placeholder, **options)
15
+ futurize_active_record(records_or_string, extends: extends, **options, &block)
20
16
  elsif records_or_string.is_a?(String)
21
17
  html_options = options.delete(:html_options)
22
- futurize_with_options(extends: extends, placeholder: placeholder, partial: records_or_string, locals: options, html_options: html_options)
18
+ futurize_with_options(extends: extends, partial: records_or_string, locals: options, html_options: html_options, &block)
23
19
  else
24
- futurize_with_options(extends: extends, placeholder: placeholder, **options)
20
+ futurize_with_options(extends: extends, **options, &block)
25
21
  end
26
22
  end
27
23
 
28
- def futurize_with_options(extends:, placeholder:, **options)
24
+ def futurize_with_options(extends:, **options, &block)
29
25
  collection = options.delete(:collection)
30
26
  if collection.nil?
27
+ placeholder = capture(&block) if block_given?
28
+
31
29
  WrappingFuturismElement.new(extends: extends, placeholder: placeholder, options: options).render
32
30
  else
33
31
  collection_class_name = collection.try(:klass).try(:name) || collection.first.class.to_s
34
32
  as = options.delete(:as) || collection_class_name.underscore
35
33
  broadcast_each = options.delete(:broadcast_each) || false
34
+
36
35
  collection.each_with_index.map { |record, index|
36
+ placeholder = capture(record, index, &block) if block_given?
37
+
37
38
  WrappingFuturismElement.new(extends: extends, placeholder: placeholder, options: options.deep_merge(
38
39
  broadcast_each: broadcast_each,
39
40
  locals: {as.to_sym => record, "#{as}_counter".to_sym => index}
@@ -42,8 +43,10 @@ module Futurism
42
43
  end
43
44
  end
44
45
 
45
- def futurize_active_record(records, extends:, placeholder:, **options)
46
- Array(records).map { |record|
46
+ def futurize_active_record(records, extends:, **options, &block)
47
+ Array(records).map.with_index { |record, index|
48
+ placeholder = capture(record, index, &block) if block_given?
49
+
47
50
  WrappingFuturismElement.new(extends: extends, options: options.merge(model: record), placeholder: placeholder).render
48
51
  }.join.html_safe
49
52
  end
@@ -9,27 +9,27 @@ module Futurism
9
9
  end
10
10
  end
11
11
 
12
- if block_given?
13
- placeholder = capture(&block)
14
- else
15
- options[:eager] = true
16
- end
12
+ options[:eager] = true unless block_given?
17
13
 
18
14
  if records_or_string.is_a?(ActiveRecord::Base) || records_or_string.is_a?(ActiveRecord::Relation)
19
- futurize_active_record(records_or_string, extends: extends, placeholder: placeholder, **options)
15
+ futurize_active_record(records_or_string, extends: extends, **options, &block)
20
16
  elsif records_or_string.is_a?(String)
21
17
  html_options = options.delete(:html_options)
22
- futurize_with_options(extends: extends, placeholder: placeholder, partial: records_or_string, locals: options, html_options: html_options)
18
+ futurize_with_options(extends: extends, partial: records_or_string, locals: options, html_options: html_options, &block)
23
19
  else
24
- futurize_with_options(extends: extends, placeholder: placeholder, **options)
20
+ futurize_with_options(extends: extends, **options, &block)
25
21
  end
26
22
  end
27
23
 
28
- def futurize_with_options(extends:, placeholder:, **options)
24
+ def futurize_with_options(extends:, **options, &block)
29
25
  collection = options.delete(:collection)
30
26
  if collection.nil?
27
+ placeholder = capture(&block) if block_given?
28
+
31
29
  WrappingFuturismElement.new(extends: extends, placeholder: placeholder, options: options).render
32
30
  else
31
+ placeholder = capture(record, index, &block) if block_given?
32
+
33
33
  collection_class_name = collection.try(:klass).try(:name) || collection.first.class.to_s
34
34
  as = options.delete(:as) || collection_class_name.underscore
35
35
  broadcast_each = options.delete(:broadcast_each) || false
@@ -42,8 +42,10 @@ module Futurism
42
42
  end
43
43
  end
44
44
 
45
- def futurize_active_record(records, extends:, placeholder:, **options)
46
- Array(records).map { |record|
45
+ def futurize_active_record(records, extends:, **options, &block)
46
+ Array(records).map.with_index { |record, index|
47
+ placeholder = capture(record, index, &block) if block_given?
48
+
47
49
  WrappingFuturismElement.new(extends: extends, options: options.merge(model: record), placeholder: placeholder).render
48
50
  }.join.html_safe
49
51
  end
@@ -53,12 +55,13 @@ module Futurism
53
55
  include ActionView::Helpers
54
56
  include Futurism::MessageVerifier
55
57
 
56
- attr_reader :extends, :placeholder, :html_options, :data_attributes, :model, :options, :eager, :controller
58
+ attr_reader :extends, :placeholder, :html_options, :data_attributes, :model, :options, :eager, :broadcast_each, :controller
57
59
 
58
60
  def initialize(extends:, placeholder:, options:)
59
61
  @extends = extends
60
62
  @placeholder = placeholder
61
63
  @eager = options.delete(:eager)
64
+ @broadcast_each = options.delete(:broadcast_each)
62
65
  @controller = options.delete(:controller)
63
66
  @html_options = options.delete(:html_options) || {}
64
67
  @data_attributes = html_options.fetch(:data, {}).except(:sgid, :signed_params)
@@ -71,6 +74,7 @@ module Futurism
71
74
  signed_params: signed_params,
72
75
  sgid: model && model.to_sgid.to_s,
73
76
  eager: eager.presence,
77
+ broadcast_each: broadcast_each.presence,
74
78
  signed_controller: signed_controller
75
79
  })
76
80
  end
@@ -1,3 +1,3 @@
1
1
  module Futurism
2
- VERSION = "1.0.1"
2
+ VERSION = "1.1.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: 1.0.1
4
+ version: 1.1.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: 2021-08-25 00:00:00.000000000 Z
11
+ date: 2021-09-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -150,7 +150,6 @@ files:
150
150
  - config/routes.rb
151
151
  - lib/futurism.rb
152
152
  - lib/futurism.rb~
153
- - lib/futurism/#helpers.rb#
154
153
  - lib/futurism/channel.rb
155
154
  - lib/futurism/channel.rb~
156
155
  - lib/futurism/engine.rb
@@ -1,105 +0,0 @@
1
- module Futurism
2
- module Helpers
3
- def futurize(records_or_string = nil, extends:, **options, &block)
4
- if Rails.env.test? && Futurism.skip_in_test
5
- if records_or_string.nil?
6
- return render(**options)
7
- else
8
- return render(records_or_string, **options)
9
- end
10
- end
11
-
12
- placeholder = capture(&block)
13
-
14
- if records_or_string.is_a?(ActiveRecord::Base) || records_or_string.is_a?(ActiveRecord::Relation)
15
- futurize_active_record(records_or_string, extends: extends, placeholder: placeholder, **options)
16
- elsif records_or_string.is_a?(String)
17
- html_options = options.delete(:html_options)
18
- futurize_with_options(extends: extends, placeholder: placeholder, partial: records_or_string, locals: options, html_options: html_options)
19
- else
20
- futurize_with_options(extends: extends, placeholder: placeholder, **options)
21
- end
22
- end
23
-
24
- def futurize_with_options(extends:, placeholder:, **options)
25
- collection = options.delete(:collection)
26
- if collection.nil?
27
- WrappingFuturismElement.new(extends: extends, placeholder: placeholder, options: options).render
28
- else
29
- collection_class_name = collection.try(:klass).try(:name) || collection.first.class.to_s
30
- as = options.delete(:as) || collection_class_name.underscore
31
- collection.each_with_index.map { |record, index|
32
- WrappingFuturismElement.new(extends: extends, placeholder: placeholder, options: options.deep_merge(locals: {as.to_sym => record, "#{as}_counter".to_sym => index})).render
33
- }.join.html_safe
34
- end
35
- end
36
-
37
- def futurize_active_record(records, extends:, placeholder:, **options)
38
- Array(records).map { |record|
39
- WrappingFuturismElement.new(extends: extends, options: options.merge(model: record), placeholder: placeholder).render
40
- }.join.html_safe
41
- end
42
-
43
- # wraps functionality for rendering a futurism element
44
- class WrappingFuturismElement
45
- include ActionView::Helpers
46
- include Futurism::MessageVerifier
47
-
48
- attr_reader :extends, :placeholder, :html_options, :data_attributes, :model, :options, :eager, :controller
49
-
50
- def initialize(extends:, placeholder:, options:)
51
- @extends = extends
52
- @placeholder = placeholder
53
- @eager = options.delete(:eager)
54
- @controller = options.delete(:controller)
55
- @html_options = options.delete(:html_options) || {}
56
- @data_attributes = html_options.fetch(:data, {}).except(:sgid, :signed_params)
57
- @model = options.delete(:model)
58
- @options = data_attributes.any? ? options.merge(data: data_attributes) : options
59
- end
60
-
61
- def dataset
62
- data_attributes.merge({
63
- signed_params: signed_params,
64
- sgid: model && model.to_sgid.to_s,
65
- eager: eager.presence,
66
- signed_controller: signed_controller
67
- })
68
- end
69
-
70
- def render
71
- case extends
72
- when :li
73
- content_tag :li, placeholder, html_options.deep_merge({data: dataset, is: "futurism-li"})
74
- when :tr
75
- content_tag :tr, placeholder, html_options.deep_merge({data: dataset, is: "futurism-table-row"})
76
- else
77
- content_tag :"futurism-element", placeholder, html_options.deep_merge({data: dataset})
78
- end
79
- end
80
-
81
- def transformed_options
82
- require_relative "shims/deep_transform_values" unless options.respond_to? :deep_transform_values
83
-
84
- options.deep_transform_values do |value|
85
- next(value) unless value.respond_to?(:to_global_id)
86
- next(value) if value.is_a?(ActiveRecord::Base) && value.new_record?
87
-
88
- value.to_global_id.to_s
89
- end
90
- end
91
-
92
- private
93
-
94
- def signed_params
95
- message_verifier.generate(transformed_options)
96
- end
97
-
98
- def signed_controller
99
- return unless controller.present?
100
-
101
- message_verifier.generate(controller.to_s)
102
- end
103
- end
104
- end
105
- end