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
|
# File 'app/serializers/forest_liana/serializer_factory.rb', line 55
def serializer_for(active_record_class)
serializer = Class.new {
include JSONAPI::Serializer
def self_link
"/forest#{super.underscore}"
end
def type
object.class.table_name.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/#{object.class.table_name}/#{object.id}/#{attribute_name}"
return ret
end
if intercom_integration?
case attribute_name
when :intercom_conversations
ret[:href] = "/forest/#{object.class.table_name}/#{object.id}/intercom_conversations"
when :intercom_attributes
ret[:href] = "/forest/#{object.class.table_name}/#{object.id}/intercom_attributes"
end
end
if stripe_integration?
case attribute_name
when :stripe_payments
ret[:href] = "/forest/#{object.class.table_name}/#{object.id}/stripe_payments"
when :stripe_invoices
ret[:href] = "/forest/#{object.class.table_name}/#{object.id}/stripe_invoices"
when :stripe_cards
ret[:href] = "/forest/#{object.class.table_name}/#{object.id}/stripe_cards"
end
end
if ret[:href].blank?
begin
relationship_records = object.send(attribute_name)
if relationship_records.respond_to?(:each)
ret[:href] = "/forest/#{object.class.table_name}/#{object.id}/#{attribute_name}"
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
}
attributes(active_record_class).each do |attr|
serializer.attribute(attr)
end
if active_record_class.respond_to?(:mount_uploader)
active_record_class.uploaders.each do |key, value|
serializer.attribute(key) { |x| object.send(key).try(:url) }
end
end
if active_record_class.respond_to?(:attachment_definitions)
active_record_class.attachment_definitions.each do |key, value|
serializer.attribute(key) { |x| object.send(key) }
end
end
if 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|
object.send(key).map(&:name)
end
end
end
SchemaUtils.associations(active_record_class).each do |a|
serializer.send(serializer_association(a), a.name) {
if [:has_one, :belongs_to].include?(a.macro)
object.send(a.name).try(:reload)
else
[]
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) { }
end
ForestLiana::SerializerFactory.define_serializer(active_record_class,
serializer)
serializer
end
|