Created
August 22, 2021 08:48
-
-
Save adriangohjw/23ee6839536d3a837d2517140c34543d to your computer and use it in GitHub Desktop.
Kill long parameters with Method Chaining
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 SalaryQuery | |
def new | |
@results = Salary.all | |
end | |
def call(companies:, source:, order_by:) | |
@results = @results.where(company: companies) | |
unless companies.blank? | |
@results = @results.where(source: 'user_submission') | |
if source == true | |
case order_by | |
when 'number_of_submissions' | |
@results = @results.order(submission_count: :desc) | |
when 'company_name' | |
@results = @results.order(company_name: :desc) | |
end | |
@results | |
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
class SalariesController < ApplicationController | |
def search | |
@results = | |
SalaryQuery.new | |
.call(companies: params[:companies], | |
source: params[:user_submission_only], | |
order_by: params[:order_by]) | |
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
class SalaryQuery | |
def new | |
@results = Salary.all | |
end | |
# filtering by attributes | |
def filter_by_companies(companies) | |
@results = @results.where(company: companies) | |
unless companies.blank? | |
self | |
end | |
# filtering by boolean value | |
def filter_by_user_submission_only?(user_submission_only) | |
@results = @results.where(source: 'user_submission') | |
if user_submission_only | |
self | |
end | |
# conditional filtering by attribute | |
def order_by_attribute(attribute) | |
case attribute | |
when 'number_of_submissions' | |
@results = @results.order(submission_count: :desc) | |
when 'company_name' | |
@results = @results.order(company_name: :desc) | |
end | |
self | |
end | |
def call | |
@results | |
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
class SalariesController < ApplicationController | |
def search | |
@results = | |
SalaryQuery.new | |
.filter_by_companies(params[:companies]) | |
.filter_by_user_submission_only?(params[:user_submission_only]) | |
.order_by_attribute(params[:order_by]) | |
.call | |
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
class SalariesController < ApplicationController | |
# For simplicity sake, I have limited it to 3 filtering logics | |
def search | |
@results = Salary.all | |
@results = @results.where(company: params[:companies]) | |
unless params[:companies].blank? | |
@results = @results.where(source: 'user_submission') | |
if params[:user_submission_only] == true | |
case params[:order_by] | |
when 'number_of_submissions' | |
@results = @results.order(submission_count: :desc) | |
when 'company_name' | |
@results = @results.order(company_name: :desc) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment