diff --git a/README.md b/README.md index 3bc328b..6c37e8a 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ A Rails view helper extension to generate HTML5 `` tag markup from the W3C HTML Responsive Images Extension Proposal. - + [w3.org/community/respimg](http://www.w3.org/community/respimg) @@ -60,7 +60,7 @@ The vanilla usage with default sizes and media queries: produces: ```html - + @@ -96,6 +96,11 @@ To preload a custom default placeholder image: <%= picture_tag(image_path, default_image: '/images/placeholder.png'}) %> ``` +To add a class to the picture tag: + +```erb +<%= picture_tag(image_path, picture_class: "some_class"}) %> +``` All `image_tag` options are valid for `picture_tag`. See [image_tag Docs](http://api.rubyonrails.org/classes/ActionView/Helpers/AssetTagHelper.html) @@ -107,17 +112,17 @@ Paperclip options for default media queries and sizes. ```ruby has_attached_file :image, { - styles: { - tiny: "320x", - small: "480x", - medium: "768x", - large: "1000x", + styles: { + tiny: "320x", + small: "480x", + medium: "768x", + large: "1000x", huge: "1600x", - %s(tiny@2x) => "640x", - %s(small@2x) => "960x", - %s(medium@2x) => "1536x", - %s(large@2x) => "2000x", - %s(large@2x) => "3200x" + %s(tiny@2x) => "640x", + %s(small@2x) => "960x", + %s(medium@2x) => "1536x", + %s(large@2x) => "2000x", + %s(huge@2x) => "3200x" } ``` @@ -126,6 +131,7 @@ has_attached_file :image, { - Add optional paperclip integration functionality - Implement Retina support +- Add Travis ci Rails 4 support ## Authors diff --git a/lib/picture_tag-rails/view_helpers.rb b/lib/picture_tag-rails/view_helpers.rb index f5f3d42..032bf24 100644 --- a/lib/picture_tag-rails/view_helpers.rb +++ b/lib/picture_tag-rails/view_helpers.rb @@ -3,8 +3,10 @@ module ViewHelpers def picture_tag(image_path, options={}) sizes = determine_sizes(options) + alt_text = "alt='#{options[:alt] ||= alt_tag(image_path)}'" + picture_class = options[:picture_class].blank? ? "" : " class='#{options[:picture_class]}'" - html = "" + html = "" sizes.each do |size, media_query| html << build_source_tag(image_path, size, media_query, options) end @@ -36,7 +38,7 @@ def build_source_tag(image_path, size, media_query=nil, options={}) srcset = options[:default_image] if options[:default_image].eql?(image_path) "" end - + def add_default_source_and_image(image_path, size, options) prefix_size = options[:prefix_size] if options[:default_image] @@ -47,7 +49,7 @@ def add_default_source_and_image(image_path, size, options) options[:prefix_size] = false size = options[:default_size] end - + img_src = build_file_path(image_path, size, prefix_size, options) html = build_source_tag(image_path, size, nil, options) html << image_tag(img_src, normalize_options(options, image_path)) @@ -60,6 +62,7 @@ def normalize_options(options, image_path) options[:max_width] = nil options[:default_size] = nil options[:default_image] = nil + options[:picture_class] = nil options end diff --git a/spec/lib/view_helpers_spec.rb b/spec/lib/view_helpers_spec.rb index d829b26..a537be0 100644 --- a/spec/lib/view_helpers_spec.rb +++ b/spec/lib/view_helpers_spec.rb @@ -1,75 +1,75 @@ require 'spec_helper' describe PictureTag::ViewHelpers, :type => :helper do - + describe "split image path" do let(:split_image_path) { helper.split_image_path_from_extension("/path/something.s-original.jpg") } it "removes the -original" do split_image_path.first.should eq "/path/something.s" end - + it "splits jpg" do split_image_path.last.should eq "jpg" end - + end - + describe "building the source tag" do - + it "builds using a media query" do helper.build_source_tag('test.jpg', 'small', "(min-width: 100px)"). should eq "" end - + it "builds without a media query" do helper.build_source_tag('/path/test.png', 'small'). should eq "" end - + it "builds without an external path" do helper.build_source_tag('http://www.image/path/test.png', 'small',"(min-width: 100px)"). should eq "" end - + it "builds given a default image" do helper.build_source_tag('/images/test.png', 'small', nil, {:default_image => '/images/test.png'}). should eq "" end - + end - + describe "default source and image" do it "builds source and img" do helper.add_default_source_and_image('test.jpg', 'small', {}). should eq "\"Test\"" end - + it "lets you specify a default image size" do helper.add_default_source_and_image('test.jpg', 'any_size', {:default_size => :large}). should eq "\"Test\"" end - + it "lets you specify a default image path" do helper.add_default_source_and_image('test.jpg', 'large', {:default_image => '/images/test.png'}). should eq "\"Test\"" end - + it "adds a class to the img tag" do helper.add_default_source_and_image('test.jpg', 'small', {:class => "span2"}). should eq "\"Test\"" end - + end - + describe "determine sizes" do let(:sizes) { {:huge => "(min-width: 1600px)", :small => "(min-width: 500px)"} } it "puts the hash into a sorted array" do helper.determine_sizes(:sizes => sizes).should eq [[:huge, "(min-width: 1600px)"], [:small, "(min-width: 500px)"]] end - + it "excludes sizes larger than tha max_width" do helper.determine_sizes(:sizes => sizes, :max_width => "501px" ).should eq [[:small, "(min-width: 500px)"]] end - + it "keeps with an equal max width" do helper.determine_sizes(:sizes => sizes, :max_width => "500px" ).should eq [[:small, "(min-width: 500px)"]] end @@ -77,29 +77,32 @@ it "removes with a lesser max width" do helper.determine_sizes(:sizes => sizes, :max_width => "499px" ).should eq [] end - + end - + describe "options" do - def html - "" + + def html(alt, picture_class="") + "" + "" + "" + "" + "" + "" + "" + - "\"Cat\"" + + "\"#{alt}\"" + "" end - it "matches the complete html" do - helper.picture_tag('/images/cat.jpg').should eq html + helper.picture_tag('/images/cat.jpg').should eq html("Cat") end - it "matches the complete html with an alt tag" do - helper.picture_tag('/images/cat.jpg', :alt => "Kitty!").should eq html.gsub("Cat", "Kitty!") + it "matches the complete html with an alt attribute" do + helper.picture_tag('/images/cat.jpg', :alt => "Kitty!").should eq html("Kitty!") + end + + it "matches the complete html with an class attribute" do + helper.picture_tag('/images/cat.jpg', :alt => "Kitty!", :picture_class => "meow").should eq html("Kitty!", "meow") end it "prefixes the size to the filename" do @@ -108,7 +111,7 @@ def html end it "overwrites the default sizes if sizes given" do - h = "" + + h = "" + "" + "" + "\"Cat\"" + @@ -117,11 +120,11 @@ def html helper.picture_tag('/images/cat.jpg', :sizes => {:hidden => "(min-width: 1px)"}). should eq h end - + it "excludes source tags with a media query above the given amount" do helper.picture_tag("cat.jpg", :max_width => "500").split('source').should have(4).things end - + describe "prefixes the size to the filename" do it "without x" do helper.build_file_path('/path/test.png', 'small', true). @@ -137,13 +140,13 @@ def html helper.build_file_path('path/test.png', 'small@2x', true). should eq "path/small@2x-test.png" end - + it "without a path" do helper.build_file_path('test.png', 'small@2x', true). should eq "small@2x-test.png" end - + end end - + end \ No newline at end of file diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 8ea7286..6de4e83 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -2,12 +2,12 @@ require "action_controller/railtie" -module Config +module RbConfig class Application < ::Rails::Application - config.active_support.deprecation = :stderr + config.active_support.deprecation = :stderr end end -Config::Application.initialize! +RbConfig::Application.initialize! require 'rspec/rails' require 'picture_tag-rails'