-
Notifications
You must be signed in to change notification settings - Fork 534
/
relationship.rb
225 lines (189 loc) · 6.39 KB
/
relationship.rb
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
58
59
60
61
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# frozen_string_literal: true
module JSONAPI
class Relationship
attr_reader :acts_as_set, :foreign_key, :options, :name,
:class_name, :polymorphic, :always_include_optional_linkage_data,
:parent_resource, :eager_load_on_include, :custom_methods,
:inverse_relationship, :allow_include
attr_writer :allow_include
attr_accessor :_routed, :_warned_missing_route
def initialize(name, options = {})
@name = name.to_s
@options = options
@acts_as_set = options.fetch(:acts_as_set, false) == true
@foreign_key = options[:foreign_key] ? options[:foreign_key].to_sym : nil
@parent_resource = options[:parent_resource]
@relation_name = options.fetch(:relation_name, @name)
@polymorphic = options.fetch(:polymorphic, false) == true
@polymorphic_types = options[:polymorphic_types]
if options[:polymorphic_relations]
ActiveSupport::Deprecation.warn('Use polymorphic_types instead of polymorphic_relations')
@polymorphic_types ||= options[:polymorphic_relations]
end
@always_include_optional_linkage_data = options.fetch(:always_include_optional_linkage_data, false) == true
@eager_load_on_include = options.fetch(:eager_load_on_include, true) == true
@allow_include = options[:allow_include]
@class_name = nil
@inverse_relationship = nil
@_routed = false
@_warned_missing_route = false
exclude_links(options.fetch(:exclude_links, JSONAPI.configuration.default_exclude_links))
# Custom methods are reserved for future use
@custom_methods = options.fetch(:custom_methods, {})
end
alias_method :polymorphic?, :polymorphic
alias_method :parent_resource_klass, :parent_resource
def primary_key
# :nocov:
@primary_key ||= resource_klass._primary_key
# :nocov:
end
def resource_klass
@resource_klass ||= @parent_resource.resource_klass_for(@class_name)
end
def table_name
# :nocov:
@table_name ||= resource_klass._table_name
# :nocov:
end
def self.polymorphic_types(name)
@poly_hash ||= {}.tap do |hash|
ObjectSpace.each_object do |klass|
next unless Module === klass
if ActiveRecord::Base > klass
klass.reflect_on_all_associations(:has_many).select{|r| r.options[:as] }.each do |reflection|
(hash[reflection.options[:as]] ||= []) << klass.name.downcase
end
end
end
end
@poly_hash[name.to_sym]
end
def resource_types
if polymorphic? && belongs_to?
@polymorphic_types ||= self.class.polymorphic_types(@relation_name).collect {|t| t.pluralize}
else
[resource_klass._type.to_s.pluralize]
end
end
def type
@type ||= resource_klass._type.to_sym
end
def relation_name(options)
case @relation_name
when Symbol
# :nocov:
@relation_name
# :nocov:
when String
@relation_name.to_sym
when Proc
@relation_name.call(options)
end
end
def belongs_to?
# :nocov:
false
# :nocov:
end
def readonly?
@options[:readonly]
end
def exclude_links(exclude)
case exclude
when :default, "default"
@_exclude_links = [:self, :related]
when :none, "none"
@_exclude_links = []
when Array
@_exclude_links = exclude.collect {|link| link.to_sym}
else
fail "Invalid exclude_links"
end
end
def _exclude_links
@_exclude_links ||= []
end
def exclude_link?(link)
_exclude_links.include?(link.to_sym)
end
class ToOne < Relationship
attr_reader :foreign_key_on
def initialize(name, options = {})
super
@class_name = options.fetch(:class_name, name.to_s.camelize)
@foreign_key ||= "#{name}_id".to_sym
@foreign_key_on = options.fetch(:foreign_key_on, :self)
if parent_resource
@inverse_relationship = options.fetch(:inverse_relationship, parent_resource._type)
end
end
def to_s
# :nocov: useful for debugging
"#{parent_resource}.#{name}(#{belongs_to? ? 'BelongsToOne' : 'ToOne'})"
# :nocov:
end
def belongs_to?
# :nocov:
foreign_key_on == :self
# :nocov:
end
def polymorphic_type
"#{name}_type" if polymorphic?
end
def include_optional_linkage_data?
@always_include_optional_linkage_data || JSONAPI::configuration.always_include_to_one_linkage_data
end
def allow_include?(context = nil)
strategy = if @allow_include.nil?
JSONAPI.configuration.default_allow_include_to_one
else
@allow_include
end
if !!strategy == strategy #check for boolean
return strategy
elsif strategy.is_a?(Symbol) || strategy.is_a?(String)
parent_resource.send(strategy, context)
else
strategy.call(context)
end
end
end
class ToMany < Relationship
attr_reader :reflect
def initialize(name, options = {})
super
@class_name = options.fetch(:class_name, name.to_s.camelize.singularize)
@foreign_key ||= "#{name.to_s.singularize}_ids".to_sym
@reflect = options.fetch(:reflect, true) == true
if parent_resource
@inverse_relationship = options.fetch(:inverse_relationship, parent_resource._type.to_s.singularize.to_sym)
end
end
def to_s
# :nocov: useful for debugging
"#{parent_resource}.#{name}(ToMany)"
# :nocov:
end
def include_optional_linkage_data?
# :nocov:
@always_include_optional_linkage_data || JSONAPI::configuration.always_include_to_many_linkage_data
# :nocov:
end
def allow_include?(context = nil)
strategy = if @allow_include.nil?
JSONAPI.configuration.default_allow_include_to_many
else
@allow_include
end
if !!strategy == strategy #check for boolean
return strategy
elsif strategy.is_a?(Symbol) || strategy.is_a?(String)
parent_resource.send(strategy, context)
else
strategy.call(context)
end
end
end
end
end