alchaplinsky / polymer-rails

Polymer and web components for Ruby on Rails
MIT License
286 stars 80 forks source link

undefined method `context_class` with new versions of sass/sprockets #60

Closed Frexuz closed 8 years ago

Frexuz commented 9 years ago

Error

/home/frexuz/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/polymer-rails-1.1.0/lib/polymer-rails/
railtie.rb:7:in `block in <class:Railtie>': undefined method `context_class' for nil:NilClass (NoMethodError)

Gemfile

source 'http://rubygems.org'
source 'http://gems.github.com/'

gem 'rails', '4.2.4'
gem 'polymer-rails'

#truncated...

group :assets do
  gem 'sass', '3.4.18'
  gem 'sass-rails', '~> 5.0.4'
  gem 'sprockets', '3.3.4'
  gem 'sprockets-rails', '3.0.0.beta2', :require => 'sprockets/railtie'
end

What's going on? :/

alchaplinsky commented 9 years ago

For some reason Rails.application.assets is nil in your app. Can you investigate that?

alchaplinsky commented 9 years ago

@Frexuz is this still an issue for you?

Frexuz commented 9 years ago

Sorry, I haven't been able to investigate further. I will let you know when I do :) @alchapone

Frexuz commented 9 years ago

Not sure how I'm supposed to debug further. I added config.assets.enabled = true in application.rb.

NoMethodError: undefined method `context_class' for nil:NilClass
/var/www/episodecalendar.com/config/environment.rb:2:in `<top (required)>'

initializers/assets.rb

Rails.application.config.assets.version = '1.0'

development.rb

Rails.application.configure do
  config.cache_classes = false
  config.eager_load = false
  config.consider_all_requests_local       = true
  config.action_controller.perform_caching = false
  config.action_controller.perform_caching = true
  config.action_mailer.raise_delivery_errors = false
  config.active_support.deprecation = :log
  config.active_record.migration_error = :page_load
  config.assets.debug = false
  config.action_mailer.default_url_options = { :host => "localhost" }
  config.assets.raise_runtime_errors = true
  config.action_view.raise_on_missing_translations = true
end

environment.rb

require File.expand_path('../application', __FILE__)
Rails.application.initialize!

application.rb

require File.expand_path('../boot', __FILE__)

require 'rails/all'
require 'csv'

Bundler.require(:default, Rails.env)

