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 layout definitions to collection.

Useful for extending the page layouts from an Alchemy module.

Usage Example

Call +Alchemy::PageLayout.add(your_layout_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.



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

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.



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

def all
  @definitions ||= read_layouts_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 layout_description = get(page_layout)
    layout_description.fetch('elements', [])
  else
    Rails.logger.warn "\n+++ Warning: No Layout Description for #{page_layout} found! in page_layouts.yml\n"
    return []
  end
end

.get(name) ⇒ Object

Returns one page layout description by given name.



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

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

.get_all_by_attributes(attributes) ⇒ Object



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

def get_all_by_attributes(attributes)
  return [] if attributes.blank?
  if attributes.class.name == 'Hash'
    layouts = []
    attributes.stringify_keys.each do |key, value|
      result = all.select { |a| a[key].to_s.downcase == value.to_s.downcase if a.has_key?(key) }
      layouts += result unless result.empty?
    end
    return layouts
  else
    return []
  end
end

.human_layout_name(layout) ⇒ Object

Translates name for given layout

Translation example

de:
  alchemy:
    page_layout_names:
      products_overview: Produktübersicht

Parameters:

  • The (String)

    layout name



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

def human_layout_name(layout)
  I18n.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 = [[I18n.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 { |layout|
    if only_layoutpages
      layout['layoutpage'] && layout_available?(layout)
    else
      !layout['layoutpage'] && layout_available?(layout)
    end
  }
end