Permalink
Browse files

Updated to support 0.70.x

  • Loading branch information...
BDQ committed Jun 30, 2011
1 parent d7cd09f commit 80fa8fe97275d4450e915a0d3fe12d73e1d5d76d
@@ -0,0 +1,18 @@
+Admin::VariantsController.class_eval do
+ update.before do
+ params[:variant][:volume_prices_attributes] ||= {}
+ end
+
+ respond_override :update => {:html => {
+ :succes => lambda { redirect_to(@variant.is_master ? volume_prices_admin_product_variant_url(@variant.product, @variant) : collection_url) },
+ :failure => lambda { redirect_to(@variant.is_master ? volume_prices_admin_product_variant_url(@variant.product, @variant) : collection_url) } } }
+
+ def load_resource_instance
+ parent
+ Variant.find(params[:id])
+ end
+
+ def volume_prices
+ @product = @variant.product
+ end
+end
@@ -0,0 +1,10 @@
+LineItem.class_eval do
+ before_update :check_volume_pricing
+
+ private
+ def check_volume_pricing
+ if changed? && changes.keys.include?("quantity")
+ self.price = variant.volume_price(quantity)
+ end
+ end
+end
@@ -0,0 +1,31 @@
+Order.class_eval do
+ # override the add_variant functionality so that we can adjust the price based on possible volume adjustment
+ def add_variant(variant, quantity=1)
+ current_item = contains?(variant)
+ price = variant.volume_price(quantity) # Added
+ if current_item
+ current_item.increment_quantity unless quantity > 1
+ current_item.quantity = (current_item.quantity + quantity) if quantity > 1
+ current_item.price = price # Added
+ current_item.save
+ else
+ current_item = line_items.create(:quantity => quantity)
+ current_item.variant = variant
+ current_item.price = price
+ current_item.save
+ end
+
+ # populate line_items attributes for additional_fields entries
+ # that have populate => [:line_item]
+ Variant.additional_fields.select{|f| !f[:populate].nil? && f[:populate].include?(:line_item) }.each do |field|
+ value = ""
+
+ if field[:only].nil? || field[:only].include?(:variant)
+ value = variant.send(field[:name].gsub(" ", "_").downcase)
+ elsif field[:only].include?(:product)
+ value = variant.product.send(field[:name].gsub(" ", "_").downcase)
+ end
+ current_item.update_attribute(field[:name].gsub(" ", "_").downcase, value)
+ end
+ end
+end
@@ -0,0 +1,13 @@
+Variant.class_eval do
+ has_many :volume_prices, :order => :position, :dependent => :destroy
+ accepts_nested_attributes_for :volume_prices
+
+ # calculates the price based on quantity
+ def volume_price(quantity)
+ volume_prices.each do |price|
+ return price.amount if price.include?(quantity)
+ end
+ self.price
+ end
+
+end
@@ -0,0 +1,11 @@
+Deface::Override.new(:virtual_path => "admin/shared/_product_tabs",
+ :name => "add_volume_pricing_admin_tab",
+ :insert_bottom => "[data-hook='admin_product_tabs']",
+ :partial => "admin/shared/vp_product_tab",
+ :disabled => false)
+
+Deface::Override.new(:virtual_path => "admin/variants/edit",
+ :name => "add_volume_pricing_field_to_variant",
+ :insert_after => "[data-hook='admin_variant_edit_form']",
+ :partial => "admin/variants/edit_fields",
+ :disabled => false)
@@ -1,4 +1,3 @@
- <li<%= ' class="active"' if current == "Volume Pricing" %>>
- <%= link_to t("volume_pricing"), volume_prices_admin_product_variant_url(@product, @product.master) %>
- </li>
-
+<li<%= ' class="active"' if current == "Volume Pricing" %>>
+ <%= link_to t("volume_pricing"), volume_prices_admin_product_variant_url(@product, @product.master) %>
+</li>
@@ -1,6 +1,3 @@
require 'spree_core'
require 'spree_volume_pricing'
-require 'spree_volume_pricing_hooks'
require 'spree_volume_pricing/engine'
-
-
@@ -5,64 +5,10 @@ module SpreeVolumePricing
class Engine < Rails::Engine
def self.activate
-
- Variant.class_eval do
- has_many :volume_prices, :order => :position, :dependent => :destroy
- accepts_nested_attributes_for :volume_prices
-
- # calculates the price based on quantity
- def volume_price(quantity)
- volume_prices.each do |price|
- return price.amount if price.include?(quantity)
- end
- self.price
- end
-
- end
-
- Order.class_eval do
- # override the add_variant functionality so that we can adjust the price based on possible volume adjustment
- def add_variant(variant, quantity=1)
- current_item = contains?(variant)
- price = variant.volume_price(quantity) # Added
- if current_item
- current_item.increment_quantity unless quantity > 1
- current_item.quantity = (current_item.quantity + quantity) if quantity > 1
- current_item.price = price # Added
- current_item.save
- else
- current_item = line_items.create(:quantity => quantity)
- current_item.variant = variant
- current_item.price = price
- current_item.save
- end
-
- # populate line_items attributes for additional_fields entries
- # that have populate => [:line_item]
- Variant.additional_fields.select{|f| !f[:populate].nil? && f[:populate].include?(:line_item) }.each do |field|
- value = ""
-
- if field[:only].nil? || field[:only].include?(:variant)
- value = variant.send(field[:name].gsub(" ", "_").downcase)
- elsif field[:only].include?(:product)
- value = variant.product.send(field[:name].gsub(" ", "_").downcase)
- end
- current_item.update_attribute(field[:name].gsub(" ", "_").downcase, value)
- end
- end
+ Dir.glob(File.join(File.dirname(__FILE__), "../../app/**/*_decorator*.rb")) do |c|
+ Rails.configuration.cache_classes ? require(c) : load(c)
end
- LineItem.class_eval do
- before_update :check_volume_pricing
-
- private
- def check_volume_pricing
- if changed? && changes.keys.include?("quantity")
- self.price = variant.volume_price(quantity)
- end
- end
- end
-
String.class_eval do
def to_range
case self.count('.')
@@ -77,31 +23,10 @@ def to_range
end
end
end
-
- Admin::VariantsController.class_eval do
- update.before do
- params[:variant][:volume_prices_attributes] ||= {}
- end
-
- update.response do |wants|
- wants.html do
- redirect_to object.is_master ? volume_prices_admin_product_variant_url(object.product, object) : collection_url
- end
- end
-
- def object
- @object ||= Variant.find(params[:id])
- end
-
- def volume_prices
- @variant = object
- @product = @variant.product
- end
- end
-
end
config.autoload_paths += %W(#{config.root}/lib)
+
config.to_prepare &method(:activate).to_proc
end
@@ -1,7 +0,0 @@
-class VolumePricingHooks < Spree::ThemeSupport::HookListener
-
- insert_after :admin_product_tabs, :partial => "admin/shared/vp_product_tab"
-
- insert_after :admin_variant_edit_form, :partial => "admin/variants/edit_fields"
-
-end
@@ -17,5 +17,5 @@ Gem::Specification.new do |s|
s.has_rdoc = false
- s.add_dependency('spree_core', '>= 0.30.0.beta1')
+ s.add_dependency('spree_core')
end

0 comments on commit 80fa8fe

Please sign in to comment.