Class: Alchemy::ElementDefinition

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Translation
Includes:
ActiveModel::Attributes, ActiveModel::Model, Hints
Defined in:
app/models/alchemy/element_definition.rb

Constant Summary collapse

DEFAULT_ICON_NAME =
"layout-bottom-2-line"

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Hints

#has_hint?, #hint

Class Method Details

.add(definition) ⇒ Object

Add additional page definitions to collection.

Useful for extending the elements from an Alchemy module.

Usage Example

Call +Alchemy::ElementDefinition.add(your_definition)+ in your engine.rb file.

Parameters:

  • You (Array || Hash)

    can pass a single element definition as Hash, or a collection of elements as Array.



61
62
63
64
# File 'app/models/alchemy/element_definition.rb', line 61

def add(definition)
  all
  @definitions += Array.wrap(definition).map { new(**_1) }
end

.allObject

Returns the definitions from elements.yml file.

Place a elements.yml file inside your apps config/alchemy folder to define your own set of elements



46
47
48
# File 'app/models/alchemy/element_definition.rb', line 46

def all
  @definitions ||= read_definitions_file.map { new(**_1) }
end

.definitions_file_pathPathname

The absolute elements.yml file path

Returns:

  • (Pathname)


80
81
82
# File 'app/models/alchemy/element_definition.rb', line 80

def definitions_file_path
  Rails.root.join("config", "alchemy", "elements.yml")
end

.get(name) ⇒ Object

Returns one element definition by given name.



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

def get(name)
  return new if name.blank?

  all.detect { _1.name.casecmp(name).zero? }
end

.reset!Object



74
75
76
# File 'app/models/alchemy/element_definition.rb', line 74

def reset!
  @definitions = nil
end

Instance Method Details

#attributesObject



110
111
112
# File 'app/models/alchemy/element_definition.rb', line 110

def attributes
  super.with_indifferent_access
end

#deprecation_noticeObject

Returns a deprecation notice for elements marked deprecated

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

Custom deprecation notices

Use general element deprecation notice

- name: old_element
  deprecated: true

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

en:
  alchemy:
    element_deprecation_notices:
      old_element: Foo baz widget is deprecated

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

en:
  alchemy:
    element_deprecation_notice: Foo baz widget is deprecated

or pass string as deprecation notice.

- name: old_element
  deprecated: This element will be removed soon.


148
149
150
151
152
153
154
155
156
157
158
159
# File 'app/models/alchemy/element_definition.rb', line 148

def deprecation_notice
  case deprecated
  when String
    deprecated
  when TrueClass
    Alchemy.t(
      name,
      scope: :element_deprecation_notices,
      default: Alchemy.t(:element_deprecated)
    )
  end
end

#icon_fileObject



161
162
163
# File 'app/models/alchemy/element_definition.rb', line 161

def icon_file
  @_icon_file ||= File.read(icon_file_path).html_safe
end

#icon_file_nameObject



165
166
167
# File 'app/models/alchemy/element_definition.rb', line 165

def icon_file_name
  "#{icon_name}.svg"
end

#icon_nameObject



169
170
171
172
173
174
175
# File 'app/models/alchemy/element_definition.rb', line 169

def icon_name
  case icon
  when TrueClass then name
  when String then icon
  else DEFAULT_ICON_NAME
  end
end

#ingredientsObject



114
115
116
# File 'app/models/alchemy/element_definition.rb', line 114

def ingredients
  super.map { IngredientDefinition.new(**_1) }
end