Skip to content

Commit

Permalink
Abstract svg read & placeholder logic
Browse files Browse the repository at this point in the history
Refactor placeholder method

Test

Re: jamesmartin#73
  • Loading branch information
Curt Howard committed Oct 10, 2017
1 parent e0aa522 commit 21e007f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 9 deletions.
37 changes: 28 additions & 9 deletions lib/inline_svg/action_view/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ module ActionView
module Helpers
def inline_svg(filename, transform_params={})
begin
svg_file = if InlineSvg::IOResource === filename
InlineSvg::IOResource.read filename
else
configured_asset_file.named filename
end
svg_file = read_svg(filename)
rescue InlineSvg::AssetFile::FileNotFound
if InlineSvg.configuration.svg_not_found_css_class.nil?
return "<svg><!-- SVG file not found: '#{filename}' #{extension_hint(filename)}--></svg>".html_safe
else
return "<svg class='#{InlineSvg.configuration.svg_not_found_css_class}'><!-- SVG file not found: '#{filename}' #{extension_hint(filename)}--></svg>".html_safe
return placeholder(filename) unless transform_params[:fallback].present?

if transform_params[:fallback].present?
begin
svg_file = read_svg(transform_params[:fallback])
rescue InlineSvg::AssetFile::FileNotFound
placeholder(filename)
end
end
end

Expand All @@ -24,6 +24,25 @@ def inline_svg(filename, transform_params={})

private

def read_svg(filename)
if InlineSvg::IOResource === filename
InlineSvg::IOResource.read filename
else
configured_asset_file.named filename
end
end

def placeholder(filename)
css_class = InlineSvg.configuration.svg_not_found_css_class
not_found_message = "'#{filename}' #{extension_hint(filename)}"

if css_class.nil?
return "<svg><!-- SVG file not found: #{not_found_message}--></svg>".html_safe
else
return "<svg class='#{css_class}'><!-- SVG file not found: #{not_found_message}--></svg>".html_safe
end
end

def configured_asset_file
InlineSvg.configuration.asset_file
end
Expand Down
14 changes: 14 additions & 0 deletions spec/helpers/inline_svg_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,20 @@ def transform(doc)
expect(output).to eq "<svg class='missing-svg'><!-- SVG file not found: 'some-other-missing-file.svg' --></svg>"
expect(output).to be_html_safe
end

context "and a fallback that does exist" do
it "displays the fallback" do
allow(InlineSvg::AssetFile).to receive(:named).
with('missing.svg').
and_raise(InlineSvg::AssetFile::FileNotFound.new)

fallback_file = <<-SVG
<svg xmlns="http://www.w3.org/2000/svg" xml:lang="en"><!-- This is a comment --></svg>
SVG
allow(InlineSvg::AssetFile).to receive(:named).with('fallback.svg').and_return(fallback_file)
expect(helper.inline_svg('missing.svg', fallback: 'fallback.svg')).to eq fallback_file
end
end
end

context "when passed an existing SVG file" do
Expand Down

0 comments on commit 21e007f

Please sign in to comment.