Skip to content

Instantly share code, notes, and snippets.

View MaryKuz's full-sized avatar

Maria Kuz MaryKuz

View GitHub Profile
@MaryKuz
MaryKuz / Add Code to File
Created March 28, 2019 10:13
Add code to the file
class Product < ActiveRecord::Base
include PgSearch
pg_search_scope :search,
against: {
name: :A,
description: :B
},
using: {
tsearch: { dictionary: :english }
@MaryKuz
MaryKuz / Correct the Tests
Created March 28, 2019 10:10
Correct the tests
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 }
end
context do
@MaryKuz
MaryKuz / Get back to search_by method
Created March 28, 2019 10:08
Get back to the .search_by method
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
collection
@MaryKuz
MaryKuz / Fill Database with Initial Information
Created March 28, 2019 10:05
Fill the database with the initial information
1000.times do
Product.create \
name: Faker::Commerce.product_name,
price: Faker::Number.between(1, 150),
description: Faker::Commerce.department
end
@MaryKuz
MaryKuz / Connect Gem to Development and Test Modes in Gemfile
Created March 28, 2019 10:03
Connect the gem to the development and test modes in Gemfile
group :development, :test do
gem 'faker'
end
@MaryKuz
MaryKuz / Correct Tests
Created March 28, 2019 10:01
Correct spec/controllers/api/products_controller_spec.rb tests
RSpec.describe Api::ProductsController, type: :controller do
....
describe '#collection' do
before { expect(subject).to receive(:params).and_return(:params) }
before { expect(Product).to receive(:search_by).with(params) }
it { expect { subject.send :collection }.to_not raise_error }
end
@MaryKuz
MaryKuz / Collect the #collection method
Created March 28, 2019 09:59
Collect the #collection method
class Api::ProductsController < ApplicationController
private
def collection
@products ||= Product.search_by(params)
end
....
end
@MaryKuz
MaryKuz / Write Tests
Created March 28, 2019 09:57
Write tests
require 'rails_helper'
RSpec.describe Product, type: :model do
describe '.search_by' do
let(:relation) { double }
before { expect(Product).to receive(:all).and_return(relation) }
context do
it { expect { Product.search_by }.to_not raise_error }
@MaryKuz
MaryKuz / Transfer Hash Parameters into Symbols
Created March 28, 2019 09:52
Transfer hash parameters into symbols
class Product < ActiveRecord::Base
class << self
def search_by params = {}
params = params.try(:symbolize_keys) || {}
collection = all
if params[:term].present?
collection = collection.where('name ILIKE ?', "#{ params[:term] }%")
end
@MaryKuz
MaryKuz / Write a Class Method for Model
Created March 28, 2019 09:49
Write a class method for model
class Product < ActiveRecord::Base
class << self
def search_by params = {}
params = params.try(:symbolize_keys) || {}
end
end
end