Using resource controller and namespace
This commit is contained in:
@@ -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
|
|
||||||
@@ -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
|
|
||||||
15
app/controllers/spree/admin/digitals_controller.rb
Normal file
15
app/controllers/spree/admin/digitals_controller.rb
Normal file
@@ -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
|
||||||
18
app/controllers/spree/digitals_controller.rb
Normal file
18
app/controllers/spree/digitals_controller.rb
Normal file
@@ -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
|
||||||
@@ -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
|
|
||||||
11
app/models/spree/digital.rb
Normal file
11
app/models/spree/digital.rb
Normal file
@@ -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
|
||||||
29
app/models/spree/digital_link.rb
Normal file
29
app/models/spree/digital_link.rb
Normal file
@@ -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
|
||||||
@@ -2,7 +2,7 @@
|
|||||||
<div class="yui-u first">
|
<div class="yui-u first">
|
||||||
<%= form_for(:digital, :url => { :controller => 'digitals', :action => 'create' }, :html => { :multipart => true }) do |f| %>
|
<%= form_for(:digital, :url => { :controller => 'digitals', :action => 'create' }, :html => { :multipart => true }) do |f| %>
|
||||||
<fieldset>
|
<fieldset>
|
||||||
<legend><%= Variant.model_name.human %> "<%= variant.options_text %>"</legend>
|
<legend><%= Spree::Variant.model_name.human %> "<%= variant.options_text %>"</legend>
|
||||||
|
|
||||||
<%= f.field_container :current_file do %>
|
<%= f.field_container :current_file do %>
|
||||||
<strong><%=t 'current_file' %>:</strong><br/>
|
<strong><%=t 'current_file' %>:</strong><br/>
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
<%= render :partial => 'admin/shared/product_sub_menu' %>
|
<%= render :partial => 'spree/admin/shared/product_sub_menu' %>
|
||||||
<%= render :partial => 'admin/shared/product_tabs', :locals => {:current => "Digital Versions"} %>
|
<%= render :partial => 'spree/admin/shared/product_tabs', :locals => {:current => "Digital Versions"} %>
|
||||||
|
|
||||||
<% if @product.has_variants? %>
|
<% if @product.has_variants? %>
|
||||||
|
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
Rails.application.routes.draw do
|
Spree::Core::Engine.routes.draw do
|
||||||
|
|
||||||
namespace :admin do
|
namespace :admin do
|
||||||
resources :products do
|
resources :products do
|
||||||
resources :digitals
|
resources :digitals
|
||||||
|
|||||||
6
db/migrate/20111207121840_rename_digital_to_namespace.rb
Normal file
6
db/migrate/20111207121840_rename_digital_to_namespace.rb
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
class RenameDigitalToNamespace < ActiveRecord::Migration
|
||||||
|
def change
|
||||||
|
rename_table :digitals, :spree_digitals
|
||||||
|
rename_table :digital_links, :spree_digital_links
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
class SpreeDigitalHooks < Spree::ThemeSupport::HookListener
|
class SpreeDigitalHooks < Spree::ThemeSupport::HookListener
|
||||||
Deface::Override.new(:virtual_path => "admin/shared/_product_tabs",
|
Deface::Override.new(:virtual_path => "spree/admin/shared/_product_tabs",
|
||||||
:name => "converted_admin_product_tabs_986577829",
|
:name => "converted_admin_product_tabs_986577829",
|
||||||
:insert_bottom => "[data-hook='admin_product_tabs'], #admin_product_tabs[data-hook]",
|
:insert_bottom => "[data-hook='admin_product_tabs'], #admin_product_tabs[data-hook]",
|
||||||
:text => " <li<%== ' class=\"active\"' if current == \"Digital Versions\" %>>
|
:text => " <li<%== ' class=\"active\"' if current == \"Digital Versions\" %>>
|
||||||
|
|||||||
Reference in New Issue
Block a user