Class: Alchemy::Content

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

Overview

Holds everything concerning the building and creating of contents and the related essence object.

Defined Under Namespace

Modules: Factory

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Factory

#build_essence, #create_essence!, #definition

Methods included from Hints

#has_hint?, #hint

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


68
69
70
71
72
73
74
# File 'app/models/alchemy/content.rb', line 68

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_value(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



238
239
240
241
242
243
244
245
# File 'app/models/alchemy/content.rb', line 238

def default_value(default = definition[:default])
  case default
  when Symbol
    Alchemy.t(default, scope: :default_content_texts)
  else
    default
  end
end

#deprecated?Boolean

Returns:

  • (Boolean)


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

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

#dom_idObject

Returns a string used as dom id on html elements.



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

def dom_id
  return "" if essence.nil?

  "#{essence_partial_name}_#{id}"
end

#essence_partial_nameObject



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

def essence_partial_name
  return "" if essence.nil?

  essence.partial_name
end

#essence_validation_failed?Boolean

Returns:

  • (Boolean)


169
170
171
# File 'app/models/alchemy/content.rb', line 169

def essence_validation_failed?
  essence.errors.any?
end

#has_custom_tinymce_config?Boolean

Returns true if there is a tinymce setting defined that contains settings.

Returns:

  • (Boolean)


225
226
227
# File 'app/models/alchemy/content.rb', line 225

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.

Returns:

  • (Boolean)


220
221
222
# File 'app/models/alchemy/content.rb', line 220

def has_tinymce?
  settings[:tinymce].present? || (essence.present? && essence.has_tinymce?)
end

#has_validations?Boolean

Returns:

  • (Boolean)


173
174
175
# File 'app/models/alchemy/content.rb', line 173

def has_validations?
  definition["validate"].present?
end

#ingredientObject

Gets the ingredient from essence



120
121
122
123
124
# File 'app/models/alchemy/content.rb', line 120

def ingredient
  return nil if essence.nil?

  essence.ingredient
end

#ingredient=(value) ⇒ Object

Sets the ingredient from essence



146
147
148
149
150
# File 'app/models/alchemy/content.rb', line 146

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

  essence.ingredient = value
end

#linked?Boolean

Returns:

  • (Boolean)


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

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

#name_for_labelObject

Returns the translated name for displaying in labels, etc.



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

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

#normalized_essence_typeObject



214
215
216
# File 'app/models/alchemy/content.rb', line 214

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.

Returns:

  • (Boolean)


198
199
200
# File 'app/models/alchemy/content.rb', line 198

def preview_content?
  !!definition["as_element_title"]
end

#preview_text(maxlength = 30) ⇒ Object

Proxy method that returns the preview text from essence.



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

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

#serializeObject

Serialized object representation for json api



128
129
130
131
132
133
134
# File 'app/models/alchemy/content.rb', line 128

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.



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

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

#settingsObject

Settings from the elements.yml definition



97
98
99
100
101
# File 'app/models/alchemy/content.rb', line 97

def settings
  return {} if definition.blank?

  @settings ||= definition.fetch(:settings, {})
end

#settings_value(key, options = {}) ⇒ Object

Fetches value from settings

Parameters:

  • key (Symbol)
    • The hash key you want to fetch the value from

  • options (Hash) (defaults to: {})
    • An optional Hash that can override the settings.

    Normally passed as options hash into the content editor view.



109
110
111
# File 'app/models/alchemy/content.rb', line 109

def settings_value(key, options = {})
  settings.update(options || {}).symbolize_keys[key.to_sym]
end

#siblingsObject



113
114
115
116
117
# File 'app/models/alchemy/content.rb', line 113

def siblings
  return [] if !element

  element.contents
end

#tinymce_class_nameObject

Returns css class names for the content textarea.



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

def tinymce_class_name
  "has_tinymce" + (has_custom_tinymce_config? ? " #{element.name}_#{name}" : "")
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



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

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.



158
159
160
161
162
163
164
165
166
167
# File 'app/models/alchemy/content.rb', line 158

def update_essence(params = {})
  raise EssenceMissingError if essence.nil?

  if essence.update(params)
    true
  else
    errors.add(:essence, :validation_failed)
    false
  end
end