picture_tag-rails 0.0.2 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -8,7 +8,7 @@ from the W3C HTML Responsive Images Extension Proposal.
8
8
 
9
9
  ## Current Version
10
10
 
11
- 0.0.1
11
+ 0.0.4
12
12
 
13
13
 
14
14
  ## Requirements
@@ -40,10 +40,6 @@ gem install picture_tag-rails
40
40
 
41
41
  Add to some Ruby file in your Rails app.
42
42
 
43
- ```ruby
44
- require 'picture_tag'
45
- ```
46
-
47
43
  Use it in your views as you would the `image_tag()` helper.
48
44
 
49
45
  ```erb
@@ -86,6 +82,12 @@ To override the default media queries and names:
86
82
  <%= picture_tag(image_path, sizes: { itty_bitty: "(min-width: 10px)" }) %>
87
83
  ```
88
84
 
85
+ To prefix (opposed to suffix) the filename with size ('/images/small-kitty.jpg'):
86
+
87
+ ```erb
88
+ <%= picture_tag(image_path, prefix_sizes: true}) %>
89
+ ```
90
+
89
91
  All `image_tag` options are valid for `picture_tag`.
90
92
  See [image_tag Docs](http://api.rubyonrails.org/classes/ActionView/Helpers/AssetTagHelper.html)
91
93
 
@@ -1,3 +1,3 @@
1
1
  module PictureTag
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -1,54 +1,79 @@
1
1
  module PictureTag
2
2
  module ViewHelpers
3
-
3
+
4
4
  def picture_tag(image_path, options={})
5
- filename, extension = split_image_path_from_extension(image_path)
6
5
  sizes = determine_sizes(options)
7
-
6
+
8
7
  html = "<picture>"
9
- sizes.each do |size, media_query|
10
- html << build_source_tag(filename, size, extension, media_query)
8
+ sizes.each do |size, media_query|
9
+ html << build_source_tag(image_path, size, media_query, options)
11
10
  end
12
- html << add_default_source_and_image(filename, sizes.last.first, extension, options)
11
+ html << add_default_source_and_image(image_path, sizes.last.first, options)
13
12
  html << "</picture>"
14
-
13
+
15
14
  html
16
15
  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.
16
+
17
+ def build_file_path(image_path, size, prefix_size=false)
18
+ filename, extension = split_image_path_from_extension(image_path)
19
+ if prefix_size
20
+ # Splits on last '/' before the first whitespace.
21
+ path, file = filename.split(/\/(?=[^\/]+(?: |$))| /)
22
+ file ? "#{path}/#{size}-#{file}.#{extension}" : "#{size}-#{path}.#{extension}"
23
+ else
24
+ "#{filename}-#{size}.#{extension}"
25
+ end
21
26
  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"
27
+
28
+ def build_source_tag(image_path, size, media_query=nil, options={})
29
+ file = build_file_path(image_path, size, options[:prefix_size])
30
+ file2x = build_file_path(image_path, "#{size}@2x", options[:prefix_size])
31
+ srcset = image_path(file) + " 1x, "
32
+ srcset << image_path(file2x) + " 2x"
26
33
  "<source #{"media='#{media_query}' " if media_query}srcset='#{srcset}' />"
27
34
  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)
35
+
36
+ def add_default_source_and_image(image_path, size, options)
37
+ img_src = build_file_path(image_path, size, options[:prefix_size])
38
+ html = build_source_tag(image_path, size, nil, options)
39
+ html << image_tag(img_src, normalize_options(options, image_path))
33
40
  html
34
41
  end
35
-
42
+
43
+ def normalize_options(options, image_path)
44
+ options[:alt] ||= alt_tag(image_path)
45
+ options[:prefix_size] = nil
46
+ options[:max_width] = nil
47
+ options
48
+ end
49
+
50
+ def alt_tag(image_path)
51
+ filename, extension = split_image_path_from_extension(image_path)
52
+ filename.split('/').last.capitalize
53
+ end
54
+
36
55
  def determine_sizes(options)
37
56
  sizes = options.delete(:sizes) || default_sizes
38
57
  max_width = options[:max_width].to_i if options[:max_width]
39
58
  sizes = sizes.delete_if {|k,query| query.match(/\d+/)[0].to_i > max_width } if max_width
40
59
  sizes = sizes.sort_by {|k,v| k.to_s }
41
60
  end
42
-
61
+
62
+ def split_image_path_from_extension(image_path)
63
+ image_path.gsub!('-original', '')
64
+ # Splits on last period before the first whitespace.
65
+ image_path.split(/\.(?=[^\.]+(?: |$))| /)
66
+ end
67
+
43
68
  def default_sizes
44
69
  {
45
- :huge => "(min-width: 1600px)",
46
- :large => "(min-width: 1000px)",
47
- :medium => "(min-width: 768px)",
70
+ :huge => "(min-width: 1600px)",
71
+ :large => "(min-width: 1000px)",
72
+ :medium => "(min-width: 768px)",
48
73
  :small => "(min-width: 480px)",
49
74
  :tiny => "(min-width: 320px)"
50
75
  }
