Class: Maglev::Content::SectionContent

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from EnhancedValueConcern

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

Instance Attribute Details

#blocksObject

Returns the value of attribute blocks.



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

def blocks
  @blocks
end

#definitionObject

Returns the value of attribute definition.



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

def definition
  @definition
end

#idObject

Returns the value of attribute id.



9
10
11
# File 'app/models/maglev/content/section_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/section_content.rb', line 9

def lock_version
  @lock_version
end

#settingsObject

Returns the value of attribute settings.



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

def settings
  @settings
end

#theme_idObject

Returns the value of attribute theme_id.



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

def theme_id
  @theme_id
end

#typeObject

Returns the value of attribute type.



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

def type
  @type
end

Class Method Details

.build(theme:, raw_section_content:) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/models/maglev/content/section_content.rb', line 71

def self.build(theme:, raw_section_content:)
  new(
    id: raw_section_content['id'],
    definition: theme.sections.find(raw_section_content['type']),
    type: raw_section_content['type'],
    settings: Maglev::Content::SettingContent::AssociationProxy.new(raw_section_content['settings']),
    theme_id: theme.id,
    lock_version: raw_section_content['lock_version']
  ).tap do |section_content|
    section_content.build_blocks(raw_section_content)
  end
end

.build_many(theme:, content:) ⇒ Object



65
66
67
68
69
# File 'app/models/maglev/content/section_content.rb', line 65

def self.build_many(theme:, content:)
  content.map do |raw_section_content|
    build(theme: theme, raw_section_content: raw_section_content)
  end
end

Instance Method Details

#block_definitionsObject



29
30
31
# File 'app/models/maglev/content/section_content.rb', line 29

def block_definitions
  definition.blocks
end

#blocks?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'app/models/maglev/content/section_content.rb', line 25

def blocks?
  blocks.present?
end

#blocks_labelObject



50
51
52
# File 'app/models/maglev/content/section_content.rb', line 50

def blocks_label
  definition.human_blocks_label(::I18n.t('maglev.editor.section_blocks.breadcrumb'))
end

#build_blocks(raw_section_content) ⇒ Object



58
59
60
61
62
63
# File 'app/models/maglev/content/section_content.rb', line 58

def build_blocks(raw_section_content)
  self.blocks = Maglev::Content::BlockContent::AssociationProxy.new(
    section_content: self,
    raw_blocks_content: raw_section_content['blocks']
  )
end

#child_block_definitions_of(block_id) ⇒ Object

return the definitions of blocks that can be added as children of the given block



43
44
45
46
47
48
# File 'app/models/maglev/content/section_content.rb', line 43

def child_block_definitions_of(block_id)
  accepted_block_types = blocks.find(block_id).accepted_child_types
  definition.blocks.select do |block_definition|
    accepted_block_types.include?(block_definition.type) && blocks.can_add?(block_definition.type)
  end
end

#child_blocks_of(block_id) ⇒ Object



54
55
56
# File 'app/models/maglev/content/section_content.rb', line 54

def child_blocks_of(block_id)
  blocks.select { |block| block.parent_id == block_id }
end

#persisted?Boolean

Returns:

  • (Boolean)


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

def persisted?
  true
end

#root_block_definitionsObject

return the definitions of root blocks that can be added



38
39
40
# File 'app/models/maglev/content/section_content.rb', line 38

def root_block_definitions
  definition.root_blocks.select { |block_definition| blocks.can_add?(block_definition.type) }
end

#root_blocksObject



33
34
35
# File 'app/models/maglev/content/section_content.rb', line 33

def root_blocks
  blocks.select(&:root?)
end

#sticky?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'app/models/maglev/content/section_content.rb', line 15

def sticky?
  definition.viewport_fixed_position?
end

#type_nameObject



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

def type_name
  definition.human_name
end