Skip to content

Commit

Permalink
Set correct current_overlay_name in the context of close_overlay
Browse files Browse the repository at this point in the history
  • Loading branch information
martinzamuner committed Sep 26, 2022
1 parent 43fc577 commit 192988d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
6 changes: 4 additions & 2 deletions app/controllers/overlastic/concerns/overlay_handling.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ def add_overlay_variant
def close_overlay(key = :last, **options)
overlay_name = helpers.overlay_name_from(key) || :overlay1

# In case a Turbo Stream is appended which renders new overlay links, they should be generated
# relative to the foremost overlay still open after closing the requested overlays.
request.headers["Overlay-Name"] = :"overlay#{helpers.overlay_number_from(overlay_name) - 1}"

options.filter { |key, _| key.in? self.class._flash_types }.each { |key, value| flash.now[key] = value }

if block_given?
Expand Down Expand Up @@ -92,8 +96,6 @@ def redirect_to(options = {}, response_options = {})

if request.variant.overlay?
if overlay_name.present?
overlay_name = nil unless helpers.valid_overlay_name?(overlay_name)

request.variant.delete :overlay
flash.merge! response_options.filter { |key, _| key.in? self.class._flash_types }

Expand Down
8 changes: 6 additions & 2 deletions app/helpers/overlastic/overlays_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@ def current_overlay_name
request.headers["Overlay-Name"]&.to_sym
end

def overlay_number_from(name)
name.to_s.scan(/\d+/)&.first.to_i
end

def overlay_name_from(key)
current_number = current_overlay_name.to_s.scan(/\d+/)&.first.to_i
current_number = overlay_number_from current_overlay_name

case key
when :first, :all
Expand All @@ -37,7 +41,7 @@ def overlay_name_from(key)
end

def valid_overlay_name?(name)
name.to_s.scan(/\d+/)&.first&.to_i&.positive?
overlay_number_from(name).positive?
end

def render_overlay(locals = {}, &block)
Expand Down
4 changes: 4 additions & 0 deletions test/helpers/overlays_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ class Overlastic::OverlaysHelperTest < ActionView::TestCase
assert_equal :overlay32, current_overlay_name
end

test "overlay_number_from :overlay13" do
assert_equal 13, overlay_number_from(:overlay13)
end

test "overlay_name_from :first" do
assert_equal :overlay1, overlay_name_from(:first)
end
Expand Down

0 comments on commit 192988d

Please sign in to comment.