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.


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

.allObject

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.


100
101
102
103
104
105
106
107
# File 'lib/alchemy/page_layout.rb', line 100

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

Parameters:

  • The (String)

    layout name


121
122
123
# File 'lib/alchemy/page_layout.rb', line 121

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

.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.


68
69
70
71
72
73
74
75
76
# File 'lib/alchemy/page_layout.rb', line 68

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.


87
88
89
90
91
92
93
94
95
96
# File 'lib/alchemy/page_layout.rb', line 87

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