@@ -25,7 +25,6 @@ class Settings
2525 :components ,
2626 :extra_styles ,
2727 :helper_class ,
28- :navbar ,
2928 :page_not_found ,
3029 :record_not_found ,
3130 :repository ,
@@ -35,8 +34,6 @@ class Settings
3534 :scripts ,
3635 :style_links
3736
38- attr_reader :pages , :resources
39-
4037 def []( key )
4138 send ( key )
4239 end
@@ -57,39 +54,15 @@ def load_settings
5754 end
5855 end
5956
60- @ pages ||= { }
61- @ resources ||= { }
57+ context . pages ||= { }
58+ context . resources ||= { }
6259 @sections ||= [ ]
6360 @root_path = '/' if @root_path == ''
64- if @authentication [ :plugin ] == Plugins ::SimpleAuth
65- @authentication [ :logout ] ||= [ 'logout' , "#{ root_path } /auth/logout" ]
66- end
67- @navbar = prepare_navbar ( sections , logout : authentication [ :logout ] )
68- end
6961
70- def prepare_navbar ( sections , logout :)
71- items = sections . each_with_object ( { } ) do |section , list |
72- slug = section [ :slug ]
73- case section [ :type ] &.to_sym
74- when :url
75- list [ slug ] = [ section [ :name ] , section [ :url ] , section [ :options ] ]
76- when :page
77- page = section [ :page ]
78- pages [ slug ] = page . is_a? ( String ) ? Object . const_get ( page ) : page
79- list [ slug ] = [ section [ :name ] , route_for ( slug ) ]
80- when :resource
81- repository = section [ :repository ] || settings . repository
82- resources [ slug ] = {
83- model : section [ :model ] . is_a? ( String ) ? Object . const_get ( section [ :model ] ) : section [ :model ] ,
84- repository : repository . is_a? ( String ) ? Object . const_get ( repository ) : repository
85- }
86- resources [ slug ] . merge! section . slice ( :resource , :only , :index , :show , :collection_actions , :member_actions )
87- hidden = section [ :options ] && ( section [ :options ] . include? ( :hidden ) || section [ :options ] . include? ( 'hidden' ) )
88- list [ slug ] = [ section [ :name ] , route_for ( slug ) ] unless hidden
89- end
62+ if @authentication [ :plugin ] <= Plugins ::SimpleAuth
63+ @authentication [ :logout ] ||= { name : 'logout' , path : "#{ root_path } /auth/logout" }
9064 end
91- items [ 'auth/logout' ] = logout if logout
92- items
65+ context . navbar = prepare_navbar ( sections , logout : authentication [ :logout ] )
9366 end
9467
9568 private
@@ -106,5 +79,44 @@ def convert_value(key, value)
10679 self [ key ] = Object . const_get ( self [ key ] )
10780 end
10881 end
82+
83+ def prepare_navbar ( sections , logout :)
84+ items = sections . each_with_object ( { } ) do |section , list |
85+ slug = section [ :slug ]
86+ case section [ :type ] &.to_sym
87+ when :url
88+ list [ slug ] = add_url_section ( slug , section )
89+ when :page
90+ list [ slug ] = add_page_section ( slug , section )
91+ when :resource
92+ list [ slug ] = add_resource_section ( slug , section )
93+ end
94+ end
95+ items [ 'auth/logout' ] = logout if logout
96+ items
97+ end
98+
99+ def add_url_section ( _slug , section )
100+ section . slice ( :name , :options ) . tap { _1 [ :path ] = section [ :url ] }
101+ end
102+
103+ def add_page_section ( slug , section )
104+ page = section [ :page ]
105+ context . pages [ slug ] = page . is_a? ( String ) ? Object . const_get ( page ) : page
106+ { name : section [ :name ] , path : route_for ( slug ) , class : context . pages [ slug ] }
107+ end
108+
109+ def add_resource_section ( slug , section )
110+ repository = section [ :repository ] || settings . repository
111+ context . resources [ slug ] = {
112+ model : section [ :model ] . is_a? ( String ) ? Object . const_get ( section [ :model ] ) : section [ :model ] ,
113+ repository : repository . is_a? ( String ) ? Object . const_get ( repository ) : repository
114+ }
115+ resource_options = section . slice ( :resource , :only , :index , :show , :collection_actions , :member_actions )
116+ resource_options [ :only ] ||= %i[ index show ]
117+ context . resources [ slug ] . merge! ( resource_options )
118+ hidden = section [ :options ] && ( section [ :options ] . include? ( :hidden ) || section [ :options ] . include? ( 'hidden' ) )
119+ { name : section [ :name ] , path : route_for ( slug ) } unless hidden
120+ end
109121 end
110122end
0 commit comments