Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/components/maglev/content/link.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ def with_text?
!!setting.options[:with_text]
end

# NOTE: the value submitted by the editor form is a string ("true", "false" or "")
def open_new_window?
!!link[:open_new_window]
ActiveModel::Type::Boolean.new.cast(link[:open_new_window]) || false
end

def target_blank
Expand Down
11 changes: 10 additions & 1 deletion app/models/maglev/setting_types/link.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,21 @@ def cast_value(value)
if value.is_a?(String)
{ text: 'Link', link_type: 'url', href: value }
elsif value
{ text: 'Link', link_type: 'url', href: '#' }.merge(value.symbolize_keys)
cast_open_new_window({ text: 'Link', link_type: 'url', href: '#' }.merge(value.symbolize_keys))
end
end

def content_label(value)
value&.fetch('text', nil)
end

private

# the editor form submits the open_new_window flag as a string ("true", "false" or "")
def cast_open_new_window(link)
return link unless link.key?(:open_new_window)

link.merge(open_new_window: ActiveModel::Type::Boolean.new.cast(link[:open_new_window]) || false)
end
end
# rubocop:enable Style/ClassAndModuleChildren
11 changes: 11 additions & 0 deletions spec/components/maglev/content/link_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,17 @@
it { expect(link.open_new_window?).to eq(true) }
end

context 'when the open_new_window flag was stored as a string by the editor form' do
[['true', true], ['false', false], ['', false]].each do |raw, expected|
context "open_new_window is #{raw.inspect}" do
let(:content) { { link_type: 'url', href: '/contact_us', text: 'Call Tomorrow :D', open_new_window: raw } }

it { expect(link.open_new_window?).to eq(expected) }
it { expect(link.target_blank).to eq(expected ? '_blank' : nil) }
end
end
end

describe 'active? method' do
it { expect(link.active?).to eq(false) }

Expand Down
13 changes: 13 additions & 0 deletions spec/models/maglev/setting_types/link_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@
it { is_expected.to eq({ link_type: 'url', href: 'https://pro.maglev.dev', text: 'Hello' }) }
end

describe 'value is a hash submitted by the editor form' do
# rubocop:disable Style/StringHashKeys
let(:value) { { 'link_type' => 'url', 'href' => '/', 'text' => 'Hello', 'open_new_window' => open_new_window } }
# rubocop:enable Style/StringHashKeys

[['true', true], ['1', true], ['false', false], ['0', false], ['', false]].each do |raw, expected|
context "open_new_window is #{raw.inspect}" do
let(:open_new_window) { raw }
it { is_expected.to include(open_new_window: expected) }
end
end
end

describe 'value is nil (got reset by the UI)' do
let(:value) { nil }
it { is_expected.to eq(nil) }
Expand Down