Skip to content

Commit

Permalink
Sync up secondary relay after any change
Browse files Browse the repository at this point in the history
This keeps the logic simpler and should always be safe.
Any change results in a new relay instance, so otherwise
we'd have to do some safe navigation like:
previous_secondary_outlet = @product.relay&.secondary_outlet

Not a huge deal but it crossed the line for me of complexity vs value.
  • Loading branch information
giladshanan committed May 6, 2024
1 parent 6620d60 commit a90cd67
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
3 changes: 1 addition & 2 deletions app/controllers/instrument_relays_controller.rb
Expand Up @@ -36,10 +36,9 @@ def create
private

def handle_relay(action_string)
previous_secondary_outlet = @product.relay.secondary_outlet
@relay = @product.replace_relay(relay_params, params[:relay][:control_mechanism])
if @relay.valid?
@relay.try(:activate_secondary_outlet) if @relay.secondary_outlet != previous_secondary_outlet
@relay.try(:activate_secondary_outlet)
flash[:notice] = "Relay was successfully updated."
redirect_to facility_instrument_relays_path(current_facility, @product)
else
Expand Down
11 changes: 6 additions & 5 deletions spec/system/admin/instrument_relay_tab_spec.rb
Expand Up @@ -7,6 +7,7 @@
let(:user) { FactoryBot.create(:user, :administrator) }

before do
allow_any_instance_of(RelaySynaccessRevA).to receive(:activate_secondary_outlet).and_return(true)
login_as user
visit facility_instrument_relays_path(facility, instrument)
end
Expand Down Expand Up @@ -104,7 +105,7 @@
fill_in "relay_building_room_number", with: "1a"
fill_in "relay_circuit_number", with: "1"
fill_in "relay_ethernet_port_number", with: "2000"

click_button "Save"
instrument.reload
expect(instrument.relay).to be_present
Expand Down Expand Up @@ -136,7 +137,7 @@
click_button "Save"
expect(page).to have_content("Outlet has already been taken")
end

context "both instruments have the same schedule" do
let!(:instrument2) { create(:instrument, facility: facility, no_relay: true, schedule: instrument.schedule) }
let!(:existing_relay) { create(:relay_syna, instrument: instrument2) }
Expand Down Expand Up @@ -199,7 +200,7 @@

context "switching relay types" do
let(:instrument) { FactoryBot.create(:setup_instrument, facility: facility, relay: build(:relay)) }

before do
click_link "Edit"
end
Expand All @@ -213,7 +214,7 @@

expect(instrument.relay).to be_a(RelayDummy)
end

end

context "from relay to reservation only" do
Expand All @@ -226,7 +227,7 @@

expect(instrument.relay).not_to be_present
end

end

context "from reservation only to timer" do
Expand Down

0 comments on commit a90cd67

Please sign in to comment.