Class: Alchemy::Element Deprecated

Inherits:
BaseRecord
  • Object
show all
Includes:
Definitions, ElementIngredients, Presenters, Hints, Logger, Taggable
Defined in:
app/models/alchemy/element/dom_id.rb,
app/models/alchemy/element.rb,
app/models/alchemy/element/presenters.rb,
app/models/alchemy/element/definitions.rb,
app/models/alchemy/element/element_ingredients.rb

Overview

Deprecated.

Use a headline ingredient with anchor setting instead.

Returns a dom id used for elements html id tag.

Uses the elements name and its position on the page. If the element is nested in a parent element it prefixes the id with the parent elements dom_id.

Register your own dom id class with

Alchemy::Element.dom_id_class = MyDomIdClass

Defined Under Namespace

Modules: Definitions, ElementIngredients, Presenters Classes: DomId

Constant Summary collapse

NAME_REGEXP =
/\A[a-z0-9_-]+\z/
FORBIDDEN_DEFINITION_ATTRIBUTES =
[
  "amount",
  "autogenerate",
  "compact",
  "deprecated",
  "hint",
  "ingredients",
  "message",
  "nestable_elements",
  "taggable",
  "warning"
].freeze

Constants included from SearchableResource

SearchableResource::SEARCHABLE_COLUMN_TYPES

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_ingredient, #preview_text

Methods included from ElementIngredients

#copy_ingredients_to, #has_validations?, #has_value_for?, #ingredient_by_role, #ingredient_by_type, #ingredient_definition_for, #ingredient_definitions, #ingredients_by_type, #ingredients_with_errors, #richtext_ingredients_ids, #value_for

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

Methods included from SearchableResource

#ransackable_associations, #ransackable_attributes, #ransortable_attributes

Instance Attribute Details

#autogenerate_nested_elementsObject

Returns the value of attribute autogenerate_nested_elements.



92
93
94
# File 'app/models/alchemy/element.rb', line 92

def autogenerate_nested_elements
  @autogenerate_nested_elements
end

Class Method Details

.all_from_clipboard(clipboard) ⇒ Object



170
171
172
173
174
# File 'app/models/alchemy/element.rb', line 170

def all_from_clipboard(clipboard)
  return none 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



178
179
180
181
182
# File 'app/models/alchemy/element.rb', line 178

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

  all_from_clipboard(clipboard).where(name: page.available_element_names)
end

.all_from_clipboard_for_parent_element(clipboard, parent_element) ⇒ Object

All elements in clipboard that could be placed as a child of ‘parent_element`



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

def all_from_clipboard_for_parent_element(clipboard, parent_element)
  return none if clipboard.nil? || parent_element.nil?

  all_from_clipboard(clipboard).where(name: parent_element.definition["nestable_elements"])
end

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

This methods does a copy of source and all its ingredients.

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


166
167
168
# File 'app/models/alchemy/element.rb', line 166

def copy(source_element, differences = {})
  Alchemy::DuplicateElement.new(source_element).call(differences)
end

.dom_id_classObject

Deprecated.

The class responsible for the dom_id of elements. Defaults to Alchemy::Element::DomId.



140
141
142
143
144
145
# File 'app/models/alchemy/element.rb', line 140

def dom_id_class
  if caller.none? { |l| l =~ Regexp.new("alchemy/element/presenters.rb:87:in `dom_id'") }
    Alchemy::Deprecation.warn("dom_id_class is deprecated and will be removed from Alchemy 8.0. Please pass an id to the element_view_for helper instead.")
  end
  @_dom_id_class || DomId
end

.dom_id_class=(klass) ⇒ Object

Deprecated.

Register a custom DomId class responsible for the dom_id of elements. Defaults to Alchemy::Element::DomId.



150
151
152
# File 'app/models/alchemy/element.rb', line 150

def dom_id_class=(klass)
  @_dom_id_class = klass
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



125
126
127
128
129
130
131
132
133
134
135
# File 'app/models/alchemy/element.rb', line 125

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

#compact?Boolean

Defined as compact element?

Returns:

  • (Boolean)


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

def compact?
  definition["compact"] == true
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



273
274
275
# File 'app/models/alchemy/element.rb', line 273

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

#expanded?Boolean

The opposite of folded?

Returns:

  • (Boolean)


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

def expanded?
  !folded?
end

#nestable_elementsObject

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



296
297
298
# File 'app/models/alchemy/element.rb', line 296

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.



207
208
209
210
# File 'app/models/alchemy/element.rb', line 207

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

#parent_element_idsObject

Heavily unoptimized naive way to get all parent ids



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

def parent_element_ids
  ids ||= []
  parent = parent_element
  while parent
    ids.push parent.id
    parent = parent.parent_element
  end
  ids
end

#prev(name = nil) ⇒ Object

Returns previous public element from same page.

Pass an element name to get previous of this kind.



216
217
218
219
# File 'app/models/alchemy/element.rb', line 216

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



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

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)


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

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

Override the view

Element partials live in app/views/alchemy/elements



291
292
293
# File 'app/models/alchemy/element.rb', line 291

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