File tree Expand file tree Collapse file tree 5 files changed +37
-16
lines changed Expand file tree Collapse file tree 5 files changed +37
-16
lines changed Original file line number Diff line number Diff line change @@ -56,15 +56,16 @@ Inheritance of page objects is supported:
56
56
57
57
``` ruby
58
58
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?
61
62
62
63
def fancy_collection
63
64
collection.map(& :fancy )
64
65
end
65
66
end
66
67
67
- FancyIndexPage .new (Account .find(99 )).to_h
68
+ FancyIndexPage .new (Account .find(99 )).to_h # => { :title => "…", fancy_collection: […] }
68
69
```
69
70
70
71
## Development
Original file line number Diff line number Diff line change @@ -20,6 +20,17 @@ def section(name, options = {})
20
20
@__sections [ n ] = section
21
21
end
22
22
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
+
23
34
def url_helper ( name )
24
35
raise "Rails url_helpers module is not defined" unless defined? ( Rails )
25
36
delegate name , to : :"Rails.application.routes.url_helpers"
@@ -30,10 +41,6 @@ def url_helper(name)
30
41
def define_section_method ( n , section )
31
42
define_method ( n ) { section . value ( self ) }
32
43
end
33
-
34
- def inherited ( subclass )
35
- subclass . instance_variable_set ( :@__sections , @__sections . dup )
36
- end
37
44
end
38
45
39
46
def to_h ( options = { } )
Original file line number Diff line number Diff line change 1
1
# frozen_string_literal: true
2
2
3
3
class Tram ::Page ::Section
4
- extend Dry ::Initializer
4
+ extend :: Dry ::Initializer
5
5
6
6
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
10
10
11
11
# Takes the page instance and extracts a hash for the section
12
12
def call ( page )
13
13
show? ( page ) ? { name => value ( page ) } : { }
14
14
end
15
15
16
16
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
18
22
end
19
23
20
24
private
21
25
26
+ def attributes
27
+ @attributes ||= self . class . dry_initializer . attributes ( self )
28
+ end
29
+
22
30
def show? ( page )
23
31
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 ]
26
34
condition
27
35
end
28
36
end
Original file line number Diff line number Diff line change 1
1
# frozen_string_literal: true
2
2
3
3
class InheritedPage < ExamplePage
4
- section :baz , skip : true
4
+ inherit_section :foo
5
+ inherit_section :bar , if : :no_bar
5
6
section :bam
6
7
7
8
def bam
8
9
baz . upcase
9
10
end
11
+
12
+ def no_bar
13
+ !bar
14
+ end
10
15
end
Original file line number Diff line number Diff line change 6
6
subject { described_class . new ( "test" ) }
7
7
8
8
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" )
10
10
end
11
11
12
12
it "doesn't change behaviour of the parent class" do
You can’t perform that action at this time.
0 commit comments