futurism 1.0.1 → 1.2.0.pre3

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: 80e0ce6e5c458215afe4266493a6644a029b75f005bfbadcdc181b82325e0d43
4
+ data.tar.gz: 902744214a43cee7e28c57f1999e52191aaf2d2022860c53c4784ba9fda200fd
5
5
  SHA512:
6
- metadata.gz: 3942643f2fcadb5be781030aaf2eebd806da1d672e034b799c5ca43b3ed3d3aed3708d97ec70d4400f843dc134d0499f236a2e8e41a3d201e7cb2ba05c6b7ec0
7
- data.tar.gz: 71d2546809de6dc4355e57e4f54e7bf783098175f4cc221deb4c874b508b35b33796990ae7d633382b80a28c753b4e9f9d34dfb3661f3535e6f05655b0d10be9
6
+ metadata.gz: a8638ce1768afd2da09199dc9e033b32168b54c401fd5ab2b26e20e127b38e4c172b93d8846cca8cc0adb28078073fb0bcd2554d70a5d070e60f631d1d596e5b
7
+ data.tar.gz: cf8c0a7ecd8451b15f7d71f496bd0e49adb358e1bf9770449424e4d4780709b410ba197f8c459a71468f46c3d7961b4ae331bd9e61e4d0e1d959720b46a7dd88
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,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
@@ -53,12 +56,13 @@ module Futurism
53
56
  include ActionView::Helpers
54
57
  include Futurism::MessageVerifier
55
58
 
56
- attr_reader :extends, :placeholder, :html_options, :data_attributes, :model, :options, :eager, :controller
59
+ attr_reader :placeholder, :html_options, :data_attributes, :model, :options, :eager, :broadcast_each, :controller
57
60
 
58
61
  def initialize(extends:, placeholder:, options:)
59
62
  @extends = extends
60
63
  @placeholder = placeholder
61
64
  @eager = options.delete(:eager)
65
+ @broadcast_each = options.delete(:broadcast_each)
62
66
  @controller = options.delete(:controller)
63
67
  @html_options = options.delete(:html_options) || {}
64
68
  @data_attributes = html_options.fetch(:data, {}).except(:sgid, :signed_params)
@@ -71,19 +75,13 @@ module Futurism
71
75
  signed_params: signed_params,
72
76
  sgid: model && model.to_sgid.to_s,
73
77
  eager: eager.presence,
78
+ broadcast_each: broadcast_each.presence,
74
79
  signed_controller: signed_controller
75
80
  })
76
81
  end
77
82
 
78
83
  def render
79
- case extends
80
- when :li
81
- content_tag :li, placeholder, html_options.deep_merge({data: dataset, is: "futurism-li"})
82
- when :tr
83
- content_tag :tr, placeholder, html_options.deep_merge({data: dataset, is: "futurism-table-row"})
84
- else
85
- content_tag :"futurism-element", placeholder, html_options.deep_merge({data: dataset})
86
- end
84
+ content_tag :"futurism-element", placeholder, html_options.deep_merge({data: dataset, extends: extends})
87
85
  end
88
86
 
89
87
  def transformed_options
@@ -108,6 +106,15 @@ module Futurism
108
106
 
109
107
  message_verifier.generate(controller.to_s)
110
108
  end
109
+
110
+ def extends
111
+ # TODO remove this in the next major version
112
+ case @extends
113
+ when :li then "list-item"
114
+ when :tr then "table-row"
115
+ else @extends
116
+ end
117
+ end
111
118
  end
112
119
  end
113
120
  end
@@ -1,3 +1,3 @@
1
1
  module Futurism
2
- VERSION = "1.0.1"
2
+ VERSION = "1.2.0.pre3"
3
3
  end
@@ -1,3 +1,3 @@
1
1
  module Futurism
2
- VERSION = "1.0.0"
2
+ VERSION = "1.2.0.pre2"
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.2.0.pre3
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-10-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -126,16 +126,16 @@ dependencies:
126
126
  name: cable_ready
127
127
  requirement: !ruby/object:Gem::Requirement
128
128
  requirements:
129
- - - ">="
129
+ - - '='
130
130
  - !ruby/object:Gem::Version
131
- version: '4'
131
+ version: 5.0.0.pre4
132
132
  type: :runtime
133
133
  prerelease: false
134
134
  version_requirements: !ruby/object:Gem::Requirement
135
135
  requirements:
136
- - - ">="
136
+ - - '='
137
137
  - !ruby/object:Gem::Version
138
- version: '4'
138
+ version: 5.0.0.pre4
139
139
  description: Uses custom html elements with attached IntersectionObserver to automatically
140
140
  lazy load partials via websockets
141
141
  email:
@@ -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
@@ -183,9 +182,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
183
182
  version: '0'
184
183
  required_rubygems_version: !ruby/object:Gem::Requirement
185
184
  requirements:
186
- - - ">="
185
+ - - ">"
187
186
  - !ruby/object:Gem::Version
188
- version: '0'
187
+ version: 1.3.1
189
188
  requirements: []
190
189
  rubygems_version: 3.1.4
191
190
  signing_key:
@@ -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