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 UserDecorator < Draper::Decorator | |
delegate_all | |
def as_json *args | |
{ | |
name: name, | |
email: email | |
} | |
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
require 'rails_helper' | |
RSpec.describe Api::UsersController, type: :controller do | |
it { should route(:post, 'api/user').to(action: :create) } | |
describe '#create.json' do | |
let(:params) do | |
{ | |
name: 'Test name', | |
email: 'test@test.com', |
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
def create | |
build_resource | |
resource.save! | |
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::UsersController < ApplicationController | |
private | |
def build_resource | |
@user = User.new resource_params | |
end | |
def resource | |
@user | |
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
Rails.application.routes.draw do | |
namespace :api do | |
... | |
resource :user, only: [:create] | |
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
require 'rails_helper' | |
RSpec.describe User, type: :model do | |
it { should have_secure_password } | |
it { should validate_presence_of :name } | |
it { should validate_presence_of :email } | |
it { should validate_uniqueness_of(:email).case_insensitive } |
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 User < ActiveRecord::Base | |
has_secure_password | |
validates :name, presence: true | |
validates :email, presence: true, uniqueness: { case_sensitive: false }, email: true | |
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 User < ActiveRecord::Base | |
has_secure_password | |
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 Product, type: :model do | |
it { should be_a PgSearch } | |
describe '.search_by' do | |
let(:relation) { double } | |
before { expect(Product).to receive(:page).with(1).and_return(relation) } | |
context do | |
it { expect { Product.search_by 'page' => 1 }.to_not raise_error } |
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
def search_by params = {} | |
params = params.try(:symbolize_keys) || {} | |
collection = page(params[:page]) | |
if params[:term].present? | |
collection = collection.where('name ILIKE ?', "#{ params[:term] }%") | |
end | |
if params[:name].present? |