Class: Alchemy::Element

Inherits:
BaseRecord
  • Object
show all
Includes:
Definitions, ElementContents, ElementEssences, Presenters, Hints, Logger, Taggable
Defined in:
app/models/alchemy/element.rb

Defined Under Namespace

Modules: Definitions, ElementContents, ElementEssences, Presenters

Constant Summary collapse

NAME_REGEXP =
/\A[a-z0-9_-]+\z/
FORBIDDEN_DEFINITION_ATTRIBUTES =
[
  "amount",
  "autogenerate",
  "nestable_elements",
  "contents",
  "hint",
  "taggable",
  "compact",
  "message",
  "deprecated",
].freeze
SKIPPED_ATTRIBUTES_ON_COPY =
[
  "cached_tag_list",
  "created_at",
  "creator_id",
  "id",
  "folded",
  "position",
  "updated_at",
  "updater_id",
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Presenters

#display_name, #display_name_with_preview_text, #dom_id, #preview_content, #preview_text

Methods included from ElementEssences

#essence_error_messages, #essence_errors, #has_ingredient?, #ingredient

Methods included from ElementContents

#content_by_name, #content_by_type, #content_definition_for, #content_definitions, #content_for_rss_description, #content_for_rss_title, #contents_by_name, #contents_by_type, #contents_with_errors, #copy_contents_to, #has_validations?, #richtext_contents_ids, #update_contents

Methods included from Definitions

#definition

Methods included from Hints

#has_hint?, #hint

Methods included from Taggable

included, #tag_list=

Methods included from Logger

#log_warning, warn

Instance Attribute Details

#autogenerate_contentsObject

Returns the value of attribute autogenerate_contents.



97
98
99
# File 'app/models/alchemy/element.rb', line 97

def autogenerate_contents
  @autogenerate_contents
end

#autogenerate_nested_elementsObject

Returns the value of attribute autogenerate_nested_elements.



98
99
100
# File 'app/models/alchemy/element.rb', line 98

def autogenerate_nested_elements
  @autogenerate_nested_elements
end

Class Method Details

.all_from_clipboard(clipboard) ⇒ Object



184
185
186
187
188
# File 'app/models/alchemy/element.rb', line 184

def all_from_clipboard(clipboard)
  return [] if clipboard.nil?

  where(id: clipboard.collect { |e| e["id"] })
end

.all_from_clipboard_for_page(clipboard, page) ⇒ Object

All elements in clipboard that could be placed on page



192
193
194
195
196
197
198
# File 'app/models/alchemy/element.rb', line 192

def all_from_clipboard_for_page(clipboard, page)
  return [] if clipboard.nil? || page.nil?

  all_from_clipboard(clipboard).select { |ce|
    page.available_element_names.include?(ce.name)
  }
end

.copy(source_element, differences = {}) ⇒ Object

This methods does a copy of source and all depending contents and all of their depending essences.

Options

You can pass a differences Hash as second option to update attributes for the copy.

Example

@copy = Alchemy::Element.copy(@element, {public: false})
@copy.public? # => false


161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'app/models/alchemy/element.rb', line 161

def copy(source_element, differences = {})
  attributes = source_element.attributes.with_indifferent_access
    .except(*SKIPPED_ATTRIBUTES_ON_COPY)
    .merge(differences)
    .merge({
      autogenerate_contents: false,
      autogenerate_nested_elements: false,
      tag_list: source_element.tag_list,
    })

  new_element = create!(attributes)

  if source_element.contents.any?
    source_element.copy_contents_to(new_element)
  end

  if source_element.nested_elements.any?
    source_element.copy_nested_elements_to(new_element)
  end

  new_element
end

.new(attributes = {}) ⇒ Object

Builds a new element as described in /config/alchemy/elements.yml

  • Returns a new Alchemy::Element object if no name is given in attributes, because the definition can not be found w/o name

  • Raises Alchemy::ElementDefinitionError if no definition for given attributes could be found



138
139
140
141
142
143
144
145
146
147
148
# File 'app/models/alchemy/element.rb', line 138

def new(attributes = {})
  return super if attributes[:name].blank?

  element_attributes = attributes.to_h.merge(name: attributes[:name].split("#").first)
  element_definition = Element.definition_by_name(element_attributes[:name])
  if element_definition.nil?
    raise(ElementDefinitionError, attributes)
  end

  super(element_definition.merge(element_attributes).except(*FORBIDDEN_DEFINITION_ATTRIBUTES))
end

Instance Method Details

#cache_keyObject

Returns the key that’s taken for cache path.

Uses the page’s published_at value that’s updated when the user publishes the page.

If the page is the current preview it uses the element’s updated_at value as cache key.



312
313
314
315
316
317
318
# File 'app/models/alchemy/element.rb', line 312

def cache_key
  if Page.current_preview == page
    "alchemy/elements/#{id}-#{updated_at}"
  else
    "alchemy/elements/#{id}-#{page.published_at}"
  end
end

#compact?Boolean

Defined as compact element?

Returns:

  • (Boolean)


252
253
254
# File 'app/models/alchemy/element.rb', line 252

def compact?
  definition["compact"] == true
end

#copy_nested_elements_to(target_element) ⇒ Object

Copy all nested elements from current element to given target element.



326
327
328
329
330
331
332
333
# File 'app/models/alchemy/element.rb', line 326

def copy_nested_elements_to(target_element)
  nested_elements.map do |nested_element|
    Element.copy(nested_element, {
      parent_element_id: target_element.id,
      page_id: target_element.page_id,
    })
  end
end

#deprecated?Boolean

Defined as deprecated element?

You can either set true or a String on your elements definition.

Passing true

- name: old_element
  deprecated: true

The deprecation notice can be translated. Either as global notice for all deprecated elements.

en:
  alchemy:
    element_deprecation_notice: Foo baz widget is deprecated

Or add a translation to your locale file for a per element notice.

en:
  alchemy:
    element_deprecation_notices:
      old_element: Foo baz widget is deprecated

Pass a String

- name: old_element
  deprecated: This element will be removed soon.

Returns:

  • (Boolean)

    Boolean



284
285
286
# File 'app/models/alchemy/element.rb', line 284

def deprecated?
  !!definition["deprecated"]
end

#expanded?Boolean

The opposite of folded?

Returns:

  • (Boolean)


247
248
249
# File 'app/models/alchemy/element.rb', line 247

def expanded?
  !folded?
end

#nestable_elementsObject

A collection of element names that can be nested inside this element.



321
322
323
# File 'app/models/alchemy/element.rb', line 321

def nestable_elements
  definition.fetch("nestable_elements", [])
end

#next(name = nil) ⇒ Object

Returns next public element from same page.

Pass an element name to get next of this kind.



205
206
207
208
# File 'app/models/alchemy/element.rb', line 205

def next(name = nil)
  elements = page.elements.published.where("position > ?", position)
  select_element(elements, name, :asc)
end

#prev(name = nil) ⇒ Object

Returns previous public element from same page.

Pass an element name to get previous of this kind.



214
215
216
217
# File 'app/models/alchemy/element.rb', line 214

def prev(name = nil)
  elements = page.elements.published.where("position < ?", position)
  select_element(elements, name, :desc)
end

#store_page(page) ⇒ Object

Stores the page into touchable_pages (Pages that have to be touched after updating the element).



220
221
222
223
224
225
226
# File 'app/models/alchemy/element.rb', line 220

def store_page(page)
  return true if page.nil?

  unless touchable_pages.include? page
    touchable_pages << page
  end
end

#taggable?Boolean

Returns true if the definition of this element has a taggable true value.

Returns:

  • (Boolean)


242
243
244
# File 'app/models/alchemy/element.rb', line 242

def taggable?
  definition["taggable"] == true
end

#to_partial_pathObject

The element’s view partial is dependent from its name

Define elements

Elements are defined in the config/alchemy/elements.yml file

- name: article
  contents:
  ...

Override the view

Element partials live in app/views/alchemy/elements



302
303
304
# File 'app/models/alchemy/element.rb', line 302

def to_partial_path
  "alchemy/elements/#{name}"
end

#trash!Object

Trashing an element means nullifying its position, folding and unpublishing it.



229
230
231
232
233
# File 'app/models/alchemy/element.rb', line 229

def trash!
  self.public = false
  self.folded = true
  remove_from_list
end

#trashed?Boolean

Returns:

  • (Boolean)


236
237
238
# File 'app/models/alchemy/element.rb', line 236

def trashed?
  position.nil?
end