Class: DiscountType

Inherits:
Object
  • Object
show all
Defined in:
app/models/discounts/discount_type.rb

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(discount) ⇒ DiscountType

Returns a new instance of DiscountType.



40
41
42
43
# File 'app/models/discounts/discount_type.rb', line 40

def initialize(discount)
  @discount = discount
  @properties = discount.properties
end

Instance Attribute Details

#propertiesObject

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

.feeObject



32
33
34
# File 'app/models/discounts/discount_type.rb', line 32

def self.fee
  ARTFULLY_CONFIG[:ticket_fee] || 0
end

.typesObject



36
37
38
# File 'app/models/discounts/discount_type.rb', line 36

def self.types
  @@types
end

Instance Method Details

#apply_discount_to_cart(*args) ⇒ Object



60
61
62
# File 'app/models/discounts/discount_type.rb', line 60

def apply_discount_to_cart(*args)
  raise "This method has not been defined in child class!"
end

#eligible_ticketsObject



53
54
55
56
57
58
# File 'app/models/discounts/discount_type.rb', line 53

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.ticket_type.name, @discount.ticket_types)}
  return tix
end

#ticketsObject



49
50
51
# File 'app/models/discounts/discount_type.rb', line 49

def tickets
  @discount.cart.tickets
end

#validateObject



45
46
47
# File 'app/models/discounts/discount_type.rb', line 45

def validate
  true
end