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 element_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
56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'app/models/alchemy/content/factory.rb', line 56 def copy(source, differences = {}) Content.new( source.attributes.with_indifferent_access. except(*SKIPPED_ATTRIBUTES_ON_COPY). merge(differences.with_indifferent_access) ).tap do |new_content| new_content.build_essence( source.essence.attributes. except(*SKIPPED_ATTRIBUTES_ON_COPY) ) new_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)
41 42 43 44 45 |
# File 'app/models/alchemy/content/factory.rb', line 41 def create(attributes = {}) new(attributes).tap do |content| content.essence.save && content.save end end |
#definitions ⇒ Object
Returns all content definitions from elements.yml
72 73 74 75 76 |
# File 'app/models/alchemy/content/factory.rb', line 72 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.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'app/models/alchemy/content/factory.rb', line 18 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? && attributes[:essence_type].nil? raise ContentDefinitionError, "No definition found in elements.yml for #{attributes.inspect} and #{element.inspect}" end super( name: attributes[:name], essence_type: attributes[:essence_type] || normalize_essence_type(definition[:type]), element: element, ).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.
86 87 88 89 90 91 |
# File 'app/models/alchemy/content/factory.rb', line 86 def normalize_essence_type(essence_type) essence_type = essence_type.classify return essence_type if is_an_essence?(essence_type) "Alchemy::#{essence_type}" end |