Skip to content

Commit d031ab9

Browse files
committed
Change inheritance config from skipping to explicit inherit of sections
1 parent ea1af70 commit d031ab9

File tree

5 files changed

+37
-16
lines changed

5 files changed

+37
-16
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,16 @@ Inheritance of page objects is supported:
5656

5757
```ruby
5858
class FancyIndexPage < IndexPage
59-
section :collection, skip: true
60-
section :fancy_collection
59+
inherit_section :title
60+
section :fancy_collection
61+
inherit_section :index_url, if: :readonly_on?
6162

6263
def fancy_collection
6364
collection.map(&:fancy)
6465
end
6566
end
6667

67-
FancyIndexPage.new(Account.find(99)).to_h
68+
FancyIndexPage.new(Account.find(99)).to_h # => { :title => "…", fancy_collection: […] }
6869
```
6970

7071
## Development

lib/tram/page.rb

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@ def section(name, options = {})
2020
@__sections[n] = section
2121
end
2222

23+
def sections
24+
@__sections
25+
end
26+
27+
def inherit_section(name, option_overrides={})
28+
n = name.to_sym
29+
parent_section = superclass.sections[n]
30+
options = Tram::Page::Section.dry_initializer.attributes(parent_section)
31+
section(name, options.merge(option_overrides))
32+
end
33+
2334
def url_helper(name)
2435
raise "Rails url_helpers module is not defined" unless defined?(Rails)
2536
delegate name, to: :"Rails.application.routes.url_helpers"
@@ -30,10 +41,6 @@ def url_helper(name)
3041
def define_section_method(n, section)
3142
define_method(n) { section.value(self) }
3243
end
33-
34-
def inherited(subclass)
35-
subclass.instance_variable_set(:@__sections, @__sections.dup)
36-
end
3744
end
3845

3946
def to_h(options = {})

lib/tram/page/section.rb

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,36 @@
11
# frozen_string_literal: true
22

33
class Tram::Page::Section
4-
extend Dry::Initializer
4+
extend ::Dry::Initializer
55

66
param :name
7-
option :value, optional: true, as: :getter
8-
option :if, optional: true, as: :positive
9-
option :unless, optional: true, as: :negative
7+
option :value, optional: true
8+
option :if, optional: true
9+
option :unless, optional: true
1010

1111
# Takes the page instance and extracts a hash for the section
1212
def call(page)
1313
show?(page) ? { name => value(page) } : {}
1414
end
1515

1616
def value(page)
17-
getter ? page.instance_exec(&getter) : page.public_send(name)
17+
if attributes[:value]
18+
page.instance_exec(&attributes[:value])
19+
else
20+
page.public_send(name)
21+
end
1822
end
1923

2024
private
2125

26+
def attributes
27+
@attributes ||= self.class.dry_initializer.attributes(self)
28+
end
29+
2230
def show?(page)
2331
condition = true
24-
condition &= page.send(positive) if positive
25-
condition &= !page.send(negative) if negative
32+
condition &= page.__send__(attributes[:if]) if attributes[:if]
33+
condition &= !page.__send__(attributes[:unless]) if attributes[:unless]
2634
condition
2735
end
2836
end

spec/support/inherited_page.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
# frozen_string_literal: true
22

33
class InheritedPage < ExamplePage
4-
section :baz, skip: true
4+
inherit_section :foo
5+
inherit_section :bar, if: :no_bar
56
section :bam
67

78
def bam
89
baz.upcase
910
end
11+
12+
def no_bar
13+
!bar
14+
end
1015
end

spec/tram/inherited_page_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
subject { described_class.new("test") }
77

88
it "returns data hash with new and without skipped fields" do
9-
expect(subject.to_h).to eq(bar: "test", foo: "test", bam: "TEST")
9+
expect(subject.to_h).to eq(foo: "test", bam: "TEST")
1010
end
1111

1212
it "doesn't change behaviour of the parent class" do

0 commit comments

Comments
 (0)