Module: Alchemy::Content::Factory::ClassMethods

Defined in:
app/models/alchemy/content/factory.rb

Constant Summary collapse

SKIPPED_ATTRIBUTES_ON_COPY =
%w(position created_at updated_at creator_id updater_id id)

Instance Method Summary collapse

Instance Method Details

#copy(source, differences = {}) ⇒ Object

Creates a copy of source and also copies the associated essence.

You can pass a differences hash to update the attributes of the copy.

Example

@copy = Alchemy::Content.copy(@content, {element_id: 3})
@copy.element_id # => 3


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/models/alchemy/content/factory.rb', line 55

def copy(source, differences = {})
  new_content = Content.new(
    source.attributes.
      except(*SKIPPED_ATTRIBUTES_ON_COPY).
      merge(differences.with_indifferent_access),
  )
  new_essence = source.essence.class.create!(
    source.essence.attributes.
      except(*SKIPPED_ATTRIBUTES_ON_COPY),
  )
  new_content.tap do |content|
    content.essence = new_essence
    content.save
  end
end

#create(attributes = {}) ⇒ Alchemy::Content

Creates a new content from elements definition in the elements.yml file.

  1. It builds the content

  2. It creates the essence record (content object gets saved)

Returns:



40
41
42
43
44
# File 'app/models/alchemy/content/factory.rb', line 40

def create(attributes = {})
  new(attributes).tap do |content|
    content.essence.save && content.save
  end
end

#definitionsObject

Returns all content definitions from elements.yml



73
74
75
76
77
# File 'app/models/alchemy/content/factory.rb', line 73

def definitions
  definitions = Element.definitions.flat_map { |e| e["contents"] }
  definitions.compact!
  definitions
end

#new(attributes = {}) ⇒ Object

Builds a new content as descriped in the elements.yml file.

Parameters:

  • The (Hash)

    content definition used for finding the content in elements.yml file



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/models/alchemy/content/factory.rb', line 17

def new(attributes = {})
  element = attributes[:element] || Element.find_by(id: attributes[:element_id])
  return super if attributes.empty? || element.nil?

  definition = element.content_definition_for(attributes[:name])
  if definition.blank?
    raise ContentDefinitionError, "No definition found in elements.yml for #{attributes.inspect} and #{element.inspect}"
  end

  super(
    name: definition[:name],
    essence_type: normalize_essence_type(definition[:type]),
    element_id: element.id
  ).tap(&:build_essence)
end

#normalize_essence_type(essence_type) ⇒ Object

Returns a normalized Essence type

Adds Alchemy module name in front of given essence type unless there is a Class with the specified name that is an essence.

Parameters:

  • the (String)

    essence type to normalize



87
88
89
90
91
92
# File 'app/models/alchemy/content/factory.rb', line 87

def normalize_essence_type(essence_type)
  essence_type = essence_type.classify
  return essence_type if is_an_essence?(essence_type)

  "Alchemy::#{essence_type}"
end