Skip to content

Instantly share code, notes, and snippets.

@braidn
Created June 20, 2014 00:47
Show Gist options
  • Save braidn/f3e197536ba01ce30051 to your computer and use it in GitHub Desktop.
Save braidn/f3e197536ba01ce30051 to your computer and use it in GitHub Desktop.
module Spree
class Promotion
module Rules
class LimitOnePerUserPromotionRule < PromotionRule
def eligible?(order)
@email = order.email
return completed_orders_using_promotion < 1
end
private
attr_reader :email
def completed_orders
::Spree::Order.where(email: email).complete
end
def promotions
completed_orders.map{|o| o.adjustments.promotion}.flatten
end
def completed_orders_using_promotion
promotions.select{|p| ::Spree::PromotionAction.find(p.originator_id).
activator_id == self.activator_id}.size
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment