Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: jumph4x/spree_core_charges
base: 886063091c
...
head fork: jumph4x/spree_core_charges
compare: 66f813211d
Checking mergeability… Don't worry, you can still create the pull request.
  • 7 commits
  • 4 files changed
  • 0 commit comments
  • 3 contributors
View
10 app/models/core_charge.rb
@@ -12,12 +12,16 @@ def applicable?
def update!
set_amount
- save!
+ save
end
# Calculates core charges by summing relevant ones
def set_amount
- self.amount = source.variant.product.core_amount * source.quantity
+ self.amount = (source.product.core_amount * source.quantity)
end
-
+
+ def update_order
+
+ end
+
end
View
12 lib/core_charges_hooks.rb
@@ -1,12 +0,0 @@
-class CoreChargesHooks < Spree::ThemeSupport::HookListener
-
- insert_after :admin_product_form_right do
- '<%= f.label :core_amount, t("core_amount") %> <br />
- <%= f.select :core_amount, [nil, 25.0, 50.0, 75.0, 100.0, 150.0, 200.0], :class => "text " %>
- <%= f.error_message_on :core_amount %>'
- end
-
-# insert_after :cart_items do
-# '<%- if @order.core_charges.any? -%><div class="right charge"><h3><%= t("core_charge_total") -%>: <span class="price"><%= number_to_currency(@order.core_charges.map(&:amount).sum) -%></span></h3></div><%- end -%>'
-# end
-end
View
7 lib/core_charges_overrides.rb
@@ -0,0 +1,7 @@
+Deface::Override.new(:virtual_path => "admin/products/_form",
+ :name => "converted_admin_product_form_right",
+ :insert_bottom => "[data-hook='admin_product_form_right'], #admin_product_form_right[data-hook]",
+ :text => "<%= f.label :core_amount, t(\"core_amount\") %> <br />
+ <%= f.select :core_amount, [nil, 25.0, 50.0, 75.0, 100.0, 150.0, 200.0], :class => \"text \" %>
+ <%= f.error_message_on :core_amount %>",
+ :disabled => false)
View
64 lib/spree_core_charges.rb
@@ -1,10 +1,11 @@
require 'spree_core'
-require 'core_charges_hooks'
+require 'core_charges_overrides'
module SpreeCoreCharges
class Engine < Rails::Engine
def self.activate
+
Adjustment.class_eval do
scope :core, lambda { where('label like ?',"#{I18n.t(:core_charge)}%") }
end
@@ -12,11 +13,15 @@ def self.activate
LineItem.class_eval do
def update_adjustment(adjustment, source)
- adjustment.amount = if adjustment.order.line_items.include? source
+ adjustment.amount = if adjustment.order(true).line_items.include? source
calculate_core_charge
else
0
end
+
+ Adjustment.skip_callback :save, :after, :update_order
+ adjustment.save
+ Adjustment.set_callback :save, :after, :update_order
end
def calculate_core_charge
@@ -25,33 +30,52 @@ def calculate_core_charge
self.quantity * product.core_amount
end
- end
+ private
+
+ def create_core_charges
+ return true if order.core_charges.detect{|cc| cc.source_id == id}
+
+ order.core_charges << CoreCharge.create({
+ :label => "#{I18n.t(:core_charge)} [#{variant.sku}]",
+ :source => self,
+ :order => order,
+ :originator => self,
+ :amount => calculate_core_charge
+ })
+ end
+
+ def destroy_core_charges
+ order.core_charges.select{|cc| cc.source_id == id}.map(&:destroy)
+ end
+
+ def update_core_charges
+
+ if self.destroyed?
+ destroy_core_charges
+ elsif
+ create_core_charges
+ end
+
+ end
- Order.register_update_hook('create_core_charges')
+ def update_order
+ update_core_charges if product.core_amount
+
+ # update the order totals, etc.
+
+ order.update!
+ end
+
+ end
Order.class_eval do
has_many :core_charges,
:dependent => :destroy,
- :class_name => 'Adjustment',
+ #:class_name => 'Adjustment',
:conditions => "source_type='LineItem'"
- private
-
- def create_core_charges
- line_items(true).collect{|item| item if item.variant.product.core_amount }.compact.each do |item|
- adjustments << Adjustment.create({
- :label => I18n.t(:core_charge) + " [#{item.variant.sku || item.variant.name}]",
- :source => item,
- :order => self,
- :originator => item,
- :amount => item.calculate_core_charge
- }) unless core_charges.find(:first, :conditions => {:source_id => item.id})
- end
- end
-
end
-
end
config.autoload_paths += %W(#{config.root}/lib)

No commit comments for this range

Something went wrong with that request. Please try again.