Class: Alchemy::Ingredient
- Inherits:
-
BaseRecord
- Object
- ActiveRecord::Base
- BaseRecord
- Alchemy::Ingredient
- Includes:
- Hints
- 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::Page, Alchemy::Ingredients::Picture, Alchemy::Ingredients::Richtext, Alchemy::Ingredients::Select, Alchemy::Ingredients::Text, Alchemy::Ingredients::Video
Defined Under Namespace
Classes: DefinitionError
Class Method Summary collapse
-
.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
-
#definition ⇒ Object
Definition hash for this ingredient from
elements.ymlfile. - #deprecated? ⇒ Boolean
-
#essence ⇒ Object
Compatibility method for access from element.
- #has_hint? ⇒ 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.ymldefinition. -
#settings_value(key, options = {}) ⇒ Object
Fetches value from settings.
-
#to_partial_path ⇒ String
The path to the view partial of the ingredient.
-
#value ⇒ Object
The value or the related object if present.
Methods included from Hints
Class Method Details
.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
66 67 68 |
# File 'app/models/alchemy/ingredient.rb', line 66 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
42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'app/models/alchemy/ingredient.rb', line 42 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. = class_name end end |
.translated_label_for(role, element_name = nil) ⇒ Object
70 71 72 73 74 75 76 |
# File 'app/models/alchemy/ingredient.rb', line 70 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
#definition ⇒ Object
Definition hash for this ingredient from elements.yml file.
106 107 108 109 110 |
# File 'app/models/alchemy/ingredient.rb', line 106 def definition return {} unless element element.ingredient_definition_for(role) || {} end |
#deprecated? ⇒ Boolean
145 146 147 |
# File 'app/models/alchemy/ingredient.rb', line 145 def deprecated? !!definition[:deprecated] end |
#essence ⇒ Object
Compatibility method for access from element
80 81 82 |
# File 'app/models/alchemy/ingredient.rb', line 80 def essence self end |
#has_hint? ⇒ Boolean
140 141 142 |
# File 'app/models/alchemy/ingredient.rb', line 140 def has_hint? !!definition[:hint] end |
#has_tinymce? ⇒ Boolean
150 151 152 |
# File 'app/models/alchemy/ingredient.rb', line 150 def has_tinymce? false end |
#has_validations? ⇒ Boolean
135 136 137 |
# File 'app/models/alchemy/ingredient.rb', line 135 def has_validations? !!definition[:validate] end |
#partial_name ⇒ String
The demodulized underscored class name of the ingredient
130 131 132 |
# File 'app/models/alchemy/ingredient.rb', line 130 def partial_name self.class.name.demodulize.underscore end |
#preview_ingredient? ⇒ Boolean
155 156 157 |
# File 'app/models/alchemy/ingredient.rb', line 155 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.
118 119 120 |
# File 'app/models/alchemy/ingredient.rb', line 118 def preview_text(maxlength = 30) value.to_s[0..maxlength - 1] end |
#settings ⇒ Object
Settings for this ingredient from the elements.yml definition.
90 91 92 |
# File 'app/models/alchemy/ingredient.rb', line 90 def settings definition[:settings] || {} end |
#settings_value(key, options = {}) ⇒ Object
Fetches value from settings
100 101 102 |
# File 'app/models/alchemy/ingredient.rb', line 100 def settings_value(key, = {}) settings.merge( || {})[key.to_sym] end |
#to_partial_path ⇒ String
The path to the view partial of the ingredient
124 125 126 |
# File 'app/models/alchemy/ingredient.rb', line 124 def to_partial_path "alchemy/ingredients/#{partial_name}_view" end |
#value ⇒ Object
The value or the related object if present
85 86 87 |
# File 'app/models/alchemy/ingredient.rb', line 85 def value || self[:value] end |