module Spree
  module CalculatedAdjustments
    extend ActiveSupport::Concern

    included do
      has_one :calculator, class_name: "Spree::Calculator", as: :calculable, inverse_of: :calculable, dependent: :destroy, autosave: true
      accepts_nested_attributes_for :calculator
      validates :calculator, presence: true
    end

    class_methods do
      def calculators
        spree_calculators.send model_name_without_spree_namespace
      end

      private

      def model_name_without_spree_namespace
        to_s.tableize.tr('/', '_').sub('spree_', '')
      end

      def spree_calculators
        Rails.application.config.spree.calculators
      end
    end

    def calculator_type
      calculator.class.to_s if calculator
    end

    def calculator_type=(calculator_type)
      klass = calculator_type.constantize if calculator_type
      self.calculator = klass.new if klass && !calculator.is_a?(klass)
    end
  end
end

module Spree
  module CalculatedAdjustments
    extend ActiveSupport::Concern

    included do
      has_one :calculator, class_name: "Spree::Calculator", as: :calculable, inverse_of: :calculable, dependent: :destroy, autosave: true
      accepts_nested_attributes_for :calculator, update_only: true
      validates :calculator, presence: true
    end

    class_methods do
      def calculators
        spree_calculators.send model_name_without_spree_namespace
      end

      private

      def model_name_without_spree_namespace
        to_s.tableize.tr('/', '_').sub('spree_', '')
      end

      def spree_calculators
        Rails.application.config.spree.calculators
      end
    end

    def calculator_type
      calculator.class.to_s if calculator
    end

    def calculator_type=(calculator_type)
      klass = calculator_type.constantize if calculator_type
      self.calculator = klass.new if klass && !calculator.is_a?(klass)
    end
  end
end

Add update_only on calculator attribute assignment
Add update_only on calculator attribute assignment

This allows us to change the type and assign attributes to the
calculator at the same time. Otherwise it would attempt to build a
calculator (with base class Spree::Calculator) when an id wasn't
specified.
