Added config to optionally disable deleting digitals on variant deletion
This commit is contained in:
@@ -10,13 +10,12 @@ Spree::Order.class_eval do
|
|||||||
false
|
false
|
||||||
end
|
end
|
||||||
|
|
||||||
# UPGRADE_CHECK
|
# UPGRADE_CHECK this works as of spree 1.1.1; make sure to check the original function on upgrade
|
||||||
# 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)
|
def available_shipping_methods(display_on = nil)
|
||||||
return [] unless ship_address
|
return [] unless ship_address
|
||||||
all_methods = Spree::ShippingMethod.all_available(self, display_on)
|
all_methods = Spree::ShippingMethod.all_available(self, display_on)
|
||||||
puts "ALL METHODS #{all_methods.count} #{display_on}"
|
|
||||||
if self.digital?
|
if self.digital?
|
||||||
all_methods.detect { |m| m.calculator.class == Spree::Calculator::DigitalDelivery }.try { |d| [d] } || all_methods
|
all_methods.detect { |m| m.calculator.class == Spree::Calculator::DigitalDelivery }.try { |d| [d] } || all_methods
|
||||||
else
|
else
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
Spree::Variant.class_eval do
|
Spree::Variant.class_eval do
|
||||||
|
has_many :digitals
|
||||||
has_many :digitals, :dependent => :destroy
|
|
||||||
after_save :destroy_digital, :if => :deleted?
|
after_save :destroy_digital, :if => :deleted?
|
||||||
|
|
||||||
# Is this variant to be downloaded by the customer?
|
# Is this variant to be downloaded by the customer?
|
||||||
@@ -10,11 +9,11 @@ Spree::Variant.class_eval do
|
|||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
# Spree never deleted Digitals, that's why ":dependent => :destroy" won't work on Digital.
|
# :dependent => :destroy needs to be handled manually
|
||||||
# We need to delete the Digital manually here as soon as the Variant is nullified.
|
# spree does not delete variants, just marks them as deleted?
|
||||||
# Otherwise you'll have orphan Digitals (and their attached files!) associated with unused Variants.
|
# optionally keep digitals around for customers who require continued access to their purchases
|
||||||
def destroy_digital
|
def destroy_digital
|
||||||
digitals.map &:destroy
|
digitals.map &:destroy unless Spree::DigitalConfiguration[:keep_digitals]
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,6 +1,12 @@
|
|||||||
module Spree
|
module Spree
|
||||||
class SpreeDigitalConfiguration < Preferences::Configuration
|
class SpreeDigitalConfiguration < Preferences::Configuration
|
||||||
|
# number of times a customer can download a digital file
|
||||||
preference :authorized_clicks, :integer, :default => 3
|
preference :authorized_clicks, :integer, :default => 3
|
||||||
|
|
||||||
|
# number of days after initial purchase the customer can download a file
|
||||||
preference :authorized_days, :integer, :default => 2
|
preference :authorized_days, :integer, :default => 2
|
||||||
|
|
||||||
|
# should digitals be kept around after the associated product is destroyed
|
||||||
|
preference :keep_digitals, :boolean, :default => false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -11,19 +11,26 @@ describe Spree::Variant do
|
|||||||
let(:digital) { @digital }
|
let(:digital) { @digital }
|
||||||
|
|
||||||
it "should destroy associated digitals by default" do
|
it "should destroy associated digitals by default" do
|
||||||
|
# default is false
|
||||||
|
Spree::DigitalConfiguration[:keep_digitals] = false
|
||||||
|
|
||||||
Spree::Digital.count.should == 1
|
Spree::Digital.count.should == 1
|
||||||
variant.digitals.present?.should be_true
|
variant.digitals.present?.should be_true
|
||||||
variant.destroy
|
variant.deleted_at = Time.now
|
||||||
|
variant.deleted?.should be_true
|
||||||
|
variant.save!
|
||||||
expect { digital.reload.present? }.to raise_error
|
expect { digital.reload.present? }.to raise_error
|
||||||
Spree::Digital.count.should == 0
|
Spree::Digital.count.should == 0
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should conditionally keep associated digitals" do
|
it "should conditionally keep associated digitals" do
|
||||||
Spree::DigitalConfiguration[:keep_digitals] = true
|
Spree::DigitalConfiguration[:keep_digitals] = true
|
||||||
|
|
||||||
Spree::Digital.count.should == 1
|
Spree::Digital.count.should == 1
|
||||||
variant.digitals.present?.should be_false
|
variant.digitals.present?.should be_true
|
||||||
variant.destroy
|
variant.deleted_at = Time.now
|
||||||
|
variant.save!
|
||||||
|
variant.deleted?.should be_true
|
||||||
expect { digital.reload.present? }.to_not raise_error
|
expect { digital.reload.present? }.to_not raise_error
|
||||||
Spree::Digital.count.should == 1
|
Spree::Digital.count.should == 1
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -4,14 +4,18 @@ ENV["RAILS_ENV"] = "test"
|
|||||||
require File.expand_path("../dummy/config/environment.rb", __FILE__)
|
require File.expand_path("../dummy/config/environment.rb", __FILE__)
|
||||||
|
|
||||||
require 'rspec/rails'
|
require 'rspec/rails'
|
||||||
|
require 'database_cleaner'
|
||||||
|
|
||||||
# Requires supporting ruby files with custom matchers and macros, etc,
|
# Requires supporting ruby files with custom matchers and macros, etc,
|
||||||
# in spec/support/ and its subdirectories.
|
# in spec/support/ and its subdirectories.
|
||||||
Dir[File.join(File.dirname(__FILE__), "support/**/*.rb")].each {|f| require f }
|
Dir[File.join(File.dirname(__FILE__), "support/**/*.rb")].each {|f| require f }
|
||||||
|
|
||||||
# Requires factories defined in spree_core
|
# Requires factories defined in spree_core
|
||||||
|
require 'spree/core/testing_support/env'
|
||||||
require 'spree/core/testing_support/factories'
|
require 'spree/core/testing_support/factories'
|
||||||
|
|
||||||
|
Dir[File.join(File.dirname(__FILE__), "factories/*.rb")].each {|f| require f }
|
||||||
|
|
||||||
RSpec.configure do |config|
|
RSpec.configure do |config|
|
||||||
# == Mock Framework
|
# == Mock Framework
|
||||||
#
|
#
|
||||||
@@ -28,10 +32,25 @@ RSpec.configure do |config|
|
|||||||
# If you're not using ActiveRecord, or you'd prefer not to run each of your
|
# If you're not using ActiveRecord, or you'd prefer not to run each of your
|
||||||
# examples within a transaction, remove the following line or assign false
|
# examples within a transaction, remove the following line or assign false
|
||||||
# instead of true.
|
# instead of true.
|
||||||
config.use_transactional_fixtures = true
|
config.use_transactional_fixtures = false
|
||||||
end
|
|
||||||
|
|
||||||
Dir[File.join(File.dirname(__FILE__), "factories/*.rb")].each {|f| require f }
|
config.before(:each) do
|
||||||
|
if example.metadata[:js]
|
||||||
|
DatabaseCleaner.strategy = :truncation, { :except => ['spree_countries', 'spree_zones', 'spree_zone_members', 'spree_states', 'spree_roles'] }
|
||||||
|
else
|
||||||
|
DatabaseCleaner.strategy = :transaction
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
config.before(:each) do
|
||||||
|
DatabaseCleaner.start
|
||||||
|
# reset_spree_preferences
|
||||||
|
end
|
||||||
|
|
||||||
|
config.after(:each) do
|
||||||
|
DatabaseCleaner.clean
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# not sure if this really adds anything, but this existed in the intial version of the spree_digital rspec testing
|
# not sure if this really adds anything, but this existed in the intial version of the spree_digital rspec testing
|
||||||
RSpec::Matchers.define :have_valid_factory do |factory_name|
|
RSpec::Matchers.define :have_valid_factory do |factory_name|
|
||||||
|
|||||||
@@ -20,4 +20,5 @@ Gem::Specification.new do |s|
|
|||||||
s.add_development_dependency 'ffaker'
|
s.add_development_dependency 'ffaker'
|
||||||
s.add_development_dependency 'rspec-rails'
|
s.add_development_dependency 'rspec-rails'
|
||||||
s.add_development_dependency 'sqlite3'
|
s.add_development_dependency 'sqlite3'
|
||||||
|
s.add_development_dependency 'database_cleaner'
|
||||||
end
|
end
|
||||||
Reference in New Issue
Block a user