app/models/concerns/products/relay_support.rb
# We only want to destroy the existing relay if the new one is valid.
# See https://andycroll.com/ruby/be-careful-assigning-to-has-one-relations/
We only want to destroy the existing relay if the new one is valid.
We only want to destroy the existing relay if the new one is valid .
def replace_relay(attributes = nil, param_control_mechanism = nil)
    self.class.transaction do
      relay&.destroy!
      case param_control_mechanism
      when Relay::CONTROL_MECHANISMS[:relay]
        create_relay!(attributes)
      when Relay::CONTROL_MECHANISMS[:timer]
        create_relay!(instrument: @product, type: "RelayDummy")
      when "manual"
        Relay.new 
      end
    end
  rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotDestroyed
    relay 
  end
def replace_relay ( attributes = nil , param_control_mechanism = nil ) self . class . transaction do relay &. destroy! case param_control_mechanism when Relay :: CONTROL_MECHANISMS [ :relay ] create_relay! ( attributes ) when Relay :: CONTROL_MECHANISMS [ :timer ] create_relay! ( instrument : @product , type : "RelayDummy" ) when "manual" Relay . new end end rescue ActiveRecord :: RecordInvalid , ActiveRecord :: RecordNotDestroyed relay end
We only want to destroy the existing relay if the new one is valid.
We only want to destroy the existing relay if the new one is valid .
# return a relay instance for the form to use # returns invalid object for error handling
attributes
<not_specific>
param_control_mechanism
<not_specific>