futurism 1.0.0 → 1.2.0.pre2

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: 414f6857d0112042c7745a9198e9e9c05d2886d3649f955d53d4ed09fed1dddc
4
- data.tar.gz: cd55e6f01214c0379e2a5b2d0505c76486cc388114daa3888439e608a0a74335
3
+ metadata.gz: 00f16be3e103b54e5c6bbd9efaea16b7196d0e4a561bde8b61051989b1e74e59
4
+ data.tar.gz: 67c9a17bb4a80fcc12f830bf8ce39174086e336b93d041f822308a20359381dc
5
5
  SHA512:
6
- metadata.gz: 14de66605b2c41ef1cc6aca2c42507a856dfd67cbb1dc23de029b77bfb5c24b5b6a5a2ecf65b95cef5622c45dfef6ecccd05059daeaf25615a12356015b5b614
7
- data.tar.gz: a63476d5c42a5db0ae0220f71b4c5a4792aaf529c0741df322a239c8be458b8433c94bd461574833c1b102e1b8f444038adb1c7f66a3a16d6b73025672fa8022
6
+ metadata.gz: ff6e67f624ad9ccd114800b457c4f7e48fa6232f87aa970f7f1513b59bd4ea3daedf05806e2582af057588d1cd19075e87ab81f8f4ffde23544f64b6e8ae2a5c
7
+ data.tar.gz: 1bafaacb876d3d50d020692a555946112a74bdb10b0cabac0e67b3b57f561b7935ba25024c0792102ec9f89addfa54fde03daf10101364cb27b51f687817956f
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.
@@ -309,8 +332,10 @@ yarn install --force
309
332
  ### Release
310
333
 
311
334
  1. Update the version numbers in `javascript/package.json` and `lib/futurism/version.rb`
312
- 2. `rake release`
313
- 3. `cd javascript && npm publish --access public`
335
+ 2. `git commit -m "Bump version to x.x.x"`
336
+ 3. Run `bundle exec rake build`
337
+ 4. Run `bundle exec rake release`
338
+ 5. `cd javascript && npm publish --access public`
314
339
 
315
340
  ## License
316
341
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -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.0"
2
+ VERSION = "1.2.0.pre2"
3
3
  end
@@ -1,3 +1,3 @@
1
1
  module Futurism
2
- VERSION = "0.8.0"
2
+ VERSION = "1.2.0.pre1"
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.0
4
+ version: 1.2.0.pre2
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-01 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