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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
|
# File 'app/serializers/forest_liana/serializer_factory.rb', line 54
def serializer_for(active_record_class)
serializer = Class.new {
include ForestAdmin::JSONAPI::Serializer
def self_link
"/forest#{super.underscore}"
end
def type
ForestLiana.name_for(object.class).demodulize
end
def format_name(attribute_name)
attribute_name.to_s
end
def unformat_name(attribute_name)
attribute_name.to_s
end
def relationship_self_link(attribute_name)
nil
end
def relationship_related_link(attribute_name)
ret = {}
current = self.has_many_relationships[attribute_name]
if current.try(:[], :options).try(:[], :name) == attribute_name
ret[:href] = "/forest/#{ForestLiana.name_for(object.class)}/#{object.id}/#{attribute_name}"
return ret
end
if intercom_integration?
case attribute_name
when :intercom_conversations
ret[:href] = "/forest/#{ForestLiana.name_for(object.class)}/#{object.id}/intercom_conversations"
when :intercom_attributes
ret[:href] = "/forest/#{ForestLiana.name_for(object.class)}/#{object.id}/intercom_attributes"
end
end
if stripe_integration?
case attribute_name
when :stripe_payments
ret[:href] = "/forest/#{ForestLiana.name_for(object.class)}/#{object.id}/stripe_payments"
when :stripe_invoices
ret[:href] = "/forest/#{ForestLiana.name_for(object.class)}/#{object.id}/stripe_invoices"
when :stripe_cards
ret[:href] = "/forest/#{ForestLiana.name_for(object.class)}/#{object.id}/stripe_cards"
when :stripe_subscriptions
ret[:href] = "/forest/#{ForestLiana.name_for(object.class)}/#{object.id}/stripe_subscriptions"
when :stripe_bank_accounts
ret[:href] = "/forest/#{ForestLiana.name_for(object.class)}/#{object.id}/stripe_bank_accounts"
end
end
if mixpanel_integration?
case attribute_name
when :mixpanel_last_events
ret[:href] = "/forest/#{ForestLiana.name_for(object.class)}/#{object.id}/mixpanel_last_events"
end
end
if ret[:href].blank?
begin
if @options[:include].try(:include?, attribute_name.to_s)
object.send(attribute_name)
end
SchemaUtils.many_associations(object.class).each do |a|
if a.name == attribute_name
ret[:href] = "/forest/#{ForestLiana.name_for(object.class)}/#{object.id}/relationships/#{attribute_name}"
end
end
rescue TypeError, ActiveRecord::StatementInvalid, NoMethodError
puts "Cannot load the association #{attribute_name} on #{object.class.name} #{object.id}."
end
end
ret
end
private
def intercom_integration?
ForestLiana.integrations
.try(:[], :intercom)
.try(:[], :mapping)
.try(:include?, object.class.name)
end
def stripe_integration?
mapping = ForestLiana.integrations
.try(:[], :stripe)
.try(:[], :mapping)
if mapping
collection_names = mapping.map do |collection_name_and_field|
collection_name_and_field.split('.')[0]
end
collection_names.include?(object.class.name)
else
false
end
end
def mixpanel_integration?
mapping = ForestLiana.integrations
.try(:[], :mixpanel)
.try(:[], :mapping)
if mapping
collection_names = mapping.map do |collection_name_and_field|
collection_name_and_field.split('.')[0]
end
collection_names.include?(object.class.name)
else
false
end
end
}
unless @is_smart_collection
attributes(active_record_class).each do |attribute|
serializer.attribute(attribute) do |x|
begin
object.send(attribute)
rescue
nil
end
end
end
attributes_time(active_record_class).each do |attribute|
serializer.attribute(attribute) do |x|
begin
value = object.send(attribute)
if value
match = /(\d{2}:\d{2}:\d{2})/.match(value.to_s)
(match && match[1]) ? match[1] : nil
else
nil
end
rescue
nil
end
end
end
attributes_serialized(active_record_class).each do |attr, serialization|
serializer.attribute(attr) do |x|
begin
value = object.send(attr)
value ? value.to_json : nil
rescue
nil
end
end
end
if active_record_class.respond_to?(:mount_uploader)
active_record_class.uploaders.each do |key, value|
serializer.attribute(key) do |x|
begin
object.send(key).try(:url)
rescue
nil
end
end
end
end
if active_record_class.respond_to?(:attachment_definitions)
active_record_class.attachment_definitions.each do |key, value|
serializer.attribute(key) do |x|
begin
object.send(key)
rescue
nil
end
end
end
end
if active_record_class.try(:taggable?) &&
active_record_class.respond_to?(:acts_as_taggable) &&
active_record_class.acts_as_taggable.respond_to?(:to_a)
active_record_class.acts_as_taggable.to_a.each do |key, value|
serializer.attribute(key) do |x|
begin
object.send(key).map(&:name)
rescue
nil
end
end
end
end
if active_record_class.respond_to?(:devise_modules?)
serializer.attribute('password') do |x|
'**********'
end
end
SchemaUtils.associations(active_record_class).each do |a|
begin
if SchemaUtils.polymorphic?(a)
serializer.send(serializer_association(a), a.name) {
if [:has_one, :belongs_to].include?(a.macro)
begin
object.send(a.name)
rescue ActiveRecord::RecordNotFound
nil
end
else
[]
end
}
elsif SchemaUtils.model_included?(a.klass)
serializer.send(serializer_association(a), a.name) {
if [:has_one, :belongs_to].include?(a.macro)
begin
object.send(a.name)
rescue ActiveRecord::RecordNotFound
nil
end
else
[]
end
}
end
rescue NameError
end
end
end
if has_intercom_integration?(active_record_class.name)
serializer.send(:has_many, :intercom_conversations) { }
serializer.send(:has_many, :intercom_attributes) { }
end
if has_stripe_integration?(active_record_class.name)
serializer.send(:has_many, :stripe_payments) { }
serializer.send(:has_many, :stripe_invoices) { }
serializer.send(:has_many, :stripe_cards) { }
serializer.send(:has_many, :stripe_subscriptions) { }
serializer.send(:has_many, :stripe_bank_accounts) { }
end
if has_mixpanel_integration?(active_record_class.name)
serializer.send(:has_many, :mixpanel_last_events) { }
end
ForestLiana::SerializerFactory.define_serializer(active_record_class, serializer)
serializer
end
|