amber_component 1.1.0 → 1.1.1
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/Gemfile.lock +1 -1
- data/README.md +32 -3
- data/lib/amber_component/configuration.rb +47 -5
- data/lib/amber_component/version.rb +1 -1
- data/lib/amber_component/views.rb +1 -1
- data/lib/amber_component.rb +1 -0
- data/lib/generators/amber_component/install_generator.rb +103 -17
- data/lib/generators/amber_component_generator.rb +22 -20
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 46dc65dbef14224da8e0ce373eb07528188c399444eb2b90968c1cf3dc049864
|
4
|
+
data.tar.gz: d547837963b2e6148a0724d961f3ba9fa1f540365a711d40d483a24ccdcf8909
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9f060bd9c1a97475c0678ddc5c0df166bf6f05001cde7f43a41bdc130c068b2ea24c15518d384016aeef2e8ef592b2928c305a41aaaa24dcebf038783a96b57b
|
7
|
+
data.tar.gz: 8f2f8029094be9ac3eccbb55b1c46a3c1ed43bb5e7d4c55263bf9bf0a2501862589f7644b8c357ceaa1d8762ea2370125f29c85bce7299925588570ba5118860
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
[](https://codeclimate.com/github/amber-ruby/amber_component/maintainability)
|
4
4
|
[](https://github.com/amber-ruby/amber_component/actions/workflows/ci_ruby.yml)
|
5
5
|
[](https://github.com/amber-ruby/amber_component/actions/workflows/ci_ruby.yml)
|
6
|
-
[]((https://rubygems.org/gems/amber_component))
|
6
|
+
<!-- []((https://rubygems.org/gems/amber_component)) -->
|
7
7
|
|
8
8
|
<img src="banner.png" width="500px" style="margin-bottom: 2rem;"/>
|
9
9
|
|
@@ -54,6 +54,13 @@ Every component consists of:
|
|
54
54
|
- a style file (css, scss, sass etc.)
|
55
55
|
- [optional] a JavaScript file with a Stimulus controller (if you installed the gem with `--stimulus`)
|
56
56
|
|
57
|
+
`amber_component` automatically detects what kind of view and stylesheet formats your app is configured to use.
|
58
|
+
|
59
|
+
So if you've got `haml-rails`, components will be generated with `haml`. When your app uses `slim-rails`, components will be generated with `slim`. When your `Gemfile` contains `sassc-rails`, components will use `scss` etc.
|
60
|
+
|
61
|
+
All of these formats can be overridden in
|
62
|
+
an initializer or by adding arguments to the component generator.
|
63
|
+
|
57
64
|
```
|
58
65
|
app/components/
|
59
66
|
├─ [name]_component.rb
|
@@ -197,13 +204,20 @@ This will generate a new component in `app/components/[name]_component.rb` along
|
|
197
204
|
app/components/
|
198
205
|
├─ [name]_component.rb
|
199
206
|
└─ [name]_component/
|
200
|
-
├─ style.css
|
201
|
-
├─ view.html.erb
|
207
|
+
├─ style.css # may be `.scss` or `.sass`
|
208
|
+
├─ view.html.erb # may be `.haml` or `.slim`
|
202
209
|
└─ controller.js # if stimulus is configured
|
203
210
|
test/components/
|
204
211
|
└─ [name]_component_test.rb
|
205
212
|
```
|
206
213
|
|
214
|
+
View and stylesheet formats can be overridden by providing options.
|
215
|
+
|
216
|
+
```
|
217
|
+
-v, [--view=VIEW] # Indicate what type of view should be generated eg. [:erb, :haml, :slim]
|
218
|
+
--styles, -c, [--css=CSS] # Indicate what type of styles should be generated eg. [:css, :scss, :sass]
|
219
|
+
```
|
220
|
+
|
207
221
|
### Component properties
|
208
222
|
|
209
223
|
There is a neat prop DSL.
|
@@ -411,6 +425,21 @@ This makes component views very flexible and convenient.
|
|
411
425
|
<% end %>
|
412
426
|
```
|
413
427
|
|
428
|
+
### Configuration
|
429
|
+
|
430
|
+
This gem can be configured in an initializer.
|
431
|
+
If you used the installer generator it should already be present.
|
432
|
+
|
433
|
+
```ruby
|
434
|
+
# config/initializers/amber_component.rb
|
435
|
+
|
436
|
+
::AmberComponent.configure do |c|
|
437
|
+
c.stimulus = nil # [nil, :importmap, :webpacker, :jsbundling, :webpack, :esbuild, :rollup]
|
438
|
+
c.stylesheet_format = :css # [:css, :scss, :sass]
|
439
|
+
c.view_format = :erb # [:erb, :haml, :slim]
|
440
|
+
end
|
441
|
+
```
|
442
|
+
|
414
443
|
### Testing Components
|
415
444
|
|
416
445
|
### Rails
|
@@ -4,18 +4,36 @@ module ::AmberComponent
|
|
4
4
|
# Object which stores configuration options
|
5
5
|
# for this gem.
|
6
6
|
class Configuration
|
7
|
-
# @return [
|
8
|
-
STIMULUS_INTEGRATIONS =
|
7
|
+
# @return [Set<Symbol>]
|
8
|
+
STIMULUS_INTEGRATIONS = ::Set[nil, :importmap, :webpacker, :jsbundling, :webpack, :esbuild, :rollup]
|
9
|
+
# @return [Set<Symbol>]
|
10
|
+
ALLOWED_STYLES = ::Set.new(%i[css scss sass])
|
11
|
+
# @return [Set<Symbol>]
|
12
|
+
ALLOWED_VIEWS = ::Set.new(%i[erb haml slim])
|
9
13
|
|
10
14
|
# How Stimulus.js is bundled in this app.
|
11
|
-
# Possible values: `[nil, :importmap, :jsbundling, :webpack, :esbuild, :rollup]`
|
15
|
+
# Possible values: `[nil, :importmap, :webpacker, :jsbundling, :webpack, :esbuild, :rollup]`
|
12
16
|
# `nil` indicates that stimulus should not be used (default behaviour).
|
13
17
|
#
|
14
18
|
# @return [Symbol, nil]
|
15
19
|
attr_reader :stimulus
|
16
20
|
|
21
|
+
# The default format that the generators will use
|
22
|
+
# for the view/template file of a component.
|
23
|
+
# Possible values: `[nil, :erb, :haml, :slim]`
|
24
|
+
#
|
25
|
+
# @return [Symbol, nil]
|
26
|
+
attr_reader :view_format
|
27
|
+
|
28
|
+
# The default format that the generators will use
|
29
|
+
# for the stylesheets of a component.
|
30
|
+
# Possible values: `[nil, :css, :scss, :sass]`
|
31
|
+
#
|
32
|
+
# @return [Symbol, nil]
|
33
|
+
attr_reader :stylesheet_format
|
34
|
+
|
17
35
|
# How Stimulus.js is bundled in this app.
|
18
|
-
# Possible values: `[nil, :importmap, :jsbundling, :webpack, :esbuild, :rollup]`
|
36
|
+
# Possible values: `[nil, :importmap, :webpacker, :jsbundling, :webpack, :esbuild, :rollup]`
|
19
37
|
# `nil` indicates that stimulus should not be used (default behaviour).
|
20
38
|
#
|
21
39
|
# @param val [Symbol, String, nil]
|
@@ -23,13 +41,37 @@ module ::AmberComponent
|
|
23
41
|
val = val&.to_sym
|
24
42
|
unless val.nil? || STIMULUS_INTEGRATIONS.include?(val)
|
25
43
|
raise(::ArgumentError,
|
26
|
-
"Invalid value for `
|
44
|
+
"Invalid value for `#{__method__}` bundling. " \
|
27
45
|
"Received #{val.inspect}, expected one of #{STIMULUS_INTEGRATIONS.inspect}")
|
28
46
|
end
|
29
47
|
|
30
48
|
@stimulus = val
|
31
49
|
end
|
32
50
|
|
51
|
+
# @param val [Symbol, String, nil]
|
52
|
+
def stylesheet_format=(val)
|
53
|
+
val = val&.to_sym
|
54
|
+
unless val.nil? || ALLOWED_STYLES.include?(val)
|
55
|
+
raise(::ArgumentError,
|
56
|
+
"Invalid value for `#{__method__}`. " \
|
57
|
+
"Received #{val.inspect}, expected one of #{ALLOWED_STYLES.inspect}")
|
58
|
+
end
|
59
|
+
|
60
|
+
@stylesheet_format = val
|
61
|
+
end
|
62
|
+
|
63
|
+
# @param val [Symbol, String, nil]
|
64
|
+
def view_format=(val)
|
65
|
+
val = val&.to_sym
|
66
|
+
unless val.nil? || ALLOWED_VIEWS.include?(val)
|
67
|
+
raise(::ArgumentError,
|
68
|
+
"Invalid value for `#{__method__}`. " \
|
69
|
+
"Received #{val.inspect}, expected one of #{ALLOWED_VIEWS.inspect}")
|
70
|
+
end
|
71
|
+
|
72
|
+
@view_format = val
|
73
|
+
end
|
74
|
+
|
33
75
|
# @return [Boolean]
|
34
76
|
def stimulus?
|
35
77
|
!@stimulus.nil?
|
@@ -8,7 +8,7 @@ module ::AmberComponent
|
|
8
8
|
# @return [Set<Symbol>]
|
9
9
|
VIEW_TYPES_WITH_RUBY = ::Set[:erb, :haml, :slim].freeze
|
10
10
|
# @return [Set<Symbol>]
|
11
|
-
ALLOWED_VIEW_TYPES = ::Set[:erb, :haml, :slim, :html
|
11
|
+
ALLOWED_VIEW_TYPES = ::Set[:erb, :haml, :slim, :html].freeze
|
12
12
|
# @return [Regexp]
|
13
13
|
VIEW_FILE_REGEXP = /^view\./.freeze
|
14
14
|
|
data/lib/amber_component.rb
CHANGED
@@ -10,14 +10,22 @@ module ::AmberComponent
|
|
10
10
|
desc 'Install the AmberComponent gem'
|
11
11
|
source_root ::File.expand_path('templates', __dir__)
|
12
12
|
|
13
|
-
# @return [Array<Symbol>]
|
14
|
-
STIMULUS_INTEGRATIONS = %i[stimulus importmap jsbundling webpack esbuild rollup].freeze
|
15
|
-
|
16
13
|
class_option :stimulus,
|
17
14
|
desc: "Configure the app to use Stimulus.js wih components to make them interactive " \
|
18
|
-
"[options: importmap (default), jsbundling, webpack, esbuild, rollup]"
|
15
|
+
"[options: importmap (default), webpacker (legacy), jsbundling, webpack, esbuild, rollup]"
|
16
|
+
|
17
|
+
class_option :styles,
|
18
|
+
desc: "Configure the app to generate components with a particular stylesheet format " \
|
19
|
+
"[options: css (default), scss, sass]"
|
20
|
+
|
21
|
+
class_option :views,
|
22
|
+
desc: "Configure the app to generate components with a particular view format " \
|
23
|
+
"[options: erb (default), haml, slim]"
|
19
24
|
|
20
25
|
def setup
|
26
|
+
detect_stimulus
|
27
|
+
detect_styles
|
28
|
+
detect_views
|
21
29
|
copy_file 'application_component.rb', 'app/components/application_component.rb'
|
22
30
|
copy_file 'application_component_test_case.rb', 'test/application_component_test_case.rb'
|
23
31
|
append_file 'test/test_helper.rb', "require_relative 'application_component_test_case'"
|
@@ -30,40 +38,107 @@ module ::AmberComponent
|
|
30
38
|
require_components_css_in 'app/assets/stylesheets/application.scss.sass'
|
31
39
|
require_components_css_in 'app/assets/stylesheets/application.sass.scss'
|
32
40
|
configure_stimulus
|
41
|
+
create_initializer
|
33
42
|
end
|
34
43
|
|
35
44
|
private
|
36
45
|
|
37
|
-
def
|
38
|
-
|
39
|
-
|
46
|
+
def detect_styles
|
47
|
+
styles_option = options[:styles]&.to_sym
|
48
|
+
if !styles_option.nil? && !Configuration::ALLOWED_STYLES.include?(styles_option)
|
49
|
+
raise ::ArgumentError, "no such `stylesheet_format` as #{styles_option.inspect}"
|
50
|
+
end
|
40
51
|
|
41
|
-
|
52
|
+
@styles =
|
53
|
+
if styles_option
|
54
|
+
styles_option
|
55
|
+
elsif defined?(::SassC)
|
56
|
+
:scss
|
57
|
+
else
|
58
|
+
:css
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def detect_views
|
63
|
+
views_option = options[:views]&.to_sym
|
64
|
+
if !views_option.nil? && !Configuration::ALLOWED_VIEWS.include?(views_option)
|
65
|
+
raise ::ArgumentError, "no such `view_format` as #{views_option.inspect}"
|
66
|
+
end
|
67
|
+
|
68
|
+
@views =
|
69
|
+
if views_option
|
70
|
+
views_option
|
71
|
+
elsif defined?(::Haml)
|
72
|
+
:haml
|
73
|
+
elsif defined?(::Slim)
|
74
|
+
:slim
|
75
|
+
else
|
76
|
+
:erb
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def detect_stimulus
|
81
|
+
stimulus_option = options[:stimulus]&.to_sym
|
82
|
+
return unless stimulus_option
|
83
|
+
|
84
|
+
case stimulus_option
|
42
85
|
when :stimulus
|
43
86
|
if defined?(::Jsbundling)
|
44
|
-
|
45
|
-
|
87
|
+
stimulus_jsbundling!
|
88
|
+
elsif defined?(::Webpacker)
|
89
|
+
stimulus_webpacker!
|
46
90
|
else
|
47
|
-
|
48
|
-
configure_stimulus_importmap
|
91
|
+
stimulus_importmap!
|
49
92
|
end
|
50
93
|
when :importmap
|
51
|
-
|
52
|
-
configure_stimulus_importmap
|
94
|
+
stimulus_importmap!
|
53
95
|
when :jsbundling, :webpack, :esbuild, :rollup
|
54
|
-
|
55
|
-
|
96
|
+
stimulus_jsbundling!
|
97
|
+
when :webpacker
|
98
|
+
stimulus_webpacker!
|
99
|
+
else
|
100
|
+
raise ::ArgumentError,
|
101
|
+
"no such stimulus integration as `#{options[:stimulus].inspect}`"
|
56
102
|
end
|
103
|
+
end
|
104
|
+
|
105
|
+
def assert_styles
|
106
|
+
return if options[:styles].nil?
|
107
|
+
return if options[:styles].nil?
|
108
|
+
end
|
57
109
|
|
110
|
+
def configure_stimulus
|
111
|
+
case @stimulus
|
112
|
+
when :importmap then configure_stimulus_importmap
|
113
|
+
when :jsbundling then configure_stimulus_jsbundling
|
114
|
+
when :webpacker then configure_stimulus_webpacker
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
def create_initializer
|
58
119
|
create_file 'config/initializers/amber_component.rb', <<~RUBY
|
59
120
|
# frozen_string_literal: true
|
60
121
|
|
61
122
|
::AmberComponent.configure do |c|
|
62
|
-
c.stimulus =
|
123
|
+
c.stimulus = #{@stimulus.inspect} # #{Configuration::STIMULUS_INTEGRATIONS.to_a}
|
124
|
+
c.stylesheet_format = #{@styles.inspect} # #{Configuration::ALLOWED_STYLES.to_a}
|
125
|
+
c.view_format = #{@views.inspect} # #{Configuration::ALLOWED_VIEWS.to_a}
|
63
126
|
end
|
64
127
|
RUBY
|
65
128
|
end
|
66
129
|
|
130
|
+
def stimulus_jsbundling!
|
131
|
+
@stimulus = :jsbundling
|
132
|
+
end
|
133
|
+
|
134
|
+
def stimulus_importmap!
|
135
|
+
@stimulus = :importmap
|
136
|
+
end
|
137
|
+
|
138
|
+
def stimulus_webpacker!
|
139
|
+
@stimulus = :webpacker
|
140
|
+
end
|
141
|
+
|
67
142
|
def configure_stimulus_importmap
|
68
143
|
install_importmap
|
69
144
|
install_stimulus
|
@@ -88,6 +163,17 @@ module ::AmberComponent
|
|
88
163
|
JS
|
89
164
|
end
|
90
165
|
|
166
|
+
def configure_stimulus_webpacker
|
167
|
+
install_stimulus
|
168
|
+
append_file 'app/javascript/packs/application.js', %(import "controllers"\n)
|
169
|
+
append_file 'app/javascript/controllers/index.js', %(import "./components"\n)
|
170
|
+
create_file 'app/javascript/controllers/components.js', <<~JS
|
171
|
+
// This file has been created by `amber_component` and will
|
172
|
+
// register all stimulus controllers from your components
|
173
|
+
import { application } from "./application"
|
174
|
+
JS
|
175
|
+
end
|
176
|
+
|
91
177
|
# @return [void]
|
92
178
|
def install_importmap
|
93
179
|
return if ::File.exist?('config/importmap.rb') && defined?(::Importmap)
|
@@ -7,33 +7,26 @@ class AmberComponentGenerator < ::Rails::Generators::NamedBase
|
|
7
7
|
desc 'Generate a new component'
|
8
8
|
source_root ::File.expand_path('templates', __dir__)
|
9
9
|
|
10
|
-
# @return [Array<Symbol>]
|
11
|
-
VIEW_FORMATS = %i[html erb haml slim].freeze
|
12
|
-
# @return [Array<Symbol>]
|
13
|
-
STYLE_FORMATS = %i[css scss sass].freeze
|
14
|
-
|
15
10
|
class_option :view,
|
16
11
|
aliases: ['-v'],
|
17
|
-
desc: "Indicate what type of view should be generated
|
12
|
+
desc: "Indicate what type of view should be generated " \
|
13
|
+
"eg. #{::AmberComponent::Configuration::ALLOWED_VIEWS}"
|
18
14
|
|
19
15
|
class_option :css,
|
20
|
-
aliases: ['--
|
21
|
-
desc: "Indicate what type of styles should be generated
|
16
|
+
aliases: ['--styles', '-c'],
|
17
|
+
desc: "Indicate what type of styles should be generated " \
|
18
|
+
"eg. #{::AmberComponent::Configuration::ALLOWED_STYLES}"
|
22
19
|
|
23
20
|
def generate_component
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
@style_format = options[:css]&.to_sym
|
21
|
+
set_view_format
|
22
|
+
set_stylesheet_format
|
28
23
|
|
29
|
-
unless
|
30
|
-
|
31
|
-
return
|
24
|
+
unless ::AmberComponent::Configuration::ALLOWED_VIEWS.include? @view_format
|
25
|
+
raise ::ArgumentError, "No such view format as `#{@view_format}`"
|
32
26
|
end
|
33
27
|
|
34
|
-
|
35
|
-
|
36
|
-
return
|
28
|
+
unless ::AmberComponent::Configuration::ALLOWED_STYLES.include?(@stylesheet_format)
|
29
|
+
raise ::ArgumentError, "No such css/style format as `#{@stylesheet_format}`"
|
37
30
|
end
|
38
31
|
|
39
32
|
template 'component.rb.erb', "app/components/#{file_path}.rb"
|
@@ -53,6 +46,14 @@ class AmberComponentGenerator < ::Rails::Generators::NamedBase
|
|
53
46
|
|
54
47
|
private
|
55
48
|
|
49
|
+
def set_view_format
|
50
|
+
@view_format = options[:view]&.to_sym || ::AmberComponent.configuration.view_format || :erb
|
51
|
+
end
|
52
|
+
|
53
|
+
def set_stylesheet_format
|
54
|
+
@stylesheet_format = options[:style]&.to_sym || ::AmberComponent.configuration.stylesheet_format || :css
|
55
|
+
end
|
56
|
+
|
56
57
|
# @return [Boolean]
|
57
58
|
def stimulus?
|
58
59
|
::AmberComponent.configuration.stimulus?
|
@@ -90,9 +91,10 @@ class AmberComponentGenerator < ::Rails::Generators::NamedBase
|
|
90
91
|
|
91
92
|
# @return [void]
|
92
93
|
def create_stylesheet
|
93
|
-
|
94
|
+
case @stylesheet_format
|
95
|
+
when :scss
|
94
96
|
template 'style.scss.erb', "app/components/#{file_path}/style.scss"
|
95
|
-
|
97
|
+
when :sass
|
96
98
|
template 'style.sass.erb', "app/components/#{file_path}/style.sass"
|
97
99
|
else
|
98
100
|
template 'style.css.erb', "app/components/#{file_path}/style.css"
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: amber_component
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ruby-Amber
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: exe
|
12
12
|
cert_chain: []
|
13
|
-
date: 2022-11-
|
13
|
+
date: 2022-11-14 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: actionview
|