Class: Maglev::Section::Block

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model
Defined in:
app/models/maglev/section/block.rb

Overview

rubocop:disable Style/ClassAndModuleChildren

Defined Under Namespace

Classes: Store

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#acceptObject

attributes ##



9
10
11
# File 'app/models/maglev/section/block.rb', line 9

def accept
  @accept
end

#limitObject

attributes ##



9
10
11
# File 'app/models/maglev/section/block.rb', line 9

def limit
  @limit
end

#nameObject

validation ##



9
10
11
# File 'app/models/maglev/section/block.rb', line 9

def name
  @name
end

#rootObject

attributes ##



9
10
11
# File 'app/models/maglev/section/block.rb', line 9

def root
  @root
end

#sectionObject

attributes ##



9
10
11
# File 'app/models/maglev/section/block.rb', line 9

def section
  @section
end

#settingsObject

attributes ##



9
10
11
# File 'app/models/maglev/section/block.rb', line 9

def settings
  @settings
end

#typeObject

attributes ##



9
10
11
# File 'app/models/maglev/section/block.rb', line 9

def type
  @type
end

Class Method Details

.build(hash, section:) ⇒ Object

class methods ##



43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/models/maglev/section/block.rb', line 43

def self.build(hash, section:)
  attributes = hash.slice('name', 'type', 'accept', 'root', 'limit')
  attributes['root'] = true if attributes['root'].nil?
  attributes['limit'] = -1 if attributes['limit'].nil?

  new(
    **attributes.merge(
      settings: Maglev::Section::Setting.build_many(hash['settings']),
      section: section
    )
  )
end

.build_many(list, section:) ⇒ Object



56
57
58
59
60
61
62
# File 'app/models/maglev/section/block.rb', line 56

def self.build_many(list, section:)
  return [] if list.blank?

  list.map do |hash_or_object|
    hash_or_object.is_a?(Hash) ? build(hash_or_object, section: section) : hash_or_object
  end
end

Instance Method Details

#advanced_settingsObject



29
30
31
# File 'app/models/maglev/section/block.rb', line 29

def advanced_settings
  settings.select(&:advanced?)
end

#as_jsonObject



37
38
39
# File 'app/models/maglev/section/block.rb', line 37

def as_json
  super(only: %i[name type settings root accept])
end

#empty?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'app/models/maglev/section/block.rb', line 33

def empty?
  settings.none?
end

#human_nameObject

methods



17
18
19
# File 'app/models/maglev/section/block.rb', line 17

def human_name
  ::I18n.t("#{section.i18n_scope}.blocks.types.#{type}", default: name.humanize)
end

#main_settingsObject



25
26
27
# File 'app/models/maglev/section/block.rb', line 25

def main_settings
  settings.reject(&:advanced?)
end

#root?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'app/models/maglev/section/block.rb', line 21

def root?
  !!root
end