Class: Apiwork::Representation::Association
- Inherits:
-
Object
- Object
- Apiwork::Representation::Association
- 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.
Instance Attribute Summary collapse
- #allow_destroy ⇒ Object readonly
-
#description ⇒ String?
readonly
The description for this association.
- #discriminator ⇒ Object readonly
-
#example ⇒ Object?
readonly
The example for this association.
-
#include ⇒ Symbol
readonly
The inclusion strategy for this association.
-
#model_class ⇒ Class<ActiveRecord::Base>
readonly
The model class for this association.
-
#name ⇒ Symbol
readonly
The name for this association.
-
#polymorphic ⇒ Array<Class<Representation::Base>>?
readonly
The polymorphic representations for this association.
- #type ⇒ Object readonly
Instance Method Summary collapse
-
#collection? ⇒ Boolean
Whether this association is a collection.
-
#deprecated? ⇒ Boolean
Whether this association is deprecated.
-
#filterable? ⇒ Boolean
Whether this association is filterable.
- #find_representation_for_type(type_value) ⇒ Object
-
#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
constructor
A new instance of Association.
-
#nullable? ⇒ Boolean
Whether this association is nullable.
-
#polymorphic? ⇒ Boolean
Whether this association is polymorphic.
-
#representation_class ⇒ Class<Representation::Base>?
Uses explicit ‘representation:` if set, otherwise inferred from the model.
- #representation_class_name ⇒ Object
-
#singular? ⇒ Boolean
Whether this association is singular.
-
#sortable? ⇒ Boolean
Whether this association is sortable.
-
#writable? ⇒ Boolean
Whether this association is writable.
-
#writable_for?(action) ⇒ Boolean
Whether this association is writable for the given action.
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! end |
Instance Attribute Details
#allow_destroy ⇒ Object (readonly)
52 53 54 |
# File 'lib/apiwork/representation/association.rb', line 52 def allow_destroy @allow_destroy end |
#description ⇒ String? (readonly)
The description for this association.
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 |
#discriminator ⇒ Object (readonly)
52 53 54 |
# File 'lib/apiwork/representation/association.rb', line 52 def discriminator @discriminator end |
#example ⇒ Object? (readonly)
The example for this association.
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 |
#include ⇒ Symbol (readonly)
The inclusion strategy for this association.
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_class ⇒ Class<ActiveRecord::Base> (readonly)
The model class for this association.
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 |
#name ⇒ Symbol (readonly)
The name for this association.
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 |
#polymorphic ⇒ Array<Class<Representation::Base>>? (readonly)
The polymorphic representations for this association.
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 |
#type ⇒ Object (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.
153 154 155 |
# File 'lib/apiwork/representation/association.rb', line 153 def collection? @type == :has_many end |
#deprecated? ⇒ Boolean
Whether this association is deprecated.
109 110 111 |
# File 'lib/apiwork/representation/association.rb', line 109 def deprecated? @deprecated end |
#filterable? ⇒ Boolean
Whether this association is filterable.
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.
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.
169 170 171 |
# File 'lib/apiwork/representation/association.rb', line 169 def polymorphic? @polymorphic.present? end |
#representation_class ⇒ Class<Representation::Base>?
Uses explicit ‘representation:` if set, otherwise inferred from the model.
198 199 200 |
# File 'lib/apiwork/representation/association.rb', line 198 def representation_class @representation_class || inferred_representation_class end |
#representation_class_name ⇒ Object
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.
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.
125 126 127 |
# File 'lib/apiwork/representation/association.rb', line 125 def sortable? @sortable end |
#writable? ⇒ Boolean
Whether this association is writable.
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.
145 146 147 |
# File 'lib/apiwork/representation/association.rb', line 145 def writable_for?(action) [true, action].include?(@writable) end |