-
-
Notifications
You must be signed in to change notification settings - Fork 529
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce
suspenders:inline_svg
generator (#1140)
Ports the existing generator to Suspenders 3.0.
- Loading branch information
1 parent
5ff19c5
commit 47bef29
Showing
5 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |