Class: Decidim::Attributes::Array

Inherits:
ActiveModel::Type::Value
  • Object
show all
Defined in:
decidim-core/lib/decidim/attributes/array.rb

Overview

Custom attributes value to represent an Array.

Direct Known Subclasses

Hash

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value_type: ::Object, default: []) ⇒ Array

Returns a new instance of Array.



9
10
11
12
# File 'decidim-core/lib/decidim/attributes/array.rb', line 9

def initialize(value_type: ::Object, default: [])
  @value_type = value_type
  @default = default
end

Instance Attribute Details

#defaultObject (readonly)

Returns the value of attribute default.



7
8
9
# File 'decidim-core/lib/decidim/attributes/array.rb', line 7

def default
  @default
end

#value_typeObject (readonly)

Returns the value of attribute value_type.



7
8
9
# File 'decidim-core/lib/decidim/attributes/array.rb', line 7

def value_type
  @value_type
end

Instance Method Details

#cast(value) ⇒ Object

The value would not be cast in case it is nil by default. We also want to cast the nil values to the default values.



20
21
22
# File 'decidim-core/lib/decidim/attributes/array.rb', line 20

def cast(value)
  cast_value(value)
end

#typeObject

:nodoc:



14
15
16
# File 'decidim-core/lib/decidim/attributes/array.rb', line 14

def type # :nodoc:
  :array
end

#validate_nested?Boolean

The nested validator should be only added for those attributes that inherit from the AttributeObject::Model type. Otherwise this would be also added e.g. for ActiveRecord objects which would cause unexpected validation errors.

The hash type inherits from the array type, so this covers both cases.

Returns:

  • (Boolean)


30
31
32
33
34
# File 'decidim-core/lib/decidim/attributes/array.rb', line 30

def validate_nested?
  return false unless value_type.is_a?(Class)

  (value_type < Decidim::AttributeObject::Model) == true
end