Class: Decidim::SettingsManifest

Inherits:
Object
  • Object
show all
Defined in:
lib/decidim/settings_manifest.rb

Overview

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

Defined Under Namespace

Classes: Attribute

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSettingsManifest

Initializes a SettingsManifest.



11
12
13
# File 'lib/decidim/settings_manifest.rb', line 11

def initialize
  @attributes = {}
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



8
9
10
# File 'lib/decidim/settings_manifest.rb', line 8

def attributes
  @attributes
end

Instance Method Details

#attribute(name, options = {}) ⇒ Object

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.



23
24
25
26
27
28
29
# 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

#schemaObject

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

Returns a Class.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# 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

    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
      else
        attribute name, attribute.type_class, default: attribute.default_value
        validates name, presence: true
      end
    end
  end

  @schema.manifest = self
  @schema
end