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

Returns:

  • (Boolean)


105
106
107
# File 'app/models/concerns/acts_as_purchasable.rb', line 105

def deferred?
  deferred_orders.any? { |order| order.deferred? }
end

#price=(value) ⇒ Object

If I have a column type of Integer, and I’m passed a non-Integer, convert it here



81
82
83
84
85
86
87
88
89
90
91
# File 'app/models/concerns/acts_as_purchasable.rb', line 81

def price=(value)
  if value.kind_of?(Integer)
    super
  elsif value.kind_of?(String) && !value.include?('.') # Looks like an integer
    super
  elsif value.blank?
    super
  else
    raise "expected price to be an Integer representing the number of cents. Got: #{value}"
  end
end

#purchasable_nameObject



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

def purchasable_name
  to_s
end

#purchased?Boolean

Returns:

  • (Boolean)


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

def purchased?
  purchased_order_id.present?
end

#purchased_atObject



109
110
111
# File 'app/models/concerns/acts_as_purchasable.rb', line 109

def purchased_at
  self[:purchased_at] || purchased_order.try(:purchased_at)
end

#purchased_by?(user) ⇒ Boolean

Returns:

  • (Boolean)


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

def purchased_by?(user)
  purchased_orders.any? { |order| order.purchased_by_id == user.id }
end

#purchased_download_urlObject

Override me if this is a digital purchase.



117
118
119
# File 'app/models/concerns/acts_as_purchasable.rb', line 117

def purchased_download_url # Override me if this is a digital purchase.
  false
end

#quantity_enabled?Boolean

Returns:

  • (Boolean)


121
122
123
# File 'app/models/concerns/acts_as_purchasable.rb', line 121

def quantity_enabled?
  false
end

#quantity_remainingObject



125
126
127
# File 'app/models/concerns/acts_as_purchasable.rb', line 125

def quantity_remaining
  quantity_max - quantity_purchased if quantity_enabled?
end

#sold_out?Boolean

Returns:

  • (Boolean)


129
130
131
# File 'app/models/concerns/acts_as_purchasable.rb', line 129

def sold_out?
  quantity_enabled? ? (quantity_remaining <= 0) : false
end

#tax_exemptObject



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

def tax_exempt
  self[:tax_exempt] || false
end