Module: Alchemy::Element::ElementContents

Included in:
Alchemy::Element
Defined in:
app/models/alchemy/element/element_contents.rb

Instance Method Summary collapse

Instance Method Details

#content_by_name(name) ⇒ Object

Find first content from element by given name.



6
7
8
# File 'app/models/alchemy/element/element_contents.rb', line 6

def content_by_name(name)
  contents_by_name(name).first
end

#content_by_type(essence_type) ⇒ Object

Find first content from element by given essence type.



11
12
13
# File 'app/models/alchemy/element/element_contents.rb', line 11

def content_by_type(essence_type)
  contents_by_type(essence_type).first
end

#content_definition_for(content_name) ⇒ Object

Returns the definition for given content_name



95
96
97
98
99
100
101
102
# File 'app/models/alchemy/element/element_contents.rb', line 95

def content_definition_for(content_name)
  if content_definitions.blank?
    log_warning "Element #{name} is missing the content definition for #{content_name}"
    return nil
  else
    content_definitions.detect { |d| d['name'] == content_name }
  end
end

#content_definitionsObject

Returns the array with the hashes for all element contents in the elements.yml file



89
90
91
92
# File 'app/models/alchemy/element/element_contents.rb', line 89

def content_definitions
  return nil if definition.blank?
  definition['contents']
end

#content_for_rss_descriptionObject

Returns the content that is marked as rss description.

Mark a content as rss description in your elements.yml file:

- name: news
  contents:
  - name: body
    type: EssenceRichtext
    rss_description: true


84
85
86
# File 'app/models/alchemy/element/element_contents.rb', line 84

def content_for_rss_description
  content_for_rss_meta('description')
end

#content_for_rss_titleObject

Returns the content that is marked as rss title.

Mark a content as rss title in your elements.yml file:

- name: news
  contents:
  - name: headline
    type: EssenceText
    rss_title: true


70
71
72
# File 'app/models/alchemy/element/element_contents.rb', line 70

def content_for_rss_title
  content_for_rss_meta('title')
end

#contents_by_name(name) ⇒ Object Also known as: all_contents_by_name

All contents from element by given name.



16
17
18
# File 'app/models/alchemy/element/element_contents.rb', line 16

def contents_by_name(name)
  contents.where(name: name)
end

#contents_by_type(essence_type) ⇒ Object Also known as: all_contents_by_type

All contents from element by given essence type.



22
23
24
# File 'app/models/alchemy/element/element_contents.rb', line 22

def contents_by_type(essence_type)
  contents.where(essence_type: Content.normalize_essence_type(essence_type))
end

#contents_with_errorsObject

All element contents where the essence validation has failed.



126
127
128
# File 'app/models/alchemy/element/element_contents.rb', line 126

def contents_with_errors
  contents.select(&:essence_validation_failed?)
end

#copy_contents_to(element) ⇒ Object

Copy current content’s contents to given target element



54
55
56
57
58
# File 'app/models/alchemy/element/element_contents.rb', line 54

def copy_contents_to(element)
  contents.map do |content|
    Content.copy(content, element_id: element.id)
  end
end

#has_validations?Boolean

True, if any of the element’s contents has essence validations defined.

Returns:

  • (Boolean)


121
122
123
# File 'app/models/alchemy/element/element_contents.rb', line 121

def has_validations?
  !contents.detect(&:has_validations?).blank?
end

#richtext_contents_idsObject

Returns an array of all EssenceRichtext contents ids from elements

This is used to re-initialize the TinyMCE editor in the element editor.



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

def richtext_contents_ids
  # This is not very efficient SQL wise I know, but we need to iterate
  # recursivly through all descendent elements and I don't know how to do this
  # in pure SQL. Anyone with a better idea is welcome to submit a patch.
  ids = contents.select(&:has_tinymce?).collect(&:id)
  expanded_nested_elements = nested_elements.expanded
  if expanded_nested_elements.present?
    ids += expanded_nested_elements.collect(&:richtext_contents_ids)
  end
  ids.flatten
end

#update_contents(contents_attributes) ⇒ Boolean

Updates all related contents by calling update_essence on each of them.

Example

@element.update_contents(
  "1" => {ingredient: "Title"},
  "2" => {link: "https://google.com"}
)

Parameters:

  • contents_attributes (Hash)

    Hash of contents attributes. The keys has to be the #id of the content to update. The values a Hash of attribute names and values

Returns:

  • (Boolean)

    True if errors are blank or contents_attributes hash is nil



44
45
46
47
48
49
50
51
# File 'app/models/alchemy/element/element_contents.rb', line 44

def update_contents(contents_attributes)
  return true if contents_attributes.nil?
  contents.each do |content|
    content_hash = contents_attributes[content.id.to_s] || next
    content.update_essence(content_hash) || errors.add(:base, :essence_validation_failed)
  end
  errors.blank?
end