22
33module Alchemy
44 class PageLayout
5+ include ActiveModel ::Model
6+ include ActiveModel ::Attributes
7+
8+ extend ActiveModel ::Translation
9+
10+ attribute :name , :string
11+ attribute :elements , default : [ ]
12+ attribute :autogenerate , default : [ ]
13+ attribute :layoutpage , :boolean , default : false
14+ attribute :unique , :boolean , default : false
15+ attribute :cache , :boolean , default : true
16+ attribute :insert_elements_at , :string , default : "bottom"
17+ attribute :fixed_attributes , default : { }
18+ attribute :searchable , :boolean , default : true
19+ attribute :searchresults , :boolean , default : false
20+ attribute :hide , :boolean , default : false
21+ attribute :editable_by
22+ attribute :hint
23+
24+ validates :name ,
25+ presence : true ,
26+ format : {
27+ with : /\A [a-z_-]+\z /
28+ }
29+
30+ delegate :[] , to : :attributes
31+
532 class << self
633 # Returns all page layouts.
734 #
835 # They are defined in +config/alchemy/page_layout.yml+ file.
936 #
1037 def all
11- @definitions ||= read_definitions_file . map ( &:with_indifferent_access )
38+ @definitions ||= read_definitions_file . map { new ( **_1 ) }
39+ end
40+
41+ def map ( ...)
42+ all . map ( ...)
1243 end
44+ alias_method :collect , :map
1345
1446 # Add additional page definitions to collection.
1547 #
@@ -22,23 +54,17 @@ def all
2254 # @param [Array || Hash]
2355 # You can pass a single layout definition as Hash, or a collection of page layouts as Array.
2456 #
25- def add ( page_layout )
57+ def add ( definition )
2658 all
27- if page_layout . is_a? ( Array )
28- @definitions += page_layout
29- elsif page_layout . is_a? ( Hash )
30- @definitions << page_layout
31- else
32- raise TypeError
33- end
59+ @definitions += Array . wrap ( definition ) . map { new ( **_1 ) }
3460 end
3561
3662 # Returns one page definition by given name.
3763 #
3864 def get ( name )
39- return { } if name . blank?
65+ return new if name . blank?
4066
41- all . detect { | a | a [ " name" ] . casecmp ( name ) . zero? }
67+ all . detect { _1 . name . casecmp ( name ) . zero? }
4268 end
4369
4470 def reset!
@@ -69,5 +95,13 @@ def read_definitions_file
6995 end
7096 end
7197 end
98+
99+ def human_name
100+ Alchemy ::Page . human_layout_name ( name )
101+ end
102+
103+ def attributes
104+ super . with_indifferent_access
105+ end
72106 end
73107end
0 commit comments