Skip to content
This repository has been archived by the owner on Feb 24, 2024. It is now read-only.

Commit

Permalink
fixed the asset helpers to clean up code and added tests for them
Browse files Browse the repository at this point in the history
  • Loading branch information
markbates committed Dec 20, 2017
1 parent a774df5 commit ac16d3e
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 35 deletions.
3 changes: 1 addition & 2 deletions plugins/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,5 +113,4 @@ func ignorePath(p string) bool {
return false
}

const warningMessage = `Could not find BUFFALO_PLUGIN_PATH environment variable, default to PATH instead.
Consider setting the BUFFALO_PLUGIN_PATH variable to speed up loading of plugins and/or to set a custom path for locating them.`
const warningMessage = `Could not find BUFFALO_PLUGIN_PATH environment variable, default to PATH instead. Consider setting the BUFFALO_PLUGIN_PATH variable to speed up loading of plugins and/or to set a custom path for locating them.`
56 changes: 23 additions & 33 deletions render/template_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,43 +32,41 @@ func assetPathFor(file string) string {
return filepath.ToSlash(filepath.Join("/assets", filePath))
}

type helperTag struct {
name string
fn func(string, tags.Options) template.HTML
}

func (s templateRenderer) addAssetsHelpers(helpers Helpers) Helpers {
helpers["assetPath"] = func(file string) (string, error) {
return s.assetPath(file)
}

helpers["javascriptTag"] = func(file string, options tags.Options) (template.HTML, error) {
h, err := s.assetPath(file)
if err != nil {
return "", errors.WithStack(err)
}
return jsTag(h, options), nil
ah := []helperTag{
{"javascriptTag", jsTag},
{"stylesheetTag", cssTag},
{"imgTag", imgTag},
}

helpers["stylesheetTag"] = func(file string, options tags.Options) (template.HTML, error) {
h, err := s.assetPath(file)
if err != nil {
return "", errors.WithStack(err)
}
return cssTag(h, options), nil
for _, h := range ah {
func(h helperTag) {
helpers[h.name] = func(file string, options tags.Options) (template.HTML, error) {
if options == nil {
options = tags.Options{}
}
f, err := s.assetPath(file)
if err != nil {
return "", errors.WithStack(err)
}
return h.fn(f, options), nil
}
}(h)
}

helpers["imgTag"] = func(file string, options tags.Options) (template.HTML, error) {
h, err := s.assetPath(file)
if err != nil {
return "", errors.WithStack(err)
}
return imgTag(h, options), nil
}
return helpers
}

func jsTag(src string, options tags.Options) template.HTML {

if options == nil {
options = tags.Options{}
}

if options["type"] == nil {
options["type"] = "text/javascript"
}
Expand All @@ -80,10 +78,6 @@ func jsTag(src string, options tags.Options) template.HTML {
}

func cssTag(href string, options tags.Options) template.HTML {
if options == nil {
options = tags.Options{}
}

if options["rel"] == nil {
options["rel"] = "stylesheet"
}
Expand All @@ -99,12 +93,8 @@ func cssTag(href string, options tags.Options) template.HTML {
}

func imgTag(src string, options tags.Options) template.HTML {
if options == nil {
options = tags.Options{}
}

options["src"] = src
imgTag := tags.New("img", options)

return imgTag.HTML()
}
}
87 changes: 87 additions & 0 deletions render/template_helpers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package render

import (
"html/template"
"testing"

"github.com/gobuffalo/packr"
"github.com/gobuffalo/tags"
"github.com/stretchr/testify/require"
)

func Test_javascriptTag(t *testing.T) {
r := require.New(t)
re := New(Options{
AssetsBox: packr.NewBox(""),
})
tr := re.Template("").(templateRenderer)
h := tr.addAssetsHelpers(Helpers{})
f := h["javascriptTag"].(func(string, tags.Options) (template.HTML, error))
s, err := f("application.js", nil)
r.NoError(err)
r.Equal(template.HTML(`<script src="/assets/application.js" type="text/javascript"></script>`), s)
}

func Test_javascriptTag_Options(t *testing.T) {
r := require.New(t)
re := New(Options{
AssetsBox: packr.NewBox(""),
})
tr := re.Template("").(templateRenderer)
h := tr.addAssetsHelpers(Helpers{})
f := h["javascriptTag"].(func(string, tags.Options) (template.HTML, error))
s, err := f("application.js", tags.Options{"class": "foo"})
r.NoError(err)
r.Equal(template.HTML(`<script class="foo" src="/assets/application.js" type="text/javascript"></script>`), s)

}
func Test_stylesheetTag(t *testing.T) {
r := require.New(t)
re := New(Options{
AssetsBox: packr.NewBox(""),
})
tr := re.Template("").(templateRenderer)
h := tr.addAssetsHelpers(Helpers{})
f := h["stylesheetTag"].(func(string, tags.Options) (template.HTML, error))
s, err := f("application.css", nil)
r.NoError(err)
r.Equal(template.HTML(`<link href="/assets/application.css" media="screen" rel="stylesheet" />`), s)
}

func Test_stylesheetTag_Options(t *testing.T) {
r := require.New(t)
re := New(Options{
AssetsBox: packr.NewBox(""),
})
tr := re.Template("").(templateRenderer)
h := tr.addAssetsHelpers(Helpers{})
f := h["stylesheetTag"].(func(string, tags.Options) (template.HTML, error))
s, err := f("application.css", tags.Options{"class": "foo"})
r.NoError(err)
r.Equal(template.HTML(`<link class="foo" href="/assets/application.css" media="screen" rel="stylesheet" />`), s)
}
func Test_imgTag(t *testing.T) {
r := require.New(t)
re := New(Options{
AssetsBox: packr.NewBox(""),
})
tr := re.Template("").(templateRenderer)
h := tr.addAssetsHelpers(Helpers{})
f := h["imgTag"].(func(string, tags.Options) (template.HTML, error))
s, err := f("foo.png", nil)
r.NoError(err)
r.Equal(template.HTML(`<img src="/assets/foo.png" />`), s)
}

func Test_imgTag_Options(t *testing.T) {
r := require.New(t)
re := New(Options{
AssetsBox: packr.NewBox(""),
})
tr := re.Template("").(templateRenderer)
h := tr.addAssetsHelpers(Helpers{})
f := h["imgTag"].(func(string, tags.Options) (template.HTML, error))
s, err := f("foo.png", tags.Options{"class": "foo"})
r.NoError(err)
r.Equal(template.HTML(`<img class="foo" src="/assets/foo.png" />`), s)
}

0 comments on commit ac16d3e

Please sign in to comment.