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
-
#copy(source, differences = {}) ⇒ Object
Creates a copy of source and also copies the associated essence.
-
#create(*args) ⇒ Alchemy::Content
(also: #create_from_scratch)
Creates a new content from elements definition in the
elements.yml
file. -
#definitions ⇒ Object
Returns all content definitions from elements.yml.
-
#new(attributes = {}) ⇒ Object
(also: #build)
Builds a new content as descriped in the elements.yml file.
-
#normalize_essence_type(essence_type) ⇒ Object
Returns a normalized Essence type.
Instance Method Details
#copy(source, differences = {}) ⇒ Object
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.
-
It builds the content
-
It creates the essence record (content object gets saved)
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 |
#definitions ⇒ Object
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.
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.
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 |