module Episodecalendar2
  class Application < Rails::Application
    config.autoload_paths += %W(#{Rails.root}/app/sweepers #{Rails.root}/lib)
    config.autoload_paths += Dir[ Rails.root.join('app', 'concerns', '**/') ]
    config.encoding = "utf-8"
    config.i18n.available_locales = ['en', 'br', 'de', 'es', 'fr', 'it', 'pl', 'pt', 'tr']
    config.i18n.fallbacks = true
    config.i18n.fallbacks = { 'br': 'pt_BR' }
    config.filter_parameters += [:password]
    config.to_prepare do
      Devise::Mailer.layout "email"
    end
    config.assets.enabled = true
    config.active_record.raise_in_transactional_callbacks = true
  end
end

gemfile (removed anything not related to assets, and the gemfile.lock, and ran bundle install again)

source 'http://rubygems.org'
source 'http://gems.github.com/'

#Main
gem 'rails', '4.2.3'
gem 'therubyracer', require: 'v8'
gem 'jquery_mobile_rails'
gem 'polymer-rails'

group :assets do
  gem 'sass', '3.4.19'
  gem 'sass-rails', '5.0.4'
  gem 'sprockets', '3.4.0'
  gem 'sprockets-rails', '3.0.0.beta2', :require => 'sprockets/railtie'
  gem 'uglifier', '>= 1.3.0'
end

Have i missed something in the configurations?

Frexuz commented 9 years ago

Also just tried a completely new rails project (4.2.3)

ONLY thing I've changed is adding the gems to the Gemfile:

gem 'polymer-rails'

group :assets do
  gem 'sass', '3.4.19'
  gem 'sass-rails', '5.0.4'
  gem 'sprockets', '3.4.0'
  gem 'sprockets-rails', '3.0.0.beta2', :require => 'sprockets/railtie'
end

rake assets:precompile RAILS_ENV="production" runs fine without polymer-rails, as soon as I add it, I get the error.

.... also tried config.assets.css_compressor = :sass

agnewtj commented 8 years ago

Getting the same error. The breakage seems to have been introduced with release 1.1.0. If I downgrade to 1.0.8, it works.

scarhand commented 8 years ago

This seems to be related to sprockets-rails 3.x as well. After I downgraded sprockets-rails to v2.3.3 the assets compiled successfully. Upgrading sprockets-rails to v3 breaks the compilation of assets.

See also the comment at https://github.com/rails/sprockets-rails - config.assets.compile "Enables Sprockets compile environment. If disabled, Rails.application.assets will be nil to prevent inadvertent compilation calls. View helpers will depend on assets being precompiled to public/assets in order to link to them. Initializers expecting Rails.application.assets during boot should be accessing the environment in a config.assets.configure block. See below."

voondo commented 8 years ago

Same error with sprockets-rails 3.x (works fine with 2.x)

tden012 commented 8 years ago

I ran into the same issue - and after some searching, found a probable fix...

I'm kind of new to git, so I don't know how to do a pull request or what's involved - so I'll post what I've found here.

This is the link that gave me the idea for the solution:

https://github.com/reactjs/react-rails/commit/a3d0dad530ab4335ba0c66cf92d5d5ec6c007710

I believe that the problem may be solved if following code in /lib/polymer-rails/railtie.rb:

initializer :polymer_html_import do
  helpers = %q{ include AssetTagHelper }
  ::ActionView::Base.module_eval(helpers)
  ::Rails.application.assets.context_class.class_eval(helpers)
end

is changed to this:

initializer :polymer_html_import do |app|
  helpers = %q{ include AssetTagHelper }
  ::ActionView::Base.module_eval(helpers)
  app.config.assets.context_class.class_eval(helpers)
end

I'm not quite sure the mechanism that this works by - but it seems that other gem developers have gone down this path.

TastyCatFood commented 8 years ago

I got the same error. shifted ::Rails.application.assets.context_class.class_eval(helpers) to config.after_initialize. app.assets pointed to nil, so I tried: Sprockets.register_preprocessor 'text/html', Polymer::Rails::DirectiveProcessor Sprockets.register_bundle_processor 'text/html', ::Sprockets::Bundle app.config.assets.register_postprocessor 'text/html', Polymer::Rails::ComponentsProcessorV3 Now I believe it's running. I'm trying to run dart-polymer, so I have a few more troubles to deal with though.

module Polymer
module Rails
class Railtie < ::Rails::Railtie
config.after_initialize do|app|
helpers = %q{ include AssetTagHelper }
::ActionView::Base.module_eval(helpers)
::Rails.application.assets.context_class.class_eval(helpers)
end

  initializer :precompile_polymer do |app|
    if app.config.respond_to? (:assets)
      app.config.assets.precompile += %w( polymer/polymer.js )
    end
  end

  initializer :add_preprocessors do |app|
    if Polymer::Rails::LEGACY_SPROCKETS
      add_preprocessors_legacy(app)
    else
      add_preprocessors(app)
    end
  end

  private

  def add_preprocessors(app)
    Sprockets.register_mime_type 'text/html', extensions: ['.html']
    Sprockets.register_preprocessor 'text/html', Polymer::Rails::DirectiveProcessor
    Sprockets.register_bundle_processor 'text/html', ::Sprockets::Bundle
    app.config.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::ComponentsProcessorV2.new(context, data).process
    end
  end

end

end end

rafalee commented 8 years ago

Having the same issue, I'm using

gem 'rails', '4.2.4' gem 'polymer-rails', '1.2.2'

anyone figure it out a solution?

hankish commented 8 years ago

@rafalee: I'm running rails 4.2.5 and was getting this issue too. When I updated my gemfile to:

gem 'rails', '4.2.5'
gem 'polymer-rails', '1.0.8'

and then ran a full bundle update it resolved the issue (and downgraded my sprockets gem version from 3.5.2 to 2.12.4). if that doesn't work for you maybe add the line gem 'sprockets', '2.12.4'?

LuisHCK commented 8 years ago

@hankish I followed your instructions and works correctly! :+1:

tostasqb commented 8 years ago

Leaving my solution here. In the gem file I removed sprockets gem, leaving only sprockets-rails and with this order.

gem 'sass-rails', '~> 5.0'
gem 'sprockets-rails'

On config/assets updated context_class lines to

config.assets.configure do |env|
  env.context_class.class_eval do
    # in my case I included....
    include Rails.application.routes.url_helpers
    include ActionView::Helpers
  end
end
Odaeus commented 8 years ago

In case it's useful, I've implemented a fix that I'm still testing, but it works so far for me: https://github.com/Odaeus/polymer-rails/commit/dca1467475875d9418f618b6ebe20aec74737198 I just moved most of it to config.after_initialize because Sprockets isn't ready until then.

Update: turns out my solution didn't work due to test/production environments receiving an immutable version of the Sprockets environment by that point! I have an even better :smile: fix now https://github.com/Odaeus/polymer-rails/commit/225f196026a315e2956c5d0403f3f31c8f10a95d that seems to be the Correct Way:tm:.

Update 2: happy to make a PR with backwards compatibility, though it's a pretty trivial change.

alchaplinsky commented 8 years ago

@Frexuz Try using latest version of polymer-rails. It should now support sprockets v3.x

hankish commented 8 years ago

This issue has been completely resolved for me and I'm pretty sure it can be closed.