Skip to content

Commit

Permalink
[close #381] Avoid Sprockets deprecations
Browse files Browse the repository at this point in the history
- Use both `register_engine` and `register_transformer` in railtie.rb. 
- In the test postprocessor use an object that responds to `call` and `new.render`.
  • Loading branch information
schneems committed Jul 21, 2016
1 parent 90c6dd1 commit 15e6092
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 10 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ gemfile:
- gemfiles/Gemfile-sprockets-2-12
- gemfiles/Gemfile-sprockets-2-8
- gemfiles/Gemfile-sprockets-3-0
- gemfiles/Gemfile-sprockets-4-0
- gemfiles/Gemfile-sprockets-rails-2-2
- gemfiles/Gemfile-sprockets-rails-master
- gemfiles/Gemfile-sass-3-1
Expand Down
7 changes: 7 additions & 0 deletions gemfiles/Gemfile-sprockets-4-0
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
source "https://rubygems.org"

# Specify your gem"s dependencies in sass-rails.gemspec
gemspec path: ".."

gem "rails"
gem "sprockets", "~> 4.x"
13 changes: 11 additions & 2 deletions lib/sass/rails/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,17 @@ class Railtie < ::Rails::Railtie
end

config.assets.configure do |env|
env.register_engine '.sass', Sass::Rails::SassTemplate
env.register_engine '.scss', Sass::Rails::ScssTemplate
if env.respond_to?(:register_engine)
env.register_engine '.sass', Sass::Rails::SassTemplate, silence_deprecation: true
env.register_engine '.scss', Sass::Rails::ScssTemplate, silence_deprecation: true
end

if env.respond_to?(:register_transformer)
env.register_transformer 'text/sass', 'text/css',
Sprockets::SassProcessor.new(importer: SassImporter, sass_config: app.config.sass)
env.register_transformer 'text/scss', 'text/css',
Sprockets::ScssProcessor.new(importer: SassImporter, sass_config: app.config.sass)
end

env.context_class.class_eval do
class_attribute :sass_config
Expand Down
10 changes: 5 additions & 5 deletions test/fixtures/engine_project/engine_project.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ require "engine_project/version"
Gem::Specification.new do |s|
s.name = "engine_project"
s.version = EngineProject::VERSION
s.authors = ["TODO: Your name"]
s.email = ["TODO: Your email"]
s.homepage = "TODO"
s.summary = "TODO: Summary of EngineProject."
s.description = "TODO: Description of EngineProject."
s.authors = ["Your name"]
s.email = ["Your email"]
s.homepage = ""
s.summary = "Summary of EngineProject."
s.description = "Description of EngineProject."

s.files = Dir["{app,config,db,lib}/**/*", "MIT-LICENSE", "Rakefile", "README.rdoc"]
s.test_files = Dir["test/**/*"]
Expand Down
26 changes: 23 additions & 3 deletions test/fixtures/scss_project/config/initializers/postprocessor.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
Rails.application.config.assets.configure do |env|
env.register_postprocessor 'text/css', :postprocessor do |context, css|
css.gsub /@import/, 'fail engine'

class SassRailsTestPostProcessor
def initialize(filename, &block)
@filename = filename
@source = block.call
end

def render(context)
self.class.run(@source)
end

def self.run(source)
source.gsub /@import/, 'fail engine'
end

def self.call(input)
source = input[:data]
result = run(source)
{ data: result }
end
end

Rails.application.config.assets.configure do |env|
env.register_postprocessor 'text/css', SassRailsTestPostProcessor
end

0 comments on commit 15e6092

Please sign in to comment.