Class: SolidusFriendlyPromotions::PromotionHandler::Coupon
- Inherits:
-
Object
- Object
- SolidusFriendlyPromotions::PromotionHandler::Coupon
- Defined in:
- app/models/solidus_friendly_promotions/promotion_handler/coupon.rb
Instance Attribute Summary collapse
-
#coupon_code ⇒ Object
readonly
Returns the value of attribute coupon_code.
-
#error ⇒ Object
Returns the value of attribute error.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#order ⇒ Object
readonly
Returns the value of attribute order.
-
#status_code ⇒ Object
Returns the value of attribute status_code.
-
#success ⇒ Object
Returns the value of attribute success.
Instance Method Summary collapse
- #apply ⇒ Object
-
#initialize(order) ⇒ Coupon
constructor
A new instance of Coupon.
- #promotion ⇒ Object
- #remove ⇒ Object
- #successful? ⇒ Boolean
Constructor Details
#initialize(order) ⇒ Coupon
Returns a new instance of Coupon.
9 10 11 12 13 |
# File 'app/models/solidus_friendly_promotions/promotion_handler/coupon.rb', line 9 def initialize(order) @order = order @errors = [] @coupon_code = order&.coupon_code&.downcase end |
Instance Attribute Details
#coupon_code ⇒ Object (readonly)
Returns the value of attribute coupon_code.
6 7 8 |
# File 'app/models/solidus_friendly_promotions/promotion_handler/coupon.rb', line 6 def coupon_code @coupon_code end |
#error ⇒ Object
Returns the value of attribute error.
7 8 9 |
# File 'app/models/solidus_friendly_promotions/promotion_handler/coupon.rb', line 7 def error @error end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
6 7 8 |
# File 'app/models/solidus_friendly_promotions/promotion_handler/coupon.rb', line 6 def errors @errors end |
#order ⇒ Object (readonly)
Returns the value of attribute order.
6 7 8 |
# File 'app/models/solidus_friendly_promotions/promotion_handler/coupon.rb', line 6 def order @order end |
#status_code ⇒ Object
Returns the value of attribute status_code.
7 8 9 |
# File 'app/models/solidus_friendly_promotions/promotion_handler/coupon.rb', line 7 def status_code @status_code end |
#success ⇒ Object
Returns the value of attribute success.
7 8 9 |
# File 'app/models/solidus_friendly_promotions/promotion_handler/coupon.rb', line 7 def success @success end |
Instance Method Details
#apply ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'app/models/solidus_friendly_promotions/promotion_handler/coupon.rb', line 15 def apply if coupon_code.present? if promotion.present? && promotion.active? handle_present_promotion elsif promotion_code&.promotion&.expired? set_error_code :coupon_code_expired else set_error_code :coupon_code_not_found end end self end |
#promotion ⇒ Object
49 50 51 52 53 |
# File 'app/models/solidus_friendly_promotions/promotion_handler/coupon.rb', line 49 def promotion @promotion ||= if promotion_code&.promotion&.active? promotion_code.promotion end end |
#remove ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'app/models/solidus_friendly_promotions/promotion_handler/coupon.rb', line 29 def remove if promotion.blank? set_error_code :coupon_code_not_found elsif !promotion_exists_on_order?(order, promotion) set_error_code :coupon_code_not_present else order.friendly_order_promotions.destroy_by( promotion: promotion ) order.recalculate set_success_code :coupon_code_removed end self end |
#successful? ⇒ Boolean
45 46 47 |
# File 'app/models/solidus_friendly_promotions/promotion_handler/coupon.rb', line 45 def successful? success.present? && error.blank? end |