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.
-
.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.
88 89 90 91 92 93 94 95 |
# File 'lib/alchemy/page_layout.rb', line 88 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 42 |
# 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
44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/alchemy/page_layout.rb', line 44 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
109 110 111 |
# File 'lib/alchemy/page_layout.rb', line 109 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.
61 62 63 64 |
# File 'lib/alchemy/page_layout.rb', line 61 def layouts_for_select(language_id, only_layoutpages = false) @map_array = [] mapped_layouts_for_select(selectable_layouts(language_id, only_layoutpages)) 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.
75 76 77 78 79 80 81 82 83 84 |
# File 'lib/alchemy/page_layout.rb', line 75 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 |