Class: Discount

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
OhNoes::Destroy
Defined in:
app/models/discount.rb

Constant Summary collapse

ALL_DISCOUNTS_STRING =
"ALL DISCOUNTS"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from OhNoes::Destroy

#destroy

Instance Attribute Details

#cartObject

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_discountObject



89
90
91
# File 'app/models/discount.rb', line 89

def clear_existing_discount
  @cart.reset_prices_on_tickets
end

#codeObject



81
82
83
# File 'app/models/discount.rb', line 81

def code
  self[:code].to_s.upcase
end

#destroyable?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'app/models/discount.rb', line 97

def destroyable?
  redeemed == 0
end

#eligible_ticketsObject



101
102
103
# File 'app/models/discount.rb', line 101

def eligible_tickets
  type.eligible_tickets
end

#ensure_discount_is_destroyableObject



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_setObject



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

def ensure_properties_are_set
  type.validate
end

#minimum_ticket_countObject



85
86
87
# File 'app/models/discount.rb', line 85

def minimum_ticket_count
  self[:minimum_ticket_count] || 0
end

#redeemedObject



93
94
95
# File 'app/models/discount.rb', line 93

def redeemed
  tickets.count
end

#set_organization_from_eventObject



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_limitObject



113
114
115
# File 'app/models/discount.rb', line 113

def tickets_fit_within_limit
  limit.blank? || eligible_tickets.count <= tickets_left
end

#tickets_leftObject



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_sObject



77
78
79
# File 'app/models/discount.rb', line 77

def to_s
  type.to_s
end

#typeObject



73
74
75
# File 'app/models/discount.rb', line 73

def type
  discount_class.new(self)
end