Class: Alchemy::Ingredient

Inherits:
BaseRecord
  • Object
show all
Defined in:
app/models/alchemy/ingredient.rb

Defined Under Namespace

Classes: DefinitionError

Constant Summary

Constants included from SearchableResource

SearchableResource::SEARCHABLE_COLUMN_TYPES

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ConfigMissing

#const_missing

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



85
86
87
# File 'app/models/alchemy/ingredient.rb', line 85

def allow_settings(settings)
  @allowed_settings = Array(settings)
end

.allowed_settingsObject

Allowed settings on the ingredient



90
91
92
# File 'app/models/alchemy/ingredient.rb', line 90

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



72
73
74
# File 'app/models/alchemy/ingredient.rb', line 72

def normalize_type(ingredient_type)
  "Alchemy::Ingredients::#{ingredient_type.to_s.classify.demodulize}"
end

Defines getter and setter method aliases for related object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/models/alchemy/ingredient.rb', line 48

def related_object_alias(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
    related_object_id
  end

  define_method :"#{name}_id=" do |id|
    self.related_object_id = id
    self.related_object_type = id.nil? ? nil : class_name
  end
end

.translated_label_for(role, element_name = nil) ⇒ Object



76
77
78
79
80
81
82
# File 'app/models/alchemy/ingredient.rb', line 76

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_editor_componentObject

The editor component of the ingredient.



182
183
184
# File 'app/models/alchemy/ingredient.rb', line 182

def as_editor_component
  editor_component_class.new(self)
end

#as_view_component(options: {}, html_options: {}) ⇒ Object

The view component of the ingredient with mapped options.



176
177
178
# File 'app/models/alchemy/ingredient.rb', line 176

def as_view_component(options: {}, html_options: {})
  view_component_class.new(self, **options, html_options: html_options)
end

#definitionObject

Definition hash for this ingredient from elements.yml file.



107
108
109
110
111
# File 'app/models/alchemy/ingredient.rb', line 107

def definition
  return IngredientDefinition.new unless element

  element.ingredient_definition_for(role) || IngredientDefinition.new
end

#deprecated?Boolean



154
155
156
# File 'app/models/alchemy/ingredient.rb', line 154

def deprecated?
  !!definition.deprecated
end

#has_tinymce?Boolean



159
160
161
# File 'app/models/alchemy/ingredient.rb', line 159

def has_tinymce?
  false
end

#has_validations?Boolean



149
150
151
# File 'app/models/alchemy/ingredient.rb', line 149

def has_validations?
  definition.validate.any?
end

#linked?Boolean



163
164
165
# File 'app/models/alchemy/ingredient.rb', line 163

def linked?
  link.try(:present?)
end

#partial_nameString

The demodulized underscored class name of the ingredient



144
145
146
# File 'app/models/alchemy/ingredient.rb', line 144

def partial_name
  self.class.name.demodulize.underscore
end

#preview_ingredient?Boolean



168
169
170
# File 'app/models/alchemy/ingredient.rb', line 168

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.



138
139
140
# File 'app/models/alchemy/ingredient.rb', line 138

def preview_text(maxlength = 30)
  value.to_s[0..maxlength - 1]
end

#settingsObject

Settings for this ingredient from the elements.yml definition.



101
102
103
# File 'app/models/alchemy/ingredient.rb', line 101

def settings
  definition.settings
end

#translated_roleObject

Returns the translated role for displaying in labels

Translate it in your locale yml file:

alchemy:
  ingredient_roles:
    foo: Bar

Optionally you can scope your ingredient role to an element:

alchemy:
  ingredient_roles:
    article:
      foo: Baz


128
129
130
# File 'app/models/alchemy/ingredient.rb', line 128

def translated_role
  self.class.translated_label_for(role, element&.name)
end

#valueObject

The value or the related object if present



96
97
98
# File 'app/models/alchemy/ingredient.rb', line 96

def value
  related_object || self[:value]
end