Created
September 26, 2014 05:01
-
-
Save aalavandhan/c933f873360187c005d1 to your computer and use it in GitHub Desktop.
Sample Query Object
This file contains hidden or 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 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