51
76
  end
52
-
77
+
53
78
  end
54
79
  end
@@ -16,29 +16,29 @@ describe PictureTag::ViewHelpers, :type => :helper do
16
16
  describe "building the source tag" do
17
17
 
18
18
  it "builds using a media query" do
19
- helper.build_source_tag('test', 'small', 'jpg', "(min-width: 100px)").
19
+ helper.build_source_tag('test.jpg', 'small', "(min-width: 100px)").
20
20
  should eq "<source media='(min-width: 100px)' srcset='/images/test-small.jpg 1x, /images/test-small@2x.jpg 2x' />"
21
21
  end
22
22
 
23
23
  it "builds without a media query" do
24
- helper.build_source_tag('/path/test', 'small', 'png').
24
+ helper.build_source_tag('/path/test.png', 'small').
25
25
  should eq "<source srcset='/path/test-small.png 1x, /path/test-small@2x.png 2x' />"
26
26
  end
27
27
 
28
28
  it "builds without an external path" do
29
- helper.build_source_tag('http://www.image/path/test', 'small', 'png',"(min-width: 100px)").
29
+ helper.build_source_tag('http://www.image/path/test.png', 'small',"(min-width: 100px)").
30
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
31
  end
32
32
  end
33
33
 
34
34
  describe "default source and image" do
35
35
  it "builds source and img" do
36
- helper.add_default_source_and_image('test', 'small', 'jpg', {}).
36
+ helper.add_default_source_and_image('test.jpg', 'small', {}).
37
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
38
  end
39
39
 
40
40
  it "adds a class to the img tag" do
41
- helper.add_default_source_and_image('test', 'small', 'jpg', {:class => "span2"}).
41
+ helper.add_default_source_and_image('test.jpg', 'small', {:class => "span2"}).
42
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
43
  end
44
44
 
@@ -86,6 +86,11 @@ describe PictureTag::ViewHelpers, :type => :helper do
86
86
  helper.picture_tag('/images/cat.jpg', :alt => "Kitty!").should eq html.gsub("Cat", "Kitty!")
87
87
  end
88
88
 
89
+ it "prefixes the size to the filename" do
90
+ html = helper.picture_tag('/images/cat.jpg', :prefix_size => true)
91
+ html.should match /src=\"\/images\/tiny-cat.jpg/i
92
+ end
93
+
89
94
  it "overwrites the default sizes if sizes given" do
90
95
  h = "<picture>" +
91
96
  "<source media='(min-width: 1px)' srcset='/images/cat-hidden.jpg 1x, /images/cat-hidden@2x.jpg 2x' />" +
@@ -100,6 +105,29 @@ describe PictureTag::ViewHelpers, :type => :helper do
100
105
  it "excludes source tags with a media query above the given amount" do
101
106
  helper.picture_tag("cat.jpg", :max_width => "500").split('source').should have(4).things
102
107
  end
108
+
109
+ describe "prefixes the size to the filename" do
110
+ it "without x" do
111
+ helper.build_file_path('/path/test.png', 'small', true).
112
+ should eq "/path/small-test.png"
113
+ end
114
+
115
+ it "with @2x" do
116
+ helper.build_file_path('/path/test.png', 'small@2x', true).
117
+ should eq "/path/small@2x-test.png"
118
+ end
119
+
120
+ it "without a slash" do
121
+ helper.build_file_path('path/test.png', 'small@2x', true).
122
+ should eq "path/small@2x-test.png"
123
+ end
124
+
125
+ it "without a path" do
126
+ helper.build_file_path('test.png', 'small@2x', true).
127
+ should eq "small@2x-test.png"
128
+ end
129
+
130
+ end
103
131
  end
104
132
 
105
133
  end
data/spec/spec_helper.rb CHANGED
@@ -10,4 +10,4 @@ end
10
10
  Config::Application.initialize!
11
11
 
12
12
  require 'rspec/rails'
13
- require 'picture_tag'
13
+ require 'picture_tag-rails'
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: picture_tag-rails
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 4
10
+ version: 0.0.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - Bookis Smuin
@@ -15,12 +15,10 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-09-26 00:00:00 Z
18
+ date: 2012-09-28 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
- name: rails
22
- prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
21
+ version_requirements: &id001 !ruby/object:Gem::Requirement
24
22
  none: false
25
23
  requirements:
26
24
  - - ">="
@@ -30,8 +28,10 @@ dependencies:
30
28
  - 3
31
29
  - 0
32
30
  version: "3.0"
31
+ prerelease: false
33
32
  type: :runtime
34
- version_requirements: *id001
33
+ name: rails
34
+ requirement: *id001
35
35
  description: Rails View Helper picture_tag extension
36
36
  email:
37
37
  - vegan.bookis@gmail.com