Module: Alchemy::Page::PageElements

Extended by:
ActiveSupport::Concern
Included in:
Alchemy::Page
Defined in:
app/models/alchemy/page/page_elements.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#available_element_definitions(only_element_named = nil) ⇒ Object

All available element definitions that can actually be placed on current page.

It extracts all definitions that are unique or limited and already on page.

Example of unique element:

- name: headline
  unique: true
  contents:
  - name: headline
    type: EssenceText

Example of limited element:

- name: article
  amount: 2
  contents:
  - name: text
    type: EssenceRichtext

82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/models/alchemy/page/page_elements.rb', line 82

def available_element_definitions(only_element_named = nil)
  @_element_definitions ||= if only_element_named
      definition = Element.definition_by_name(only_element_named)
      element_definitions_by_name(definition["nestable_elements"])
    else
      element_definitions
    end

  return [] if @_element_definitions.blank?

  existing_elements = all_elements.not_nested.not_trashed
  @_existing_element_names = existing_elements.pluck(:name)
  delete_unique_element_definitions!
  delete_outnumbered_element_definitions!

  @_element_definitions
end

#available_element_namesObject

All names of elements that can actually be placed on current page.


102
103
104
# File 'app/models/alchemy/page/page_elements.rb', line 102

def available_element_names
  @_available_element_names ||= available_element_definitions.map { |e| e["name"] }
end

#available_elements_within_current_scope(parent) ⇒ Object

Available element definitions excluding nested unique elements.


108
109
110
111
112
113
114
115
116
117
# File 'app/models/alchemy/page/page_elements.rb', line 108

def available_elements_within_current_scope(parent)
  @_available_elements = if parent
      parents_unique_nested_elements = parent.nested_elements.where(unique: true).pluck(:name)
      available_element_definitions(parent.name).reject do |e|
        parents_unique_nested_elements.include? e["name"]
      end
    else
      available_element_definitions
    end
end

#descendent_element_definitionsObject

All element definitions defined for page's page layout including nestable element definitions


130
131
132
133
134
135
136
# File 'app/models/alchemy/page/page_elements.rb', line 130

def descendent_element_definitions
  definitions = element_definitions_by_name(element_definition_names)
  definitions.select { |d| d.key?("nestable_elements") }.each do |d|
    definitions += element_definitions_by_name(d["nestable_elements"])
  end
  definitions.uniq { |d| d["name"] }
end

#element_definition_namesObject

All names of elements that are defined in the page definition.

Assign elements to a page in config/alchemy/page_layouts.yml.

Example of page_layouts.yml:

- name: contact
  elements: [headline, contactform]

147
148
149
# File 'app/models/alchemy/page/page_elements.rb', line 147

def element_definition_names
  definition["elements"] || []
end

#element_definitionsObject

All element definitions defined for page's page layout

Warning: Since elements can be unique or limited in number, it is more safe to ask for available_element_definitions


124
125
126
# File 'app/models/alchemy/page/page_elements.rb', line 124

def element_definitions
  @_element_definitions ||= element_definitions_by_name(element_definition_names)
end

#element_definitions_by_name(names) ⇒ Array

Element definitions with given name(s)

Parameters:

  • one (Array || String)

    or many Alchemy::Element names. Pass 'all' to get all Element definitions

Returns:

  • (Array)

    An Array of element definitions


158
159
160
161
162
163
164
165
166
# File 'app/models/alchemy/page/page_elements.rb', line 158

def element_definitions_by_name(names)
  return [] if names.blank?

  if names.to_s == "all"
    Element.definitions
  else
    Element.definitions.select { |e| names.include? e["name"] }
  end
end

#feed_elementsObject

Returns all elements that should be feeded via rss.

Define feedable elements in your page_layouts.yml:

- name: news
  feed: true
  feed_elements: [element_name, element_2_name]

176
177
178
# File 'app/models/alchemy/page/page_elements.rb', line 176

def feed_elements
  elements.named(definition["feed_elements"])
end

#richtext_contents_idsObject

Returns an array of all EssenceRichtext contents ids from not folded elements


182
183
184
185
186
187
# File 'app/models/alchemy/page/page_elements.rb', line 182

def richtext_contents_ids
  Alchemy::Content.joins(:element)
    .where(Element.table_name => { page_id: id, folded: false })
    .select(&:has_tinymce?)
    .collect(&:id)
end