diff --git a/README.textile b/README.textile
index a6e7dfa..067b7d6 100644
--- a/README.textile
+++ b/README.textile
@@ -2,19 +2,17 @@ h1. Spree Digital
This is a spree extension to enable downloadable products. The design goal is to keep it robust. It should survive future spree versions out of the box.
-NOTE: This is still under development, but since things were falling into place, I thought I'd publish it.
+The master branch is compatible with Spree 1.1.x, <= 1.0 versions are referenced in the Versionfile.
-The master branch may work with spree 1.0, but the latest confirmed working version if referenced in the Versionfile.
-
-The idea is simple. You attach a file to a Product (or a Variant of this Product) and when people buy it, they will receive a link via email where they can download it once. There are a few assumptions that spree_digital (currently) makes and it's important to be aware of them. These might change, but since I programmed digital_spree over night, this is what you get ;)
+The idea is simple. You attach a file to a Product (or a Variant of this Product) and when people buy it, they will receive a link via email where they can download it once. There are a few assumptions that spree_digital (currently) makes and it's important to be aware of them.
* The table structure of spree_core is not touched. Spree_digital lives parallel to spree_core and does not do any changes to your existing database, except adding two new tables.
* The download links will be sent via email in the order confirmation (or "resend" from the admin section). The links do *not* appear in the order "overview" that the customer sees.
* There should only be "live" payments. Once the order is checked-out, the download links will immediately be sent (i.e. in the order confirmation).
-* You should define a shipping method called "download" and it should be cost-free. Spree_digital will use that one when all products in the cart are digital
+* You should create a ShippingMethod based on the Digital Delivery calculator type. The default cost for digital delivery is 0, but you can define a flat rate (creating a per-item digital delivery fee would be possible as well).
* One may buy several items of the same digital product in one cart. The customer will simply receive several links by doing so. This allows customer's to legally purchase multiple copies of the same product and maybe give one away to a friend.
-* The links will only work 3 times (but we tell the customer that it works only once). After 24 hours the links are deactivated. This should keep you reasonably free from complaints of people who are not capable of appropriately clicking on a link. They can try a whole day long, 3 times per link. The file should really be downloadable for weird customers. I understand that this is a little bit security by obscurity, but it's better than other solutions that I've seen.
-* The file @views/order_mailer/confirm_email.text.erb@ is the only thing that should need customization. But since I assume you do that anyway, it doesn't hurt to do it when you're using spree_digital. The reason is that the download links are added to the confirmation email to the customer.
+* The links will only work 3 times (but we tell the customer that it works only once). After 24 hours the links are deactivated. This should keep you reasonably free from complaints of people who are not capable of appropriately clicking on a link. They can try a whole day long, 3 times per link. The file should really be downloadable for weird customers. I understand that this is a little bit security by obscurity, but it's better than other solutions that I've seen. The number of clicks and days the links are valid for is customizable via preferences.
+* The file @views/order_mailer/confirm_email.text.erb@ is the only thing that should need customization. But since I assume you do that anyway, it doesn't hurt to do it when you're using spree_digital. The reason is that the download links are added to the confirmation email to the customer. "http://github.com/iloveitaly/spree-html-email":http://github.com/iloveitaly/spree-html-email also supports spree_digital.
* A purchased product can be downloaded even if you disable the product immediately. You would have to remove the attached file in your admin section to prevent people from downloading purchased products.
* File are uploaded to @rails_root/private@. Make sure it's symlinked in case you're using Capistrano.
* We use send_file to send the files on download. Yes, it goes through the entire stack right now.
@@ -40,7 +38,7 @@ This should be all there is to do.
h3. Important!
-You should go to the spree admin section and create a shipping method that has the word @download@ somehow in its name (it should be cost-free, but it doesn't have to). It will be detected by spree_digital. Otherwise your customer will be forced to choose something like "UPS" even if they purchase only downloadable products.
+You should create a ShippingMethod based on the Digital Delivery calculator type. It will be detected by spree_digital. Otherwise your customer will be forced to choose something like "UPS" even if they purchase only downloadable products.
h2. Usage
@@ -87,6 +85,11 @@ Bring up the test application (you only need to do this whenever you fiddle arou
This link may be very helpful to you: "http://github.com/spree/spree":http://github.com/spree/spree
+h3. Authors
+
+* "iloveitaly":http://github.com/iloveitaly/
+* "funkensturm":http://github.com/funkensturm
+
h3. License
Copyright (c) 2011 funkensturm.
diff --git a/app/models/spree/calculator/digital_delivery.rb b/app/models/spree/calculator/digital_delivery.rb
new file mode 100644
index 0000000..0befa3a
--- /dev/null
+++ b/app/models/spree/calculator/digital_delivery.rb
@@ -0,0 +1,18 @@
+# https://github.com/spree/spree/issues/1439
+require_dependency 'spree/calculator'
+
+module Spree
+ class Calculator::DigitalDelivery < Calculator::FlatRate
+ def self.description
+ I18n.t(:digital_delivery)
+ end
+
+ def compute(object=nil)
+ self.preferred_amount
+ end
+
+ def available?(order)
+ order.digital?
+ end
+ end
+end
diff --git a/app/models/spree/order_decorator.rb b/app/models/spree/order_decorator.rb
index d634d70..5ac00de 100644
--- a/app/models/spree/order_decorator.rb
+++ b/app/models/spree/order_decorator.rb
@@ -1,26 +1,25 @@
Spree::Order.class_eval do
-
- # Are all products/variants of this Order to be downloaded by the customer?
+ # all products are digital
def digital?
line_items.map { |item| return false unless item.digital? }
true
end
- # Is at least one product/variant digital?
def some_digital?
line_items.map { |item| return true if item.digital? }
false
end
-
- # Determine which method to use for shipping of digital products.
- def digital_shipping_method
- rates = rate_hash
- # If there is a shipping method has "Download" in its name then we take that one.
- rates.each { |rate| return rate if rate[:name].downcase.include?('download') }
- # Other than that, we take the first one that we find that doesn't cost anything.
- rates.each { |rate| return rate if rate[:cost] == 0 }
- # Well, at this point we have a problem. No shipping method is cost-free or called "download".
- nil
- end
+ # TODO this works as of spree 1.1.1; make sure to check the original function on upgrade
+
+ def available_shipping_methods(display_on = nil)
+ return [] unless ship_address
+ all_methods = Spree::ShippingMethod.all_available(self, display_on)
+
+ if self.digital?
+ all_methods.detect { |m| m.calculator.class == Spree::Calculator::DigitalDelivery }.try { |d| [d] } || all_methods
+ else
+ all_methods
+ end
+ end
end
diff --git a/app/models/spree/product_decorator.rb b/app/models/spree/product_decorator.rb
index d50ab3d..57ebbe7 100644
--- a/app/models/spree/product_decorator.rb
+++ b/app/models/spree/product_decorator.rb
@@ -1,4 +1,3 @@
-
Spree::Product.class_eval do
has_many :digitals, :through => :variants_including_master
end
\ No newline at end of file
diff --git a/app/overrides/add_digital_downloads_to_invoice.rb b/app/overrides/add_digital_downloads_to_invoice.rb
new file mode 100644
index 0000000..4ae7fae
--- /dev/null
+++ b/app/overrides/add_digital_downloads_to_invoice.rb
@@ -0,0 +1,14 @@
+Deface::Override.new(:virtual_path => "spree/shared/_order_details",
+ :name => "add_digital_downloads_to_invoice",
+ :insert_bottom => "td[data-hook='order_item_description']",
+ :text => %q{
+<% if order.state == 'complete' and item.variant.digital? %>
+ <%= content_tag(:p, :class => 'download_links') do %>
+ <% item.digital_links.each do |digital_link| %>
+ <% format = File.extname(digital_link.digital.attachment.path).downcase %>
+ <%= link_to t(:digital_download, :type => t("digital_format#{format}")), digital_url(:host => Spree::Config.get(:site_url), :secret => digital_link.secret), :class => "btn #{format}" %>
+ <% end %>
+ <% end %>
+<% end %>
+ },
+ :disabled => false)
diff --git a/app/overrides/modify_shipping_options_display.rb b/app/overrides/modify_shipping_options_display.rb
deleted file mode 100644
index 69cf011..0000000
--- a/app/overrides/modify_shipping_options_display.rb
+++ /dev/null
@@ -1,39 +0,0 @@
-Deface::Override.new(:virtual_path => "spree/checkout/_delivery",
- :name => "modify_shipping_options_display",
- :replace_contents => "#shipping_method #methods p.radios",
- :original => %q{
- <% @order.rate_hash.each do |shipping_method| %>
-
- <% end %>
- },
- :text => %q{
-<% if @order.digital? && @order.digital_shipping_method.present? %>
-
-<% else %>
-<% filtered_rate_hash = @order.rate_hash.select { |m| !(@order.digital_shipping_method && m[:id] == @order.digital_shipping_method[:id]) } %>
-<% if filtered_rate_hash.count > 0 %>
-<% filtered_rate_hash.each do |shipping_method| %>
-
-<% end %>
-<% else %>
-<%= t :no_shipping_methods %>
-<% end %>
-<% end %>
-})
\ No newline at end of file
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 17ed8bc..05f1758 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -5,7 +5,12 @@ en:
delete_file: Delete this file
broken_file: Warning! this file is broken
delete_file_cofirmation: Are you sure you want to delete the file %{filename}?
- digital_shipping: Delivery per email to %{email}
-
+ digital_delivery: Digital Delivery
+ digital_download: Download %{type} ↓
+ digital_download_links: Digital Download Links
+ digital_format:
+ mp3: Audio MP3
+ mobi: Kindle eBook
+ epub: ePub eBook
spree_digital:
upload: Upload
\ No newline at end of file
diff --git a/lib/spree_digital/engine.rb b/lib/spree_digital/engine.rb
index d4cd82e..5aea332 100644
--- a/lib/spree_digital/engine.rb
+++ b/lib/spree_digital/engine.rb
@@ -4,14 +4,13 @@ module SpreeDigital
config.autoload_paths += %W(#{config.root}/lib)
- # Use RSpec for tests
- config.generators do |g|
- g.test_framework :rspec
- end
-
initializer "spree.spree_digital.preferences", :after => "spree.environment" do |app|
Spree::DigitalConfiguration = Spree::SpreeDigitalConfiguration.new
end
+
+ initializer "spree.register.digital_shipping", :after => 'spree.register.calculators' do |app|
+ app.config.spree.calculators.shipping_methods << Spree::Calculator::DigitalDelivery
+ end
def self.activate
Dir.glob(File.join(File.dirname(__FILE__), "../../app/**/*_decorator*.rb")) do |c|
diff --git a/spec/models/order_spec.rb b/spec/models/order_spec.rb
index b361524..9600aa0 100644
--- a/spec/models/order_spec.rb
+++ b/spec/models/order_spec.rb
@@ -49,6 +49,20 @@ describe Spree::Order do
order.digital?.should be_false
end
end
+
+ context "digital shipping" do
+ before do
+ # TODO create digital shipping factory
+ end
+
+ it "should only offer digital shipping if all items are digital" do
+
+ end
+
+ it "should not offer digital shipping if only some items are digital" do
+
+ end
+ end
end
diff --git a/spree_digital.gemspec b/spree_digital.gemspec
index aa17873..b0325e0 100644
--- a/spree_digital.gemspec
+++ b/spree_digital.gemspec
@@ -1,9 +1,9 @@
Gem::Specification.new do |s|
s.platform = Gem::Platform::RUBY
s.name = 'spree_digital'
- s.version = '1.0.0'
+ s.version = '1.1.0'
s.summary = ''
- s.description = 'This gem is supposed to be used in connection with spree_core. It was tested with spree_core 0.66.99 but it might work with newer versions as well.'
+ s.description = 'Add digital download functionality to spree'
s.author = 'funkensturm.'
s.homepage = 'http://www.funkensturm.com'
s.files = `git ls-files`.split("\n")
@@ -11,7 +11,7 @@ Gem::Specification.new do |s|
s.require_path = 'lib'
s.requirements << 'none'
s.required_ruby_version = '>= 1.8.7'
- s.add_dependency('spree_core', '>= 1.0.0')
+ s.add_dependency('spree_core', '>= 1.1.0')
# test suite
s.add_development_dependency 'shoulda-matchers'