Clone order cycles

This commit is contained in:
Rohan Mitchell
2013-08-22 16:53:11 +10:00
parent 3d32e6480b
commit f1fa90d7c3
8 changed files with 135 additions and 2 deletions

View File

@@ -61,6 +61,13 @@ module Admin
end
end
def clone
@order_cycle = OrderCycle.find params[:order_cycle_id]
@order_cycle.clone!
redirect_to main_app.admin_order_cycles_path, :notice => "Your order cycle #{@order_cycle.name} has been cloned."
end
protected
def collection
OrderCycle.managed_by(spree_current_user)

View File

@@ -21,7 +21,32 @@ class Exchange < ActiveRecord::Base
scope :to_enterprises, lambda { |enterprises| where('exchanges.receiver_id IN (?)', enterprises) }
scope :with_variant, lambda { |variant| joins(:exchange_variants).where('exchange_variants.variant_id = ?', variant) }
def clone!(new_order_cycle)
exchange = self.dup
exchange.order_cycle = new_order_cycle
exchange.enterprise_fee_ids = self.enterprise_fee_ids
exchange.variant_ids = self.variant_ids
exchange.save!
exchange
end
def incoming?
receiver == order_cycle.coordinator
end
def to_h(core=false)
h = attributes.merge({ 'variant_ids' => variant_ids, 'enterprise_fee_ids' => enterprise_fee_ids })
h.reject! { |k| %w(id order_cycle_id created_at updated_at).include? k } if core
h
end
def eql?(e)
if e.respond_to? :to_h
self.to_h(true) == e.to_h(true)
else
super e
end
end
end

View File

@@ -28,6 +28,17 @@ class OrderCycle < ActiveRecord::Base
end
}
def clone!
oc = self.dup
oc.name = "COPY OF #{oc.name}"
oc.orders_open_at = oc.orders_close_at = nil
oc.coordinator_fee_ids = self.coordinator_fee_ids
oc.save!
self.exchanges.each { |e| e.clone!(oc) }
oc.reload
end
def suppliers
self.exchanges.where(:receiver_id => self.coordinator).map(&:sender).uniq
end

View File

@@ -17,7 +17,7 @@
%th Suppliers
%th Distributors
%th Products
%th
%th.actions
%tbody
= f.fields_for :collection do |order_cycle_form|
- order_cycle = order_cycle_form.object
@@ -37,5 +37,6 @@
%td.products
- order_cycle.variants.each do |v|
= image_tag(v.images.first.attachment.url(:mini)) if v.images.present?
%td
%td.actions
= link_to '', main_app.admin_order_cycle_clone_path(order_cycle), class: 'clone-order-cycle icon-copy no-text'
= f.submit 'Update'

View File

@@ -19,6 +19,7 @@ Openfoodweb::Application.routes.draw do
namespace :admin do
resources :order_cycles do
post :bulk_update, :on => :collection, :as => :bulk_update
get :clone
end
resources :enterprises do

View File

@@ -343,6 +343,22 @@ feature %q{
OrderCycle.all.map { |oc| oc.orders_close_at.sec }.should == [1, 3, 5]
end
scenario "cloning an order cycle" do
# Given an order cycle
oc = create(:order_cycle)
# When I clone it
login_to_admin_section
click_link 'Order Cycles'
first('a.clone-order-cycle').click
flash_message.should == "Your order cycle #{oc.name} has been cloned."
# Then I should have clone of the order cycle
occ = OrderCycle.last
occ.name.should == "COPY OF #{oc.name}"
end
context 'as an Enterprise user' do
let(:supplier1) { create(:supplier_enterprise, name: 'First Supplier') }

View File

@@ -97,4 +97,61 @@ describe Exchange do
Exchange.with_variant(v).should == [ex]
end
end
it "clones itself" do
oc = create(:order_cycle)
new_oc = create(:simple_order_cycle)
ex1 = oc.exchanges.last
ex2 = ex1.clone! new_oc
ex1.eql?(ex2).should be_true
end
describe "converting to hash" do
let(:oc) { create(:order_cycle) }
let(:exchange) do
exchange = oc.exchanges.last
exchange.payment_enterprise = Enterprise.last
exchange.save!
exchange
end
it "converts to a hash" do
exchange.to_h.should ==
{'id' => exchange.id, 'order_cycle_id' => oc.id,
'sender_id' => exchange.sender_id, 'receiver_id' => exchange.receiver_id,
'payment_enterprise_id' => exchange.payment_enterprise_id, 'variant_ids' => exchange.variant_ids,
'enterprise_fee_ids' => exchange.enterprise_fee_ids,
'pickup_time' => exchange.pickup_time, 'pickup_instructions' => exchange.pickup_instructions,
'created_at' => exchange.created_at, 'updated_at' => exchange.updated_at}
end
it "converts to a hash of core attributes only" do
exchange.to_h(true).should ==
{'sender_id' => exchange.sender_id, 'receiver_id' => exchange.receiver_id,
'payment_enterprise_id' => exchange.payment_enterprise_id, 'variant_ids' => exchange.variant_ids,
'enterprise_fee_ids' => exchange.enterprise_fee_ids,
'pickup_time' => exchange.pickup_time, 'pickup_instructions' => exchange.pickup_instructions}
end
end
describe "comparing equality" do
it "compares Exchanges using to_h" do
e1 = Exchange.new
e2 = Exchange.new
e1.stub(:to_h) { {'sender_id' => 456} }
e2.stub(:to_h) { {'sender_id' => 456} }
e1.eql?(e2).should be_true
end
it "compares other objects using super" do
exchange = Exchange.new
exchange_fee = ExchangeFee.new
exchange.eql?(exchange_fee).should be_false
end
end
end

View File

@@ -141,6 +141,21 @@ describe OrderCycle do
end
end
it "clones itself" do
oc = create(:order_cycle)
occ = oc.clone!
occ = OrderCycle.last
occ.name.should == "COPY OF #{oc.name}"
occ.orders_open_at.should be_nil
occ.orders_close_at.should be_nil
occ.coordinator.should == oc.coordinator
occ.coordinator_fee_ids.should == oc.coordinator_fee_ids
(0..occ.exchanges.count).all? { |i| occ.exchanges[i].eql? oc.exchanges[i] }.should be_true
end
describe "creating adjustments for a line item" do
let(:oc) { OrderCycle.new }
let(:line_item) { double(:line_item, variant: 123) }