Skip to content

Commit 47bef29

Browse files
authored
Introduce suspenders:inline_svg generator (#1140)
Ports the existing generator to Suspenders 3.0.
1 parent 5ff19c5 commit 47bef29

File tree

5 files changed

+124
-0
lines changed

5 files changed

+124
-0
lines changed

NEWS.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Unreleased
33
* Remove `suspenders` system executable
44
* Introduce `suspenders:accessibility` generator
55
* Introduce `Suspenders::Generators::APIAppUnsupported` module and concern
6+
* Introduce `suspenders:inline_svg` generator
67

78
20230113.0 (January, 13, 2023)
89

README.md

+9
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@ Installs [capybara_accessibility_audit] and [capybara_accessible_selectors]
3030
[capybara_accessibility_audit]: https://github.com/thoughtbot/capybara_accessibility_audit
3131
[capybara_accessible_selectors]: https://github.com/citizensadvice/capybara_accessible_selectors
3232

33+
### Inline SVG
34+
35+
Render SVG images inline using the [inline_svg] gem, as a potential performance
36+
improvement for the viewer.
37+
38+
`bin/rails g suspenders:inline_svg`
39+
40+
[inline_svg]: https://github.com/jamesmartin/inline_svg
41+
3342
## Contributing
3443

3544
See the [CONTRIBUTING] document.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module Suspenders
2+
module Generators
3+
class InlineSvgGenerator < Rails::Generators::Base
4+
include Suspenders::Generators::APIAppUnsupported
5+
source_root File.expand_path("../../templates/inline_svg", __FILE__)
6+
desc "Render SVG images inline, as a potential performance improvement for the viewer."
7+
8+
def add_inline_svg_gem
9+
gem "inline_svg"
10+
Bundler.with_unbundled_env { run "bundle install" }
11+
end
12+
13+
def configure_inline_svg
14+
copy_file "inline_svg.rb", "config/initializers/inline_svg.rb"
15+
end
16+
end
17+
end
18+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
InlineSvg.configure do |config|
2+
config.raise_on_file_not_found = true
3+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
require "test_helper"
2+
require "generators/suspenders/inline_svg_generator"
3+
4+
module Suspenders
5+
module Generators
6+
class InlinveSvgGeneratorTest < Rails::Generators::TestCase
7+
include Suspenders::TestHelpers
8+
9+
tests Suspenders::Generators::InlineSvgGenerator
10+
destination Rails.root
11+
setup :prepare_destination
12+
teardown :restore_destination
13+
14+
test "raises if API only application" do
15+
within_api_only_app do
16+
assert_raises Suspenders::Generators::APIAppUnsupported::Error do
17+
run_generator
18+
end
19+
20+
assert_file app_root("Gemfile") do |file|
21+
assert_no_match "inline_svg", file
22+
end
23+
end
24+
end
25+
26+
test "does not raise if API configuration is commented out" do
27+
within_api_only_app do
28+
path = app_root("config/application.rb")
29+
content = File.binread(path).gsub!("config.api_only = true", "# config.api_only = true")
30+
File.binwrite(path, content)
31+
32+
run_generator
33+
34+
assert_file app_root("Gemfile") do |file|
35+
assert_match "inline_svg", file
36+
end
37+
end
38+
end
39+
40+
test "adds gems to Gemfile" do
41+
expected_output = <<~RUBY
42+
gem "inline_svg"
43+
RUBY
44+
45+
run_generator
46+
47+
assert_file app_root("Gemfile") do |file|
48+
assert_match(expected_output, file)
49+
end
50+
end
51+
52+
test "installs gems with Bundler" do
53+
Bundler.stubs(:with_unbundled_env).yields
54+
generator.expects(:run).with("bundle install").once
55+
56+
capture(:stdout) do
57+
generator.add_inline_svg_gem
58+
end
59+
end
60+
61+
test "generator has a description" do
62+
description = "Render SVG images inline, as a potential performance improvement for the viewer."
63+
64+
assert_equal description, Suspenders::Generators::InlineSvgGenerator.desc
65+
end
66+
67+
test "configures raising an error when an SVG file is not found" do
68+
expected_configuration = <<~RUBY
69+
InlineSvg.configure do |config|
70+
config.raise_on_file_not_found = true
71+
end
72+
RUBY
73+
74+
run_generator
75+
76+
assert_file app_root("config/initializers/inline_svg.rb") do |file|
77+
assert_match(expected_configuration, file)
78+
end
79+
end
80+
81+
private
82+
83+
def prepare_destination
84+
touch "Gemfile"
85+
end
86+
87+
def restore_destination
88+
remove_file_if_exists "Gemfile"
89+
remove_file_if_exists "config/initializers/inline_svg.rb"
90+
end
91+
end
92+
end
93+
end

0 commit comments

Comments
 (0)