Using resource controller and namespace

This commit is contained in:
Michael Gall
2011-12-08 01:42:35 +11:00
committed by future
parent 02fd4a64d1
commit 4ecf99a4ae
21 changed files with 84 additions and 69 deletions

View File

@@ -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

View File

@@ -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

View 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

View 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

View File

@@ -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

View File

@@ -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

View 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

View 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

View File

@@ -2,7 +2,7 @@
<div class="yui-u first">
<%= form_for(:digital, :url => { :controller => 'digitals', :action => 'create' }, :html => { :multipart => true }) do |f| %>
<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 %>
<strong><%=t 'current_file' %>:</strong><br/>

View File

@@ -1,5 +1,5 @@
<%= render :partial => 'admin/shared/product_sub_menu' %>
<%= render :partial => 'admin/shared/product_tabs', :locals => {:current => "Digital Versions"} %>
<%= render :partial => 'spree/admin/shared/product_sub_menu' %>
<%= render :partial => 'spree/admin/shared/product_tabs', :locals => {:current => "Digital Versions"} %>
<% if @product.has_variants? %>

View File

@@ -1,5 +1,4 @@
Rails.application.routes.draw do
Spree::Core::Engine.routes.draw do
namespace :admin do
resources :products do
resources :digitals

View 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

View File

@@ -1,5 +1,5 @@
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",
:insert_bottom => "[data-hook='admin_product_tabs'], #admin_product_tabs[data-hook]",
:text => " <li<%== ' class=\"active\"' if current == \"Digital Versions\" %>>