Class: Alchemy::PageLayout
- Inherits:
-
Object
- Object
- Alchemy::PageLayout
- Defined in:
- lib/alchemy/page_layout.rb
Class Method Summary collapse
-
.add(page_layout) ⇒ Object
Add additional page definitions to collection.
-
.all ⇒ Object
Returns all page layouts.
-
.element_names_for(page_layout) ⇒ Object
Returns all names of elements defined in given page layout.
-
.get(name) ⇒ Object
Returns one page definition by given name.
- .get_all_by_attributes(attributes) ⇒ Object
-
.human_layout_name(layout) ⇒ Object
Translates name for given layout.
-
.layouts_for_select(language_id, only_layoutpages = false) ⇒ Object
Returns page layouts ready for Rails’ select form helper.
-
.layouts_with_own_for_select(page_layout_name, language_id, only_layoutpages = false) ⇒ Object
Returns page layouts including given layout ready for Rails’ select form helper.
-
.selectable_layouts(language_id, only_layoutpages = false) ⇒ Object
Returns all layouts that can be used for creating a new page.
Class Method Details
.add(page_layout) ⇒ Object
Add additional page definitions to collection.
Useful for extending the page layouts from an Alchemy module.
Usage Example
Call +Alchemy::PageLayout.add(your_definition)+ in your engine.rb file.
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/alchemy/page_layout.rb', line 25 def add(page_layout) all if page_layout.is_a?(Array) @definitions += page_layout elsif page_layout.is_a?(Hash) @definitions << page_layout else raise TypeError end end |
.all ⇒ Object
Returns all page layouts.
They are defined in config/alchemy/page_layout.yml
file.
10 11 12 |
# File 'lib/alchemy/page_layout.rb', line 10 def all @definitions ||= read_definitions_file end |
.element_names_for(page_layout) ⇒ Object
Returns all names of elements defined in given page layout.
99 100 101 102 103 104 105 106 |
# File 'lib/alchemy/page_layout.rb', line 99 def element_names_for(page_layout) if definition = get(page_layout) definition.fetch('elements', []) else Rails.logger.warn "\n+++ Warning: No layout definition for #{page_layout} found! in page_layouts.yml\n" [] end end |
.get(name) ⇒ Object
Returns one page definition by given name.
38 39 40 41 |
# File 'lib/alchemy/page_layout.rb', line 38 def get(name) return {} if name.blank? all.detect { |a| a['name'].casecmp(name).zero? } end |
.get_all_by_attributes(attributes) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/alchemy/page_layout.rb', line 43 def get_all_by_attributes(attributes) return [] if attributes.blank? if attributes.is_a? Hash layouts = [] attributes.stringify_keys.each do |key, value| result = all.select { |l| l.key?(key) && l[key].to_s.casecmp(value.to_s).zero? } layouts += result unless result.empty? end layouts else [] end end |
.human_layout_name(layout) ⇒ Object
Translates name for given layout
Translation example
en:
alchemy:
page_layout_names:
products_overview: Products Overview
120 121 122 |
# File 'lib/alchemy/page_layout.rb', line 120 def human_layout_name(layout) Alchemy.t(layout, scope: 'page_layout_names', default: layout.to_s.humanize) end |
.layouts_for_select(language_id, only_layoutpages = false) ⇒ Object
Returns page layouts ready for Rails’ select form helper.
60 61 62 63 |
# File 'lib/alchemy/page_layout.rb', line 60 def layouts_for_select(language_id, only_layoutpages = false) @map_array = [] mapped_layouts_for_select(selectable_layouts(language_id, only_layoutpages)) end |
.layouts_with_own_for_select(page_layout_name, language_id, only_layoutpages = false) ⇒ Object
Returns page layouts including given layout ready for Rails’ select form helper.
67 68 69 70 71 72 73 74 75 |
# File 'lib/alchemy/page_layout.rb', line 67 def layouts_with_own_for_select(page_layout_name, language_id, only_layoutpages = false) layouts = selectable_layouts(language_id, only_layoutpages) if layouts.detect { |l| l['name'] == page_layout_name }.nil? @map_array = [[human_layout_name(page_layout_name), page_layout_name]] else @map_array = [] end mapped_layouts_for_select(layouts) end |
.selectable_layouts(language_id, only_layoutpages = false) ⇒ Object
Returns all layouts that can be used for creating a new page.
It removes all layouts from available layouts that are unique and already taken and that are marked as hide.
86 87 88 89 90 91 92 93 94 95 |
# File 'lib/alchemy/page_layout.rb', line 86 def selectable_layouts(language_id, only_layoutpages = false) @language_id = language_id all.select do |layout| if only_layoutpages layout['layoutpage'] && layout_available?(layout) else !layout['layoutpage'] && layout_available?(layout) end end end |