Skip to content

Instantly share code, notes, and snippets.

@aalavandhan
Created September 26, 2014 05:01
Show Gist options
  • Save aalavandhan/c933f873360187c005d1 to your computer and use it in GitHub Desktop.
Save aalavandhan/c933f873360187c005d1 to your computer and use it in GitHub Desktop.
Sample Query Object
class OrdersQuery
def initialize(relation = Order.all)
@relation = relation.extending(Scopes)
end
def find
@relation.includes(:order_state,
:region,
:merchant_payment_method_currency => [
:merchant, :payment_method_currency => [ :payment_method_branch ]
],
:merchant_withdraw => :currency).recent_first
end
module Scopes
include BaseQuery
def for_a_merchant(merchant_id)
where("merchants.id" => merchant_id).references(:merchants)
end
def for_a_region(region_id)
return self if region_id.blank?
where("regions.id = ?", region_id).references(:regions)
end
def for_payment_method_branches(*payment_method_branch_ids)
return self if payment_method_branch_ids.blank_ext?
where("payment_method_branches.id" => payment_method_branch_ids).references(:payment_method_branches)
end
def for_withdraw_currencies(*currency_ids)
return self if currency_ids.blank_ext?
where("currencies.id" => currency_ids).references(:currencies)
end
def for_order_states(*states)
return self if states.blank_ext?
where("order_states.name" => states).references(:order_states)
end
def recent_first
order("orders.created_at DESC")
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment