Class: Alchemy::Ingredient

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

Defined Under Namespace

Classes: DefinitionError

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Hints

#hint

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

Parameters:

  • Ingredient (String)

    class name

Returns:

  • (String)

68
69
70
# File 'app/models/alchemy/ingredient.rb', line 68

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

Defines getter and setter method aliases for related object

Parameters:

  • The (String|Symbol)

    name of the alias

  • The (String)

    class name of the related object


44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/models/alchemy/ingredient.rb', line 44

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 = class_name
  end
end

.translated_label_for(role, element_name = nil) ⇒ Object


72
73
74
75
76
77
78
# File 'app/models/alchemy/ingredient.rb', line 72

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

#definitionObject

Definition hash for this ingredient from elements.yml file.


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

def definition
  return {} unless element

  element.ingredient_definition_for(role) || {}
end

#deprecated?Boolean

Returns:

  • (Boolean)

147
148
149
# File 'app/models/alchemy/ingredient.rb', line 147

def deprecated?
  !!definition[:deprecated]
end

#essenceObject

Compatibility method for access from element


82
83
84
# File 'app/models/alchemy/ingredient.rb', line 82

def essence
  self
end

#has_hint?Boolean

Returns:

  • (Boolean)

142
143
144
# File 'app/models/alchemy/ingredient.rb', line 142

def has_hint?
  !!definition[:hint]
end

#has_tinymce?Boolean

Returns:

  • (Boolean)

152
153
154
# File 'app/models/alchemy/ingredient.rb', line 152

def has_tinymce?
  false
end

#has_validations?Boolean

Returns:

  • (Boolean)

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

def has_validations?
  !!definition[:validate]
end

#partial_nameString

The demodulized underscored class name of the ingredient

Returns:

  • (String)

132
133
134
# File 'app/models/alchemy/ingredient.rb', line 132

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

#preview_ingredient?Boolean

Returns:

  • (Boolean)

157
158
159
# File 'app/models/alchemy/ingredient.rb', line 157

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.

Parameters:

  • max_length (Integer)

    (30)


120
121
122
# File 'app/models/alchemy/ingredient.rb', line 120

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

#settingsObject

Settings for this ingredient from the elements.yml definition.


92
93
94
# File 'app/models/alchemy/ingredient.rb', line 92

def settings
  definition[:settings] || {}
end

#settings_value(key, options = {}) ⇒ Object

Fetches value from settings

Parameters:

  • key (Symbol)
    • The hash key you want to fetch the value from

  • options (Hash) (defaults to: {})
    • An optional Hash that can override the settings.

    Normally passed as options hash into the content editor view.


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

def settings_value(key, options = {})
  settings.merge(options || {})[key.to_sym]
end

#to_partial_pathString

The path to the view partial of the ingredient

Returns:

  • (String)

126
127
128
# File 'app/models/alchemy/ingredient.rb', line 126

def to_partial_path
  "alchemy/ingredients/#{partial_name}_view"
end

#valueObject

The value or the related object if present


87
88
89
# File 'app/models/alchemy/ingredient.rb', line 87

def value
  related_object || self[:value]
end