Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

undefined method `replace_engine' for nil:NilClass when using custom cache engine #639

Open
BastianHofmann opened this issue Aug 13, 2015 · 2 comments

Comments

@BastianHofmann
Copy link

I'm getting a undefined method 'replace_engine' for nil:NilClass error when using a custom cache engine.

  config.cache_engine = Rabl::CustomCacheEngine.new

.
.
.

module Rabl
  class CustomCacheEngine

    def fetch(key, cache_options, &block)
      if defined?(Rails)
        Rails.cache.fetch(prefix_key(key), cache_options, &block)
      else
        yield
      end
    end

    def write(key, value, options = {})
      if defined?(Rails)
        Rails.cache.write(prefix_key(key), value, options)
      end
    end

    def read_multi(*keys)
      options = keys.extract_options!
      keys.map! { |key| prefix_key(key) }
      if defined?(Rails)
        Rails.cache.read_multi(*keys, options)
      else
        keys.inject({}) { |hash, key| hash[key] = nil; hash }
      end
    end

    protected

    def prefix_key(key)
      "#{Apartment::Tenant.current_tenant}/#{key}"
    end
  end
end

Stack trace:

rabl (0.11.6) lib/rabl/multi_builder.rb:97:in `block in replace_engines_with_cache_results'
rabl (0.11.6) lib/rabl/multi_builder.rb:94:in `each'
rabl (0.11.6) lib/rabl/multi_builder.rb:94:in `replace_engines_with_cache_results'
rabl (0.11.6) lib/rabl/multi_builder.rb:24:in `to_a'
rabl (0.11.6) lib/rabl/engine.rb:88:in `to_hash'
rabl (0.11.6) lib/rabl/engine.rb:101:in `to_dumpable'
rabl (0.11.6) lib/rabl/engine.rb:110:in `to_json'
rabl (0.11.6) lib/rabl/engine.rb:48:in `block in render'
activesupport (4.1.9) lib/active_support/cache.rb:296:in `block in fetch'
activesupport (4.1.9) lib/active_support/cache.rb:587:in `block in save_block_result_to_cache'
activesupport (4.1.9) lib/active_support/cache.rb:548:in `instrument'
activesupport (4.1.9) lib/active_support/cache.rb:586:in `save_block_result_to_cache'
activesupport (4.1.9) lib/active_support/cache.rb:296:in `fetch'
app/features/rabl/custom_cache_engine.rb:6:in `fetch'
@BastianHofmann
Copy link
Author

For now I was able to monkey patch with:

  class MultiBuilder
    def replace_engines_with_cache_results
      @cache_results.each do |key, value|
        engine = @cache_key_to_engine[key]
        builder = @engine_to_builder[engine]
        builder.replace_engine(engine, value) if value && builder
      end
    end
  end

@MilanJ88
Copy link

Had the same issue, the monkeypatch solved it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants