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


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/models/alchemy/content/factory.rb', line 61

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(*args) ⇒ Alchemy::Content Also known as: create_from_scratch

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:



37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/models/alchemy/content/factory.rb', line 37

def create(*args)
  attributes = args.last || {}
  if args.length > 1
    Alchemy::Deprecation.warn 'Passing an element as first argument to Alchemy::Content.create is deprecated! Pass an attribute hash with element inside instead.'
    element = args.first
  else
    element = attributes[:element]
  end
  new(attributes.merge(element: element)).tap do |content|
    content.create_essence!(attributes[:essence_type])
  end
end

#definitionsObject

Returns all content definitions from elements.yml



79
80
81
82
83
# File 'app/models/alchemy/content/factory.rb', line 79

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

#new(attributes = {}) ⇒ Object Also known as: build

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
# 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'], element_id: element.id)
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



93
94
95
96
97
98
# File 'app/models/alchemy/content/factory.rb', line 93

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

  "Alchemy::#{essence_type}"
end