Skip to content

Instantly share code, notes, and snippets.

View MaryKuz's full-sized avatar

Maria Kuz MaryKuz

View GitHub Profile
@MaryKuz
MaryKuz / Error Description on the Screen
Created March 28, 2019 09:27
Error description on the screen
{
"errors": {
"ActiveRecord::RecordNotFound": [
"Couldn't find Product with 'id'=4"
]
}
}
@MaryKuz
MaryKuz / gist:3dace8fe39f16c4c364eaa1b72e4ac2f
Created March 28, 2019 09:16
Create views/application/exception.json.erb and write code there
<%= sanitize({ errors: { @exception.class.name => [@exception.to_s] } }.to_json) %>
@MaryKuz
MaryKuz / Add Error to ApplicationController
Created March 28, 2019 09:14
Add error to ApplicationController
class ApplicationController < ActionController::Base
...
rescue_from ActiveRecord::RecordNotFound do |exception|
@exception = exception
render :exception
end
...
@MaryKuz
MaryKuz / Write Tests for Decorator
Created March 28, 2019 09:12
Write tests for the decorator
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 }
@MaryKuz
MaryKuz / Create a New Decorator
Created March 28, 2019 09:09
create a new decorator
class ProductDecorator < Draper::Decorator
delegate_all
def as_json *args
{
id: id,
name: name,
price: price,
description: description
}
@MaryKuz
MaryKuz / Run Console Command
Created March 28, 2019 09:07
Run console command
Product.create!([{ name: 'apple', price: 5, description: 'green' },
{ name: 'beer', price: 10, description: 'cold' }])
@MaryKuz
MaryKuz / Add Code to ApplicationController
Created March 28, 2019 09:02
Add code to ApplicationController
class ApplicationController < ActionController::Base
...
helper_method :resource, :collection
...
end
@MaryKuz
MaryKuz / Fix Tests for Actions
Created March 28, 2019 09:01
Fix tests for actions
class Api::ProductsController < ApplicationController
private
def collection
@products ||= Product.all
end
def resource
@product ||= Product.find params[:id]
end
end
@MaryKuz
MaryKuz / Write Tests for Actions
Created March 28, 2019 08:58
Write tests for actions
RSpec.describe Api::ProductsController, type: :controller do
...
describe '#index.json' do
before { get :index, format: :json }
it { should render_template :index }
end
@MaryKuz
MaryKuz / Fix Errors
Created March 28, 2019 08:56
Fix Errors
namespace :api do
resources :products, only: [:index, :show]
end