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
- .polymorphic_association?(definition, relationship_name) ⇒ Boolean
- .polymorphic_association_for_association?(association, definition) ⇒ Boolean
- .resolve_and_find_related_record(identifier, association:, definition:, relationship_name:) ⇒ Object
- .serialize_identifier(record, association:, definition:, use_instance_class: false) ⇒ Object
- .sti_subclass?(instance_class, association_class) ⇒ Boolean
Class Method Details
.determine_model_class(record, association:, definition:, use_instance_class:) ⇒ Object
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/json_api/support/resource_identifier.rb', line 69 def determine_model_class(record, association:, definition:, use_instance_class:) if association && (polymorphic_association_for_association?(association, definition) || (use_instance_class && sti_subclass?(record.class, association.klass))) record.class elsif association association.klass else record.class end 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 |
.polymorphic_association?(definition, relationship_name) ⇒ Boolean
54 55 56 57 58 59 60 61 |
# File 'lib/json_api/support/resource_identifier.rb', line 54 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
80 81 82 83 84 85 |
# File 'lib/json_api/support/resource_identifier.rb', line 80 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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# 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) unless is_polymorphic expected_type = TypeConversion.model_type_name(association.klass) if type != expected_type raise ArgumentError, "Invalid relationship type: expected #{expected_type}, got #{type}" end end begin = if is_polymorphic TypeConversion.type_to_class_name(type).constantize else association.klass end .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 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
63 64 65 66 67 |
# File 'lib/json_api/support/resource_identifier.rb', line 63 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 |