Module: ActsAsPurchasable
- Extended by:
- ActiveSupport::Concern
- Defined in:
- app/models/concerns/acts_as_purchasable.rb
Overview
Acts as purchasable Add to your model: t.integer :purchased_order_id t.integer :price t.boolean :tax_exempt, default: false t.string :qb_item_name or add_column :resources, :purchased_order_id, :integer add_column :resources, :price, :integer add_column :resources, :tax_exempt, :boolean, default: false add_column :resources, :qb_item_name, :string
You can also optionally add :purchased_at :datetime, and purchased_by_id and purchased_by_type
Defined Under Namespace
Modules: Base, ClassMethods
Instance Method Summary
collapse
Instance Method Details
#deferred? ⇒ Boolean
126
127
128
|
# File 'app/models/concerns/acts_as_purchasable.rb', line 126
def deferred?
deferred_orders.any? { |order| order.deferred? }
end
|
#deferred_at ⇒ Object
134
135
136
|
# File 'app/models/concerns/acts_as_purchasable.rb', line 134
def deferred_at
self[:deferred_at] || orders.find { |order| order.deferred? }.try(:deferred_at)
end
|
#price=(value) ⇒ Object
If I have a column type of Integer, and I’m passed a non-Integer, convert it here
93
94
95
96
97
98
99
100
101
102
103
|
# File 'app/models/concerns/acts_as_purchasable.rb', line 93
def price=(value)
if value.kind_of?(Integer)
super
elsif value.kind_of?(String) && !value.include?('.') super
elsif value.blank?
super
else
raise "expected price to be an Integer representing the number of cents. Got: #{value}"
end
end
|
#purchasable_name ⇒ Object
105
106
107
|
# File 'app/models/concerns/acts_as_purchasable.rb', line 105
def purchasable_name
to_s
end
|
#purchased? ⇒ Boolean
122
123
124
|
# File 'app/models/concerns/acts_as_purchasable.rb', line 122
def purchased?
purchased_order_id.present?
end
|
#purchased_after?(date) ⇒ Boolean
149
150
151
152
153
154
|
# File 'app/models/concerns/acts_as_purchasable.rb', line 149
def purchased_after?(date)
return false unless purchased?
return false unless purchased_at.present?
purchased_at >= date
end
|
#purchased_at ⇒ Object
130
131
132
|
# File 'app/models/concerns/acts_as_purchasable.rb', line 130
def purchased_at
self[:purchased_at] || purchased_order.try(:purchased_at)
end
|
#purchased_before?(date) ⇒ Boolean
142
143
144
145
146
147
|
# File 'app/models/concerns/acts_as_purchasable.rb', line 142
def purchased_before?(date)
return false unless purchased?
return false unless purchased_at.present?
purchased_at < date
end
|
#purchased_by?(user) ⇒ Boolean
138
139
140
|
# File 'app/models/concerns/acts_as_purchasable.rb', line 138
def purchased_by?(user)
purchased_orders.any? { |order| order.purchased_by_id == user.id }
end
|
#purchased_download_url ⇒ Object
Override me if this is a digital purchase.
156
157
158
|
# File 'app/models/concerns/acts_as_purchasable.rb', line 156
def purchased_download_url false
end
|
#purchased_or_deferred? ⇒ Boolean
113
114
115
|
# File 'app/models/concerns/acts_as_purchasable.rb', line 113
def purchased_or_deferred?
purchased_order_id.present? || orders.any? { |order| order.purchased? || order.deferred? }
end
|
#purchased_or_deferred_at ⇒ Object
117
118
119
120
|
# File 'app/models/concerns/acts_as_purchasable.rb', line 117
def purchased_or_deferred_at
order = orders.find { |order| order.purchased? } || orders.find { |order| order.deferred? }
order&.purchased_at || order&.deferred_at
end
|
#quantity_enabled? ⇒ Boolean
160
161
162
|
# File 'app/models/concerns/acts_as_purchasable.rb', line 160
def quantity_enabled?
false
end
|
#quantity_remaining ⇒ Object
164
165
166
|
# File 'app/models/concerns/acts_as_purchasable.rb', line 164
def quantity_remaining
quantity_max - quantity_purchased if quantity_enabled?
end
|
#sold_out? ⇒ Boolean
168
169
170
|
# File 'app/models/concerns/acts_as_purchasable.rb', line 168
def sold_out?
quantity_enabled? ? (quantity_remaining <= 0) : false
end
|
#tax_exempt ⇒ Object
109
110
111
|
# File 'app/models/concerns/acts_as_purchasable.rb', line 109
def tax_exempt
self[:tax_exempt] || false
end
|