Skip to content

Instantly share code, notes, and snippets.

@dlupu
Last active June 13, 2016 19:21
Show Gist options
  • Save dlupu/9345c484bb0e0e3c7b95e45e2cd7cc68 to your computer and use it in GitHub Desktop.
Save dlupu/9345c484bb0e0e3c7b95e45e2cd7cc68 to your computer and use it in GitHub Desktop.
Solidus (Spree) render API serializers outside the API
# app/controllers/products_controller.rb
class ProductsController < Spree::ProductsController
helper Spree::Api::ApiHelpers
def index
@searcher = build_searcher(params.merge(include_images: true))
@products = @searcher.retrieve_products
json = Rabl.render(nil, 'spree/api/products/index.v1', {
format: :json,
locals: => {
products: @products,
},
:scope => view_context
}
)
render json
end
end
# config/initializers/rabl_init.rb
require 'rabl'
Rabl.configure do |config|
# hack to be able to use json serializers defined in the solidus api outside solidus api
solidus_api_gem = Bundler.load.specs.find{|s| s.name == "solidus_api" }
config.view_paths += [ solidus_api_gem.full_gem_path + "/app/views/"]
end
@dlupu
Copy link
Author

dlupu commented Jun 13, 2016

please note the code the API serializers have not really been conceived to be used outside the API.
Serializers rely A LOT on helpers available in the API controllers and views which makes it VERY difficult to use outside the API.
I finally decided to write my own serializers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment