diff --git a/app/controllers/admin/digitals_controller.rb b/app/controllers/admin/digitals_controller.rb deleted file mode 100644 index f30d3a6..0000000 --- a/app/controllers/admin/digitals_controller.rb +++ /dev/null @@ -1,10 +0,0 @@ -class Admin::DigitalsController < Spree::Admin::ResourceController - - index.before do - @product = Product.find_by_permalink(params[:product_id]) - end - - create.wants.html { redirect_to admin_product_digitals_url(@product) } - destroy.wants.html { redirect_to admin_product_digitals_url(@product) } - -end diff --git a/app/controllers/digitals_controller.rb b/app/controllers/digitals_controller.rb deleted file mode 100644 index 4a42470..0000000 --- a/app/controllers/digitals_controller.rb +++ /dev/null @@ -1,16 +0,0 @@ -class DigitalsController < Spree::BaseController - - ssl_required :show - - def show - link = DigitalLink.find_by_secret(params[:secret]) - if link.present? and link.digital.attachment.present? - attachment = link.digital.attachment - if link.authorize! and File.file?(attachment.path) - send_file attachment.path :filename => attachment.original_filename, :type => attachment.content_type and return - end - end - render :unauthorized - end - -end diff --git a/app/controllers/spree/admin/digitals_controller.rb b/app/controllers/spree/admin/digitals_controller.rb new file mode 100644 index 0000000..56d79f7 --- /dev/null +++ b/app/controllers/spree/admin/digitals_controller.rb @@ -0,0 +1,15 @@ +module Spree + class Admin::DigitalsController < Spree::Admin::ResourceController + belongs_to "spree/product", find_by: :permalink + + def load_resource + @object = @product = Product.find_by_permalink(params[:product_id]) + end + + + protected + def location_after_save + admin_product_digitals_url(@product) + end + end +end \ No newline at end of file diff --git a/app/controllers/spree/digitals_controller.rb b/app/controllers/spree/digitals_controller.rb new file mode 100644 index 0000000..8e9d55d --- /dev/null +++ b/app/controllers/spree/digitals_controller.rb @@ -0,0 +1,18 @@ +module Spree + class DigitalsController < Spree::BaseController + + ssl_required :show + + def show + link = DigitalLink.find_by_secret(params[:secret]) + if link.present? and link.digital.attachment.present? + attachment = link.digital.attachment + if link.authorize! and File.file?(attachment.path) + send_file attachment.path :filename => attachment.original_filename, :type => attachment.content_type and return + end + end + render :unauthorized + end + + end +end \ No newline at end of file diff --git a/app/models/digital.rb b/app/models/digital.rb deleted file mode 100644 index 79d8c0d..0000000 --- a/app/models/digital.rb +++ /dev/null @@ -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 \ No newline at end of file diff --git a/app/models/digital_link.rb b/app/models/digital_link.rb deleted file mode 100644 index 813ae00..0000000 --- a/app/models/digital_link.rb +++ /dev/null @@ -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 \ No newline at end of file diff --git a/app/models/spree/digital.rb b/app/models/spree/digital.rb new file mode 100644 index 0000000..d1d4bd1 --- /dev/null +++ b/app/models/spree/digital.rb @@ -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 \ No newline at end of file diff --git a/app/models/spree/digital_link.rb b/app/models/spree/digital_link.rb new file mode 100644 index 0000000..797d9ba --- /dev/null +++ b/app/models/spree/digital_link.rb @@ -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 \ No newline at end of file diff --git a/app/models/line_item_decorator.rb b/app/models/spree/line_item_decorator.rb similarity index 100% rename from app/models/line_item_decorator.rb rename to app/models/spree/line_item_decorator.rb diff --git a/app/models/order_decorator.rb b/app/models/spree/order_decorator.rb similarity index 100% rename from app/models/order_decorator.rb rename to app/models/spree/order_decorator.rb diff --git a/app/models/variant_decorator.rb b/app/models/spree/variant_decorator.rb similarity index 100% rename from app/models/variant_decorator.rb rename to app/models/spree/variant_decorator.rb diff --git a/app/views/admin/digitals/_digital.html.erb b/app/views/spree/admin/digitals/_digital.html.erb similarity index 100% rename from app/views/admin/digitals/_digital.html.erb rename to app/views/spree/admin/digitals/_digital.html.erb diff --git a/app/views/admin/digitals/_form.html.erb b/app/views/spree/admin/digitals/_form.html.erb similarity index 91% rename from app/views/admin/digitals/_form.html.erb rename to app/views/spree/admin/digitals/_form.html.erb index 9a90456..09662da 100644 --- a/app/views/admin/digitals/_form.html.erb +++ b/app/views/spree/admin/digitals/_form.html.erb @@ -2,7 +2,7 @@