Class: Alchemy::ContentEditor

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
app/decorators/alchemy/content_editor.rb

Instance Method Summary collapse

Instance Method Details

#css_classesObject


11
12
13
14
15
16
17
# File 'app/decorators/alchemy/content_editor.rb', line 11

def css_classes
  [
    "content_editor",
    essence_partial_name,
    deprecated? ? "deprecated" : nil,
  ].compact
end

#data_attributesObject


19
20
21
22
23
24
# File 'app/decorators/alchemy/content_editor.rb', line 19

def data_attributes
  {
    content_id: id,
    content_name: name,
  }
end

#deprecation_noticeObject

Returns a deprecation notice for contents marked deprecated

You can either use localizations or pass a String as notice in the content definition.

Custom deprecation notices

Use general content deprecation notice

- name: element_name
  contents:
    - name: old_content
      type: EssenceText
      deprecated: true

Add a translation to your locale file for a per content notice.

en:
  alchemy:
    content_deprecation_notices:
      element_name:
        old_content: Foo baz widget is deprecated

or use the global translation that apply to all deprecated contents.

en:
  alchemy:
    content_deprecation_notice: Foo baz widget is deprecated

or pass string as deprecation notice.

- name: element_name
  contents:
    - name: old_content
      type: EssenceText
      deprecated: This content will be removed soon.

108
109
110
111
112
113
114
115
116
117
# File 'app/decorators/alchemy/content_editor.rb', line 108

def deprecation_notice
  case definition["deprecated"]
  when String
    definition["deprecated"]
  when TrueClass
    Alchemy.t(name,
              scope: [:content_deprecation_notices, element.name],
              default: Alchemy.t(:content_deprecated))
  end
end

#form_field_id(essence_column = "ingredient") ⇒ Object


44
45
46
# File 'app/decorators/alchemy/content_editor.rb', line 44

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_editor.form_field_name, content_editor.ingredient %>

Options:

You can pass an Essence column_name. Default is 'ingredient'

Example:

<%= text_field_tag content_editor.form_field_name(:link), content_editor.ingredient %>

40
41
42
# File 'app/decorators/alchemy/content_editor.rb', line 40

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

#has_warnings?Boolean

Returns:

  • (Boolean)

56
57
58
# File 'app/decorators/alchemy/content_editor.rb', line 56

def has_warnings?
  definition.blank? || deprecated?
end

#respond_to?(method_name) ⇒ Boolean

Fixes Rails partial renderer calling to_model on the object which reveals the delegated content instead of this decorator.

Returns:

  • (Boolean)

50
51
52
53
54
# File 'app/decorators/alchemy/content_editor.rb', line 50

def respond_to?(method_name)
  return false if method_name == :to_model

  super
end

#to_partial_pathObject


7
8
9
# File 'app/decorators/alchemy/content_editor.rb', line 7

def to_partial_path
  "alchemy/essences/#{essence_partial_name}_editor"
end

#warningsObject


60
61
62
63
64
65
66
67
68
69
# File 'app/decorators/alchemy/content_editor.rb', line 60

def warnings
  return unless has_warnings?

  if definition.blank?
    Logger.warn("Content #{name} is missing its definition", caller(1..1))
    Alchemy.t(:content_definition_missing)
  else
    deprecation_notice
  end
end