imgix / paperclip-imgix

Paperclip plugin to integrate with Imgix
MIT License
15 stars 5 forks source link

undefined local variable or method `plural_cache` #4

Closed crystalneth closed 9 years ago

crystalneth commented 10 years ago

After installing the gem from github directly, I get this error upon calling the attachment url method.

I'm on Rails 3.2.12 and Paperclip 3.5.4

Trace:

1.9.3p429 :002 > p.photo.url
NameError: undefined local variable or method `plural_cache' for Paperclip::Imgix::Interpolations:Module
  from /Users/alex/.rvm/gems/ruby-1.9.3-p429@app/gems/paperclip-3.5.4/lib/paperclip/interpolations.rb:88:in `class'
  from /Users/alex/.rvm/gems/ruby-1.9.3-p429@app/bundler/gems/paperclip-imgix-7435ad69c7ea/lib/paperclip-imgix/interpolations.rb:16:in `block (2 levels) in interpolate'
  from /Users/alex/.rvm/gems/ruby-1.9.3-p429@app/bundler/gems/paperclip-imgix-7435ad69c7ea/lib/paperclip-imgix/interpolations.rb:15:in `gsub'
  from /Users/alex/.rvm/gems/ruby-1.9.3-p429@app/bundler/gems/paperclip-imgix-7435ad69c7ea/lib/paperclip-imgix/interpolations.rb:15:in `block in interpolate'
  from /Users/alex/.rvm/gems/ruby-1.9.3-p429@app/bundler/gems/paperclip-imgix-7435ad69c7ea/lib/paperclip-imgix/interpolations.rb:14:in `each'
  from /Users/alex/.rvm/gems/ruby-1.9.3-p429@app/bundler/gems/paperclip-imgix-7435ad69c7ea/lib/paperclip-imgix/interpolations.rb:14:in `inject'
  from /Users/alex/.rvm/gems/ruby-1.9.3-p429@app/bundler/gems/paperclip-imgix-7435ad69c7ea/lib/paperclip-imgix/interpolations.rb:14:in `interpolate'
  from /Users/alex/.rvm/gems/ruby-1.9.3-p429@app/gems/paperclip-3.5.4/lib/paperclip/attachment.rb:455:in `interpolate'
  from /Users/alex/.rvm/gems/ruby-1.9.3-p429@app/gems/paperclip-3.5.4/lib/paperclip/attachment.rb:165:in `path'
  from /Users/alex/Development/app/app/models/photo.rb:100:in `block in <class:Photo>'
  from /Users/alex/.rvm/gems/ruby-1.9.3-p429@app/bundler/gems/paperclip-imgix-7435ad69c7ea/lib/paperclip-imgix/interpolations.rb:16:in `block (2 levels) in interpolate'
  from /Users/alex/.rvm/gems/ruby-1.9.3-p429@app/bundler/gems/paperclip-imgix-7435ad69c7ea/lib/paperclip-imgix/interpolations.rb:15:in `gsub'
  from /Users/alex/.rvm/gems/ruby-1.9.3-p429@app/bundler/gems/paperclip-imgix-7435ad69c7ea/lib/paperclip-imgix/interpolations.rb:15:in `block in interpolate'
  from /Users/alex/.rvm/gems/ruby-1.9.3-p429@app/bundler/gems/paperclip-imgix-7435ad69c7ea/lib/paperclip-imgix/interpolations.rb:14:in `each'
  from /Users/alex/.rvm/gems/ruby-1.9.3-p429@app/bundler/gems/paperclip-imgix-7435ad69c7ea/lib/paperclip-imgix/interpolations.rb:14:in `inject'
  from /Users/alex/.rvm/gems/ruby-1.9.3-p429@app/bundler/gems/paperclip-imgix-7435ad69c7ea/lib/paperclip-imgix/interpolations.rb:14:in `interpolate'
  from /Users/alex/.rvm/gems/ruby-1.9.3-p429@app/gems/paperclip-3.5.4/lib/paperclip/url_generator.rb:13:in `for'
  from /Users/alex/.rvm/gems/ruby-1.9.3-p429@app/bundler/gems/paperclip-imgix-7435ad69c7ea/lib/paperclip-imgix/url_generator.rb:10:in `for'
  from /Users/alex/.rvm/gems/ruby-1.9.3-p429@app/gems/paperclip-3.5.4/lib/paperclip/attachment.rb:149:in `url'
  from (irb):2
  from /Users/alex/.rvm/gems/ruby-1.9.3-p429@app/gems/railties-3.2.12/lib/rails/commands/console.rb:47:in `start'
  from /Users/alex/.rvm/gems/ruby-1.9.3-p429@app/gems/railties-3.2.12/lib/rails/commands/console.rb:8:in `start'
  from /Users/alex/.rvm/gems/ruby-1.9.3-p429@app/gems/railties-3.2.12/lib/rails/commands.rb:41:in `<top (required)>'
  from script/rails:6:in `require'
  from script/rails:6:in `<main>'1.9.3p429 :003 > 
pihman commented 10 years ago

I believe this is an NPE bug. To workaround, check that your attachment is not null.

crystalneth commented 10 years ago

This isn't an NPE. The attachment exists.

It appears that your interpolations Paperclip::Imgix::Interpolations is extending the Paperclip::Interpolations module. However I don't think extending ruby modules pulls in class methods, only instance methods, and plural_cache is a class method. They should really be calling it by using the fully qualified name, or not use module class methods. The class method lives on the module itself.

This is the reason you've had to copy/paste the #interpolate and #all into your module extension.

My guess is these class methods were recently added.

There is probably a more paperclippy way of doing this, but I don't know it. I've found Paperclip a nightmare to extend in my experience, so not surprised you've had issues like this.

I was able to fix this exception by adding this to your module:

    def self.plural_cache
      @plural_cache ||= Paperclip::Interpolations::PluralCache.new
    end

There are other class methods there, though they don't yet seem to be causing issue.

On Mon, Jul 14, 2014 at 12:24 PM, pihman notifications@github.com wrote:

I believe this is an NPE bug. To workaround, check that your attachment is not null.

— Reply to this email directly or view it on GitHub https://github.com/zebrafishlabs/paperclip-imgix/issues/4#issuecomment-48947379 .

Sent from a device that makes me spell badly and write short emails Alex Neth @crystalneth +1 206 499 4995

kellysutton commented 9 years ago

Unfortunately, this library is no longer maintained. We're looking for better ways of hooking into Paperclip that is more future-proof than this gem. As a result, we're cleaning up this repo and closing all old tickets.

We do have a lighter-weight gem for building and signing imgix URLs, which works well in tandem with normal paperclip. Please check that out here: https://github.com/imgix/imgix-rb