-
-
Save SeanRoberts/11217572 to your computer and use it in GitHub Desktop.
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 QuickMoveInSearch | |
attr_accessor :params | |
def initialize(params) | |
@params = params | |
end | |
def results | |
results = scope | |
filters.each do |filter| | |
results = filter.new(results, params).apply | |
end | |
results | |
end | |
private | |
def scope | |
QuickeMoveIn.includes(:floorplan) | |
end | |
def filters | |
[ | |
FloorplanFilters::BedroomFilter, | |
FloorplanFilters::BathroomFilter, | |
FloorplanFilters::DesignFilter, | |
FloorplanFilters::PriceFilter, | |
FloorplanFilters::SizeFilter | |
] | |
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
module FloorplanFilters | |
class BedroomFilter < BaseSearchFilter | |
def apply | |
unless @params[:bedrooms].blank? | |
apply_bedrooms_filter | |
end | |
@scope | |
end | |
private | |
def apply_bedrooms_filter | |
if @params[:bedrooms].to_s.match(/\+$/) | |
@scope = gt_or_equal | |
else | |
@scope = equal | |
end | |
end | |
def gt_or_equal | |
@scope.where('floorplans.bedrooms >= ?', @params[:bedrooms].to_i) | |
end | |
def equal | |
@scope.where('floorplans.bedrooms = ?', @params[:bedrooms].to_i) | |
end | |
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 'spec_helper' | |
describe QuickMoveInSearch do | |
let(:qmi) { QuickMoveInSearch.new(params) } | |
let(:fake_scope) { double } | |
before do | |
qmi.stub(:scope) { fake_scope } | |
end | |
context 'when searching bedrooms' do | |
let(:params) { { bedrooms: bedrooms } } | |
context 'when looking for 2 bedrooms' do | |
let(:bedrooms) { 2 } | |
it 'should search for Quick Move-Ins with 2 bedrooms' do | |
expect(fake_scope).to receive(:where).with('floorplans.bedrooms = ?', 2) | |
qmi.results | |
end | |
end | |
context 'when looking for 3+ bedrooms' do | |
let(:bedrooms) { '3+' } | |
it 'should search for Quick Move-Ins with 3+ bedrooms' do | |
expect(fake_scope).to receive(:where).with('floorplans.bedrooms >= ?', 3) | |
qmi.results | |
end | |
end | |
context 'when bedrooms is an empty string' do | |
let(:bedrooms) { '' } | |
it 'should not search for bedrooms' do | |
expect(fake_scope).not_to receive(:where).with('floorplans.bedrooms = ?', 0) | |
qmi.results | |
end | |
end | |
end | |
context 'when searching bathrooms' do | |
let(:params) { { bathrooms: bathrooms } } | |
context 'when looking for 2 bathrooms' do | |
let(:bathrooms) { 2 } | |
it 'should search for Quick Move-Ins with 2 bathrooms' do | |
expect(fake_scope).to receive(:where).with('floorplans.bathrooms = ?', 2) | |
qmi.results | |
end | |
end | |
context 'when looking for 3+ bathrooms' do | |
let(:bathrooms) { '3+' } | |
it 'should search for Quick Move-Ins with 3+ bathrooms' do | |
expect(fake_scope).to receive(:where).with('floorplans.bathrooms >= ?', 3) | |
qmi.results | |
end | |
end | |
context 'when bathrooms is an empty string' do | |
let(:bathrooms) { '' } | |
it 'should not search for bathrooms' do | |
expect(fake_scope).not_to receive(:where).with('floorplans.bathrooms = ?', 0) | |
qmi.results | |
end | |
end | |
end | |
context 'when searching design' do | |
let(:params) { { design: design } } | |
context 'when looking for a Bungalow' do | |
let(:design) { 'Bungalow' } | |
it 'should search for Quick Move-Ins with 2 bathrooms' do | |
expect(fake_scope).to receive(:where).with('floorplans.design = ?', 'Bungalow') | |
qmi.results | |
end | |
end | |
context 'when design is an empty string' do | |
let(:design) { '' } | |
it 'should not search on design' do | |
expect(fake_scope).not_to receive(:where).with('floorplans.design = ?', '') | |
qmi.results | |
end | |
end | |
end | |
context 'when searching price' do | |
let(:params) { { price: price } } | |
context 'when looking for a range' do | |
let(:price) { '10000-50000' } | |
it 'should search within that range' do | |
expect(fake_scope).to receive(:where) | |
.with('floorplans.price >= ? AND floorplans.price <= ?', 10000, 50000) | |
qmi.results | |
end | |
end | |
context 'when price ends with a +' do | |
let(:price) { '50000+' } | |
it 'should search for gt or equal to that price' do | |
expect(fake_scope).to receive(:where).with('floorplans.price >= ?', 50000) | |
qmi.results | |
end | |
end | |
context 'when price is nonsense' do | |
let(:price) { 'blahblah' } | |
it 'should not search on price' do | |
filter = FloorplanFilters::PriceFilter.any_instance | |
expect(filter).not_to receive(:apply_filter) | |
qmi.results | |
end | |
end | |
end | |
context 'when searching size' do | |
let(:params) { { size: size } } | |
context 'when looking for a range' do | |
let(:size) { '1000-5000' } | |
it 'should search within that range' do | |
expect(fake_scope).to receive(:where) | |
.with('floorplans.square_footage >= ? AND floorplans.square_footage <= ?', 1000, 5000) | |
qmi.results | |
end | |
end | |
context 'when size ends with a +' do | |
let(:size) { '5000+' } | |
it 'should search for gt or equal to that size' do | |
expect(fake_scope).to receive(:where).with('floorplans.square_footage >= ?', 5000) | |
qmi.results | |
end | |
end | |
context 'when size is nonsense' do | |
let(:size) { 'blahblah' } | |
it 'should not search on size' do | |
filter = FloorplanFilters::PriceFilter.any_instance | |
expect(filter).not_to receive(:apply_filter) | |
qmi.results | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment