Class: Apiwork::Representation::Association

Inherits:
Object
  • Object
show all
Defined in:
lib/apiwork/representation/association.rb

Overview

Represents an association defined on a representation.

Associations map to model relationships and define serialization behavior. Used by adapters to build contracts and serialize records.

Examples:

association = InvoiceRepresentation.associations[:customer]
association.name # => :customer
association.type # => :belongs_to
association.representation_class # => CustomerRepresentation

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type, owner_representation_class, allow_destroy: false, deprecated: false, description: nil, example: nil, filterable: false, include: :optional, nullable: nil, polymorphic: nil, representation: nil, sortable: false, writable: false) ⇒ Association

Returns a new instance of Association.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/apiwork/representation/association.rb', line 62

def initialize(
  name,
  type,
  owner_representation_class,
  allow_destroy: false,
  deprecated: false,
  description: nil,
  example: nil,
  filterable: false,
  include: :optional,
  nullable: nil,
  polymorphic: nil,
  representation: nil,
  sortable: false,
  writable: false
)
  @name = name
  @type = type
  @owner_representation_class = owner_representation_class
  @model_class = owner_representation_class.model_class
  @representation_class = representation
  validate_representation!
  @polymorphic = normalize_polymorphic(polymorphic)

  @filterable = filterable
  @sortable = sortable
  @include = include
  @writable = writable
  @allow_destroy = allow_destroy
  @nullable = nullable
  @description = description
  @example = example
  @deprecated = deprecated

  detect_polymorphic_discriminator! if @polymorphic

  validate_include_option!
  validate_association_exists!
  validate_polymorphic!
  validate_nested_attributes!
  validate_query_options!
end

Instance Attribute Details

#allow_destroyObject (readonly)



52
53
54
# File 'lib/apiwork/representation/association.rb', line 52

def allow_destroy
  @allow_destroy
end

#descriptionString? (readonly)

The description for this association.

Returns:

  • (String, nil)


52
53
54
55
56
57
58
59
60
# File 'lib/apiwork/representation/association.rb', line 52

attr_reader :allow_destroy,
:description,
:discriminator,
:example,
:include,
:model_class,
:name,
:polymorphic,
:type

#discriminatorObject (readonly)



52
53
54
# File 'lib/apiwork/representation/association.rb', line 52

def discriminator
  @discriminator
end

#exampleObject? (readonly)

The example for this association.

Returns:



52
53
54
55
56
57
58
59
60
# File 'lib/apiwork/representation/association.rb', line 52

attr_reader :allow_destroy,
:description,
:discriminator,
:example,
:include,
:model_class,
:name,
:polymorphic,
:type

#includeSymbol (readonly)

The inclusion strategy for this association.

Returns:

  • (Symbol)


52
53
54
55
56
57
58
59
60
# File 'lib/apiwork/representation/association.rb', line 52

attr_reader :allow_destroy,
:description,
:discriminator,
:example,
:include,
:model_class,
:name,
:polymorphic,
:type

#model_classClass<ActiveRecord::Base> (readonly)

The model class for this association.

Returns:

  • (Class<ActiveRecord::Base>)


52
53
54
55
56
57
58
59
60
# File 'lib/apiwork/representation/association.rb', line 52

attr_reader :allow_destroy,
:description,
:discriminator,
:example,
:include,
:model_class,
:name,
:polymorphic,
:type

#nameSymbol (readonly)

The name for this association.

Returns:

  • (Symbol)


52
53
54
55
56
57
58
59
60
# File 'lib/apiwork/representation/association.rb', line 52

attr_reader :allow_destroy,
:description,
:discriminator,
:example,
:include,
:model_class,
:name,
:polymorphic,
:type

#polymorphicArray<Class<Representation::Base>>? (readonly)

The polymorphic representations for this association.

Returns:



52
53
54
55
56
57
58
59
60
# File 'lib/apiwork/representation/association.rb', line 52

attr_reader :allow_destroy,
:description,
:discriminator,
:example,
:include,
:model_class,
:name,
:polymorphic,
:type

#typeObject (readonly)



52
53
54
55
56
57
58
59
60
# File 'lib/apiwork/representation/association.rb', line 52

attr_reader :allow_destroy,
:description,
:discriminator,
:example,
:include,
:model_class,
:name,
:polymorphic,
:type

Instance Method Details

#collection?Boolean

Whether this association is a collection.

Returns:

  • (Boolean)


153
154
155
# File 'lib/apiwork/representation/association.rb', line 153

def collection?
  @type == :has_many
end

#deprecated?Boolean

Whether this association is deprecated.

Returns:

  • (Boolean)


109
110
111
# File 'lib/apiwork/representation/association.rb', line 109

def deprecated?
  @deprecated
end

#filterable?Boolean

Whether this association is filterable.

Returns:

  • (Boolean)


117
118
119
# File 'lib/apiwork/representation/association.rb', line 117

def filterable?
  @filterable
end

#find_representation_for_type(type_value) ⇒ Object



210
211
212
213
214
215
216
# File 'lib/apiwork/representation/association.rb', line 210

def find_representation_for_type(type_value)
  return nil unless @polymorphic

  @polymorphic.find do |representation_class|
    representation_class.model_class.polymorphic_name == type_value
  end
end

#nullable?Boolean

Whether this association is nullable.

Returns:

  • (Boolean)


177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/apiwork/representation/association.rb', line 177

def nullable?
  return @nullable unless @nullable.nil?

  case @type
  when :belongs_to
    return false unless @model_class

    foreign_key = detect_foreign_key
    column = column_for(foreign_key)
    return false unless column

    column.null
  when :has_one, :has_many
    false
  end
end

#polymorphic?Boolean

Whether this association is polymorphic.

Returns:

  • (Boolean)


169
170
171
# File 'lib/apiwork/representation/association.rb', line 169

def polymorphic?
  @polymorphic.present?
end

#representation_classClass<Representation::Base>?

Uses explicit ‘representation:` if set, otherwise inferred from the model.

Returns:



198
199
200
# File 'lib/apiwork/representation/association.rb', line 198

def representation_class
  @representation_class || inferred_representation_class
end

#representation_class_nameObject



202
203
204
205
206
207
208
# File 'lib/apiwork/representation/association.rb', line 202

def representation_class_name
  @representation_class_name ||= @owner_representation_class
    .name
    .demodulize
    .delete_suffix('Representation')
    .underscore
end

#singular?Boolean

Whether this association is singular.

Returns:

  • (Boolean)


161
162
163
# File 'lib/apiwork/representation/association.rb', line 161

def singular?
  %i[has_one belongs_to].include?(@type)
end

#sortable?Boolean

Whether this association is sortable.

Returns:

  • (Boolean)


125
126
127
# File 'lib/apiwork/representation/association.rb', line 125

def sortable?
  @sortable
end

#writable?Boolean

Whether this association is writable.

Returns:

  • (Boolean)

See Also:



134
135
136
# File 'lib/apiwork/representation/association.rb', line 134

def writable?
  [true, :create, :update].include?(@writable)
end

#writable_for?(action) ⇒ Boolean

Whether this association is writable for the given action.

Parameters:

  • action (Symbol)
    :create, :update

    The action.

Returns:

  • (Boolean)

See Also:



145
146
147
# File 'lib/apiwork/representation/association.rb', line 145

def writable_for?(action)
  [true, action].include?(@writable)
end