forked from jseifer/spree-s3-download
/
s3_download_extension.rb
51 lines (41 loc) · 1.36 KB
/
s3_download_extension.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
class S3DownloadExtension < Spree::Extension
version "1.0"
description "S3 Download Extension"
url "http://www.railsenvy.com"
def self.require_gems(config)
config.gem 'right_aws'
end
# Register the s3 extension module for product class to have a clean activation of it.
module ProductS3Extension
def self.included(base)
base.has_many :s3_products
end
end
def activate
Product.send(:include, ProductS3Extension)
User.class_eval do
def purchased_products
orders.completed.map{|o| o.line_items.map{|l| l.variant.product}}.flatten
end
def s3_products
purchased_products.select{|p| !p.s3_products.empty?}
end
end
Order.class_eval do
named_scope :completed, {
:include => {:line_items => {:variant => :product}}, # :conditions => {:status => Order::Status::PAID}, We're going to trust the user here.
:order => 'created_at desc'
}
named_scope :completed_bare, {
:conditions => {:status => :checkout_complete},
:order => 'created_at desc'
}
end
Admin::ProductsController.class_eval do
before_filter :add_s3_product_admin_tab
def add_s3_product_admin_tab
@product_admin_tabs << {:name => 'S3 Downloadables', :url => 'admin_product_s3_products_url'}
end
end
end
end