Class: Alchemy::Content

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Factory, Hints, Logger, Touching
Defined in:
app/models/alchemy/content.rb

Defined Under Namespace

Modules: Factory

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Factory

#create_essence!, #description

Methods included from Hints

#has_hint?, #hint

Methods included from Touching

#touch

Methods included from Logger

#log_warning, warn

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)
  I18n.t(
    content_name,
    scope: "content_names.#{element_name}",
    default: I18n.t("content_names.#{content_name}", default: content_name.humanize)
  )
end

Instance Method Details

#default_text(default) ⇒ Object

Returns the default value from content description If the value is a symbol it gets passed through i18n inside the alchemy.default_content_texts scope



245
246
247
248
249
250
251
252
# File 'app/models/alchemy/content.rb', line 245

def default_text(default)
  case default
  when Symbol
    I18n.t(default, scope: :default_content_texts)
  else
    default
  end
end

#dom_idObject

Returns a string used as dom id on html elements.



194
195
196
197
# File 'app/models/alchemy/content.rb', line 194

def dom_id
  return '' if essence.nil?
  "#{essence_partial_name}_#{id}"
end

#essence_partial_nameObject



222
223
224
225
# File 'app/models/alchemy/content.rb', line 222

def essence_partial_name
  return '' if essence.nil?
  essence.partial_name
end

#essence_validation_failed?Boolean

Returns:

  • (Boolean)


163
164
165
# File 'app/models/alchemy/content.rb', line 163

def essence_validation_failed?
  essence.errors.any?
end

#form_field_id(essence_column = 'ingredient') ⇒ Object



189
190
191
# File 'app/models/alchemy/content.rb', line 189

def form_field_id(essence_column = 'ingredient')
  "contents_#{self.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 %>


185
186
187
# File 'app/models/alchemy/content.rb', line 185

def form_field_name(essence_column = 'ingredient')
  "contents[#{self.id}][#{essence_column}]"
end

#has_custom_tinymce_config?Boolean

Returns:

  • (Boolean)


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

def has_custom_tinymce_config?
  settings[:tinymce].present?
end

#has_validations?Boolean

Returns:

  • (Boolean)


167
168
169
# File 'app/models/alchemy/content.rb', line 167

def has_validations?
  description['validate'].present?
end

#ingredientObject

Gets the ingredient from essence



117
118
119
120
# File 'app/models/alchemy/content.rb', line 117

def ingredient
  return nil if essence.nil?
  essence.ingredient
end

#ingredient=(value) ⇒ Object

Sets the ingredient from essence



142
143
144
145
# File 'app/models/alchemy/content.rb', line 142

def ingredient=(value)
  raise EssenceMissingError if essence.nil?
  essence.ingredient = value
end

#linked?Boolean

Returns:

  • (Boolean)


204
205
206
# File 'app/models/alchemy/content.rb', line 204

def linked?
  essence && !essence.link.blank?
end

#name_for_labelObject

Returns the translated name for displaying in labels, etc.



200
201
202
# File 'app/models/alchemy/content.rb', line 200

def name_for_label
  self.class.translated_label_for(self.name, self.element.name)
end

#normalized_essence_typeObject



227
228
229
# File 'app/models/alchemy/content.rb', line 227

def normalized_essence_type
  self.class.normalize_essence_type(self.essence_type)
end

#preview_content?Boolean

Returns true if this content should be taken for element preview.

Returns:

  • (Boolean)


209
210
211
212
213
214
# File 'app/models/alchemy/content.rb', line 209

def preview_content?
  if description['take_me_for_preview']
    ActiveSupport::Deprecation.warn("Content definition's `take_me_for_preview` key is deprecated. Please use `as_element_title` instead.")
  end
  !!description['take_me_for_preview'] || !!description['as_element_title']
end

#preview_text(maxlength = 30) ⇒ Object

Proxy method that returns the preview text from essence.



218
219
220
# File 'app/models/alchemy/content.rb', line 218

def preview_text(maxlength = 30)
  essence.preview_text(maxlength)
end

#scope_conditionObject

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

#serializeObject

Serialized object representation for json api



124
125
126
127
128
129
130
# File 'app/models/alchemy/content.rb', line 124

def serialize
  {
    name: name,
    value: serialized_ingredient,
    link: essence.try(:link)
  }.delete_if { |_k, v| v.blank? }
end

#serialized_ingredientObject

Ingredient value from essence for json api

If the essence responds to serialized_ingredient method it takes this otherwise it uses the ingredient column.



137
138
139
# File 'app/models/alchemy/content.rb', line 137

def serialized_ingredient
  essence.try(:serialized_ingredient) || ingredient
end

#settingsObject

Settings from the elements.yml definition



106
107
108
109
# File 'app/models/alchemy/content.rb', line 106

def settings
  return {} if description.blank?
  @settings ||= description.fetch('settings', {}).symbolize_keys
end

#siblingsObject



111
112
113
114
# File 'app/models/alchemy/content.rb', line 111

def siblings
  return [] if !element
  self.element.contents
end

#tinymce_class_nameObject



235
236
237
238
239
240
241
# File 'app/models/alchemy/content.rb', line 235

def tinymce_class_name
  if has_custom_tinymce_config?
    "custom_tinymce #{element.name}_#{name}"
  else
    "default_tinymce"
  end
end

#to_partial_pathObject

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.



153
154
155
156
157
158
159
160
161
# File 'app/models/alchemy/content.rb', line 153

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