Class: Maglev::Content::BlockContent
- Inherits:
-
Object
- Object
- Maglev::Content::BlockContent
- Includes:
- ActiveModel::Model, EnhancedValueConcern
- Defined in:
- app/models/maglev/content/block_content.rb
Defined Under Namespace
Classes: AssociationProxy
Instance Attribute Summary collapse
-
#definition ⇒ Object
Returns the value of attribute definition.
-
#id ⇒ Object
Returns the value of attribute id.
-
#lock_version ⇒ Object
Returns the value of attribute lock_version.
-
#parent_id ⇒ Object
Returns the value of attribute parent_id.
-
#position ⇒ Object
Returns the value of attribute position.
-
#position_in_parent ⇒ Object
Returns the value of attribute position_in_parent.
-
#settings ⇒ Object
Returns the value of attribute settings.
-
#type ⇒ Object
Returns the value of attribute type.
Class Method Summary collapse
- .build(raw_block_content:, definition:, position:) ⇒ Object
- .build_many(raw_blocks_content:, section_content:) ⇒ Object
- .compute_position_in_parent(blocks) ⇒ Object
Instance Method Summary collapse
Methods included from EnhancedValueConcern
#image, #image?, #label, #label?
Instance Attribute Details
#definition ⇒ Object
Returns the value of attribute definition.
9 10 11 |
# File 'app/models/maglev/content/block_content.rb', line 9 def definition @definition end |
#id ⇒ Object
Returns the value of attribute id.
9 10 11 |
# File 'app/models/maglev/content/block_content.rb', line 9 def id @id end |
#lock_version ⇒ Object
Returns the value of attribute lock_version.
9 10 11 |
# File 'app/models/maglev/content/block_content.rb', line 9 def lock_version @lock_version end |
#parent_id ⇒ Object
Returns the value of attribute parent_id.
9 10 11 |
# File 'app/models/maglev/content/block_content.rb', line 9 def parent_id @parent_id end |
#position ⇒ Object
Returns the value of attribute position.
9 10 11 |
# File 'app/models/maglev/content/block_content.rb', line 9 def position @position end |
#position_in_parent ⇒ Object
Returns the value of attribute position_in_parent.
9 10 11 |
# File 'app/models/maglev/content/block_content.rb', line 9 def position_in_parent @position_in_parent end |
#settings ⇒ Object
Returns the value of attribute settings.
9 10 11 |
# File 'app/models/maglev/content/block_content.rb', line 9 def settings @settings end |
#type ⇒ Object
Returns the value of attribute type.
9 10 11 |
# File 'app/models/maglev/content/block_content.rb', line 9 def type @type end |
Class Method Details
.build(raw_block_content:, definition:, position:) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 |
# File 'app/models/maglev/content/block_content.rb', line 25 def self.build(raw_block_content:, definition:, position:) new( id: raw_block_content['id'], definition: definition, type: raw_block_content['type'], settings: Maglev::Content::SettingContent::AssociationProxy.new(raw_block_content['settings']), parent_id: raw_block_content['parent_id'], position: position, lock_version: raw_block_content['lock_version'] ) end |
.build_many(raw_blocks_content:, section_content:) ⇒ Object
37 38 39 40 41 42 43 44 45 46 |
# File 'app/models/maglev/content/block_content.rb', line 37 def self.build_many(raw_blocks_content:, section_content:) blocks = raw_blocks_content.each_with_index.map do |raw_block_content, index| build( raw_block_content: raw_block_content, definition: section_content.definition.blocks.find(raw_block_content['type']), position: index ) end compute_position_in_parent(blocks) end |
.compute_position_in_parent(blocks) ⇒ Object
48 49 50 51 52 53 54 55 56 |
# File 'app/models/maglev/content/block_content.rb', line 48 def self.compute_position_in_parent(blocks) memo = {} # key: parent_id, value: number of blocks blocks.each do |block| memo[block.parent_id] ||= 0 block.position_in_parent = memo[block.parent_id] memo[block.parent_id] += 1 end blocks end |
Instance Method Details
#accepted_child_types ⇒ Object
17 18 19 |
# File 'app/models/maglev/content/block_content.rb', line 17 def accepted_child_types definition.accept || [] end |
#name ⇒ Object
21 22 23 |
# File 'app/models/maglev/content/block_content.rb', line 21 def name definition.human_name end |
#persisted? ⇒ Boolean
11 12 13 |
# File 'app/models/maglev/content/block_content.rb', line 11 def persisted? true end |