nesquena / rabl

General ruby templating with json, bson, xml, plist and msgpack support
http://blog.codepath.com/2011/06/27/building-a-platform-api-on-rails/
MIT License
3.65k stars 335 forks source link

Use `view_paths` helper for `view_path` option when using a Rails application #612

Open BenMorganIO opened 9 years ago

BenMorganIO commented 9 years ago

I'm currently trying to deal with view paths. I have a Rabl template that is extending from another rabl template located in a rails engine. I've added to my Rabl configuration:

Rabl.configure do |config|
  config.cache_all_output = !Rails.env.development?
  config.cache_sources = !Rails.env.development?
  config.perform_caching = !Rails.env.development?
  config.include_json_root = true
  config.view_paths = [
    "#{Rails.root}/app/views",
    "#{Gem.loaded_specs['spree_api'].full_gem_path}/app/views"
  ]
end

This is what I'm doing inside of my application controller:

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception

  private

  def render_json(object, path)
    JSON.parse Rabl::Renderer.json object, path, view_path: view_paths
  end
end

And inside of my products controller:

class ProductsController < ApplicationController
  def index
    gon.current_order = render_json current_order, "api/orders/show"
    @products = Rails.cache.fetch("products_with_master") do
      Spree::Product.with_master
    end
  end
end

The current_order does return an order object. The path to "api/orders/show" is successful. It renders this:

extends "spree/api/orders/show"

Spree is an opensource project that is using Rabl and I would like to extend one of their views. In doing this, I received this error:

undefined local variable or method `lookup_context' for #<Rabl::Engine:0x007fd410c0c600>

I'm wondering if we can drop the option to select the view path and autoset it if you're using Rails via the view_paths helper from ActionController.

BenMorganIO commented 9 years ago

Commenting to bump this issue.

DouweM commented 9 years ago

Hi @BenMorganIO

Could you check where exactly that error is originating and whether you're using the latest version of Rabl? From what I can see, use of the lookup_context method is only ever used after it's been verified to exist via respond_to?.

—Douwe

PS. I know that's not exactly the point of this issue, but I want to get us on the same page first.

BenMorganIO commented 9 years ago

The version of Rabl currently being used is 0.9.4.pre1. I'm unable to update due to large amounts test failures on Spree's end.

BenMorganIO commented 9 years ago

I'll need to recreate the work for the stack trace.