Class: Alchemy::Content
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Alchemy::Content
- Defined in:
- app/models/alchemy/content.rb
Defined Under Namespace
Modules: Factory
Class Method Summary collapse
-
.translated_label_for(content_name, element_name = nil) ⇒ Object
Returns the translated label for a content name.
Instance Method Summary collapse
-
#default_text(default) ⇒ Object
Returns the default value from content definition.
-
#dom_id ⇒ Object
Returns a string used as dom id on html elements.
- #essence_partial_name ⇒ Object
- #essence_validation_failed? ⇒ Boolean
- #form_field_id(essence_column = 'ingredient') ⇒ Object
-
#form_field_name(essence_column = 'ingredient') ⇒ Object
Returns a string to be passed to Rails form field tags to ensure we have same params layout everywhere.
-
#has_custom_tinymce_config? ⇒ Boolean
Returns true if there is a tinymce setting defined that contains settings.
-
#has_tinymce? ⇒ Boolean
Returns true if there is a tinymce setting defined on the content definiton or if the
essence.has_tinymce?
returns true. - #has_validations? ⇒ Boolean
-
#ingredient ⇒ Object
Gets the ingredient from essence.
-
#ingredient=(value) ⇒ Object
Sets the ingredient from essence.
- #linked? ⇒ Boolean
-
#name_for_label ⇒ Object
Returns the translated name for displaying in labels, etc.
- #normalized_essence_type ⇒ Object
-
#preview_content? ⇒ Boolean
Returns true if this content should be taken for element preview.
-
#preview_text(maxlength = 30) ⇒ Object
Proxy method that returns the preview text from essence.
-
#scope_condition ⇒ Object
ActsAsList scope.
-
#serialize ⇒ Object
Serialized object representation for json api.
-
#serialized_ingredient ⇒ Object
Ingredient value from essence for json api.
-
#settings ⇒ Object
Settings from the elements.yml definition.
-
#settings_value(key, options = {}) ⇒ Object
Fetches value from settings.
- #siblings ⇒ Object
-
#tinymce_class_name ⇒ Object
Returns css class names for the content textarea.
-
#to_partial_path ⇒ Object
The content’s view partial is dependent from its name.
-
#update_essence(params = {}) ⇒ Object
Updates the essence.
Methods included from Factory
Methods included from Hints
Methods included from Touching
Methods included from Logger
Class Method Details
.translated_label_for(content_name, element_name = nil) ⇒ Object
Returns the translated label for a content name.
Translate it in your locale yml file:
alchemy:
content_names:
foo: Bar
Optionally you can scope your content name to an element:
alchemy:
content_names:
article:
foo: Baz
77 78 79 80 81 82 83 |
# File 'app/models/alchemy/content.rb', line 77 def translated_label_for(content_name, element_name = nil) Alchemy.t( content_name, scope: "content_names.#{element_name}", default: Alchemy.t("content_names.#{content_name}", default: content_name.humanize) ) end |
Instance Method Details
#default_text(default) ⇒ Object
Returns the default value from content definition
If the value is a symbol it gets passed through i18n inside the alchemy.default_content_texts
scope
261 262 263 264 265 266 267 268 |
# File 'app/models/alchemy/content.rb', line 261 def default_text(default) case default when Symbol Alchemy.t(default, scope: :default_content_texts) else default end end |
#dom_id ⇒ Object
Returns a string used as dom id on html elements.
204 205 206 207 |
# File 'app/models/alchemy/content.rb', line 204 def dom_id return '' if essence.nil? "#{essence_partial_name}_#{id}" end |
#essence_partial_name ⇒ Object
232 233 234 235 |
# File 'app/models/alchemy/content.rb', line 232 def essence_partial_name return '' if essence.nil? essence.partial_name end |
#essence_validation_failed? ⇒ Boolean
173 174 175 |
# File 'app/models/alchemy/content.rb', line 173 def essence_validation_failed? essence.errors.any? end |
#form_field_id(essence_column = 'ingredient') ⇒ Object
199 200 201 |
# File 'app/models/alchemy/content.rb', line 199 def form_field_id(essence_column = 'ingredient') "contents_#{id}_#{essence_column}" end |
#form_field_name(essence_column = 'ingredient') ⇒ Object
Returns a string to be passed to Rails form field tags to ensure we have same params layout everywhere.
Example:
<%= text_field_tag content.form_field_name, content.ingredient %>
Options:
You can pass an Essence column_name. Default is ‘ingredient’
Example:
<%= text_field_tag content.form_field_name(:link), content.ingredient %>
195 196 197 |
# File 'app/models/alchemy/content.rb', line 195 def form_field_name(essence_column = 'ingredient') "contents[#{id}][#{essence_column}]" end |
#has_custom_tinymce_config? ⇒ Boolean
Returns true if there is a tinymce setting defined that contains settings.
248 249 250 |
# File 'app/models/alchemy/content.rb', line 248 def has_custom_tinymce_config? settings[:tinymce].is_a?(Hash) end |
#has_tinymce? ⇒ Boolean
Returns true if there is a tinymce setting defined on the content definiton or if the essence.has_tinymce?
returns true.
243 244 245 |
# File 'app/models/alchemy/content.rb', line 243 def has_tinymce? settings[:tinymce].present? || (essence.present? && essence.has_tinymce?) end |
#has_validations? ⇒ Boolean
177 178 179 |
# File 'app/models/alchemy/content.rb', line 177 def has_validations? definition['validate'].present? end |
#ingredient ⇒ Object
Gets the ingredient from essence
127 128 129 130 |
# File 'app/models/alchemy/content.rb', line 127 def ingredient return nil if essence.nil? essence.ingredient end |
#ingredient=(value) ⇒ Object
Sets the ingredient from essence
152 153 154 155 |
# File 'app/models/alchemy/content.rb', line 152 def ingredient=(value) raise EssenceMissingError if essence.nil? essence.ingredient = value end |
#linked? ⇒ Boolean
214 215 216 |
# File 'app/models/alchemy/content.rb', line 214 def linked? essence && !essence.link.blank? end |
#name_for_label ⇒ Object
Returns the translated name for displaying in labels, etc.
210 211 212 |
# File 'app/models/alchemy/content.rb', line 210 def name_for_label self.class.translated_label_for(name, element.name) end |
#normalized_essence_type ⇒ Object
237 238 239 |
# File 'app/models/alchemy/content.rb', line 237 def normalized_essence_type self.class.normalize_essence_type(essence_type) end |
#preview_content? ⇒ Boolean
Returns true if this content should be taken for element preview.
219 220 221 222 223 224 |
# File 'app/models/alchemy/content.rb', line 219 def preview_content? if definition['take_me_for_preview'] ActiveSupport::Deprecation.warn("Content definition's `take_me_for_preview` key is deprecated. Please use `as_element_title` instead.") end !!definition['take_me_for_preview'] || !!definition['as_element_title'] end |
#preview_text(maxlength = 30) ⇒ Object
Proxy method that returns the preview text from essence.
228 229 230 |
# File 'app/models/alchemy/content.rb', line 228 def preview_text(maxlength = 30) essence.preview_text(maxlength) end |
#scope_condition ⇒ Object
ActsAsList scope
35 36 37 38 |
# File 'app/models/alchemy/content.rb', line 35 def scope_condition # Fixes a bug with postgresql having a wrong element_id value, if element_id is nil. "element_id = #{element_id || 'null'} AND essence_type = '#{essence_type}'" end |
#serialize ⇒ Object
Serialized object representation for json api
134 135 136 137 138 139 140 |
# File 'app/models/alchemy/content.rb', line 134 def serialize { name: name, value: serialized_ingredient, link: essence.try(:link) }.delete_if { |_k, v| v.blank? } end |
#serialized_ingredient ⇒ Object
Ingredient value from essence for json api
If the essence responds to serialized_ingredient
method it takes this otherwise it uses the ingredient column.
147 148 149 |
# File 'app/models/alchemy/content.rb', line 147 def serialized_ingredient essence.try(:serialized_ingredient) || ingredient end |
#settings ⇒ Object
Settings from the elements.yml definition
106 107 108 109 |
# File 'app/models/alchemy/content.rb', line 106 def settings return {} if definition.blank? @settings ||= definition.fetch('settings', {}).symbolize_keys end |
#settings_value(key, options = {}) ⇒ Object
Fetches value from settings
117 118 119 |
# File 'app/models/alchemy/content.rb', line 117 def settings_value(key, = {}) settings.update( || {}).symbolize_keys[key.to_sym] end |
#siblings ⇒ Object
121 122 123 124 |
# File 'app/models/alchemy/content.rb', line 121 def siblings return [] if !element element.contents end |
#tinymce_class_name ⇒ Object
Returns css class names for the content textarea.
253 254 255 |
# File 'app/models/alchemy/content.rb', line 253 def tinymce_class_name "has_tinymce" + (has_custom_tinymce_config? ? " #{element.name}_#{name}" : "") end |
#to_partial_path ⇒ Object
The content’s view partial is dependent from its name
Define contents
Contents are defined in the config/alchemy/elements.yml
file
- name: article
contents:
- name: headline
type: EssenceText
Override the view
Content partials live in app/views/alchemy/essences
101 102 103 |
# File 'app/models/alchemy/content.rb', line 101 def to_partial_path "alchemy/essences/#{essence_partial_name}_view" end |
#update_essence(params = {}) ⇒ Object
Updates the essence.
Called from Alchemy::Element#update_contents
Adds errors to self.base if essence validation fails.
163 164 165 166 167 168 169 170 171 |
# File 'app/models/alchemy/content.rb', line 163 def update_essence(params = {}) raise EssenceMissingError if essence.nil? if essence.update(params) return true else errors.add(:essence, :validation_failed) return false end end |