class Decidim::SettingsManifest

This class serves as a DSL that enables specifying an arbitrary settings to a component, so the admin panel can show a standarized UI to configure them.

Attributes

attributes[R]

Public Class Methods

model_name() click to toggle source
# File lib/decidim/settings_manifest.rb, line 56
def self.model_name
  ActiveModel::Name.new(self, nil, "Settings")
end
new() click to toggle source

Initializes a SettingsManifest.

# File lib/decidim/settings_manifest.rb, line 11
def initialize
  @attributes = {}
end

Public Instance Methods

attribute(name, options = {}) click to toggle source

Public: Adds a new attribute field to the SettingsManifest.

name - The name of the attribute to inject. options - A set of options to configure the attribute.

:type - The type of the attribute as found in Attribute::TYPES
:default - The default value of this settings field.

Returns nothing.

# File lib/decidim/settings_manifest.rb, line 23
def attribute(name, options = {})
  attribute = Attribute.new(options)
  attribute.validate!
  @attributes[name.to_sym] = attribute

  @schema = nil
end
manifest() click to toggle source
# File lib/decidim/settings_manifest.rb, line 60
def manifest
  self.class.manifest
end
required_attributes_for_authorization() click to toggle source
# File lib/decidim/settings_manifest.rb, line 80
def required_attributes_for_authorization
  attributes.select { |_, attribute| attribute.required_for_authorization? }
end
schema() click to toggle source

Public: Creates a model Class that sanitizes, coerces and filters data to the right types given a hash of serialized information.

Returns a Class.

# File lib/decidim/settings_manifest.rb, line 35
def schema
  return @schema if @schema

  manifest = self

  @schema = Class.new do
    include Virtus.model
    include ActiveModel::Validations
    include TranslatableAttributes

    cattr_accessor :manifest
    attr_reader :default_locale

    # Overwrites Virtus::InstanceMethods::Constructor#initialize to allow
    # passing a default_locale needed to validate translatable attributes.
    # See TranslatablePresenceValidator#default_locale_for(record).
    def initialize(attributes = nil, default_locale = nil)
      @default_locale = default_locale
      super(attributes)
    end

    def self.model_name
      ActiveModel::Name.new(self, nil, "Settings")
    end

    def manifest
      self.class.manifest
    end

    manifest.attributes.each do |name, attribute|
      if attribute.translated?
        translatable_attribute name, attribute.type_class, default: attribute.default_value
        validates name, translatable_presence: true if attribute.required
      else
        attribute name, attribute.type_class, default: attribute.default_value
        validates name, presence: true if attribute.required
        validates name, inclusion: { in: attribute.build_choices } if attribute.type == :enum
      end
    end
  end

  @schema.manifest = self
  @schema
end