Class: Alchemy::ElementDefinition
- Inherits:
-
Object
- Object
- Alchemy::ElementDefinition
- 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
-
.add(definition) ⇒ Object
Add additional page definitions to collection.
-
.all ⇒ Object
Returns the definitions from elements.yml file.
-
.definitions_file_path ⇒ Pathname
The absolute
elements.ymlfile path. -
.get(name) ⇒ Object
Returns one element definition by given name.
- .reset! ⇒ Object
Instance Method Summary collapse
- #attributes ⇒ Object
-
#deprecation_notice ⇒ Object
Returns a deprecation notice for elements marked deprecated.
- #icon_file ⇒ Object
- #icon_file_name ⇒ Object
- #icon_name ⇒ Object
- #ingredients ⇒ Object
Methods included from Hints
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.
62 63 64 65 |
# File 'app/models/alchemy/element_definition.rb', line 62 def add(definition) all @definitions += Array.wrap(definition).map { new(**_1) } end |
.all ⇒ Object
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
47 48 49 |
# File 'app/models/alchemy/element_definition.rb', line 47 def all @definitions ||= read_definitions_file.map { new(**_1) } end |
.definitions_file_path ⇒ Pathname
The absolute elements.yml file path
81 82 83 |
# File 'app/models/alchemy/element_definition.rb', line 81 def definitions_file_path Rails.root.join("config", "alchemy", "elements.yml") end |
.get(name) ⇒ Object
Returns one element definition by given name.
69 70 71 72 73 |
# File 'app/models/alchemy/element_definition.rb', line 69 def get(name) return new if name.blank? all.detect { _1.name.casecmp(name).zero? } end |
.reset! ⇒ Object
75 76 77 |
# File 'app/models/alchemy/element_definition.rb', line 75 def reset! @definitions = nil end |
Instance Method Details
#attributes ⇒ Object
111 112 113 |
# File 'app/models/alchemy/element_definition.rb', line 111 def attributes super.with_indifferent_access end |
#deprecation_notice ⇒ Object
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.
149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'app/models/alchemy/element_definition.rb', line 149 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_file ⇒ Object
162 163 164 |
# File 'app/models/alchemy/element_definition.rb', line 162 def icon_file @_icon_file ||= File.read(icon_file_path).html_safe end |
#icon_file_name ⇒ Object
166 167 168 |
# File 'app/models/alchemy/element_definition.rb', line 166 def icon_file_name "#{icon_name}.svg" end |
#icon_name ⇒ Object
170 171 172 173 174 175 176 |
# File 'app/models/alchemy/element_definition.rb', line 170 def icon_name case icon when TrueClass then name when String then icon else DEFAULT_ICON_NAME end end |
#ingredients ⇒ Object
115 116 117 |
# File 'app/models/alchemy/element_definition.rb', line 115 def ingredients super.map { IngredientDefinition.new(**_1) } end |