Class: Alchemy::PageLayout

Inherits:
Object
  • Object
show all
Defined in:
lib/alchemy/page_layout.rb

Class Method Summary collapse

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.

Parameters:

  • You (Array || Hash)

    can pass a single layout definition as Hash, or a collection of page layouts as Array.



23
24
25
26
27
28
29
30
31
32
# File 'lib/alchemy/page_layout.rb', line 23

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

.allObject

Returns all page layouts.

They are defined in config/alchemy/page_layout.yml file.



8
9
10
# File 'lib/alchemy/page_layout.rb', line 8

def all
  @definitions ||= read_definitions_file
end

.element_names_for(page_layout) ⇒ Object

Returns all names of elements defined in given page layout.



97
98
99
100
101
102
103
104
# File 'lib/alchemy/page_layout.rb', line 97

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"
    return []
  end
end

.get(name) ⇒ Object

Returns one page definition by given name.



36
37
38
39
# File 'lib/alchemy/page_layout.rb', line 36

def get(name)
  return {} if name.blank?
  all.detect { |a| a['name'].casecmp(name) == 0 }
end

.get_all_by_attributes(attributes) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/alchemy/page_layout.rb', line 41

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) == 0 }
      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

Parameters:

  • The (String)

    layout name



118
119
120
# File 'lib/alchemy/page_layout.rb', line 118

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.



58
59
60
61
# File 'lib/alchemy/page_layout.rb', line 58

def layouts_for_select(language_id, only_layoutpages = false)
  @map_array = [[Alchemy.t('Please choose'), '']]
  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.



65
66
67
68
69
70
71
72
73
# File 'lib/alchemy/page_layout.rb', line 65

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.

Parameters:

  • language_id (Fixnum)

    of current used Language.

  • (false) (Boolean)

    Pass true to only select layouts for global/layout pages.



84
85
86
87
88
89
90
91
92
93
# File 'lib/alchemy/page_layout.rb', line 84

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