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

Constant Summary collapse

REGISTERED_TYPES =

constants ##

%w[text image checkbox link color select collection_item icon divider hint].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#defaultObject

attributes ##



12
13
14
# File 'app/models/maglev/section/setting.rb', line 12

def default
  @default
end

#idObject

validations ##



12
13
14
# File 'app/models/maglev/section/setting.rb', line 12

def id
  @id
end

#labelObject

attributes ##



12
13
14
# File 'app/models/maglev/section/setting.rb', line 12

def label
  @label
end

#optionsObject

attributes ##



12
13
14
# File 'app/models/maglev/section/setting.rb', line 12

def options
  @options
end

#typeObject

attributes ##



12
13
14
# File 'app/models/maglev/section/setting.rb', line 12

def type
  @type
end

Class Method Details

.build(hash) ⇒ Object

class methods ##



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

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



49
50
51
# File 'app/models/maglev/section/setting.rb', line 49

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

.registered_typesObject



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

def self.registered_types
  @registered_types ||= REGISTERED_TYPES.index_with do |type|
    "Maglev::SettingTypes::#{type.camelize}".constantize.new
  end
end

Instance Method Details

#build_default_content(custom_default = nil) ⇒ Object

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



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

def build_default_content(custom_default = nil)
  default = custom_default.nil? ? self.default : custom_default

  # special case: text type
  default ||= label if text_type?

  cast_value(default)
end

#cast_value(value) ⇒ Object



27
28
29
# File 'app/models/maglev/section/setting.rb', line 27

def cast_value(value)
  self.class.registered_types[type.to_s].cast_value(value)
end