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_definitionsObject

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


69
70
71
72
73
74
75
76
# File 'app/models/alchemy/page/page_elements.rb', line 69

def available_element_definitions
  @elements_for_layout ||= element_definitions
  return [] if @elements_for_layout.blank?
  @page_element_names = elements.not_trashed.pluck(:name)
  delete_unique_element_definitions!
  delete_outnumbered_element_definitions!
  @elements_for_layout
end

#available_element_namesObject

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



80
81
82
# File 'app/models/alchemy/page/page_elements.rb', line 80

def available_element_names
  available_element_definitions.collect { |e| e['name'] }
end

#element_definition_namesObject

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

Assign elements to a page in config/alchemy/page_layouts.yml and/or config/alchemy/cells.yml file.

Example of page_layouts.yml:

- name: contact
  cells: [right_column]
  elements: [headline, contactform]

Example of cells.yml:

- name: right_column
  elements: [teaser]


107
108
109
# File 'app/models/alchemy/page/page_elements.rb', line 107

def element_definition_names
  element_names_from_page_definition | element_names_from_cell_definition
end

#element_definitionsObject

All element definitions defined for page’s page layout



86
87
88
# File 'app/models/alchemy/page/page_elements.rb', line 86

def element_definitions
  element_definitions_by_name(element_definition_names)
end

#element_definitions_by_name(names) ⇒ Array

Returns 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



126
127
128
129
130
131
132
133
# File 'app/models/alchemy/page/page_elements.rb', line 126

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

#element_names_from_cell_definitionObject



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

def element_names_from_cell_definition
  cell_definitions.map { |d| d['elements'] }.flatten
end

#element_names_from_page_definitionObject



111
112
113
# File 'app/models/alchemy/page/page_elements.rb', line 111

def element_names_from_page_definition
  definition['elements'] || []
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]


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

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

#find_elements(options = {}, show_non_public = false) ⇒ ActiveRecord::Relation Also known as: find_selected_elements

Finds elements of page.

Parameters:

  • options (Hash) (defaults to: {})

    hash

  • (false) (Boolean)

    Pass true, if you want to also have not published elements.

Options Hash (options):

  • only (Array)

    Returns only elements with given names

  • except (Array)

    Returns all elements except the ones with given names

  • count (Fixnum)

    Limit the count of returned elements

  • offset (Fixnum)

    Starts with an offset while returning elements

  • random (Boolean) — default: false

    Return elements randomly shuffled

  • from_cell (Alchemy::Cell || String)

    Return elements from given cell

Returns:

  • (ActiveRecord::Relation)


157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'app/models/alchemy/page/page_elements.rb', line 157

def find_elements(options = {}, show_non_public = false)
  elements = elements_from_cell_or_self(options[:from_cell])
  if options[:only].present?
    elements = elements.named(options[:only])
  elsif options[:except].present?
    elements = elements.excluded(options[:except])
  end
  if options[:reverse_sort] || options[:reverse]
    elements = elements.reverse_order
  end
  elements = elements.offset(options[:offset]).limit(options[:count])
  if options[:random]
    elements = elements.order("RAND()")
  end
  show_non_public ? elements : elements.published
end

#richtext_contents_idsObject

Returns an array of all EssenceRichtext contents ids



189
190
191
# File 'app/models/alchemy/page/page_elements.rb', line 189

def richtext_contents_ids
  contents.essence_richtexts.pluck("#{Content.table_name}.id")
end