├── .gitignore ├── .rspec ├── .travis.yml ├── ChangeLog.md ├── Gemfile ├── LICENSE ├── README.md ├── Rakefile ├── lib ├── assets │ └── stylesheets │ │ ├── rails-sass-images.sass │ │ └── rails-sass-images │ │ ├── hidpi-background.sass │ │ ├── hidpi-image.sass │ │ ├── hidpi-inline.sass │ │ └── image-size.sass ├── rails-sass-images.rb └── rails-sass-images │ ├── railtie.rb │ ├── sass.rb │ ├── sass │ ├── inline.rb │ └── size.rb │ └── version.rb ├── rails-sass-images.gemspec └── spec ├── app ├── .gitignore ├── app │ ├── assets │ │ ├── config │ │ │ └── manifest.js │ │ ├── images │ │ │ ├── monolith.png │ │ │ └── square.svg │ │ └── stylesheets │ │ │ ├── hidpi-background.sass │ │ │ ├── hidpi-image.sass │ │ │ ├── hidpi-inline.sass │ │ │ ├── image-size.sass │ │ │ ├── inline-svg.sass │ │ │ ├── inline.sass │ │ │ ├── relative.sass │ │ │ ├── size.sass │ │ │ └── wrong-inline.sass │ └── controllers │ │ ├── application_controller.rb │ │ └── css_controller.rb ├── config.ru └── config │ ├── application.rb │ ├── boot.rb │ ├── environment.rb │ ├── environments │ └── test.rb │ └── routes.rb ├── plain_spec.rb ├── rails_spec.rb └── spec_helper.rb /.gitignore: -------------------------------------------------------------------------------- 1 | *.DS_Store 2 | *~ 3 | 4 | pkg/ 5 | 6 | .bundle 7 | Gemfile.lock 8 | -------------------------------------------------------------------------------- /.rspec: -------------------------------------------------------------------------------- 1 | --format documentation --colour 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: ruby 2 | rvm: 3 | - "2.5" 4 | - "2.6" 5 | -------------------------------------------------------------------------------- /ChangeLog.md: -------------------------------------------------------------------------------- 1 | ## 2.0.0 2 | * Add sprockets 4 support ([#9](https://github.com/ai/rails-sass-images/pull/9)) 3 | * Drop sprockets 3 support ([#9](https://github.com/ai/rails-sass-images/pull/9)) 4 | 5 | ## 1.0.3 6 | * Fix Sprockets 3 support. 7 | * Better error message for wrong `load_from`. 8 | 9 | ## 1.0.2 10 | * Fix file path in error message. 11 | 12 | ### 1.0.1 13 | * Better error message for unknown MIME-type. 14 | 15 | ### 1.0 “Correggio” 16 | * Better encoding for SVG (by Alexey Plutalov). 17 | * Remove official Ruby 1.9 support. 18 | 19 | ### 0.5 “Gentile Bellini” 20 | * Allow to use gem without Sprockets. 21 | * Add `hidpi-background` mixin for repeated backgrounds. 22 | 23 | ### 0.4 “Alessandro di Mariano di Vanni Filipepi” 24 | * Allow to work in Rails with disabled `initialize_on_precompile`. 25 | 26 | ### 0.3 “Michelangelo di Lodovico Buonarroti Simoni” 27 | * Allow to use with Sprockets in plain Ruby application. 28 | * Clean up code by removing Ruby 1.8 support. 29 | 30 | ### 0.2 “Raffaello Sanzio da Urbino” 31 | * Rename `retina-image` and `retina-inline` mixins to `hidpi-image` 32 | and `hidpi-inline`. 33 | 34 | ### 0.1 “Leonardo di ser Piero da Vinci” 35 | * Initial release. 36 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | gemspec 4 | gem 'rake' 5 | gem 'rspec-rails' 6 | 7 | gem 'rails', '>= 5' 8 | gem 'sassc-rails' 9 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright 2013 Andrey Sitnik 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Rails Sass Images [![Build Status](https://travis-ci.org/ai/rails-sass-images.svg)](https://travis-ci.org/ai/rails-sass-images) 2 | 3 | Sass functions and mixins to inline images and get images size: 4 | 5 | ```sass 6 | .icon 7 | +image-size("icon.png") 8 | background: inline("icon.png") 9 | 10 | .icon-wrap 11 | width: image-width("icon.png") + 6px 12 | height: image-height("icon.png") 13 | ``` 14 | 15 | with HiDPI (Retina) support: 16 | 17 | ```sass 18 | .icon 19 | +hidpi-inline("icon.png") 20 | 21 | .background 22 | +hidpi-image("big-image.jpg") 23 | ``` 24 | 25 | and fonts support: 26 | 27 | ```sass 28 | @font-face 29 | font-family: "MyFont" 30 | src: inline("my.woff") format('woff') 31 | ``` 32 | 33 | 34 | Sponsored by Evil Martians 35 | 36 | 37 | ## Features 38 | 39 | Instead of Compass, Rails Sass Images has: 40 | 41 | * HiDPI (Retina) support. 42 | * Full Assets Pipeline support. 43 | * Useful shortcuts. 44 | * More file types support. 45 | * Smaller and cleaner code. 46 | 47 | If you still need Compass for CSS 3 prefixes, 48 | see [Autoprefixer](https://github.com/ai/autoprefixer). 49 | 50 | ## Usage 51 | 52 | ### Ruby on Rails 53 | 54 | Add gem to your Rails `Gemfile`: 55 | 56 | ```ruby 57 | gem "rails-sass-images" 58 | ``` 59 | 60 | and import mixins in your `application.sass`: 61 | 62 | ```sass 63 | @import "rails-sass-images" 64 | ``` 65 | 66 | ### Sprockets 67 | 68 | You can use Rails Sass Images with plain Ruby application with Sprockets. 69 | Just install in to Sprockets environment: 70 | 71 | ```ruby 72 | require 'rails-sass-images' 73 | 74 | RailsSassImages.install(sprockets_env) 75 | ``` 76 | 77 | and import mixins in your Sass files: 78 | 79 | ```sass 80 | @import "rails-sass-images" 81 | ``` 82 | 83 | ### Other 84 | 85 | You can use Rails Sass Images without Sprockets. Just set dir to load assets: 86 | 87 | ```ruby 88 | require 'rails-sass-images' 89 | 90 | RailsSassImages.load_from = './images/' 91 | ``` 92 | 93 | By default, load dir will be current dir `.`. 94 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require 'rubygems' 2 | 3 | require 'bundler/setup' 4 | Bundler::GemHelper.install_tasks 5 | 6 | require 'rspec/core/rake_task' 7 | RSpec::Core::RakeTask.new 8 | task :default => :spec 9 | 10 | task :clobber_package do 11 | rm_r 'pkg' rescue nil 12 | end 13 | 14 | desc 'Delete all generated files' 15 | task :clobber => [:clobber_package] 16 | -------------------------------------------------------------------------------- /lib/assets/stylesheets/rails-sass-images.sass: -------------------------------------------------------------------------------- 1 | @import "rails-sass-images/image-size" 2 | @import "rails-sass-images/hidpi-image" 3 | @import "rails-sass-images/hidpi-inline" 4 | @import "rails-sass-images/hidpi-background" 5 | -------------------------------------------------------------------------------- /lib/assets/stylesheets/rails-sass-images/hidpi-background.sass: -------------------------------------------------------------------------------- 1 | // Set HiDPI (Retina) image background. 2 | // 3 | // .loader 4 | // background-repeat: repeat-x 5 | // +hidpi-background("nice/button.png") 6 | @mixin hidpi-background($path) 7 | background-image: image-url($path) 8 | background-size: (image-width($path) / 2) (image-height($path) / 2) 9 | -------------------------------------------------------------------------------- /lib/assets/stylesheets/rails-sass-images/hidpi-image.sass: -------------------------------------------------------------------------------- 1 | // Set HiDPI (Retina) image sizes and background. 2 | // 3 | // .button 4 | // +hidpi-image("nice/button.png") 5 | @mixin hidpi-image($path) 6 | +image-size($path, 2) 7 | +hidpi-background($path) 8 | background-repeat: no-repeat 9 | -------------------------------------------------------------------------------- /lib/assets/stylesheets/rails-sass-images/hidpi-inline.sass: -------------------------------------------------------------------------------- 1 | // Set image sizes and background and inline file as base64 data:uri. 2 | // It will use `background-size` to set HiDPI (Retina) image. 3 | // 4 | // .button 5 | // +hidpi-inline("nice/button.png") 6 | @mixin hidpi-inline($path) 7 | +image-size($path, 2) 8 | background: inline($path) no-repeat 9 | background-size: (image-width($path) / 2) (image-height($path) / 2) 10 | -------------------------------------------------------------------------------- /lib/assets/stylesheets/rails-sass-images/image-size.sass: -------------------------------------------------------------------------------- 1 | // Add `width` and `height` properties with image size. 2 | // For HiDPI images you can set scale `factor` and width and height 3 | // will be divided by this factor. 4 | // 5 | // .button 6 | // +image-size("button/nice.png") 7 | // background: image-path("button/nice.png") 8 | @mixin image-size($path, $factor: 1) 9 | width: image-width($path) / $factor 10 | height: image-height($path) / $factor 11 | -------------------------------------------------------------------------------- /lib/rails-sass-images.rb: -------------------------------------------------------------------------------- 1 | require 'pathname' 2 | 3 | module RailsSassImages 4 | # Return asset by file `path` from Sass parser 5 | def self.asset(path) 6 | path = path.value 7 | 8 | @load_from = @load_from.call() if @load_from.is_a? Proc 9 | 10 | if @load_from.is_a? Pathname 11 | asset = @load_from.join(path) 12 | raise "Can't find asset #{path} in #{@load_from}" unless asset.exist? 13 | elsif sprockets? @load_from 14 | asset = @load_from[path] 15 | raise "Can't find asset #{path}" unless asset 16 | asset = Pathname.new(asset.filename) 17 | else 18 | raise "Unknown type of RailsSassImages.load_from" 19 | end 20 | 21 | asset 22 | end 23 | 24 | # Set Sprockets environment and add Rails Sass Images styles paths 25 | def self.install(sprockets) 26 | sprockets.append_path(Pathname(__FILE__).dirname.join('assets/stylesheets')) 27 | @load_from = sprockets 28 | end 29 | 30 | # Set Sprockets environment or assets dir path 31 | def self.load_from=(source) 32 | source = Pathname(source) if source.is_a? String 33 | @load_from = source 34 | end 35 | 36 | # Get Sprockets environment or assets dir path 37 | def self.load_from 38 | @load_from 39 | end 40 | 41 | private 42 | 43 | # Safe detect is `var` is a Sprockets environment 44 | def self.sprockets?(var) 45 | return false unless defined? Sprockets 46 | var.is_a? Sprockets::Environment or var.is_a? Sprockets::CachedEnvironment 47 | end 48 | end 49 | 50 | RailsSassImages.load_from = '.' 51 | 52 | dir = Pathname(__FILE__).dirname.join('rails-sass-images') 53 | require dir.join('version') 54 | require dir.join('sass') 55 | require dir.join('railtie') if defined?(Rails) 56 | -------------------------------------------------------------------------------- /lib/rails-sass-images/railtie.rb: -------------------------------------------------------------------------------- 1 | module RailsSassImages 2 | # Enable `lib/assets/` 3 | class Engine < ::Rails::Engine 4 | end 5 | 6 | # Rails integration 7 | class Railtie < Rails::Railtie 8 | RailsSassImages.load_from = proc do 9 | Rails.application.assets || 10 | Sprockets::Railtie.build_environment(Rails.application) 11 | end 12 | end 13 | end 14 | -------------------------------------------------------------------------------- /lib/rails-sass-images/sass.rb: -------------------------------------------------------------------------------- 1 | require 'sassc' 2 | 3 | dir = Pathname(__FILE__).dirname.join('sass') 4 | Dir.glob(dir.join('*.rb').to_s) { |ext| require ext } 5 | 6 | module SassC::Script::Functions 7 | include RailsSassImages::Sass 8 | end 9 | 10 | if defined? Sprockets 11 | module Sprockets 12 | class SasscProcessor 13 | module Functions 14 | include RailsSassImages::Sass 15 | end 16 | end 17 | end 18 | end 19 | -------------------------------------------------------------------------------- /lib/rails-sass-images/sass/inline.rb: -------------------------------------------------------------------------------- 1 | require 'cgi' 2 | require 'mime-types' 3 | 4 | module RailsSassImages::Sass 5 | # Inline asset file to CSS by data-uri. Can be used for images and fonts. 6 | # 7 | # .icon 8 | # background: inline("icon.png") 9 | # 10 | # @font-face 11 | # font-family: "MyFont" 12 | # src: inline("my.woff") format('woff') 13 | def inline(path) 14 | asset = RailsSassImages.asset(path) 15 | 16 | mimes = MIME::Types.type_for(asset.to_s) 17 | raise "Unknown MIME-type for #{ asset.to_s }" unless mimes.first 18 | 19 | mime = mimes.first.content_type 20 | file = asset.read 21 | 22 | if mime == 'image/svg+xml' 23 | file = CGI::escape(file).gsub('+', '%20') 24 | encoding = 'charset=utf-8' 25 | else 26 | file = [file].flatten.pack('m').gsub("\n", '') 27 | encoding = 'base64' 28 | end 29 | 30 | SassC::Script::Value::String.new("url('data:#{mime};#{encoding},#{file}')") 31 | end 32 | end 33 | -------------------------------------------------------------------------------- /lib/rails-sass-images/sass/size.rb: -------------------------------------------------------------------------------- 1 | require 'dimensions' 2 | 3 | module RailsSassImages::Sass 4 | # Get image width 5 | def image_width(path) 6 | asset = RailsSassImages.asset(path) 7 | SassC::Script::Value::Number.new(Dimensions.width(asset), ["px"]) 8 | end 9 | 10 | # Get image height 11 | def image_height(path) 12 | asset = RailsSassImages.asset(path) 13 | SassC::Script::Value::Number.new(Dimensions.height(asset), ["px"]) 14 | end 15 | end 16 | -------------------------------------------------------------------------------- /lib/rails-sass-images/version.rb: -------------------------------------------------------------------------------- 1 | module RailsSassImages 2 | VERSION = '2.0.0'.freeze unless defined? RailsSassImages::VERSION 3 | end 4 | -------------------------------------------------------------------------------- /rails-sass-images.gemspec: -------------------------------------------------------------------------------- 1 | require './lib/rails-sass-images/version' 2 | 3 | Gem::Specification.new do |s| 4 | s.platform = Gem::Platform::RUBY 5 | s.name = 'rails-sass-images' 6 | s.version = RailsSassImages::VERSION.dup 7 | s.date = Time.now.strftime('%Y-%m-%d') 8 | s.summary = 'Sass functions and mixins to inline images ' + 9 | 'and get images size' 10 | 11 | s.files = `git ls-files`.split("\n") 12 | s.test_files = `git ls-files -- {spec}/*`.split("\n") 13 | s.extra_rdoc_files = ['README.md', 'LICENSE', 'ChangeLog.md'] 14 | s.require_path = 'lib' 15 | 16 | s.author = 'Andrey "A.I." Sitnik' 17 | s.email = 'andrey@sitnik.ru' 18 | s.homepage = 'https://github.com/ai/rails-sass-images' 19 | s.license = 'MIT' 20 | 21 | s.add_dependency 'sassc', ['> 0'] 22 | s.add_dependency 'dimensions', ['> 0'] 23 | s.add_dependency 'mime-types', ['> 0'] 24 | end 25 | -------------------------------------------------------------------------------- /spec/app/.gitignore: -------------------------------------------------------------------------------- 1 | /log 2 | /tmp 3 | -------------------------------------------------------------------------------- /spec/app/app/assets/config/manifest.js: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ai/rails-sass-images/d453d90c7890168288b83e8cae9aef3d485c08df/spec/app/app/assets/config/manifest.js -------------------------------------------------------------------------------- /spec/app/app/assets/images/monolith.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ai/rails-sass-images/d453d90c7890168288b83e8cae9aef3d485c08df/spec/app/app/assets/images/monolith.png -------------------------------------------------------------------------------- /spec/app/app/assets/images/square.svg: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /spec/app/app/assets/stylesheets/hidpi-background.sass: -------------------------------------------------------------------------------- 1 | @import "rails-sass-images" 2 | 3 | .icon 4 | +hidpi-background("monolith.png") 5 | -------------------------------------------------------------------------------- /spec/app/app/assets/stylesheets/hidpi-image.sass: -------------------------------------------------------------------------------- 1 | @import "rails-sass-images" 2 | 3 | .icon 4 | +hidpi-image("monolith.png") 5 | -------------------------------------------------------------------------------- /spec/app/app/assets/stylesheets/hidpi-inline.sass: -------------------------------------------------------------------------------- 1 | @import "rails-sass-images" 2 | 3 | .icon 4 | +hidpi-inline("monolith.png") 5 | -------------------------------------------------------------------------------- /spec/app/app/assets/stylesheets/image-size.sass: -------------------------------------------------------------------------------- 1 | @import "rails-sass-images" 2 | 3 | .icon 4 | +image-size("monolith.png") 5 | -------------------------------------------------------------------------------- /spec/app/app/assets/stylesheets/inline-svg.sass: -------------------------------------------------------------------------------- 1 | .icon 2 | background: inline("square.svg") 3 | -------------------------------------------------------------------------------- /spec/app/app/assets/stylesheets/inline.sass: -------------------------------------------------------------------------------- 1 | .icon 2 | background: inline("monolith.png") 3 | -------------------------------------------------------------------------------- /spec/app/app/assets/stylesheets/relative.sass: -------------------------------------------------------------------------------- 1 | .icon 2 | width: image-width("spec/app/app/assets/images/monolith.png") 3 | height: image-height("spec/app/app/assets/images/monolith.png") 4 | -------------------------------------------------------------------------------- /spec/app/app/assets/stylesheets/size.sass: -------------------------------------------------------------------------------- 1 | .icon 2 | width: image-width("monolith.png") 3 | height: image-height("monolith.png") 4 | -------------------------------------------------------------------------------- /spec/app/app/assets/stylesheets/wrong-inline.sass: -------------------------------------------------------------------------------- 1 | .icon 2 | background: inline("no.png") 3 | -------------------------------------------------------------------------------- /spec/app/app/controllers/application_controller.rb: -------------------------------------------------------------------------------- 1 | class ApplicationController < ActionController::Base 2 | end 3 | -------------------------------------------------------------------------------- /spec/app/app/controllers/css_controller.rb: -------------------------------------------------------------------------------- 1 | class CssController < ApplicationController 2 | def test 3 | file = params[:file] + '.css' 4 | render plain: Rails.application.assets[file] 5 | end 6 | end 7 | -------------------------------------------------------------------------------- /spec/app/config.ru: -------------------------------------------------------------------------------- 1 | require ::File.expand_path('../config/environment', __FILE__) 2 | run App::Application 3 | -------------------------------------------------------------------------------- /spec/app/config/application.rb: -------------------------------------------------------------------------------- 1 | require File.expand_path('../boot', __FILE__) 2 | 3 | require "action_controller/railtie" 4 | require "sprockets/railtie" 5 | 6 | if defined?(Bundler) 7 | Bundler.require(*Rails.groups(assets: %w(development test))) 8 | end 9 | 10 | module App 11 | class Application < Rails::Application 12 | config.encoding = 'utf-8' 13 | config.secret_key_base = 'foo' 14 | end 15 | end 16 | -------------------------------------------------------------------------------- /spec/app/config/boot.rb: -------------------------------------------------------------------------------- 1 | require 'rubygems' 2 | ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__) 3 | require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE']) 4 | -------------------------------------------------------------------------------- /spec/app/config/environment.rb: -------------------------------------------------------------------------------- 1 | require File.expand_path('../application', __FILE__) 2 | App::Application.initialize! 3 | -------------------------------------------------------------------------------- /spec/app/config/environments/test.rb: -------------------------------------------------------------------------------- 1 | App::Application.configure do 2 | config.eager_load = false 3 | config.cache_classes = true 4 | config.serve_static_assets = true 5 | config.static_cache_control = "public, max-age=3600" 6 | config.active_support.deprecation = :stderr 7 | config.consider_all_requests_local = true 8 | config.action_dispatch.show_exceptions = false 9 | config.action_controller.perform_caching = false 10 | config.action_controller.allow_forgery_protection = false 11 | 12 | config.assets.css_compressor = :sass 13 | end 14 | -------------------------------------------------------------------------------- /spec/app/config/routes.rb: -------------------------------------------------------------------------------- 1 | App::Application.routes.draw do 2 | root to: 'css#test' 3 | end 4 | -------------------------------------------------------------------------------- /spec/plain_spec.rb: -------------------------------------------------------------------------------- 1 | require File.expand_path('../spec_helper', __FILE__) 2 | 3 | require 'sprockets' 4 | 5 | describe RailsSassImages do 6 | before do 7 | @original = RailsSassImages.load_from 8 | 9 | @assets = Sprockets::Environment.new 10 | @assets.append_path(DIR.join('app/app/assets/images')) 11 | @assets.append_path(DIR.join('app/app/assets/stylesheets')) 12 | @assets.css_compressor = :sass 13 | 14 | @assets.context_class.class_eval do 15 | def asset_path(path, options = {}) 16 | "/assets/#{path}" 17 | end 18 | end 19 | 20 | @assets.context_class.instance_eval do 21 | def sass_config 22 | { } 23 | end 24 | end 25 | end 26 | 27 | after do 28 | RailsSassImages.load_from = @original 29 | end 30 | 31 | describe 'assets loading' do 32 | 33 | it "loads from dir" do 34 | RailsSassImages.load_from = DIR.join('app/app/assets/images') 35 | expect(@assets['size.css'].to_s).to eq ".icon{width:4px;height:6px}\n" 36 | end 37 | 38 | it "loads by default from current dir" do 39 | RailsSassImages.load_from = '.' 40 | expect(@assets['relative.css'].to_s).to eq ".icon{width:4px;height:6px}\n" 41 | end 42 | 43 | it "loads assets from sprockets" do 44 | RailsSassImages.load_from = @assets 45 | expect(@assets['size.css'].to_s).to eq ".icon{width:4px;height:6px}\n" 46 | end 47 | 48 | it "loads assets lazy" do 49 | RailsSassImages.load_from = proc { @assets } 50 | expect(@assets['size.css'].to_s).to eq ".icon{width:4px;height:6px}\n" 51 | end 52 | 53 | it "raises error on unknown file in assets" do 54 | RailsSassImages.load_from = @assets 55 | expect { 56 | @assets['wrong-inline.css'] 57 | }.to raise_error(/Can't find asset no\.png/) 58 | end 59 | 60 | it "raises error on unknown file in path" do 61 | RailsSassImages.load_from = '.' 62 | expect { 63 | @assets['wrong-inline.css'] 64 | }.to raise_error(/Can't find asset no\.png in \./) 65 | end 66 | 67 | end 68 | 69 | describe 'mixins' do 70 | before do 71 | RailsSassImages.install(@assets) 72 | end 73 | 74 | it "inlines assets" do 75 | expect(@assets['inline.css'].to_s).to eq ".icon{background:#{INLINE}}\n" 76 | end 77 | 78 | it "inlines SVG" do 79 | expect(@assets['inline-svg.css'].to_s).to eq ".icon{background:#{INLINE_SVG}}\n" 80 | end 81 | 82 | it "gets image size" do 83 | expect(@assets['size.css'].to_s).to eq ".icon{width:4px;height:6px}\n" 84 | end 85 | 86 | it "gets image size by mixin" do 87 | expect(@assets['image-size.css'].to_s).to eq( 88 | ".icon{width:4px;height:6px}\n") 89 | end 90 | 91 | it "has hidpi-background mixin" do 92 | expect(@assets['hidpi-background.css'].to_s).to eq (".icon{" + 93 | "background-image:url(/assets/monolith.png);" + 94 | "background-size:2px 3px}\n") 95 | end 96 | 97 | it "has hidpi-image mixin" do 98 | expect(@assets['hidpi-image.css'].to_s).to eq ".icon{" + 99 | "width:2px;height:3px;" + 100 | "background-image:url(/assets/monolith.png);" + 101 | "background-size:2px 3px;" + 102 | "background-repeat:no-repeat}\n" 103 | end 104 | 105 | it "has hidpi-inline mixin" do 106 | expect(@assets['hidpi-inline.css'].to_s).to eq ".icon{" + 107 | "width:2px;height:3px;" + 108 | "background:#{INLINE} no-repeat;" + 109 | "background-size:2px 3px}\n" 110 | end 111 | 112 | end 113 | end 114 | -------------------------------------------------------------------------------- /spec/rails_spec.rb: -------------------------------------------------------------------------------- 1 | require File.expand_path('../spec_helper', __FILE__) 2 | 3 | describe CssController, type: :controller do 4 | after :all do 5 | Rails.root.join('tmp').rmtree 6 | Rails.root.join('log').rmtree 7 | end 8 | 9 | it "inlines assets" do 10 | get :test, params: { file: 'inline' } 11 | expect(response).to be_successful 12 | expect(response.body).to eq ".icon{background:#{INLINE}}\n" 13 | end 14 | 15 | it "raises error on unknown file" do 16 | expect { 17 | get :test, params: { file: 'wrong-inline' } 18 | }.to raise_error(/Can't find asset no\.png/) 19 | end 20 | 21 | it "gets image size" do 22 | get :test, params: { file: 'size' } 23 | expect(response).to be_successful 24 | expect(response.body).to eq ".icon{width:4px;height:6px}\n" 25 | end 26 | 27 | it "gets image size by mixin" do 28 | get :test, params: { file: 'image-size' } 29 | expect(response).to be_successful 30 | expect(response.body).to eq ".icon{width:4px;height:6px}\n" 31 | end 32 | 33 | it "has hidpi-background mixin" do 34 | get :test, params: { file: 'hidpi-background' } 35 | expect(response).to be_successful 36 | expect(response.body).to include ".icon{" + 37 | "background-image:url(/assets/monolith" 38 | expect(response.body).to include "background-size:2px 3px}" 39 | end 40 | 41 | it "has hidpi-image mixin" do 42 | get :test, params: { file: 'hidpi-image' } 43 | expect(response).to be_successful 44 | expect(response.body).to include ".icon{" + 45 | "width:2px;height:3px;" + 46 | "background-image:url(/assets/monolith" 47 | expect(response.body).to include "background-size:2px 3px;" + 48 | "background-repeat:no-repeat" 49 | end 50 | 51 | it "has hidpi-inline mixin" do 52 | get :test, params: { file: 'hidpi-inline' } 53 | expect(response).to be_successful 54 | expect(response.body).to eq ".icon{" + 55 | "width:2px;height:3px;" + 56 | "background:#{INLINE} no-repeat;" + 57 | "background-size:2px 3px}\n" 58 | end 59 | end 60 | -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | ENV['RAILS_ENV'] ||= 'test' 2 | 3 | require_relative 'app/config/environment' 4 | require_relative '../lib/rails-sass-images' 5 | 6 | require 'rspec/rails' 7 | 8 | DIR = Pathname(__FILE__).dirname 9 | INLINE = 'url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAQAAAAGCAAAAADB' + 10 | 'UmCpAAAAC0lEQVQI12NgwAcAAB4AAW6FRzIAAAAASUVORK5CYII=")' 11 | INLINE_SVG = 'url("data:image/svg+xml;charset=utf-8,%3Csvg%20xmlns%3D%22http%3A%2F%2F' + 12 | 'www.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%201%201%22%3E%3Cpath' + 13 | '%20d%3D%22M0%2C0h1v1H0%22%20fill%3D%22%23ff0000%22%2F%3E%3C%2Fsvg%3E%0A")' 14 | --------------------------------------------------------------------------------