I've been trying to simplify a search method in a project that I've taken over. Is it possible to make the code any cleaner than I already have done?
purchase_controller#search (original)
def search
parms = search_params
@purchases = []
if parms[:active].nil?
active = false
else
active = parms[:active] == 'true'
end
uid = parms[:customer_id]
pvid = parms[:provider_id]
pat = parms[:patient_id]
num_records = 10 if parms[:limit].nil?
num_records = parms[:limit].to_i unless parms[:limit].nil?
offset = 0 if parms[:offset].nil?
offset = parms[:offset].to_i unless parms[:offset].nil?
if parms[:active].nil?
# all three
@purchases = Purchase.where(customer_id: uid, provider_id: pvid, patient_id: pid).all.order(created_at: :desc).limit(num_records).offset(offset) if !uid.nil? && !pvid.nil? && !pat.nil?
# two of three
@purchases = Purchase.where(provider_id: pvid, patient_id: pid).all.order(created_at: :desc).limit(num_records).offset(offset) if !pvid.nil? && !pat.nil? && uid.nil?
@purchases = Purchase.where(customer_id: uid, patient_id: pid).all.order(created_at: :desc).limit(num_records).offset(offset) if !uid.nil? && !pat.nil? && pvid.nil?
@purchases = Purchase.where(customer_id: uid, provider_id: pvid).all.order(created_at: :desc).limit(num_records).offset(offset) if !uid.nil? && !pvid.nil? && pat.nil?
# one of three
@purchases = Purchase.where(customer_id: uid).all.order(created_at: :desc).limit(num_records).offset(offset) if !uid.nil? && pvid.nil? && pat.nil?
@purchases = Purchase.where(provider_id: pvid).all.order(created_at: :desc).limit(num_records).offset(offset) if uid.nil? && !pvid.nil? && pat.nil?
@purchases = Purchase.where(patient_id: pid).all.order(created_at: :desc).limit(num_records).offset(offset) if uid.nil? && pvid.nil? && !pat.nil?
# none of three
@purchases = Purchase.all.order(created_at: :desc).limit(num_records).offset(offset) if uid.nil? && pvid.nil? && pat.nil?
else
# all three
@purchases = Purchase.where(customer_id: uid, provider_id: pvid, active: active, patient_id: pid).offset(offset).all.order(created_at: :desc).limit(num_records) if !uid.nil? && !pvid.nil? && !pat.nil?
# two of three
@purchases = Purchase.where(provider_id: pvid, active: active, patient_id: pid).all.order(created_at: :desc).limit(num_records).offset(offset) if !pvid.nil? && !pat.nil? && uid.nil?
@purchases = Purchase.where(customer_id: uid, active: active, patient_id: pid).all.order(created_at: :desc).limit(num_records).offset(offset) if !uid.nil? && !pat.nil? && pvid.nil?
@purchases = Purchase.where(customer_id: uid, provider_id: pvid, active: active).all.order(created_at: :desc).limit(num_records).offset(offset) if !uid.nil? && !pvid.nil? && pat.nil?
# one of three
@purchases = Purchase.where(customer_id: uid, active: active).all.order(created_at: :desc).limit(num_records).offset(offset) if !uid.nil? && pvid.nil? && pat.nil?
@purchases = Purchase.where(provider_id: pvid, active: active).all.order(created_at: :desc).limit(num_records).offset(offset) if uid.nil? && !pvid.nil? && pat.nil?
@purchases = Purchase.where(active: active, patient_id: pid).all.order(created_at: :desc).limit(num_records).offset(offset) if uid.nil? && pvid.nil? && !pat.nil?
# none of three
@purchases = Purchase.where(active: active).all.order(created_at: :desc).limit(num_records).offset(offset) if uid.nil? && pvid.nil? && pat.nil?
end
@purchases.each do |hc|
hc.stripe_charge_id = nil
hc.stripe_transaction_id = nil
end
render :index, status: :ok, json: @purchases
end
purchases_controller#search (improved ?)
def search
parms = search_params
@purchases = []
if parms[:active].nil?
active = false
else
active = parms[:active] == 'true'
end
uid = parms[:customer_id]
pvid = parms[:provider_id]
pat = parms[:patient_id]
num_records = 10 if parms[:limit].nil?
num_records = parms[:limit].to_i unless parms[:limit].nil?
offset = 0 if parms[:offset].nil?
offset = parms[:offset].to_i unless parms[:offset].nil?
base_scope = Purchase.order(created_at: :desc).offset(offset).limit(num_records)
if parms[:active].nil?
# all three
@purchases = base_scope.where(customer_id: uid, provider_id: pvid, patient_id: pid) if !uid.nil? && !pvid.nil? && !pat.nil?
# two of three
@purchases = base_scope.where(provider_id: pvid, patient_id: pid) if !pvid.nil? && !pat.nil? && uid.nil?
@purchases = base_scope.where(customer_id: uid, patient_id: pid) if !uid.nil? && !pat.nil? && pvid.nil?
@purchases = base_scope.where(customer_id: uid, provider_id: pvid) if !uid.nil? && !pvid.nil? && pat.nil?
# one of three
@purchases = base_scope.where(customer_id: uid) if !uid.nil? && pvid.nil? && pat.nil?
@purchases = base_scope.where(provider_id: pvid) if uid.nil? && !pvid.nil? && pat.nil?
@purchases = base_scope.where(patient_id: pid) if uid.nil? && pvid.nil? && !pat.nil?
# none of three
@purchases = base_scope if uid.nil? && pvid.nil? && pat.nil?
else
active_scope = base_scope.where(active: active)
# all three
@purchases = active_scope.where(customer_id: uid, provider_id: pvid, patient_id: pid) if !uid.nil? && !pvid.nil? && !pat.nil?
# two of three
@purchases = active_scope.where(provider_id: pvid, patient_id: pid) if !pvid.nil? && !pat.nil? && uid.nil?
@purchases = active_scope.where(customer_id: uid, patient_id: pid) if !uid.nil? && !pat.nil? && pvid.nil?
@purchases = active_scope.where(customer_id: uid, provider_id: pvid) if !uid.nil? && !pvid.nil? && pat.nil?
# one of three
@purchases = active_scope.where(customer_id: uid) if !uid.nil? && pvid.nil? && pat.nil?
@purchases = active_scope.where(provider_id: pvid) if uid.nil? && !pvid.nil? && pat.nil?
@purchases = active_scope.where(patient_id: pid) if uid.nil? && pvid.nil? && !pat.nil?
# none of three
@purchases = active_scope if uid.nil? && pvid.nil? && pat.nil?
end
@purchases.each do |hc|
hc.stripe_charge_id = nil
hc.stripe_transaction_id = nil
end
render :index, status: :ok, json: @purchases
end