Module: JSONAPI::ResourceIdentifier
- Defined in:
- lib/json_api/support/resource_identifier.rb
Class Method Summary collapse
- .determine_model_class(record, association:, definition:, use_instance_class:) ⇒ Object
- .extract_id(identifier) ⇒ Object
- .extract_type(identifier) ⇒ Object
- .find_related_record(type, id, association, is_polymorphic) ⇒ Object
- .polymorphic_association?(definition, relationship_name) ⇒ Boolean
- .polymorphic_association_for_association?(association, definition) ⇒ Boolean
- .resolve_and_find_related_record(identifier, association:, definition:, relationship_name:) ⇒ Object
- .resolve_related_model_class(type, association, is_polymorphic) ⇒ Object
- .resolve_type_format_from_incoming(type, definition) ⇒ Object
- .serialize_identifier(record, association:, definition:, use_instance_class: false) ⇒ Object
- .sti_subclass?(instance_class, association_class) ⇒ Boolean
- .validate_relationship_type!(type, association, definition = nil) ⇒ Object
Class Method Details
.determine_model_class(record, association:, definition:, use_instance_class:) ⇒ Object
89 90 91 92 93 94 95 |
# File 'lib/json_api/support/resource_identifier.rb', line 89 def determine_model_class(record, association:, definition:, use_instance_class:) return record.class if association.nil? return record.class if polymorphic_association_for_association?(association, definition) return record.class if use_instance_class && sti_subclass?(record.class, association.klass) association.klass end |
.extract_id(identifier) ⇒ Object
16 17 18 |
# File 'lib/json_api/support/resource_identifier.rb', line 16 def extract_id(identifier) identifier[:id].to_s.presence end |
.extract_type(identifier) ⇒ Object
20 21 22 |
# File 'lib/json_api/support/resource_identifier.rb', line 20 def extract_type(identifier) identifier[:type] end |
.find_related_record(type, id, association, is_polymorphic) ⇒ Object
59 60 61 62 63 64 65 66 |
# File 'lib/json_api/support/resource_identifier.rb', line 59 def (type, id, association, is_polymorphic) = (type, association, is_polymorphic) .find(id) rescue ActiveRecord::RecordNotFound raise ArgumentError, "Related resource not found: #{type} with id #{id}" rescue NameError raise ArgumentError, "Invalid relationship type: #{type} does not correspond to a valid model class" end |
.polymorphic_association?(definition, relationship_name) ⇒ Boolean
74 75 76 77 78 79 80 81 |
# File 'lib/json_api/support/resource_identifier.rb', line 74 def polymorphic_association?(definition, relationship_name) relationship_def = definition.relationship_definitions.find do |r| r[:name].to_s == relationship_name.to_s end return false unless relationship_def relationship_def[:options][:polymorphic] == true end |
.polymorphic_association_for_association?(association, definition) ⇒ Boolean
97 98 99 100 101 102 |
# File 'lib/json_api/support/resource_identifier.rb', line 97 def polymorphic_association_for_association?(association, definition) return false unless definition relationship_name = association.name polymorphic_association?(definition, relationship_name) end |
.resolve_and_find_related_record(identifier, association:, definition:, relationship_name:) ⇒ Object
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/json_api/support/resource_identifier.rb', line 24 def (identifier, association:, definition:, relationship_name:) type = extract_type(identifier) id = extract_id(identifier) raise ArgumentError, "Missing type or id in relationship data" unless type && id is_polymorphic = polymorphic_association?(definition, relationship_name) validate_relationship_type!(type, association, definition) unless is_polymorphic (type, id, association, is_polymorphic) end |
.resolve_related_model_class(type, association, is_polymorphic) ⇒ Object
68 69 70 71 72 |
# File 'lib/json_api/support/resource_identifier.rb', line 68 def (type, association, is_polymorphic) return TypeConversion.type_to_class_name(type).constantize if is_polymorphic association.klass end |
.resolve_type_format_from_incoming(type, definition) ⇒ Object
48 49 50 51 52 53 54 55 56 57 |
# File 'lib/json_api/support/resource_identifier.rb', line 48 def resolve_type_format_from_incoming(type, definition) # Detect format from incoming type string if type.include?("/") :prefixed elsif definition.respond_to?(:type_format) && definition.type_format definition.type_format else JSONAPI.configuration.namespace_type_format end end |
.serialize_identifier(record, association:, definition:, use_instance_class: false) ⇒ Object
7 8 9 10 11 12 13 14 |
# File 'lib/json_api/support/resource_identifier.rb', line 7 def serialize_identifier(record, association:, definition:, use_instance_class: false) model_class = determine_model_class(record, association:, definition:, use_instance_class:,) = JSONAPI::ResourceLoader.find_for_model(model_class) = TypeConversion.resource_type_name() { type: , id: record.id.to_s } end |
.sti_subclass?(instance_class, association_class) ⇒ Boolean
83 84 85 86 87 |
# File 'lib/json_api/support/resource_identifier.rb', line 83 def sti_subclass?(instance_class, association_class) return false unless instance_class.respond_to?(:base_class) instance_class.base_class == association_class && instance_class != association_class end |
.validate_relationship_type!(type, association, definition = nil) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/json_api/support/resource_identifier.rb', line 35 def validate_relationship_type!(type, association, definition = nil) # Get expected type using the same format as the incoming type format = resolve_type_format_from_incoming(type, definition) expected_type = TypeConversion.model_type_name(association.klass, format:) # Also check flat type for backwards compatibility expected_flat = TypeConversion.model_type_name(association.klass, format: :flat) return if type == expected_type || type == expected_flat raise ArgumentError, "Invalid relationship type: expected #{expected_type}, got #{type}" end |