Skip to content

Commit

Permalink
Merge pull request #5570 from luisramos0/ship_methods_form
Browse files Browse the repository at this point in the history
[Spree 2.1] Make Shipping methods form save calculator details
  • Loading branch information
luisramos0 authored Jun 15, 2020
2 parents 257b311 + 0c1ed17 commit 7db519a
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 14 deletions.
7 changes: 6 additions & 1 deletion app/controllers/spree/admin/shipping_methods_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,12 @@ def permitted_resource_params
params.require(:shipping_method).permit(
:name, :description, :display_on,
:require_ship_address, :tag_list, :calculator_type,
distributor_ids: []
distributor_ids: [],
calculator_attributes: [
:id, :preferred_currency, :preferred_amount, :preferred_per_kg, :preferred_flat_percent,
:preferred_first_item, :preferred_additional_item, :preferred_max_items,
:preferred_minimal_amount, :preferred_normal_amount, :preferred_discount_amount
]
)
end
end
Expand Down
101 changes: 88 additions & 13 deletions spec/controllers/spree/admin/shipping_methods_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,104 @@
describe Spree::Admin::ShippingMethodsController, type: :controller do
include AuthenticationWorkflow

describe "shipping method not referenced by order" do
describe "#update" do
let(:shipping_method) { create(:shipping_method) }
let(:params) {
{
id: shipping_method.id,
shipping_method: {
calculator_attributes: {
id: shipping_method.calculator.id
}
}
}
}

scenario "is soft deleted" do
login_as_admin
expect(shipping_method.deleted_at).to be_nil
before { login_as_admin }

spree_delete :destroy, "id" => shipping_method.id
it "updates preferred_amount and preferred_currency of a FlatRate calculator" do
shipping_method.calculator = create(:calculator_flat_rate, calculable: shipping_method)
params[:shipping_method][:calculator_attributes][:preferred_amount] = 123
params[:shipping_method][:calculator_attributes][:preferred_currency] = "EUR"

expect(shipping_method.reload.deleted_at).not_to be_nil
spree_post :update, params

expect(shipping_method.reload.calculator.preferred_amount).to eq 123
expect(shipping_method.reload.calculator.preferred_currency).to eq "EUR"
end

it "updates preferred_per_kg of a Weight calculator" do
shipping_method.calculator = create(:weight_calculator, calculable: shipping_method)
params[:shipping_method][:calculator_attributes][:preferred_per_kg] = 10

spree_post :update, params

expect(shipping_method.reload.calculator.preferred_per_kg).to eq 10
end

it "updates preferred_flat_percent of a FlatPercentPerItem calculator" do
shipping_method.calculator = Calculator::FlatPercentPerItem.new(preferred_flat_percent: 20,

calculable: shipping_method)
params[:shipping_method][:calculator_attributes][:preferred_flat_percent] = 30

spree_post :update, params

expect(shipping_method.reload.calculator.preferred_flat_percent).to eq 30
end

it "updates details of a FlexiRate calculator" do
shipping_method.calculator = Spree::Calculator::FlexiRate.new(calculable: shipping_method)
params[:shipping_method][:calculator_attributes][:preferred_first_item] = 10
params[:shipping_method][:calculator_attributes][:preferred_additional_item] = 20
params[:shipping_method][:calculator_attributes][:preferred_max_items] = 30

spree_post :update, params

expect(shipping_method.reload.calculator.preferred_first_item).to eq 10
expect(shipping_method.reload.calculator.preferred_additional_item).to eq 20
expect(shipping_method.reload.calculator.preferred_max_items).to eq 30
end

it "updates details of a PriceSack calculator" do
shipping_method.calculator = Spree::Calculator::PriceSack.new(calculable: shipping_method)
params[:shipping_method][:calculator_attributes][:preferred_minimal_amount] = 10
params[:shipping_method][:calculator_attributes][:preferred_normal_amount] = 20
params[:shipping_method][:calculator_attributes][:preferred_discount_amount] = 30

spree_post :update, params

expect(shipping_method.reload.calculator.preferred_minimal_amount).to eq 10
expect(shipping_method.reload.calculator.preferred_normal_amount).to eq 20
expect(shipping_method.reload.calculator.preferred_discount_amount).to eq 30
end
end

describe "shipping method referenced by order" do
let(:order) { create(:order_with_line_items) }
describe "#delete" do
describe "shipping method not referenced by order" do
let(:shipping_method) { create(:shipping_method) }

scenario "is soft deleted" do
login_as_admin
expect(shipping_method.deleted_at).to be_nil

spree_delete :destroy, "id" => shipping_method.id

expect(shipping_method.reload.deleted_at).not_to be_nil
end
end

describe "shipping method referenced by order" do
let(:order) { create(:order_with_line_items) }

scenario "is not soft deleted" do
login_as_admin
expect(order.shipping_method.deleted_at).to be_nil
scenario "is not soft deleted" do
login_as_admin
expect(order.shipping_method.deleted_at).to be_nil

spree_delete :destroy, "id" => order.shipping_method.id
spree_delete :destroy, "id" => order.shipping_method.id

expect(order.shipping_method.reload.deleted_at).to be_nil
expect(order.shipping_method.reload.deleted_at).to be_nil
end
end
end
end

0 comments on commit 7db519a

Please sign in to comment.