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
159
160
161
|
# File 'app/models/concerns/acts_as_purchasable.rb', line 159
def deferred?
deferred_orders.any? { |order| order.deferred? }
end
|
#deferred_at ⇒ Object
167
168
169
|
# File 'app/models/concerns/acts_as_purchasable.rb', line 167
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
126
127
128
129
130
131
132
133
134
135
136
|
# File 'app/models/concerns/acts_as_purchasable.rb', line 126
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
138
139
140
|
# File 'app/models/concerns/acts_as_purchasable.rb', line 138
def purchasable_name
to_s
end
|
#purchased? ⇒ Boolean
155
156
157
|
# File 'app/models/concerns/acts_as_purchasable.rb', line 155
def purchased?
purchased_order_id.present?
end
|
#purchased_after?(date) ⇒ Boolean
182
183
184
185
186
187
|
# File 'app/models/concerns/acts_as_purchasable.rb', line 182
def purchased_after?(date)
return false unless purchased?
return false unless purchased_at.present?
purchased_at >= date
end
|
#purchased_at ⇒ Object
163
164
165
|
# File 'app/models/concerns/acts_as_purchasable.rb', line 163
def purchased_at
self[:purchased_at] || purchased_order.try(:purchased_at)
end
|
#purchased_before?(date) ⇒ Boolean
175
176
177
178
179
180
|
# File 'app/models/concerns/acts_as_purchasable.rb', line 175
def purchased_before?(date)
return false unless purchased?
return false unless purchased_at.present?
purchased_at < date
end
|
#purchased_by?(user) ⇒ Boolean
171
172
173
|
# File 'app/models/concerns/acts_as_purchasable.rb', line 171
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.
189
190
191
|
# File 'app/models/concerns/acts_as_purchasable.rb', line 189
def purchased_download_url false
end
|
#purchased_or_deferred? ⇒ Boolean
146
147
148
|
# File 'app/models/concerns/acts_as_purchasable.rb', line 146
def purchased_or_deferred?
purchased_order_id.present? || orders.any? { |order| order.purchased? || order.deferred? }
end
|
#purchased_or_deferred_at ⇒ Object
150
151
152
153
|
# File 'app/models/concerns/acts_as_purchasable.rb', line 150
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
193
194
195
|
# File 'app/models/concerns/acts_as_purchasable.rb', line 193
def quantity_enabled?
false
end
|
#quantity_remaining ⇒ Object
197
198
199
|
# File 'app/models/concerns/acts_as_purchasable.rb', line 197
def quantity_remaining
quantity_max - quantity_purchased if quantity_enabled?
end
|
#sold_out? ⇒ Boolean
201
202
203
|
# File 'app/models/concerns/acts_as_purchasable.rb', line 201
def sold_out?
quantity_enabled? ? (quantity_remaining <= 0) : false
end
|
#tax_exempt ⇒ Object
142
143
144
|
# File 'app/models/concerns/acts_as_purchasable.rb', line 142
def tax_exempt
self[:tax_exempt] || false
end
|