Class: Discount
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Discount
- Includes:
- OhNoes::Destroy
- Defined in:
- app/models/discount.rb
Constant Summary collapse
- ALL_DISCOUNTS_STRING =
"ALL DISCOUNTS"
Instance Attribute Summary collapse
-
#cart ⇒ Object
Returns the value of attribute cart.
Class Method Summary collapse
-
.unique_codes_for(organization) ⇒ Object
Returns an array of the unique codes (not the discount objects, but the actual strings) that this organization is using.
Instance Method Summary collapse
- #apply_discount_to_cart(cart = nil) ⇒ Object
- #clear_existing_discount ⇒ Object
- #code ⇒ Object
- #destroyable? ⇒ Boolean
- #eligible_tickets ⇒ Object
- #ensure_discount_is_destroyable ⇒ Object
- #ensure_properties_are_set ⇒ Object
- #minimum_ticket_count ⇒ Object
- #redeemed ⇒ Object
- #set_organization_from_event ⇒ Object
- #tickets_fit_within_limit ⇒ Object
- #tickets_left ⇒ Object
- #to_s ⇒ Object
- #type ⇒ Object
Methods included from OhNoes::Destroy
Instance Attribute Details
#cart ⇒ Object
Returns the value of attribute cart.
8 9 10 |
# File 'app/models/discount.rb', line 8 def cart @cart end |
Class Method Details
.unique_codes_for(organization) ⇒ Object
Returns an array of the unique codes (not the discount objects, but the actual strings) that this organization is using
45 46 47 |
# File 'app/models/discount.rb', line 45 def self.unique_codes_for(organization) Discount.where(:organization_id => organization.id).pluck(:code).uniq end |
Instance Method Details
#apply_discount_to_cart(cart = nil) ⇒ Object
49 50 51 52 53 54 55 56 57 58 |
# File 'app/models/discount.rb', line 49 def apply_discount_to_cart(cart=nil) @cart ||= cart unless cart.nil? transaction do @cart.discount = self ensure_discount_is_allowed clear_existing_discount type.apply_discount_to_cart @cart.save! end end |
#clear_existing_discount ⇒ Object
89 90 91 |
# File 'app/models/discount.rb', line 89 def clear_existing_discount @cart.prepare_for_discount! end |
#code ⇒ Object
81 82 83 |
# File 'app/models/discount.rb', line 81 def code self[:code].to_s.upcase end |
#destroyable? ⇒ Boolean
97 98 99 |
# File 'app/models/discount.rb', line 97 def destroyable? redeemed == 0 end |
#eligible_tickets ⇒ Object
101 102 103 |
# File 'app/models/discount.rb', line 101 def eligible_tickets type.eligible_tickets end |
#ensure_discount_is_destroyable ⇒ Object
64 65 66 67 68 69 70 71 |
# File 'app/models/discount.rb', line 64 def ensure_discount_is_destroyable if redeemed > 0 self.errors.add(:base, "Ticket must be unused if it is to be destroyed. Consider disabling it instead.") false else true end end |
#ensure_properties_are_set ⇒ Object
60 61 62 |
# File 'app/models/discount.rb', line 60 def ensure_properties_are_set type.validate end |
#minimum_ticket_count ⇒ Object
85 86 87 |
# File 'app/models/discount.rb', line 85 def minimum_ticket_count self[:minimum_ticket_count] || 0 end |
#redeemed ⇒ Object
93 94 95 |
# File 'app/models/discount.rb', line 93 def redeemed tickets.count end |
#set_organization_from_event ⇒ Object
37 38 39 |
# File 'app/models/discount.rb', line 37 def set_organization_from_event self.organization ||= self.event.try(:organization) end |
#tickets_fit_within_limit ⇒ Object
113 114 115 |
# File 'app/models/discount.rb', line 113 def tickets_fit_within_limit limit.blank? || eligible_tickets.count <= tickets_left end |
#tickets_left ⇒ Object
105 106 107 108 109 110 111 |
# File 'app/models/discount.rb', line 105 def tickets_left if limit.present? limit - redeemed > 0 ? limit - redeemed : 0 # Any negative numbers become 0. else raise "Infinite tickets left in discount when there's no limit." end end |
#to_s ⇒ Object
77 78 79 |
# File 'app/models/discount.rb', line 77 def to_s type.to_s end |
#type ⇒ Object
73 74 75 |
# File 'app/models/discount.rb', line 73 def type discount_class.new(self) end |