forked from JSONAPI-Resources/jsonapi-resources
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcross_schema_relationships.rb
More file actions
57 lines (45 loc) · 1.44 KB
/
cross_schema_relationships.rb
File metadata and controls
57 lines (45 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
53
54
55
56
57
# frozen_string_literal: true
module JSONAPI
module CrossSchemaRelationships
extend ActiveSupport::Concern
included do
class_attribute :_cross_schema_relationships, default: {}
end
class_methods do
# Store cross-schema relationship information
def has_one(*args)
options = args.extract_options!
schema = options.delete(:schema)
if schema
args.each do |name|
register_cross_schema_relationship(name, schema, :has_one, options)
end
end
super(*args, options)
end
def has_many(*args)
options = args.extract_options!
schema = options.delete(:schema)
if schema
args.each do |name|
register_cross_schema_relationship(name, schema, :has_many, options)
end
end
super(*args, options)
end
private
def register_cross_schema_relationship(name, schema, type, options)
self._cross_schema_relationships = _cross_schema_relationships.merge(
name => { schema: schema, type: type, options: options }
)
end
end
# Instance methods to handle cross-schema relationships
def cross_schema_relationship?(relationship_name)
self.class._cross_schema_relationships.key?(relationship_name.to_sym)
end
def cross_schema_for(relationship_name)
self.class._cross_schema_relationships[relationship_name.to_sym]
end
end
end