Class: Decidim::SettingsManifest::Attribute

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations, AttributeObject::Model
Defined in:
decidim-core/lib/decidim/settings_manifest.rb

Overview

Semi-private: Attributes are an abstraction used by SettingsManifest to encapsulate behavior related to each individual settings field. Should not be used from the outside.

Constant Summary collapse

TYPES =
{
  boolean: { klass: Boolean, default: false },
  integer: { klass: Integer, default: 0 },
  integer_with_units: { klass: Decidim::Attributes::IntegerWithUnits, default: [5, "minutes"] },
  string: { klass: String, default: nil },
  float: { klass: Float, default: nil },
  text: { klass: String, default: nil },
  array: { klass: Array, default: [] },
  enum: { klass: String, default: nil },
  select: { klass: String, default: nil },
  scope: { klass: Integer, default: nil },
  time: { klass: Decidim::Attributes::TimeWithZone, default: nil },
  taxonomy_filters: { klass: Array, default: [] }
}.freeze

Constants included from AttributeObject::TypeMap

AttributeObject::TypeMap::Boolean, AttributeObject::TypeMap::Decimal

Instance Method Summary collapse

Methods included from AttributeObject::Model

#[], #[]=, #attributes, #attributes_with_values, #initialize, #to_h

Instance Method Details

#build_choices(context = nil) ⇒ Object



158
159
160
# File 'decidim-core/lib/decidim/settings_manifest.rb', line 158

def build_choices(context = nil)
  choices.try(:call, context) || choices
end

#build_unitsObject



162
163
164
# File 'decidim-core/lib/decidim/settings_manifest.rb', line 162

def build_units
  units.try(:call) || units
end

#default_valueObject



154
155
156
# File 'decidim-core/lib/decidim/settings_manifest.rb', line 154

def default_value
  default || TYPES[type][:default]
end

#editor?(context) ⇒ Boolean

Returns:



170
171
172
173
174
# File 'decidim-core/lib/decidim/settings_manifest.rb', line 170

def editor?(context)
  return editor.call(context) if editor.respond_to?(:call)

  editor
end

#readonly?(context) ⇒ Boolean

Returns:



166
167
168
# File 'decidim-core/lib/decidim/settings_manifest.rb', line 166

def readonly?(context)
  readonly&.call(context)
end

#type_classObject



148
149
150
151
152
# File 'decidim-core/lib/decidim/settings_manifest.rb', line 148

def type_class
  return Decidim::Attributes::RichText if type == :text && editor == true

  TYPES[type][:klass]
end

#validate_integer_with_units_structureObject



139
140
141
142
143
144
145
146
# File 'decidim-core/lib/decidim/settings_manifest.rb', line 139

def validate_integer_with_units_structure
  return unless type == :integer_with_units

  errors.add(:default, :invalid) unless default_value.is_a?(::Array) &&
                                        default_value.size == 2 &&
                                        default_value[0].is_a?(Integer) &&
                                        build_units.include?(default_value[1])
end