Class: Decidim::SettingsManifest

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

Overview

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.

Defined Under Namespace

Classes: Attribute

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSettingsManifest

Initializes a SettingsManifest.



11
12
13
# File 'decidim-core/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 'decidim-core/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 'decidim-core/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

#required_attributes_for_authorizationObject



80
81
82
# File 'decidim-core/lib/decidim/settings_manifest.rb', line 80

def required_attributes_for_authorization
  attributes.select { |_, attribute| attribute.required_for_authorization? }
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
69
70
71
72
73
74
75
76
77
78
# File 'decidim-core/lib/decidim/settings_manifest.rb', line 35

def schema
  return @schema if @schema

  manifest = self

  @schema = Class.new do
    include Decidim::AttributeObject::Model
    include ActiveModel::Validations
    include TranslatableAttributes

    cattr_accessor :manifest
    attr_reader :default_locale

    # Overwrites Decidim::AttributeObject::Model#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