Class: Maglev::Section::Setting

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

Overview

rubocop:disable Style/ClassAndModuleChildren

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#defaultObject

attributes ##



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

def default
  @default
end

#idObject

validations ##



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

def id
  @id
end

#labelObject

attributes ##



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

def label
  @label
end

#optionsObject

attributes ##



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

def options
  @options
end

#typeObject

attributes ##



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

def type
  @type
end

Class Method Details

.build(hash) ⇒ Object

class methods ##



51
52
53
54
55
56
# File 'app/models/maglev/section/setting.rb', line 51

def self.build(hash)
  attributes = hash.slice('id', 'label', 'type', 'default')
  options = hash.except('id', 'label', 'type', 'default')

  new(attributes.merge(options: options))
end

.build_many(list) ⇒ Object



58
59
60
# File 'app/models/maglev/section/setting.rb', line 58

def self.build_many(list)
  list.map { |hash| build(hash) }
end

Instance Method Details

#build_default_checkbox_content(default) ⇒ Object



42
43
44
# File 'app/models/maglev/section/setting.rb', line 42

def build_default_checkbox_content(default)
  default.presence || false
end

#build_default_collection_item_content(default) ⇒ Object



46
47
48
# File 'app/models/maglev/section/setting.rb', line 46

def build_default_collection_item_content(default)
  { id: default }
end

#build_default_content(custom_default = nil) ⇒ Object

NOTE: any modification to that method must be reflected to the JS editor



18
19
20
21
22
23
24
25
26
27
28
# File 'app/models/maglev/section/setting.rb', line 18

def build_default_content(custom_default = nil)
  default = custom_default.nil? ? self.default : custom_default
  case type.to_sym
  when :image then build_default_image_content(default)
  when :link then build_default_link_content(default)
  when :checkbox then build_default_checkbox_content(default)
  when :collection_item then build_default_collection_item_content(default)
  else
    default || label
  end
end

#build_default_image_content(default) ⇒ Object



30
31
32
# File 'app/models/maglev/section/setting.rb', line 30

def build_default_image_content(default)
  default.is_a?(String) ? { url: default } : default || {}
end


34
35
36
37
38
39
40
# File 'app/models/maglev/section/setting.rb', line 34

def build_default_link_content(default)
  if default.is_a?(String)
    { text: 'Link', link_type: 'url', href: default }
  else
    { text: 'Link', link_type: 'url', href: '#' }.merge(default)
  end
end