From b194c38bcfe0157f84a133aa2317e6a9bbb2a9cf Mon Sep 17 00:00:00 2001 From: SamuelMartini Date: Fri, 14 Feb 2020 17:35:32 +0100 Subject: [PATCH 1/2] Allow option type to be nil when option value delegates option_type_name to it Spree::OptionValue#name is delegated to Spree::OptionType with option_type prefix (option_type_name). https://github.com/solidusio/solidus/blob/v2.9.5/core/app/models/spree/option_value.rb#L17 The json response of Spree::Api::OptionValuesController uses the *option_value_attributes helper variable which has :option_type_name. If for some reason an option value is created without option_type the above controller will return a 500 error because the name delegation. This commit also remove check_option_values from the option_values_controller_spec.rb because is not used anywhere in the file. --- .../spree/api/option_values_controller_spec.rb | 16 ++++++++++------ core/app/models/spree/option_value.rb | 2 +- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/api/spec/requests/spree/api/option_values_controller_spec.rb b/api/spec/requests/spree/api/option_values_controller_spec.rb index b20e697968a..e60d632b1ec 100644 --- a/api/spec/requests/spree/api/option_values_controller_spec.rb +++ b/api/spec/requests/spree/api/option_values_controller_spec.rb @@ -12,12 +12,6 @@ module Spree stub_authentication! end - def check_option_values(option_values) - expect(option_values.count).to eq(1) - expect(option_values.first).to have_attributes([:id, :name, :presentation, - :option_type_name, :option_type_id]) - end - context "without any option type scoping" do before do # Create another option value with a brand new option type @@ -116,6 +110,16 @@ def check_option_values(option_values) expect(option_value.name).to eq("Option Value") end + it "can create an option value" do + post spree.api_option_values_path, params: { option_value: { + name: "Option Value", + presentation: 'option value' + } } + expect(response.status).to eq(201) + + expect(json_response).to have_attributes(attributes) + end + it "permits the correct attributes" do expect_any_instance_of(Spree::Api::OptionValuesController).to receive(:permitted_option_value_attributes) put spree.api_option_value_path(option_value), params: { option_value: { name: "" } } diff --git a/core/app/models/spree/option_value.rb b/core/app/models/spree/option_value.rb index a0b79252b9a..c25574b842a 100644 --- a/core/app/models/spree/option_value.rb +++ b/core/app/models/spree/option_value.rb @@ -14,7 +14,7 @@ class OptionValue < Spree::Base after_save :touch, if: :saved_changes? after_touch :touch_all_variants - delegate :name, :presentation, to: :option_type, prefix: :option_type + delegate :name, :presentation, to: :option_type, prefix: :option_type, allow_nil: true self.whitelisted_ransackable_attributes = %w[name presentation] From 14a2261f7b475d3f665b80a76cf251465bc666a0 Mon Sep 17 00:00:00 2001 From: SamuelMartini Date: Fri, 14 Feb 2020 17:50:18 +0100 Subject: [PATCH 2/2] Remove duplicated option_values resources configuration The above line already configures all the routes for the option value resource. --- api/config/routes.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/api/config/routes.rb b/api/config/routes.rb index 94b800c801d..520a02433d7 100644 --- a/api/config/routes.rb +++ b/api/config/routes.rb @@ -59,7 +59,6 @@ end resources :option_values - resources :option_values, only: :index get '/orders/mine', to: 'orders#mine', as: 'my_orders' get "/orders/current", to: "orders#current", as: "current_order"