Skip to content

Commit

Permalink
Merge pull request #56 from bjarosze/support_sprockets3
Browse files Browse the repository at this point in the history
Added support for sprockets v3
  • Loading branch information
Alex Chaplinsky committed Aug 6, 2015
2 parents ea25f44 + eb53057 commit c5e4c28
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 13 deletions.
12 changes: 11 additions & 1 deletion lib/polymer-rails.rb
@@ -1,7 +1,17 @@
module Polymer
module Rails
LEGACY_SPROCKETS = Gem::Version.new(Sprockets::VERSION) < Gem::Version.new('3.0.0')
end
end

require "sprockets"
require "polymer-rails/version"
require "polymer-rails/processors/directive_processor"
require "polymer-rails/processors/components_processor"
if Polymer::Rails::LEGACY_SPROCKETS
require "polymer-rails/processors/components_processor_v2"
else
require "polymer-rails/processors/components_processor_v3"
end
require "polymer-rails/helpers/asset_tag_helper"
require "polymer-rails/engine" if defined?(::Rails)
require "polymer-rails/railtie" if defined?(::Rails)
10 changes: 1 addition & 9 deletions lib/polymer-rails/processors/components_processor.rb
@@ -1,13 +1,6 @@
require 'polymer-rails/component'

module Polymer
module Rails
class ComponentsProcessor < Sprockets::Processor

def initialize(context, data)
@context = context
@component = Component.new(data)
end
module ComponentsProcessor

def process
inline_styles
Expand Down Expand Up @@ -61,7 +54,6 @@ def absolute_asset_path(file)
def find_asset(asset_path)
::Rails.application.assets.find_asset(asset_path)
end

end
end
end
16 changes: 16 additions & 0 deletions lib/polymer-rails/processors/components_processor_v2.rb
@@ -0,0 +1,16 @@
require 'polymer-rails/component'
require 'polymer-rails/processors/components_processor'

module Polymer
module Rails
class ComponentsProcessorV2 < Sprockets::Processor
include ::Polymer::Rails::ComponentsProcessor

def initialize(context, data)
@context = context
@component = Component.new(data)
end

end
end
end
33 changes: 33 additions & 0 deletions lib/polymer-rails/processors/components_processor_v3.rb
@@ -0,0 +1,33 @@
require 'polymer-rails/component'
require 'polymer-rails/processors/components_processor'

module Polymer
module Rails
class ComponentsProcessorV3
include ::Polymer::Rails::ComponentsProcessor

def self.instance
@instance ||= new
end

def self.call(input)
instance.call(input)
end

def call(input)
prepare(input)
data = process

@context.metadata.merge(data: data)
end

private

def prepare(input)
@context = input[:environment].context_class.new(input)
@component = Component.new(input[:data])
end

end
end
end
21 changes: 19 additions & 2 deletions lib/polymer-rails/railtie.rb
Expand Up @@ -14,10 +14,27 @@ class Railtie < ::Rails::Railtie
end

initializer :add_preprocessors do |app|
app.assets.register_mime_type "text/html", ".html"
if Polymer::Rails::LEGACY_SPROCKETS
add_preprocessors_legacy(app)
else
add_preprocessors(app)
end
end

private

def add_preprocessors(app)
app.assets.register_mime_type 'text/html', extensions: ['.html']
app.assets.register_preprocessor 'text/html', Polymer::Rails::DirectiveProcessor
app.assets.register_bundle_processor 'text/html', ::Sprockets::Bundle
app.assets.register_postprocessor 'text/html', Polymer::Rails::ComponentsProcessorV3
end

def add_preprocessors_legacy(app)
app.assets.register_mime_type "text/html", '.html'
app.assets.register_preprocessor "text/html", Polymer::Rails::DirectiveProcessor
app.assets.register_postprocessor 'text/html', :web do |context, data|
Polymer::Rails::ComponentsProcessor.new(context, data).process
Polymer::Rails::ComponentsProcessorV2.new(context, data).process
end
end

Expand Down
1 change: 0 additions & 1 deletion polymer-rails.gemspec
Expand Up @@ -18,7 +18,6 @@ Gem::Specification.new do |spec|
spec.add_runtime_dependency "rails", ">= 3.1.0"
spec.add_runtime_dependency "nokogiri", "~> 1.6"
spec.add_runtime_dependency "nokogumbo", "~> 1.1"
spec.add_runtime_dependency "sprockets", "< 3.0.0"

spec.add_development_dependency "bundler", "~> 1.5"
spec.add_development_dependency "rake", "~> 0"
Expand Down

0 comments on commit c5e4c28

Please sign in to comment.