boom
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
class Digital < ActiveRecord::Base
|
||||
|
||||
belongs_to :variant
|
||||
has_many :digital_links, :dependent => :destroy
|
||||
|
||||
has_attached_file :attachment, :path => ":rails_root/private/digitals/:id/:basename.:extension"
|
||||
|
||||
# TODO: Limit the attachment to one single file. Paperclip supports many by default :/
|
||||
|
||||
end
|
||||
@@ -0,0 +1,27 @@
|
||||
class DigitalLink < ActiveRecord::Base
|
||||
|
||||
belongs_to :digital
|
||||
belongs_to :line_item
|
||||
|
||||
before_validation :set_defaults, :on => :create
|
||||
|
||||
# Can this link stil be used? It is valid if it's less than 24 hours old and was not accessed more than 3 times
|
||||
def authorizable?
|
||||
self.created_at > 1.day.ago and self.access_counter < 3
|
||||
end
|
||||
|
||||
# This method should be called when a download is initiated.
|
||||
# It returns +true+ or +false+ depending on whether the authorization is granted.
|
||||
def authorize!
|
||||
authorizable? && increment!(:access_counter) ? true : false
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# Populating the secret automatically and zero'ing the access_counter (otherwise it might turn out to be NULL)
|
||||
def set_defaults
|
||||
self.secret = SecureRandom.hex(15)
|
||||
self.access_counter = 0
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,22 @@
|
||||
LineItem.class_eval do
|
||||
|
||||
has_many :digital_links
|
||||
|
||||
after_save :create_digital_links, :if => :digital?
|
||||
|
||||
# Is this item digital?
|
||||
def digital?
|
||||
variant.digital?
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# Create the download link for this item if it is digital.
|
||||
def create_digital_links
|
||||
digital_links.delete_all
|
||||
self.quantity.times do
|
||||
digital_links.create!(:digital => variant.digital)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,20 @@
|
||||
Order.class_eval do
|
||||
|
||||
# Are all products/variants of this Order to be downloaded by the customer?
|
||||
def digital?
|
||||
line_items.map { |item| return false unless item.digital? }
|
||||
true
|
||||
end
|
||||
|
||||
# Determine which method to use for shipping of digital products.
|
||||
def digital_shipping_method
|
||||
rates = rate_hash
|
||||
# If there is a shipping method has "Download" in its name then we take that one.
|
||||
rates.each { |rate| return rate if rate[:name].downcase.include?('download') }
|
||||
# Other than that, we take the first one that we find that doesn't cost anything.
|
||||
rates.each { |rate| return rate if rate[:cost] == 0 }
|
||||
# Well, at this point we have a problem. No shipping method is cost-free or called "download".
|
||||
nil
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,20 @@
|
||||
Variant.class_eval do
|
||||
|
||||
has_one :digital, :dependent => :destroy
|
||||
after_save :destroy_digital, :if => :deleted?
|
||||
|
||||
# Is this variant to be downloaded by the customer?
|
||||
def digital?
|
||||
digital.present?
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# Spree never deleted Digitals, that's why ":dependent => :destroy" won't work on Digital.
|
||||
# We need to delete the Digital manually here as soon as the Variant is nullified.
|
||||
# Otherwise you'll have orphan Digitals (and their attached files!) associated with unused Variants.
|
||||
def destroy_digital
|
||||
digital.destroy
|
||||
end
|
||||
|
||||
end
|
||||
Reference in New Issue
Block a user