Skip to content

Commit

Permalink
Introduce suspenders:inline_svg generator (#1140)
Browse files Browse the repository at this point in the history
Ports the existing generator to Suspenders 3.0.
  • Loading branch information
crackofdusk authored and stevepolitodesign committed May 10, 2024
1 parent adb85fb commit d96e2b0
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 0 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Unreleased
* Remove `suspenders` system executable
* Introduce `suspenders:accessibility` generator
* Introduce `Suspenders::Generators::APIAppUnsupported` module and concern
* Introduce `suspenders:inline_svg` generator

20230113.0 (January, 13, 2023)

Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ Installs [capybara_accessibility_audit] and [capybara_accessible_selectors]
[capybara_accessibility_audit]: https://github.com/thoughtbot/capybara_accessibility_audit
[capybara_accessible_selectors]: https://github.com/citizensadvice/capybara_accessible_selectors

### Inline SVG

Render SVG images inline using the [inline_svg] gem, as a potential performance
improvement for the viewer.

`bin/rails g suspenders:inline_svg`

[inline_svg]: https://github.com/jamesmartin/inline_svg

## Contributing

See the [CONTRIBUTING] document.
Expand Down
18 changes: 18 additions & 0 deletions lib/generators/suspenders/inline_svg_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module Suspenders
module Generators
class InlineSvgGenerator < Rails::Generators::Base
include Suspenders::Generators::APIAppUnsupported
source_root File.expand_path("../../templates/inline_svg", __FILE__)
desc "Render SVG images inline, as a potential performance improvement for the viewer."

def add_inline_svg_gem
gem "inline_svg"
Bundler.with_unbundled_env { run "bundle install" }
end

def configure_inline_svg
copy_file "inline_svg.rb", "config/initializers/inline_svg.rb"
end
end
end
end
3 changes: 3 additions & 0 deletions lib/generators/templates/inline_svg/inline_svg.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
InlineSvg.configure do |config|
config.raise_on_file_not_found = true
end
93 changes: 93 additions & 0 deletions test/generators/suspenders/inline_svg_generator_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
require "test_helper"
require "generators/suspenders/inline_svg_generator"

module Suspenders
module Generators
class InlinveSvgGeneratorTest < Rails::Generators::TestCase
include Suspenders::TestHelpers

tests Suspenders::Generators::InlineSvgGenerator
destination Rails.root
setup :prepare_destination
teardown :restore_destination

test "raises if API only application" do
within_api_only_app do
assert_raises Suspenders::Generators::APIAppUnsupported::Error do
run_generator
end

assert_file app_root("Gemfile") do |file|
assert_no_match "inline_svg", file
end
end
end

test "does not raise if API configuration is commented out" do
within_api_only_app do
path = app_root("config/application.rb")
content = File.binread(path).gsub!("config.api_only = true", "# config.api_only = true")
File.binwrite(path, content)

run_generator

assert_file app_root("Gemfile") do |file|
assert_match "inline_svg", file
end
end
end

test "adds gems to Gemfile" do
expected_output = <<~RUBY
gem "inline_svg"
RUBY

run_generator

assert_file app_root("Gemfile") do |file|
assert_match(expected_output, file)
end
end

test "installs gems with Bundler" do
Bundler.stubs(:with_unbundled_env).yields
generator.expects(:run).with("bundle install").once

capture(:stdout) do
generator.add_inline_svg_gem
end
end

test "generator has a description" do
description = "Render SVG images inline, as a potential performance improvement for the viewer."

assert_equal description, Suspenders::Generators::InlineSvgGenerator.desc
end

test "configures raising an error when an SVG file is not found" do
expected_configuration = <<~RUBY
InlineSvg.configure do |config|
config.raise_on_file_not_found = true
end
RUBY

run_generator

assert_file app_root("config/initializers/inline_svg.rb") do |file|
assert_match(expected_configuration, file)
end
end

private

def prepare_destination
touch "Gemfile"
end

def restore_destination
remove_file_if_exists "Gemfile"
remove_file_if_exists "config/initializers/inline_svg.rb"
end
end
end
end

0 comments on commit d96e2b0

Please sign in to comment.