Last active
June 13, 2016 19:21
-
-
Save dlupu/9345c484bb0e0e3c7b95e45e2cd7cc68 to your computer and use it in GitHub Desktop.
Solidus (Spree) render API serializers outside the API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.