Method: Kadmin::Form.delegate_attributes

Defined in:
app/components/kadmin/form.rb

.delegate_attributes(*attributes) ⇒ Object

Delegates the list of attributes to the model, both readers and writers. If the attribute value passed is a hash and not a symbol, assumes it is a hash of one key, whose value is an array contained :reader, :writer, or both.

Examples:

delegate_attributes :first_name, { last_name: [:reader] }

Parameters:

  • attributes (Array<Symbol, Hash<Symbol, Array<Symbol>>>)

    list of attributes to delegate to the model



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/components/kadmin/form.rb', line 82

def delegate_attributes(*attributes)
  delegates = attributes.each_with_object([]) do |attribute, acc|
    case attribute
    when Hash
      key, value = attribute.first
      acc << key if value.include?(:reader)
      acc << "#{key}=" if value.include?(:writer)
    when Symbol, String
      acc.push(attribute, "#{attribute}=")
    else
      raise(ArgumentError, 'Attribute must be one of: Hash, Symbol, String')
    end
  end

  delegate(*delegates, to: :model)
end