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(attributes = {}) ⇒ Alchemy::Content
Creates a new content from elements definition in the
elements.yml
file. -
#definitions ⇒ Object
Returns all content definitions from elements.yml.
-
#new(attributes = {}) ⇒ Object
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
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.
-
It builds the content
-
It creates the essence record (content object gets saved)
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 |
#definitions ⇒ Object
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.
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.
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 |