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

Defined Under Namespace

Modules: Base, ClassMethods

Instance Method Summary collapse

Instance Method Details

#deferred?Boolean

Returns:

  • (Boolean)


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

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



78
79
80
81
82
83
84
85
86
87
88
# File 'app/models/concerns/acts_as_purchasable.rb', line 78

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



90
91
92
# File 'app/models/concerns/acts_as_purchasable.rb', line 90

def purchasable_name
  to_s
end

#purchased?Boolean

Returns:

  • (Boolean)


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

def purchased?
  purchased_order_id.present?
end

#purchased_atObject



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

def purchased_at
  purchased_order.try(:purchased_at)
end

#purchased_by?(user) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#purchased_download_urlObject

Override me if this is a digital purchase.



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

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

#quantity_enabled?Boolean

Returns:

  • (Boolean)


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

def quantity_enabled?
  false
end

#quantity_remainingObject



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

def quantity_remaining
  quantity_max - quantity_purchased if quantity_enabled?
end

#sold_out?Boolean

Returns:

  • (Boolean)


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

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

#tax_exemptObject



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

def tax_exempt
  self[:tax_exempt] || false
end