lite-component 1.0.7 → 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 +4 -4
- data/.rubocop.yml +23 -1
- data/CHANGELOG.md +24 -0
- data/Gemfile.lock +75 -64
- data/README.md +46 -1
- data/app/helpers/lite/component/component_helper.rb +3 -3
- data/lib/lite/component.rb +1 -1
- data/lib/lite/component/base.rb +57 -15
- data/lib/lite/component/locals.rb +3 -0
- data/lib/lite/component/version.rb +1 -1
- data/lite-component.gemspec +1 -0
- metadata +21 -8
- data/lib/lite/component/collection.rb +0 -58
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 2d9bdfa9c79cc9a007b6b1e35c42072dcbbbf6cf319c09b7d69738ef0133f284
|
|
4
|
+
data.tar.gz: 790df7f0ced92eec0f4403740ac4e433dc80e11f1f5ab2305d3d2fe935359afd
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: bda36b4310e86b25b3f1f6e57b24a42b8a1eec4afe178bcdcf4886a7bffcd025de9110a15aba00589062c3aacf5ae5294763acccfa70bd2690f8a45229db4196
|
|
7
|
+
data.tar.gz: 139d5bd118979cdb4d6ed7edcfe5f5e95e34d517aba028a5ef6bd462fe5955e7a2c36bed6dd7381bd79bba81eb69b95d48d3dbbc65f09eb870d26d9e507b917f
|
data/.rubocop.yml
CHANGED
|
@@ -1,10 +1,16 @@
|
|
|
1
1
|
require:
|
|
2
2
|
- rubocop-performance
|
|
3
|
+
- rubocop-rake
|
|
3
4
|
- rubocop-rspec
|
|
4
5
|
AllCops:
|
|
5
|
-
TargetRubyVersion: 2.
|
|
6
|
+
TargetRubyVersion: 2.7
|
|
6
7
|
DisplayCopNames: true
|
|
7
8
|
DisplayStyleGuide: true
|
|
9
|
+
NewCops: enable
|
|
10
|
+
Gemspec/RequiredRubyVersion:
|
|
11
|
+
Enabled: false
|
|
12
|
+
Layout/EmptyLinesAroundAttributeAccessor:
|
|
13
|
+
Enabled: true
|
|
8
14
|
Layout/EmptyLinesAroundBlockBody:
|
|
9
15
|
Exclude:
|
|
10
16
|
- 'spec/**/**/*'
|
|
@@ -14,6 +20,12 @@ Layout/EmptyLinesAroundModuleBody:
|
|
|
14
20
|
EnforcedStyle: empty_lines_except_namespace
|
|
15
21
|
Layout/LineLength:
|
|
16
22
|
Max: 100
|
|
23
|
+
Layout/SpaceAroundMethodCallOperator:
|
|
24
|
+
Enabled: true
|
|
25
|
+
Lint/RaiseException:
|
|
26
|
+
Enabled: true
|
|
27
|
+
Lint/StructNewOverride:
|
|
28
|
+
Enabled: true
|
|
17
29
|
Metrics/BlockLength:
|
|
18
30
|
Enabled: false
|
|
19
31
|
Metrics/ClassLength:
|
|
@@ -30,3 +42,13 @@ Style/Documentation:
|
|
|
30
42
|
Enabled: false
|
|
31
43
|
Style/ExpandPathArguments:
|
|
32
44
|
Enabled: false
|
|
45
|
+
Style/ExponentialNotation:
|
|
46
|
+
Enabled: true
|
|
47
|
+
Style/HashEachMethods:
|
|
48
|
+
Enabled: true
|
|
49
|
+
Style/HashTransformKeys:
|
|
50
|
+
Enabled: true
|
|
51
|
+
Style/HashTransformValues:
|
|
52
|
+
Enabled: true
|
|
53
|
+
Style/SlicingWithRange:
|
|
54
|
+
Enabled: false
|
data/CHANGELOG.md
CHANGED
|
@@ -6,6 +6,30 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
6
6
|
|
|
7
7
|
## [Unreleased]
|
|
8
8
|
|
|
9
|
+
## [1.1.0] - 2021-01-20
|
|
10
|
+
### Added
|
|
11
|
+
- Add improved collection rendering
|
|
12
|
+
### Removed
|
|
13
|
+
- Collection class is no longer needed
|
|
14
|
+
|
|
15
|
+
## [1.0.11] - 2020-04-24
|
|
16
|
+
### Added
|
|
17
|
+
- Added `to_h` and `to_hash` aliases for dumping locals
|
|
18
|
+
|
|
19
|
+
## [1.0.10] - 2020-04-06
|
|
20
|
+
### Added
|
|
21
|
+
- Added `render?` method for checking complex logic
|
|
22
|
+
|
|
23
|
+
## [1.0.9] - 2020-01-08
|
|
24
|
+
### Added
|
|
25
|
+
- Added block support to yield other components
|
|
26
|
+
### Changed
|
|
27
|
+
- Changed how the helper generated the component
|
|
28
|
+
|
|
29
|
+
## [1.0.8] - 2019-12-26
|
|
30
|
+
### Added
|
|
31
|
+
- Added `c` local variable to access the component
|
|
32
|
+
|
|
9
33
|
## [1.0.7] - 2019-12-24
|
|
10
34
|
### Changed
|
|
11
35
|
- Fixed path to nested partials
|
data/Gemfile.lock
CHANGED
|
@@ -1,59 +1,60 @@
|
|
|
1
1
|
PATH
|
|
2
2
|
remote: .
|
|
3
3
|
specs:
|
|
4
|
-
lite-component (1.0
|
|
4
|
+
lite-component (1.1.0)
|
|
5
5
|
actionview
|
|
6
6
|
|
|
7
7
|
GEM
|
|
8
8
|
remote: https://rubygems.org/
|
|
9
9
|
specs:
|
|
10
|
-
actionpack (6.
|
|
11
|
-
actionview (= 6.
|
|
12
|
-
activesupport (= 6.
|
|
13
|
-
rack (~> 2.0, >= 2.0.
|
|
10
|
+
actionpack (6.1.1)
|
|
11
|
+
actionview (= 6.1.1)
|
|
12
|
+
activesupport (= 6.1.1)
|
|
13
|
+
rack (~> 2.0, >= 2.0.9)
|
|
14
14
|
rack-test (>= 0.6.3)
|
|
15
15
|
rails-dom-testing (~> 2.0)
|
|
16
16
|
rails-html-sanitizer (~> 1.0, >= 1.2.0)
|
|
17
|
-
actionview (6.
|
|
18
|
-
activesupport (= 6.
|
|
17
|
+
actionview (6.1.1)
|
|
18
|
+
activesupport (= 6.1.1)
|
|
19
19
|
builder (~> 3.1)
|
|
20
20
|
erubi (~> 1.4)
|
|
21
21
|
rails-dom-testing (~> 2.0)
|
|
22
22
|
rails-html-sanitizer (~> 1.1, >= 1.2.0)
|
|
23
|
-
activesupport (6.
|
|
23
|
+
activesupport (6.1.1)
|
|
24
24
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
|
25
|
-
i18n (>=
|
|
26
|
-
minitest (
|
|
27
|
-
tzinfo (~>
|
|
28
|
-
zeitwerk (~> 2.
|
|
29
|
-
ast (2.4.
|
|
25
|
+
i18n (>= 1.6, < 2)
|
|
26
|
+
minitest (>= 5.1)
|
|
27
|
+
tzinfo (~> 2.0)
|
|
28
|
+
zeitwerk (~> 2.3)
|
|
29
|
+
ast (2.4.1)
|
|
30
30
|
builder (3.2.4)
|
|
31
31
|
colorize (0.8.1)
|
|
32
|
-
concurrent-ruby (1.1.
|
|
33
|
-
crass (1.0.
|
|
34
|
-
diff-lcs (1.
|
|
35
|
-
erubi (1.
|
|
36
|
-
fasterer (0.8.
|
|
32
|
+
concurrent-ruby (1.1.8)
|
|
33
|
+
crass (1.0.6)
|
|
34
|
+
diff-lcs (1.4.4)
|
|
35
|
+
erubi (1.10.0)
|
|
36
|
+
fasterer (0.8.3)
|
|
37
37
|
colorize (~> 0.7)
|
|
38
38
|
ruby_parser (>= 3.14.1)
|
|
39
39
|
generator_spec (0.9.4)
|
|
40
40
|
activesupport (>= 3.0.0)
|
|
41
41
|
railties (>= 3.0.0)
|
|
42
|
-
i18n (1.7
|
|
42
|
+
i18n (1.8.7)
|
|
43
43
|
concurrent-ruby (~> 1.0)
|
|
44
|
-
|
|
45
|
-
loofah (2.4.0)
|
|
44
|
+
loofah (2.9.0)
|
|
46
45
|
crass (~> 1.0.2)
|
|
47
46
|
nokogiri (>= 1.5.9)
|
|
48
|
-
method_source (0.
|
|
49
|
-
mini_portile2 (2.
|
|
50
|
-
minitest (5.
|
|
51
|
-
nokogiri (1.
|
|
52
|
-
mini_portile2 (~> 2.
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
47
|
+
method_source (1.0.0)
|
|
48
|
+
mini_portile2 (2.5.0)
|
|
49
|
+
minitest (5.14.3)
|
|
50
|
+
nokogiri (1.11.1)
|
|
51
|
+
mini_portile2 (~> 2.5.0)
|
|
52
|
+
racc (~> 1.4)
|
|
53
|
+
parallel (1.20.1)
|
|
54
|
+
parser (3.0.0.0)
|
|
55
|
+
ast (~> 2.4.1)
|
|
56
|
+
racc (1.5.2)
|
|
57
|
+
rack (2.2.3)
|
|
57
58
|
rack-test (1.1.0)
|
|
58
59
|
rack (>= 1.0, < 3)
|
|
59
60
|
rails-dom-testing (2.0.3)
|
|
@@ -61,48 +62,57 @@ GEM
|
|
|
61
62
|
nokogiri (>= 1.6)
|
|
62
63
|
rails-html-sanitizer (1.3.0)
|
|
63
64
|
loofah (~> 2.3)
|
|
64
|
-
railties (6.
|
|
65
|
-
actionpack (= 6.
|
|
66
|
-
activesupport (= 6.
|
|
65
|
+
railties (6.1.1)
|
|
66
|
+
actionpack (= 6.1.1)
|
|
67
|
+
activesupport (= 6.1.1)
|
|
67
68
|
method_source
|
|
68
69
|
rake (>= 0.8.7)
|
|
69
|
-
thor (
|
|
70
|
+
thor (~> 1.0)
|
|
70
71
|
rainbow (3.0.0)
|
|
71
|
-
rake (13.0.
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
rspec-
|
|
76
|
-
|
|
77
|
-
rspec-
|
|
78
|
-
rspec-
|
|
72
|
+
rake (13.0.3)
|
|
73
|
+
regexp_parser (2.0.3)
|
|
74
|
+
rexml (3.2.4)
|
|
75
|
+
rspec (3.10.0)
|
|
76
|
+
rspec-core (~> 3.10.0)
|
|
77
|
+
rspec-expectations (~> 3.10.0)
|
|
78
|
+
rspec-mocks (~> 3.10.0)
|
|
79
|
+
rspec-core (3.10.1)
|
|
80
|
+
rspec-support (~> 3.10.0)
|
|
81
|
+
rspec-expectations (3.10.1)
|
|
79
82
|
diff-lcs (>= 1.2.0, < 2.0)
|
|
80
|
-
rspec-support (~> 3.
|
|
81
|
-
rspec-mocks (3.
|
|
83
|
+
rspec-support (~> 3.10.0)
|
|
84
|
+
rspec-mocks (3.10.1)
|
|
82
85
|
diff-lcs (>= 1.2.0, < 2.0)
|
|
83
|
-
rspec-support (~> 3.
|
|
84
|
-
rspec-support (3.
|
|
85
|
-
rubocop (
|
|
86
|
-
jaro_winkler (~> 1.5.1)
|
|
86
|
+
rspec-support (~> 3.10.0)
|
|
87
|
+
rspec-support (3.10.1)
|
|
88
|
+
rubocop (1.8.1)
|
|
87
89
|
parallel (~> 1.10)
|
|
88
|
-
parser (>=
|
|
90
|
+
parser (>= 3.0.0.0)
|
|
89
91
|
rainbow (>= 2.2.2, < 4.0)
|
|
92
|
+
regexp_parser (>= 1.8, < 3.0)
|
|
93
|
+
rexml
|
|
94
|
+
rubocop-ast (>= 1.2.0, < 2.0)
|
|
90
95
|
ruby-progressbar (~> 1.7)
|
|
91
|
-
unicode-display_width (>= 1.4.0, <
|
|
92
|
-
rubocop-
|
|
93
|
-
|
|
94
|
-
rubocop-
|
|
95
|
-
rubocop (>= 0.
|
|
96
|
-
|
|
97
|
-
|
|
96
|
+
unicode-display_width (>= 1.4.0, < 3.0)
|
|
97
|
+
rubocop-ast (1.4.0)
|
|
98
|
+
parser (>= 2.7.1.5)
|
|
99
|
+
rubocop-performance (1.9.2)
|
|
100
|
+
rubocop (>= 0.90.0, < 2.0)
|
|
101
|
+
rubocop-ast (>= 0.4.0)
|
|
102
|
+
rubocop-rake (0.5.1)
|
|
103
|
+
rubocop
|
|
104
|
+
rubocop-rspec (2.1.0)
|
|
105
|
+
rubocop (~> 1.0)
|
|
106
|
+
rubocop-ast (>= 1.1.0)
|
|
107
|
+
ruby-progressbar (1.11.0)
|
|
108
|
+
ruby_parser (3.15.1)
|
|
98
109
|
sexp_processor (~> 4.9)
|
|
99
|
-
sexp_processor (4.
|
|
100
|
-
thor (1.0
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
zeitwerk (2.2.2)
|
|
110
|
+
sexp_processor (4.15.2)
|
|
111
|
+
thor (1.1.0)
|
|
112
|
+
tzinfo (2.0.4)
|
|
113
|
+
concurrent-ruby (~> 1.0)
|
|
114
|
+
unicode-display_width (2.0.0)
|
|
115
|
+
zeitwerk (2.4.2)
|
|
106
116
|
|
|
107
117
|
PLATFORMS
|
|
108
118
|
ruby
|
|
@@ -116,7 +126,8 @@ DEPENDENCIES
|
|
|
116
126
|
rspec
|
|
117
127
|
rubocop
|
|
118
128
|
rubocop-performance
|
|
129
|
+
rubocop-rake
|
|
119
130
|
rubocop-rspec
|
|
120
131
|
|
|
121
132
|
BUNDLED WITH
|
|
122
|
-
2.
|
|
133
|
+
2.1.4
|
data/README.md
CHANGED
|
@@ -104,6 +104,20 @@ Render collection of components just as you would render collections of partials
|
|
|
104
104
|
<%= component("comment_card", collection: @comments, spacer_template: "components/spacer") %>
|
|
105
105
|
```
|
|
106
106
|
|
|
107
|
+
If you can skip rendering by evaluating complex logic in the `render?` method:
|
|
108
|
+
|
|
109
|
+
```ruby
|
|
110
|
+
# app/components/alert_component.rb
|
|
111
|
+
|
|
112
|
+
class AlertComponent < Lite::Component::Base
|
|
113
|
+
|
|
114
|
+
def render?
|
|
115
|
+
object.some_complex_check?
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
end
|
|
119
|
+
```
|
|
120
|
+
|
|
107
121
|
### Context
|
|
108
122
|
|
|
109
123
|
All components include `ActionView::Context` which will give you access to request context such as
|
|
@@ -146,6 +160,7 @@ end
|
|
|
146
160
|
### Locals
|
|
147
161
|
|
|
148
162
|
All components include access to partial locals via the `locals` or `l` methods.
|
|
163
|
+
You can dump locals to hash via the `to_h` and `to_hash` methods.
|
|
149
164
|
*Note: Objects will be automatically added to locals when rendering collections.*
|
|
150
165
|
|
|
151
166
|
```erb
|
|
@@ -220,7 +235,7 @@ accessed by their key.
|
|
|
220
235
|
```
|
|
221
236
|
|
|
222
237
|
Access to anything provided within its `*_component.rb` file can be done using the
|
|
223
|
-
`component` local which is the instance of the component.
|
|
238
|
+
`component` or `c` local methods which is the instance of the component.
|
|
224
239
|
|
|
225
240
|
```erb
|
|
226
241
|
<%# app/views/components/_alert.html.erb %>
|
|
@@ -262,6 +277,36 @@ class AlertComponent < Lite::Component::Base
|
|
|
262
277
|
end
|
|
263
278
|
```
|
|
264
279
|
|
|
280
|
+
To add components as part of another component build a `block` and `yield` it in the component's view.
|
|
281
|
+
|
|
282
|
+
```erb
|
|
283
|
+
<%# app/views/components/_sidebar.html.erb %>
|
|
284
|
+
|
|
285
|
+
<div class="sidebar">
|
|
286
|
+
<%= component("sidebar/navigation", locals: { class_name: "js-nav-links" }) do |c| %>
|
|
287
|
+
<% c.add("sidebar/navigation/link", locals: { text: "Link: 1", path: "/home", active: false }) %>
|
|
288
|
+
<% c.add("sidebar/navigation/link", locals: { text: "Link: 2", path: "/about", active: true }) %>
|
|
289
|
+
<% c.add("sidebar/navigation/link", locals: { text: "Link: 3", path: "/help", active: false }) do |n_c| %>
|
|
290
|
+
<% n_c.add("sidebar/something", locals: { test: "something" }) %>
|
|
291
|
+
<% end %>
|
|
292
|
+
<% end %>
|
|
293
|
+
</div>
|
|
294
|
+
```
|
|
295
|
+
|
|
296
|
+
```erb
|
|
297
|
+
<%# app/views/components/sidebar/_navigation.html.erb %>
|
|
298
|
+
|
|
299
|
+
<div class="sidebar-navigation">
|
|
300
|
+
<%= c.yield %>
|
|
301
|
+
</div>
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
```erb
|
|
305
|
+
<%# app/views/components/sidebar/navigation/_link.html.erb %>
|
|
306
|
+
|
|
307
|
+
<%= link_to(text, path, class: ("active" if l.active)) %>
|
|
308
|
+
```
|
|
309
|
+
|
|
265
310
|
## Development
|
|
266
311
|
|
|
267
312
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
@@ -4,9 +4,9 @@ module Lite
|
|
|
4
4
|
module Component
|
|
5
5
|
module ComponentHelper
|
|
6
6
|
|
|
7
|
-
def component(name, options = {})
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
def component(name, options = {}, &block)
|
|
8
|
+
klass = Lite::Component::Base.build(name)
|
|
9
|
+
klass.render(self, options, &block)
|
|
10
10
|
end
|
|
11
11
|
|
|
12
12
|
end
|
data/lib/lite/component.rb
CHANGED
data/lib/lite/component/base.rb
CHANGED
|
@@ -7,20 +7,34 @@ module Lite
|
|
|
7
7
|
include ActionView::Context
|
|
8
8
|
include ActionView::Helpers
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
attr_accessor :components
|
|
11
|
+
attr_reader :context, :iteration, :options
|
|
12
|
+
|
|
13
|
+
# rubocop:disable Layout/LineLength
|
|
14
|
+
def initialize(context, options = {}, &block)
|
|
15
|
+
@components = []
|
|
16
|
+
@iteration = (options[:locals] || {}).delete(:iteration) || Lite::Component::Iteration.new(1, 0)
|
|
12
17
|
|
|
13
|
-
def initialize(context, options = {})
|
|
14
18
|
@context = context
|
|
15
19
|
@options = default_options.deep_merge(options)
|
|
20
|
+
|
|
21
|
+
yield(self) if block
|
|
16
22
|
end
|
|
23
|
+
# rubocop:enable Layout/LineLength
|
|
17
24
|
|
|
18
25
|
alias helpers context
|
|
19
26
|
alias c context
|
|
20
27
|
alias h helpers
|
|
28
|
+
alias i iteration
|
|
21
29
|
|
|
22
30
|
class << self
|
|
23
31
|
|
|
32
|
+
def build(name)
|
|
33
|
+
return name if name.respond_to?(:component_path)
|
|
34
|
+
|
|
35
|
+
"#{name}_component".classify.constantize
|
|
36
|
+
end
|
|
37
|
+
|
|
24
38
|
def component_name
|
|
25
39
|
component_path.split('/').last
|
|
26
40
|
end
|
|
@@ -29,19 +43,17 @@ module Lite
|
|
|
29
43
|
name.underscore.sub('_component', '')
|
|
30
44
|
end
|
|
31
45
|
|
|
32
|
-
def render(context, options = {})
|
|
33
|
-
klass = new(context, options)
|
|
46
|
+
def render(context, options = {}, &block)
|
|
47
|
+
klass = new(context, options, &block)
|
|
34
48
|
klass.render
|
|
35
49
|
end
|
|
36
50
|
|
|
37
51
|
end
|
|
38
52
|
|
|
39
|
-
def
|
|
40
|
-
|
|
53
|
+
def add(name, options = {}, &block)
|
|
54
|
+
components << [name, options, block]
|
|
41
55
|
end
|
|
42
56
|
|
|
43
|
-
alias i iteration
|
|
44
|
-
|
|
45
57
|
def locals
|
|
46
58
|
@locals ||= Lite::Component::Locals.new(options[:locals])
|
|
47
59
|
end
|
|
@@ -49,14 +61,16 @@ module Lite
|
|
|
49
61
|
alias l locals
|
|
50
62
|
|
|
51
63
|
def render
|
|
64
|
+
return unless render?
|
|
65
|
+
|
|
52
66
|
collection = options.delete(:collection)
|
|
53
|
-
return
|
|
67
|
+
return render_collection(collection) if collection.respond_to?(:each)
|
|
68
|
+
|
|
69
|
+
render_content
|
|
70
|
+
end
|
|
54
71
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
component: self,
|
|
58
|
-
spacer_template: options.delete(:spacer_template)
|
|
59
|
-
)
|
|
72
|
+
def render?
|
|
73
|
+
true
|
|
60
74
|
end
|
|
61
75
|
|
|
62
76
|
def render_content
|
|
@@ -67,6 +81,10 @@ module Lite
|
|
|
67
81
|
"components/#{self.class.component_path}"
|
|
68
82
|
end
|
|
69
83
|
|
|
84
|
+
def yield
|
|
85
|
+
context.safe_join(yield_content)
|
|
86
|
+
end
|
|
87
|
+
|
|
70
88
|
private
|
|
71
89
|
|
|
72
90
|
def default_options
|
|
@@ -75,12 +93,36 @@ module Lite
|
|
|
75
93
|
object: self,
|
|
76
94
|
as: :component,
|
|
77
95
|
locals: {
|
|
96
|
+
c: self,
|
|
78
97
|
object: nil,
|
|
79
98
|
iteration: iteration
|
|
80
99
|
}
|
|
81
100
|
}
|
|
82
101
|
end
|
|
83
102
|
|
|
103
|
+
# rubocop:disable Metrics/AbcSize
|
|
104
|
+
def render_collection(collection)
|
|
105
|
+
collection_size = collection.size
|
|
106
|
+
spacer_template = options.delete(:spacer_template)
|
|
107
|
+
|
|
108
|
+
results = collection.each_with_object([]).with_index do |(item, array), index|
|
|
109
|
+
array << context.render(spacer_template) if index.positive? && spacer_template
|
|
110
|
+
iteration = Lite::Component::Iteration.new(collection_size, index)
|
|
111
|
+
instance = self.class.new(context, locals: { object: item, iteration: iteration })
|
|
112
|
+
array << instance.render
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
context.safe_join(results)
|
|
116
|
+
end
|
|
117
|
+
# rubocop:enable Metrics/AbcSize
|
|
118
|
+
|
|
119
|
+
def yield_content
|
|
120
|
+
components.map do |name, options, block|
|
|
121
|
+
klass = self.class.build(name)
|
|
122
|
+
klass.render(context, options, &block)
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
|
|
84
126
|
end
|
|
85
127
|
end
|
|
86
128
|
end
|
data/lite-component.gemspec
CHANGED
|
@@ -46,5 +46,6 @@ Gem::Specification.new do |spec|
|
|
|
46
46
|
spec.add_development_dependency 'rspec'
|
|
47
47
|
spec.add_development_dependency 'rubocop'
|
|
48
48
|
spec.add_development_dependency 'rubocop-performance'
|
|
49
|
+
spec.add_development_dependency 'rubocop-rake'
|
|
49
50
|
spec.add_development_dependency 'rubocop-rspec'
|
|
50
51
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: lite-component
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.0
|
|
4
|
+
version: 1.1.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Juan Gomez
|
|
8
|
-
autorequire:
|
|
8
|
+
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date:
|
|
11
|
+
date: 2021-01-21 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: actionview
|
|
@@ -122,6 +122,20 @@ dependencies:
|
|
|
122
122
|
- - ">="
|
|
123
123
|
- !ruby/object:Gem::Version
|
|
124
124
|
version: '0'
|
|
125
|
+
- !ruby/object:Gem::Dependency
|
|
126
|
+
name: rubocop-rake
|
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
|
128
|
+
requirements:
|
|
129
|
+
- - ">="
|
|
130
|
+
- !ruby/object:Gem::Version
|
|
131
|
+
version: '0'
|
|
132
|
+
type: :development
|
|
133
|
+
prerelease: false
|
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
135
|
+
requirements:
|
|
136
|
+
- - ">="
|
|
137
|
+
- !ruby/object:Gem::Version
|
|
138
|
+
version: '0'
|
|
125
139
|
- !ruby/object:Gem::Dependency
|
|
126
140
|
name: rubocop-rspec
|
|
127
141
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -136,7 +150,7 @@ dependencies:
|
|
|
136
150
|
- - ">="
|
|
137
151
|
- !ruby/object:Gem::Version
|
|
138
152
|
version: '0'
|
|
139
|
-
description:
|
|
153
|
+
description:
|
|
140
154
|
email:
|
|
141
155
|
- j.gomez@drexed.com
|
|
142
156
|
executables: []
|
|
@@ -166,7 +180,6 @@ files:
|
|
|
166
180
|
- lib/generators/rails/templates/component.scss
|
|
167
181
|
- lib/lite/component.rb
|
|
168
182
|
- lib/lite/component/base.rb
|
|
169
|
-
- lib/lite/component/collection.rb
|
|
170
183
|
- lib/lite/component/engine.rb
|
|
171
184
|
- lib/lite/component/iteration.rb
|
|
172
185
|
- lib/lite/component/locals.rb
|
|
@@ -176,7 +189,7 @@ homepage: http://drexed.github.io/lite-component
|
|
|
176
189
|
licenses:
|
|
177
190
|
- MIT
|
|
178
191
|
metadata: {}
|
|
179
|
-
post_install_message:
|
|
192
|
+
post_install_message:
|
|
180
193
|
rdoc_options: []
|
|
181
194
|
require_paths:
|
|
182
195
|
- lib
|
|
@@ -191,8 +204,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
191
204
|
- !ruby/object:Gem::Version
|
|
192
205
|
version: '0'
|
|
193
206
|
requirements: []
|
|
194
|
-
rubygems_version: 3.
|
|
195
|
-
signing_key:
|
|
207
|
+
rubygems_version: 3.2.5
|
|
208
|
+
signing_key:
|
|
196
209
|
specification_version: 4
|
|
197
210
|
summary: Generate component from collections of data points
|
|
198
211
|
test_files: []
|
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module Lite
|
|
4
|
-
module Component
|
|
5
|
-
class Collection
|
|
6
|
-
|
|
7
|
-
attr_reader :collection, :component, :spacer_template
|
|
8
|
-
|
|
9
|
-
def initialize(collection, component:, spacer_template: nil)
|
|
10
|
-
@collection = collection
|
|
11
|
-
@component = component
|
|
12
|
-
@spacer_template = spacer_template
|
|
13
|
-
end
|
|
14
|
-
|
|
15
|
-
class << self
|
|
16
|
-
|
|
17
|
-
def render(collection, component)
|
|
18
|
-
klass = new(collection, component)
|
|
19
|
-
klass.render
|
|
20
|
-
end
|
|
21
|
-
|
|
22
|
-
end
|
|
23
|
-
|
|
24
|
-
def render
|
|
25
|
-
component.context.safe_join(iterated_collection)
|
|
26
|
-
end
|
|
27
|
-
|
|
28
|
-
private
|
|
29
|
-
|
|
30
|
-
def collection_size
|
|
31
|
-
@collection_size ||= begin
|
|
32
|
-
if collection.respond_to?(:size)
|
|
33
|
-
collection.size
|
|
34
|
-
elsif collection.respond_to?(:to_a)
|
|
35
|
-
collection.to_a.size
|
|
36
|
-
elsif collection.respond_to?(:to_hash)
|
|
37
|
-
collection.to_hash.size
|
|
38
|
-
end
|
|
39
|
-
end
|
|
40
|
-
end
|
|
41
|
-
|
|
42
|
-
# rubocop:disable Metrics/AbcSize
|
|
43
|
-
def iterated_collection
|
|
44
|
-
collection.each_with_object([]).with_index do |(object, array), index|
|
|
45
|
-
component.iteration = Lite::Component::Iteration.new(collection_size, index)
|
|
46
|
-
component.options.deep_merge!(locals: { object: object, iteration: component.iteration })
|
|
47
|
-
|
|
48
|
-
array << component.render_content
|
|
49
|
-
next unless spacer_template && !component.iteration.last?
|
|
50
|
-
|
|
51
|
-
array << component.context.render(spacer_template)
|
|
52
|
-
end
|
|
53
|
-
end
|
|
54
|
-
# rubocop:enable Metrics/AbcSize
|
|
55
|
-
|
|
56
|
-
end
|
|
57
|
-
end
|
|
58
|
-
end
|