Class: Alchemy::Ingredient
- Inherits:
-
BaseRecord
- Object
- ActiveRecord::Base
- BaseRecord
- Alchemy::Ingredient
- Defined in:
- app/models/alchemy/ingredient.rb
Direct Known Subclasses
Alchemy::Ingredients::Audio, Alchemy::Ingredients::Boolean, Alchemy::Ingredients::Datetime, Alchemy::Ingredients::File, Alchemy::Ingredients::Headline, Alchemy::Ingredients::Html, Alchemy::Ingredients::Link, Alchemy::Ingredients::Node, Alchemy::Ingredients::Number, Alchemy::Ingredients::Page, Alchemy::Ingredients::Picture, Alchemy::Ingredients::Richtext, Alchemy::Ingredients::Select, Alchemy::Ingredients::Text, Alchemy::Ingredients::Video
Defined Under Namespace
Classes: DefinitionError
Constant Summary
Constants included from SearchableResource
SearchableResource::SEARCHABLE_COLUMN_TYPES
Class Method Summary collapse
-
.allow_settings(settings) ⇒ Object
Allow to define settings on the ingredient definition.
-
.allowed_settings ⇒ Object
Allowed settings on the ingredient.
-
.normalize_type(ingredient_type) ⇒ String
Modulize ingredient type.
-
.related_object_alias(name, class_name:) ⇒ Object
Defines getter and setter method aliases for related object.
- .translated_label_for(role, element_name = nil) ⇒ Object
Instance Method Summary collapse
-
#as_view_component(options: {}, html_options: {}) ⇒ Object
The view component of the ingredient with mapped options.
-
#definition ⇒ Object
Definition hash for this ingredient from
elements.yml
file. - #deprecated? ⇒ Boolean
- #has_tinymce? ⇒ Boolean
- #has_validations? ⇒ Boolean
-
#partial_name ⇒ String
The demodulized underscored class name of the ingredient.
- #preview_ingredient? ⇒ Boolean
-
#preview_text(maxlength = 30) ⇒ Object
The first 30 characters of the value.
-
#settings ⇒ Object
Settings for this ingredient from the
elements.yml
definition. -
#value ⇒ Object
The value or the related object if present.
Methods included from ConfigMissing
Methods included from SearchableResource
#ransackable_associations, #ransackable_attributes, #ransackable_scopes, #ransortable_attributes
Class Method Details
.allow_settings(settings) ⇒ Object
Allow to define settings on the ingredient definition
83 84 85 |
# File 'app/models/alchemy/ingredient.rb', line 83 def allow_settings(settings) @allowed_settings = Array(settings) end |
.allowed_settings ⇒ Object
Allowed settings on the ingredient
88 89 90 |
# File 'app/models/alchemy/ingredient.rb', line 88 def allowed_settings @allowed_settings ||= [] end |
.normalize_type(ingredient_type) ⇒ String
Modulize ingredient type
Makes sure the passed ingredient type is in the Alchemy::Ingredients
module namespace.
If you add custom ingredient class, put them in the Alchemy::Ingredients
module namespace
70 71 72 |
# File 'app/models/alchemy/ingredient.rb', line 70 def normalize_type(ingredient_type) "Alchemy::Ingredients::#{ingredient_type.to_s.classify.demodulize}" end |
.related_object_alias(name, class_name:) ⇒ Object
Defines getter and setter method aliases for related object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'app/models/alchemy/ingredient.rb', line 46 def (name, class_name:) alias_method name, :related_object alias_method :"#{name}=", :related_object= # Somehow Rails STI does not allow us to use `alias_method` for the related_object_id define_method :"#{name}_id" do end define_method :"#{name}_id=" do |id| self. = id self. = id.nil? ? nil : class_name end end |
.translated_label_for(role, element_name = nil) ⇒ Object
74 75 76 77 78 79 80 |
# File 'app/models/alchemy/ingredient.rb', line 74 def translated_label_for(role, element_name = nil) Alchemy.t( role, scope: "ingredient_roles.#{element_name}", default: Alchemy.t("ingredient_roles.#{role}", default: role.humanize) ) end |
Instance Method Details
#as_view_component(options: {}, html_options: {}) ⇒ Object
The view component of the ingredient with mapped options.
151 152 153 |
# File 'app/models/alchemy/ingredient.rb', line 151 def as_view_component(options: {}, html_options: {}) view_component_class.new(self, **, html_options: ) end |
#definition ⇒ Object
Definition hash for this ingredient from elements.yml
file.
105 106 107 108 109 |
# File 'app/models/alchemy/ingredient.rb', line 105 def definition return IngredientDefinition.new unless element element.ingredient_definition_for(role) || IngredientDefinition.new end |
#deprecated? ⇒ Boolean
133 134 135 |
# File 'app/models/alchemy/ingredient.rb', line 133 def deprecated? !!definition.deprecated end |
#has_tinymce? ⇒ Boolean
138 139 140 |
# File 'app/models/alchemy/ingredient.rb', line 138 def has_tinymce? false end |
#has_validations? ⇒ Boolean
128 129 130 |
# File 'app/models/alchemy/ingredient.rb', line 128 def has_validations? definition.validate.any? end |
#partial_name ⇒ String
The demodulized underscored class name of the ingredient
123 124 125 |
# File 'app/models/alchemy/ingredient.rb', line 123 def partial_name self.class.name.demodulize.underscore end |
#preview_ingredient? ⇒ Boolean
143 144 145 |
# File 'app/models/alchemy/ingredient.rb', line 143 def preview_ingredient? !!definition.as_element_title end |
#preview_text(maxlength = 30) ⇒ Object
The first 30 characters of the value
Used by the Element#preview_text method.
117 118 119 |
# File 'app/models/alchemy/ingredient.rb', line 117 def preview_text(maxlength = 30) value.to_s[0..maxlength - 1] end |
#settings ⇒ Object
Settings for this ingredient from the elements.yml
definition.
99 100 101 |
# File 'app/models/alchemy/ingredient.rb', line 99 def settings definition.settings end |
#value ⇒ Object
The value or the related object if present
94 95 96 |
# File 'app/models/alchemy/ingredient.rb', line 94 def value || self[:value] end |