Sha256: 2c1240c80a27d945758ab9ef4c32c65a8b142c22ade008d9537e873c6092bbed

Contents?: true

Size: 1.5 KB

Versions: 8

Compression:

Stored size: 1.5 KB

Contents

# frozen_string_literal: true

# rubocop:disable Style/ClassAndModuleChildren
class Maglev::Section::Setting
  ## concerns ##
  include ActiveModel::Model

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

  ## attributes ##
  attr_accessor :id, :label, :type, :default, :options

  ## validations ##
  validates :id, :label, :type, :default, 'maglev/presence': true
  validates :type, inclusion: { in: REGISTERED_TYPES }

  ## methods ##

  # shortcuts
  REGISTERED_TYPES.each do |type|
    define_method(:"#{type}_type?") do
      self.type.to_s == type
    end
  end

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

  # NOTE: any modification to that method must be reflected in the JS editor
  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

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

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

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

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

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
maglevcms-1.7.3 app/models/maglev/section/setting.rb
maglevcms-1.7.2 app/models/maglev/section/setting.rb
maglevcms-1.7.1 app/models/maglev/section/setting.rb
maglevcms-1.7.0 app/models/maglev/section/setting.rb
maglevcms-1.6.1 app/models/maglev/section/setting.rb
maglevcms-1.6.0 app/models/maglev/section/setting.rb
maglevcms-1.5.1 app/models/maglev/section/setting.rb
maglevcms-1.4.0 app/models/maglev/section/setting.rb