Skip to content

Commit

Permalink
Add a new raw helper for full path
Browse files Browse the repository at this point in the history
  • Loading branch information
mfinelli committed Jan 6, 2020
1 parent 5859325 commit 01bb87a
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -7,6 +7,7 @@ which adheres to [semantic versioning](https://semver.org).

* Add new configuration file and `hashless` option (file glob array) to upload
files to the CDN without their md5 hash appended.
* Add new `raw` helper to just return the full URL of an asset.
* Add upload support for PDF files.

## v2.0.1 2019-07-30
Expand Down
9 changes: 8 additions & 1 deletion README.md
Expand Up @@ -62,7 +62,14 @@ For images you can optionally pass a hash of options to apply additional
attributes to the image:

```slim
== image(img.jpg, alt: 'img')
== image('img.jpg', alt: 'img')
```

To get the full path of an asset:

```slim
a href=raw('file.pdf')
| Click me!
```

### Rake
Expand Down
11 changes: 11 additions & 0 deletions lib/bankrupt.rb
Expand Up @@ -79,6 +79,17 @@ def stylesheet(path)
asset_html(path, STYLESHEET_CDN, STYLESHEET_LOCAL)
end

# Get the full path to the asset for use in e.g. a tags.
#
# @param path [String] relative (from public) path to the asset
# @return [String] full path to the asset
def raw(path)
details = ASSETS.fetch(path)
create_fullpath(path, details[:md5], details[:hashless])
rescue KeyError
"/#{path}"
end

private

# Return a precomputed asset path if it exists
Expand Down
80 changes: 80 additions & 0 deletions spec/bankrupt_spec.rb
Expand Up @@ -735,4 +735,84 @@ def initialize
end
end
end

describe '#raw' do
context 'with no cdn url' do
before { stub_const('CDN', '') }

context 'with the asset in the manifest' do
let(:i) { klass.new }

before do
stub_const('ASSETS',
'test.png' => {
filename: 'test.png',
md5: 'abc',
sri: '123'
})
end

it 'returns the correct path' do
expect(i.raw('test.png')).to eq('/test.png')
end
end

context 'with the asset not in the manifest' do
let(:i) { klass.new }

before { stub_const('ASSETS', {}) }

it 'returns the correct path' do
expect(i.raw('ok.png')).to eq('/ok.png')
end
end
end

context 'with a cdn url' do
before { stub_const('CDN', 'https://example.com') }

context 'with the asset in the manifest' do
before do
stub_const('ASSETS',
'cool.png' => {
filename: 'cool.png',
md5: 'abc',
sri: '123'
},
'hope.png' => {
filename: 'hope.png',
md5: 'def',
sri: '456',
hashless: true
})
end

context 'with a normal asset' do
let(:i) { klass.new }

it 'returns the correct path' do
expect(i.raw('cool.png')).to eq('https://example.com/cool-abc.png')
end
end

context 'with a hashless asset' do
let(:i) { klass.new }

it 'returns the correct path' do
expect(i.raw('hope.png')).to eq('https://example.com/hope.png')
end
end
end

context 'with the asset not in the manifest' do
let(:i) { klass.new }

before { stub_const('ASSETS', {}) }

it 'returns the correct path' do
expect(i.raw('zoom.png')).to eq('/zoom.png')
end
end
end
end
end

0 comments on commit 01bb87a

Please sign in to comment.