Created
November 4, 2011 15:55
-
-
Save fahim/1339683 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 Promotion::Rules::OnePerUser < PromotionRule | |
def eligible?(order) | |
used_promotion_credits(order).empty? | |
end | |
def used_promotion_credits(order) | |
p = PromotionCredit.arel_table | |
o = Order.arel_table | |
if order.user.anonymous? | |
user_condition = o[:email].eq order.email | |
else | |
user_condition = o[:user_id].eq order.user_id | |
end | |
this_promo = p[:source_id].eq(promotion_id) | |
not_this_order = o[:id].eq(order.id).not | |
finalized_orders = o[:state].eq(Order::FINALIZED_STATES) | |
by_this_user = user_condition | |
# checking if this promotion is used by the user in another order. | |
PromotionCredit.includes(:order).where(this_promo, not_this_order, finalized_orders, by_this_user) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment