picture_tag-rails 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +21 -0
- data/Gemfile +12 -0
- data/Guardfile +9 -0
- data/LICENSE.txt +22 -0
- data/README.md +76 -0
- data/Rakefile +1 -0
- data/lib/picture_tag/railtie.rb +8 -0
- data/lib/picture_tag/version.rb +3 -0
- data/lib/picture_tag/view_helpers.rb +54 -0
- data/lib/picture_tag.rb +2 -0
- data/picture_tag-rails.gemspec +19 -0
- data/spec/lib/view_helpers_spec.rb +105 -0
- data/spec/spec_helper.rb +13 -0
- metadata +79 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
gemspec;
|
3
|
+
gem 'actionpack', '>= 3.0.0'
|
4
|
+
gem 'activesupport', '>= 3.0.0'
|
5
|
+
gem 'activemodel', '>= 3.0.0'
|
6
|
+
gem 'railties', '>= 3.0.0'
|
7
|
+
gem 'tzinfo'
|
8
|
+
group :test, :development do
|
9
|
+
gem 'guard-rspec'
|
10
|
+
gem 'rb-fsevent', '~> 0.9.1'
|
11
|
+
gem 'rspec-rails'
|
12
|
+
end
|
data/Guardfile
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
# A sample Guardfile
|
2
|
+
# More info at https://github.com/guard/guard#readme
|
3
|
+
|
4
|
+
guard 'rspec', :version => 2 do
|
5
|
+
watch(%r{^spec/.+_spec\.rb$})
|
6
|
+
watch(%r{^lib/picture_tag/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
|
7
|
+
watch('spec/spec_helper.rb') { "spec" }
|
8
|
+
end
|
9
|
+
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Bookis Smuin
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
# picture_tag
|
2
|
+
|
3
|
+
A Rails view helper extension to generate the proposed HTML5 picture tag markup.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
`gem 'picture_tag-rails'`
|
10
|
+
|
11
|
+
Or install it yourself as:
|
12
|
+
|
13
|
+
`gem install picture_tag-rails`
|
14
|
+
|
15
|
+
## Usage
|
16
|
+
|
17
|
+
```erb
|
18
|
+
<%= picture_tag(image_path, options) %>
|
19
|
+
```
|
20
|
+
|
21
|
+
With default sizes and media queries
|
22
|
+
```erb
|
23
|
+
<%= picture_tag('/images/cat.jpg', alt: "Kitty cat!") %>
|
24
|
+
```
|
25
|
+
|
26
|
+
produces
|
27
|
+
|
28
|
+
```html
|
29
|
+
<picture>
|
30
|
+
<source media='(min-width: 1600px)' srcset='/images/cat-large.jpg 1x, /images/cat-large@2x.jpg 2x' />
|
31
|
+
<source media='(min-width: 1000px)' srcset='/images/cat-medium.jpg 1x, /images/cat-medium@2x.jpg 2x' />
|
32
|
+
<source media='(min-width: 768px)' srcset='/images/cat-small.jpg 1x, /images/cat-small@2x.jpg 2x' />
|
33
|
+
<source media='(min-width: 480px)' srcset='/images/cat-tiny.jpg 1x, /images/cat-tiny@2x.jpg 2x' />
|
34
|
+
<source srcset='/images/cat-tiny.jpg 1x, /images/cat-tiny@2x.jpg 2x' />
|
35
|
+
<img alt=\"Kitty cat!\" src=\"/images/cat-tiny.jpg\" />
|
36
|
+
</picture>
|
37
|
+
```
|
38
|
+
|
39
|
+
## Paperclip
|
40
|
+
|
41
|
+
Paperclip options for default media queries and sizes
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
has_attached_file :image, {
|
45
|
+
styles: {
|
46
|
+
tiny: "320x",
|
47
|
+
small: "480x",
|
48
|
+
medium: "768x",
|
49
|
+
large: "1000x",
|
50
|
+
huge: "1600x",
|
51
|
+
%s(tiny@2x) => "640x",
|
52
|
+
%s(small@2x) => "960x",
|
53
|
+
%s(medium@2x) => "1536x",
|
54
|
+
%s(large@2x) => "2000x",
|
55
|
+
%s(large@2x) => "3200x"
|
56
|
+
}
|
57
|
+
```
|
58
|
+
|
59
|
+
## Options
|
60
|
+
|
61
|
+
To exclude `<source>` elements with media queries above a max width:
|
62
|
+
```erb
|
63
|
+
<%= picture_tag(image_path, max_width: "600px") %>
|
64
|
+
```
|
65
|
+
|
66
|
+
To override the default media queries and names:
|
67
|
+
```erb
|
68
|
+
<%= picture_tag(image_path, sizes: {itty_bitty: "(min-width: 10px)"}) %>
|
69
|
+
```
|
70
|
+
|
71
|
+
All `image_tag` options are valid for `picture_tag`. See [image_tag Docs](http://api.rubyonrails.org/classes/ActionView/Helpers/AssetTagHelper.html)
|
72
|
+
|
73
|
+
## Todo
|
74
|
+
|
75
|
+
- Add optional paperclip integration functionality
|
76
|
+
- Implement Retina support
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,54 @@
|
|
1
|
+
module PictureTag
|
2
|
+
module ViewHelpers
|
3
|
+
|
4
|
+
def picture_tag(image_path, options={})
|
5
|
+
filename, extension = split_image_path_from_extension(image_path)
|
6
|
+
sizes = determine_sizes(options)
|
7
|
+
|
8
|
+
html = "<picture>"
|
9
|
+
sizes.each do |size, media_query|
|
10
|
+
html << build_source_tag(filename, size, extension, media_query)
|
11
|
+
end
|
12
|
+
html << add_default_source_and_image(filename, sizes.last.first, extension, options)
|
13
|
+
html << "</picture>"
|
14
|
+
|
15
|
+
html
|
16
|
+
end
|
17
|
+
|
18
|
+
def split_image_path_from_extension(image_path)
|
19
|
+
image_path.gsub!('-original', '')
|
20
|
+
image_path.split(/\.(?=[^\.]+(?: |$))| /) # Splits on last period before the first whitespace.
|
21
|
+
end
|
22
|
+
|
23
|
+
def build_source_tag(filename, size, extension, media_query=nil)
|
24
|
+
srcset = image_path("#{filename}-#{size}.#{extension}") + " 1x, "
|
25
|
+
srcset << image_path("#{filename}-#{size}@2x.#{extension}") + " 2x"
|
26
|
+
"<source #{"media='#{media_query}' " if media_query}srcset='#{srcset}' />"
|
27
|
+
end
|
28
|
+
|
29
|
+
def add_default_source_and_image(filename, size, extension, options)
|
30
|
+
html = build_source_tag(filename, size, extension)
|
31
|
+
options[:alt] ||= filename.split('/').last.capitalize
|
32
|
+
html << image_tag("#{filename}-#{size}.#{extension}", options)
|
33
|
+
html
|
34
|
+
end
|
35
|
+
|
36
|
+
def determine_sizes(options)
|
37
|
+
sizes = options.delete(:sizes) || default_sizes
|
38
|
+
max_width = options[:max_width].to_i if options[:max_width]
|
39
|
+
sizes = sizes.delete_if {|k,query| query.match(/\d+/)[0].to_i > max_width } if max_width
|
40
|
+
sizes = sizes.sort_by {|k,v| k.to_s }
|
41
|
+
end
|
42
|
+
|
43
|
+
def default_sizes
|
44
|
+
{
|
45
|
+
:huge => "(min-width: 1600px)",
|
46
|
+
:large => "(min-width: 1000px)",
|
47
|
+
:medium => "(min-width: 768px)",
|
48
|
+
:small => "(min-width: 480px)",
|
49
|
+
:tiny => "(min-width: 320px)"
|
50
|
+
}
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
end
|
data/lib/picture_tag.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'picture_tag/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "picture_tag-rails"
|
8
|
+
gem.version = PictureTag::VERSION
|
9
|
+
gem.authors = ["Bookis Smuin"]
|
10
|
+
gem.email = ["vegan.bookis@gmail.com"]
|
11
|
+
gem.description = %q{Rails View Helper picture_tag extension}
|
12
|
+
gem.summary = %q{A Rails view helper extension to generate the proposed HTML5 picture tag markup.}
|
13
|
+
gem.homepage = ""
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
end
|
@@ -0,0 +1,105 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
describe PictureTag::ViewHelpers, :type => :helper do
|
3
|
+
|
4
|
+
describe "split image path" do
|
5
|
+
let(:split_image_path) { helper.split_image_path_from_extension("/path/something.s-original.jpg") }
|
6
|
+
it "removes the -original" do
|
7
|
+
split_image_path.first.should eq "/path/something.s"
|
8
|
+
end
|
9
|
+
|
10
|
+
it "splits jpg" do
|
11
|
+
split_image_path.last.should eq "jpg"
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
|
16
|
+
describe "building the source tag" do
|
17
|
+
|
18
|
+
it "builds using a media query" do
|
19
|
+
helper.build_source_tag('test', 'small', 'jpg', "(min-width: 100px)").
|
20
|
+
should eq "<source media='(min-width: 100px)' srcset='/images/test-small.jpg 1x, /images/test-small@2x.jpg 2x' />"
|
21
|
+
end
|
22
|
+
|
23
|
+
it "builds without a media query" do
|
24
|
+
helper.build_source_tag('/path/test', 'small', 'png').
|
25
|
+
should eq "<source srcset='/path/test-small.png 1x, /path/test-small@2x.png 2x' />"
|
26
|
+
end
|
27
|
+
|
28
|
+
it "builds without an external path" do
|
29
|
+
helper.build_source_tag('http://www.image/path/test', 'small', 'png',"(min-width: 100px)").
|
30
|
+
should eq "<source media='(min-width: 100px)' srcset='http://www.image/path/test-small.png 1x, http://www.image/path/test-small@2x.png 2x' />"
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
describe "default source and image" do
|
35
|
+
it "builds source and img" do
|
36
|
+
helper.add_default_source_and_image('test', 'small', 'jpg', {}).
|
37
|
+
should eq "<source srcset='/images/test-small.jpg 1x, /images/test-small@2x.jpg 2x' /><img alt=\"Test\" src=\"/images/test-small.jpg\" />"
|
38
|
+
end
|
39
|
+
|
40
|
+
it "adds a class to the img tag" do
|
41
|
+
helper.add_default_source_and_image('test', 'small', 'jpg', {:class => "span2"}).
|
42
|
+
should eq "<source srcset='/images/test-small.jpg 1x, /images/test-small@2x.jpg 2x' /><img alt=\"Test\" class=\"span2\" src=\"/images/test-small.jpg\" />"
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
describe "determine sizes" do
|
48
|
+
let(:sizes) { {:huge => "(min-width: 1600px)", :small => "(min-width: 500px)"} }
|
49
|
+
it "puts the hash into a sorted array" do
|
50
|
+
helper.determine_sizes(:sizes => sizes).should eq [[:huge, "(min-width: 1600px)"], [:small, "(min-width: 500px)"]]
|
51
|
+
end
|
52
|
+
|
53
|
+
it "excludes sizes larger than tha max_width" do
|
54
|
+
helper.determine_sizes(:sizes => sizes, :max_width => "501px" ).should eq [[:small, "(min-width: 500px)"]]
|
55
|
+
end
|
56
|
+
|
57
|
+
it "keeps with an equal max width" do
|
58
|
+
helper.determine_sizes(:sizes => sizes, :max_width => "500px" ).should eq [[:small, "(min-width: 500px)"]]
|
59
|
+
end
|
60
|
+
|
61
|
+
it "removes with a lesser max width" do
|
62
|
+
helper.determine_sizes(:sizes => sizes, :max_width => "499px" ).should eq []
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
66
|
+
|
67
|
+
describe "options" do
|
68
|
+
def html
|
69
|
+
"<picture>" +
|
70
|
+
"<source media='(min-width: 1600px)' srcset='/images/cat-huge.jpg 1x, /images/cat-huge@2x.jpg 2x' />" +
|
71
|
+
"<source media='(min-width: 1000px)' srcset='/images/cat-large.jpg 1x, /images/cat-large@2x.jpg 2x' />" +
|
72
|
+
"<source media='(min-width: 768px)' srcset='/images/cat-medium.jpg 1x, /images/cat-medium@2x.jpg 2x' />" +
|
73
|
+
"<source media='(min-width: 480px)' srcset='/images/cat-small.jpg 1x, /images/cat-small@2x.jpg 2x' />" +
|
74
|
+
"<source media='(min-width: 320px)' srcset='/images/cat-tiny.jpg 1x, /images/cat-tiny@2x.jpg 2x' />" +
|
75
|
+
"<source srcset='/images/cat-tiny.jpg 1x, /images/cat-tiny@2x.jpg 2x' />" +
|
76
|
+
"<img alt=\"Cat\" src=\"/images/cat-tiny.jpg\" />" +
|
77
|
+
"</picture>"
|
78
|
+
end
|
79
|
+
|
80
|
+
|
81
|
+
it "matches the complete html" do
|
82
|
+
helper.picture_tag('/images/cat.jpg').should eq html
|
83
|
+
end
|
84
|
+
|
85
|
+
it "matches the complete html with an alt tag" do
|
86
|
+
helper.picture_tag('/images/cat.jpg', :alt => "Kitty!").should eq html.gsub("Cat", "Kitty!")
|
87
|
+
end
|
88
|
+
|
89
|
+
it "overwrites the default sizes if sizes given" do
|
90
|
+
h = "<picture>" +
|
91
|
+
"<source media='(min-width: 1px)' srcset='/images/cat-hidden.jpg 1x, /images/cat-hidden@2x.jpg 2x' />" +
|
92
|
+
"<source srcset='/images/cat-hidden.jpg 1x, /images/cat-hidden@2x.jpg 2x' />" +
|
93
|
+
"<img alt=\"Cat\" src=\"/images/cat-hidden.jpg\" />" +
|
94
|
+
"</picture>"
|
95
|
+
|
96
|
+
helper.picture_tag('/images/cat.jpg', :sizes => {:hidden => "(min-width: 1px)"}).
|
97
|
+
should eq h
|
98
|
+
end
|
99
|
+
|
100
|
+
it "excludes source tags with a media query above the given amount" do
|
101
|
+
helper.picture_tag("cat.jpg", :max_width => "500").split('source').should have(4).things
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
|
3
|
+
require "action_controller/railtie"
|
4
|
+
|
5
|
+
module Config
|
6
|
+
class Application < ::Rails::Application
|
7
|
+
config.active_support.deprecation = :stderr
|
8
|
+
end
|
9
|
+
end
|
10
|
+
Config::Application.initialize!
|
11
|
+
|
12
|
+
require 'rspec/rails'
|
13
|
+
require 'picture_tag'
|
metadata
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: picture_tag-rails
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Bookis Smuin
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2012-09-25 00:00:00 Z
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: Rails View Helper picture_tag extension
|
22
|
+
email:
|
23
|
+
- vegan.bookis@gmail.com
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files: []
|
29
|
+
|
30
|
+
files:
|
31
|
+
- .gitignore
|
32
|
+
- Gemfile
|
33
|
+
- Guardfile
|
34
|
+
- LICENSE.txt
|
35
|
+
- README.md
|
36
|
+
- Rakefile
|
37
|
+
- lib/picture_tag.rb
|
38
|
+
- lib/picture_tag/railtie.rb
|
39
|
+
- lib/picture_tag/version.rb
|
40
|
+
- lib/picture_tag/view_helpers.rb
|
41
|
+
- picture_tag-rails.gemspec
|
42
|
+
- spec/lib/view_helpers_spec.rb
|
43
|
+
- spec/spec_helper.rb
|
44
|
+
homepage: ""
|
45
|
+
licenses: []
|
46
|
+
|
47
|
+
post_install_message:
|
48
|
+
rdoc_options: []
|
49
|
+
|
50
|
+
require_paths:
|
51
|
+
- lib
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
hash: 3
|
58
|
+
segments:
|
59
|
+
- 0
|
60
|
+
version: "0"
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
|
+
none: false
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
hash: 3
|
67
|
+
segments:
|
68
|
+
- 0
|
69
|
+
version: "0"
|
70
|
+
requirements: []
|
71
|
+
|
72
|
+
rubyforge_project:
|
73
|
+
rubygems_version: 1.8.24
|
74
|
+
signing_key:
|
75
|
+
specification_version: 3
|
76
|
+
summary: A Rails view helper extension to generate the proposed HTML5 picture tag markup.
|
77
|
+
test_files:
|
78
|
+
- spec/lib/view_helpers_spec.rb
|
79
|
+
- spec/spec_helper.rb
|