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
  ingredients:
  - name: headline
    type: Text

Example of limited element:

- name: article
  amount: 2
  ingredients:
  - name: text
    type: Richtext


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'app/models/alchemy/page/page_elements.rb', line 73

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 = draft_version.elements.not_nested
  @_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.



93
94
95
# File 'app/models/alchemy/page/page_elements.rb', line 93

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.



99
100
101
102
103
104
105
106
107
108
# File 'app/models/alchemy/page/page_elements.rb', line 99

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



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

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]


138
139
140
# File 'app/models/alchemy/page/page_elements.rb', line 138

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



115
116
117
# File 'app/models/alchemy/page/page_elements.rb', line 115

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



149
150
151
152
153
154
155
156
157
# File 'app/models/alchemy/page/page_elements.rb', line 149

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

#richtext_ingredients_idsObject

Returns an array of all Richtext ingredients ids from not folded elements



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

def richtext_ingredients_ids
  Alchemy::Ingredient.richtexts.joins(:element)
    .where(Element.table_name => {page_version_id: draft_version.id, folded: false})
    .select(&:has_tinymce?)
    .collect(&:id)
end