Class: DiscountType
- Inherits:
-
Object
- Object
- DiscountType
- Defined in:
- app/models/discounts/discount_type.rb
Direct Known Subclasses
BuyOneGetOneFreeDiscountType, DollarsOffTicketsDiscountType, PercentageOffTicketsDiscountType
Constant Summary collapse
- @@types =
This is essentially copied from the payment class. There’s an issue with how Rails auto-loads models, and finding out what classes have inherited from you.
A subclass For example, a subclass called WonkaPayment can call payment_method :wonka Then callers can instantiate a WonkaPayment by calling Payment.create :wonka
This will also define an instance method called “payment_method” on the subclass which will return a stringification of the symbol
HashWithIndifferentAccess.new
Instance Attribute Summary collapse
-
#properties ⇒ Object
Returns the value of attribute properties.
Class Method Summary collapse
Instance Method Summary collapse
- #apply_discount_to_cart(*args) ⇒ Object
- #eligible_tickets ⇒ Object
-
#initialize(discount) ⇒ DiscountType
constructor
A new instance of DiscountType.
- #tickets ⇒ Object
- #validate ⇒ Object
Constructor Details
#initialize(discount) ⇒ DiscountType
Returns a new instance of DiscountType.
36 37 38 39 |
# File 'app/models/discounts/discount_type.rb', line 36 def initialize(discount) @discount = discount @properties = discount.properties end |
Instance Attribute Details
#properties ⇒ Object
Returns the value of attribute properties.
2 3 4 |
# File 'app/models/discounts/discount_type.rb', line 2 def properties @properties end |
Class Method Details
.discount_type(names) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'app/models/discounts/discount_type.rb', line 15 def self.discount_type names names = Array.wrap(names) names.each do |name| @@types[name] = self end self.class_eval(<<-EOS, __FILE__, __LINE__) def discount_type "#{names[0].to_s.gsub('_',' ').capitalize}" end def self.discount_type "#{names[0].to_s.gsub('_',' ').capitalize}" end EOS end |
.types ⇒ Object
32 33 34 |
# File 'app/models/discounts/discount_type.rb', line 32 def self.types @@types end |
Instance Method Details
#apply_discount_to_cart(*args) ⇒ Object
56 57 58 |
# File 'app/models/discounts/discount_type.rb', line 56 def apply_discount_to_cart(*args) raise "This method has not been defined in child class!" end |
#eligible_tickets ⇒ Object
49 50 51 52 53 54 |
# File 'app/models/discounts/discount_type.rb', line 49 def eligible_tickets is_in = ->(element, list){list.blank? || !! list.find_index(element)} tix = tickets.find_all {|t| is_in.call(t.show.id, @discount.show_ids)} tix = tix.find_all{|t| is_in.call(t.section.name, @discount.sections)} return tix end |
#tickets ⇒ Object
45 46 47 |
# File 'app/models/discounts/discount_type.rb', line 45 def tickets @discount.cart.tickets end |
#validate ⇒ Object
41 42 43 |
# File 'app/models/discounts/discount_type.rb', line 41 def validate true end |