app/models/promotion.rb in spree_promo-0.30.2 vs app/models/promotion.rb in spree_promo-0.40.0

- old
+ new

@@ -4,38 +4,41 @@ alias credits promotion_credits has_many :promotion_rules accepts_nested_attributes_for :promotion_rules alias_method :rules, :promotion_rules - + validates :name, :code, :presence => true MATCH_POLICIES = %w(all any) scope :automatic, where("code IS NULL OR code = ''") - def eligible?(order) !expired? && rules_are_eligible?(order) end def expired? starts_at && Time.now < starts_at || expires_at && Time.now > expires_at || - usage_limit && promotion_credits.with_order.count >= usage_limit + usage_limit && credits_count >= usage_limit end + def credits_count + credits.with_order.count + end + def rules_are_eligible?(order) return true if rules.none? if match_policy == 'all' rules.all?{|r| r.eligible?(order)} else rules.any?{|r| r.eligible?(order)} end end def create_discount(order) - return if order.promotion_credits.reload.detect { |credit| credit.source_id == self.id } + return if order.promotion_credit_exists?(self) if eligible?(order) and amount = calculator.compute(order) amount = order.item_total if amount > order.item_total order.promotion_credits.reload.clear unless combine? and order.promotion_credits.all? { |credit| credit.source.combine? } order.promotion_credits.create({ :label => "#{I18n.t(:coupon)} (#{code})",