Skip to content

Commit ff22353

Browse files
committed
feat: implement active? method for link settings
1 parent 4700a88 commit ff22353

File tree

5 files changed

+40
-3
lines changed

5 files changed

+40
-3
lines changed

app/components/maglev/block_component.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class BlockComponent < BaseComponent
55
include TagHelper
66

77
extend Forwardable
8-
def_delegators :section, :site, :config
8+
def_delegators :section, :site, :page, :config
99

1010
attr_reader :section, :id, :name, :type, :settings, :attributes, :definition
1111
attr_accessor :children

app/components/maglev/content/base.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class Base
66
include ::Maglev::Inspector
77

88
extend Forwardable
9-
def_delegators :scope, :site, :config
9+
def_delegators :scope, :site, :config, :page
1010

1111
attr_accessor :scope, :content, :setting
1212

app/components/maglev/content/link.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ def target_blank
2323
open_new_window? ? '_blank' : nil
2424
end
2525

26+
def active?
27+
link[:link_type] == 'page' && link[:link_id] == page.id
28+
end
29+
2630
def to_s
2731
href
2832
end

app/components/maglev/section_component.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class SectionComponent < BaseComponent
55
include TagHelper
66

77
extend Forwardable
8-
def_delegators :parent, :site, :config
8+
def_delegators :parent, :site, :page, :config
99

1010
attr_reader :parent, :id, :type, :settings, :attributes, :definition, :templates_root_path, :rendering_mode
1111

spec/components/maglev/content/link_spec.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
describe Maglev::Content::Link do
66
let(:section_component) { double('Maglev::SectionComponent') }
77
let(:link) { described_class.new(section_component, content, setting) }
8+
let(:scope) { double('Scope', page: double('Page', id: 1)) }
9+
10+
before do
11+
allow(link).to receive(:scope).and_return(scope)
12+
end
813

914
context 'content is a string' do
1015
let(:content) { '/contact_us' }
@@ -32,6 +37,10 @@
3237
describe 'open_new_window? method' do
3338
it { expect(link.open_new_window?).to eq(false) }
3439
end
40+
41+
describe 'active? method' do
42+
it { expect(link.active?).to eq(false) }
43+
end
3544
end
3645

3746
context 'data provided by section yaml' do
@@ -60,6 +69,10 @@
6069
describe 'open_new_window? method' do
6170
it { expect(link.open_new_window?).to eq(false) }
6271
end
72+
73+
describe 'active? method' do
74+
it { expect(link.active?).to eq(false) }
75+
end
6376
end
6477

6578
context 'data provided by DB' do
@@ -91,5 +104,25 @@
91104
describe 'open_new_window? method' do
92105
it { expect(link.open_new_window?).to eq(true) }
93106
end
107+
108+
describe 'active? method' do
109+
it { expect(link.active?).to eq(false) }
110+
111+
context 'when link type is page and matches current path' do
112+
let(:content) do
113+
{ link_type: 'page', link_id: 1, text: 'Call Tomorrow :D',
114+
open_new_window: true }
115+
end
116+
it { expect(link.active?).to eq(true) }
117+
end
118+
119+
context 'when link type is page but does not match current path' do
120+
let(:content) do
121+
{ link_type: 'page', link_id: 2, text: 'Call Tomorrow :D',
122+
open_new_window: true }
123+
end
124+
it { expect(link.active?).to eq(false) }
125+
end
126+
end
94127
end
95128
end

0 commit comments

Comments
 (0)