Module: Alchemy::Element::ElementContents
- Included in:
- Alchemy::Element
- Defined in:
- app/models/alchemy/element/element_contents.rb
Instance Method Summary collapse
-
#content_by_name(name) ⇒ Object
Find first content from element by given name.
-
#content_by_type(essence_type) ⇒ Object
Find first content from element by given essence type.
-
#content_definition_for(content_name) ⇒ Object
Returns the definition for given content_name.
-
#content_definitions ⇒ Object
Returns the array with the hashes for all element contents in the elements.yml file.
-
#content_for_rss_description ⇒ Object
Returns the content that is marked as rss description.
-
#content_for_rss_title ⇒ Object
Returns the content that is marked as rss title.
-
#contents_by_name(name) ⇒ Object
(also: #all_contents_by_name)
All contents from element by given name.
-
#contents_by_type(essence_type) ⇒ Object
(also: #all_contents_by_type)
All contents from element by given essence type.
-
#contents_with_errors ⇒ Object
All element contents where the essence validation has failed.
-
#copy_contents_to(element) ⇒ Object
Copy current content’s contents to given target element.
-
#has_validations? ⇒ Boolean
True, if any of the element’s contents has essence validations defined.
-
#richtext_contents_ids ⇒ Object
Returns an array of all EssenceRichtext contents ids from elements.
-
#update_contents(contents_attributes) ⇒ Boolean
Updates all related contents by calling
update_essence
on each of them.
Instance Method Details
#content_by_name(name) ⇒ Object
Find first content from element by given name.
8 9 10 |
# File 'app/models/alchemy/element/element_contents.rb', line 8 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.
13 14 15 |
# File 'app/models/alchemy/element/element_contents.rb', line 13 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
99 100 101 102 103 104 105 106 |
# File 'app/models/alchemy/element/element_contents.rb', line 99 def content_definition_for(content_name) if content_definitions.blank? log_warning "Element #{name} is missing the content definition for #{content_name}" nil else content_definitions.detect { |d| d['name'] == content_name.to_s } end end |
#content_definitions ⇒ Object
Returns the array with the hashes for all element contents in the elements.yml file
93 94 95 96 |
# File 'app/models/alchemy/element/element_contents.rb', line 93 def content_definitions return nil if definition.blank? definition['contents'] end |
#content_for_rss_description ⇒ Object
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
88 89 90 |
# File 'app/models/alchemy/element/element_contents.rb', line 88 def content_for_rss_description ('description') end |
#content_for_rss_title ⇒ Object
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
74 75 76 |
# File 'app/models/alchemy/element/element_contents.rb', line 74 def content_for_rss_title ('title') end |
#contents_by_name(name) ⇒ Object Also known as: all_contents_by_name
All contents from element by given name.
18 19 20 |
# File 'app/models/alchemy/element/element_contents.rb', line 18 def contents_by_name(name) contents.select { |content| content.name == name.to_s } end |
#contents_by_type(essence_type) ⇒ Object Also known as: all_contents_by_type
All contents from element by given essence type.
24 25 26 27 28 |
# File 'app/models/alchemy/element/element_contents.rb', line 24 def contents_by_type(essence_type) contents.select do |content| content.essence_type == Content.normalize_essence_type(essence_type) end end |
#contents_with_errors ⇒ Object
All element contents where the essence validation has failed.
130 131 132 |
# File 'app/models/alchemy/element/element_contents.rb', line 130 def contents_with_errors contents.select(&:essence_validation_failed?) end |
#copy_contents_to(element) ⇒ Object
Copy current content’s contents to given target element
58 59 60 61 62 |
# File 'app/models/alchemy/element/element_contents.rb', line 58 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.
125 126 127 |
# File 'app/models/alchemy/element/element_contents.rb', line 125 def has_validations? !contents.detect(&:has_validations?).blank? end |
#richtext_contents_ids ⇒ Object
Returns an array of all EssenceRichtext contents ids from elements
This is used to re-initialize the TinyMCE editor in the element editor.
112 113 114 115 116 117 118 119 120 121 122 |
# File 'app/models/alchemy/element/element_contents.rb', line 112 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) = nested_elements. if .present? ids += .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"}
)
48 49 50 51 52 53 54 55 |
# File 'app/models/alchemy/element/element_contents.rb', line 48 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 |