Class: Maglev::Content::BlockContent

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model, EnhancedValueConcern
Defined in:
app/models/maglev/content/block_content.rb

Defined Under Namespace

Classes: AssociationProxy

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from EnhancedValueConcern

#image, #image?, #label, #label?

Instance Attribute Details

#definitionObject

Returns the value of attribute definition.



9
10
11
# File 'app/models/maglev/content/block_content.rb', line 9

def definition
  @definition
end

#idObject

Returns the value of attribute id.



9
10
11
# File 'app/models/maglev/content/block_content.rb', line 9

def id
  @id
end

#lock_versionObject

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_idObject

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

#positionObject

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_parentObject

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

#settingsObject

Returns the value of attribute settings.



9
10
11
# File 'app/models/maglev/content/block_content.rb', line 9

def settings
  @settings
end

#typeObject

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_typesObject



17
18
19
# File 'app/models/maglev/content/block_content.rb', line 17

def accepted_child_types
  definition.accept || []
end

#nameObject



21
22
23
# File 'app/models/maglev/content/block_content.rb', line 21

def name
  definition.human_name
end

#persisted?Boolean

Returns:

  • (Boolean)


11
12
13
# File 'app/models/maglev/content/block_content.rb', line 11

def persisted?
  true
end