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
{ | |
"errors": { | |
"ActiveRecord::RecordNotFound": [ | |
"Couldn't find Product with 'id'=4" | |
] | |
} | |
} |
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
<%= sanitize({ errors: { @exception.class.name => [@exception.to_s] } }.to_json) %> |
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
class ApplicationController < ActionController::Base | |
... | |
rescue_from ActiveRecord::RecordNotFound do |exception| | |
@exception = exception | |
render :exception | |
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
require 'rails_helper' | |
describe ProductDecorator do | |
describe '#as_json' do | |
let(:product) { stub_model Product, id: 1, name: 'apple', price: 10.0, description: 'green' } | |
subject { product.decorate.as_json } | |
its([:id]) { should eq 1 } |
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
class ProductDecorator < Draper::Decorator | |
delegate_all | |
def as_json *args | |
{ | |
id: id, | |
name: name, | |
price: price, | |
description: description | |
} |
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
Product.create!([{ name: 'apple', price: 5, description: 'green' }, | |
{ name: 'beer', price: 10, description: 'cold' }]) |
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
class ApplicationController < ActionController::Base | |
... | |
helper_method :resource, :collection | |
... | |
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
class Api::ProductsController < ApplicationController | |
private | |
def collection | |
@products ||= Product.all | |
end | |
def resource | |
@product ||= Product.find params[:id] | |
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
RSpec.describe Api::ProductsController, type: :controller do | |
... | |
describe '#index.json' do | |
before { get :index, format: :json } | |
it { should render_template :index } | |
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
namespace :api do | |
resources :products, only: [:index, :show] | |
end |