Using resource controller and namespace
This commit is contained in:
@@ -1,10 +0,0 @@
|
||||
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
|
||||
@@ -1,27 +0,0 @@
|
||||
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,11 @@
|
||||
module Spree
|
||||
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
|
||||
end
|
||||
@@ -0,0 +1,29 @@
|
||||
module Spree
|
||||
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
|
||||
end
|
||||
Reference in New Issue
Block a user