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
|
# File 'app/serializers/forest_liana/serializer_factory.rb', line 44
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.tableize.dasherize
end
def format_name(attribute_name)
attribute_name.to_s
end
def unformat_name(attribute_name)
attribute_name.to_s.underscore
end
def relationship_self_link(attribute_name)
nil
end
def relationship_related_link(attribute_name)
ret = {}
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?
relationship_records = object.send(attribute_name)
if relationship_records.respond_to?(:each)
if Rails::VERSION::MAJOR == 4
ret[:href] = "/forest/#{object.class.table_name}/#{object.id}/#{attribute_name}"
ret[:meta] = { count: relationship_records.distinct.count }
else
ret[:href] = "/forest/#{object.class.table_name}/#{object.id}/#{attribute_name}"
ret[:meta] = {
count: relationship_records.count(:id, distinct: true)
}
end
end
end
ret
end
private
def intercom_integration?
object.class.name == ForestLiana.integrations
.try(:[], :intercom)
.try(:[], :user_collection)
end
def stripe_integration?
object.class.name == ForestLiana.integrations
.try(:[], :stripe)
.try(:[], :user_collection)
end
}
attributes(active_record_class).each do |attr|
serializer.attribute(attr)
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
SchemaUtils.associations(active_record_class).each do |a|
serializer.send(serializer_association(a), a.name)
end
if active_record_class.name == ForestLiana.integrations
.try(:[], :intercom).try(:[], :user_collection)
serializer.send(:has_many, :intercom_conversations) { }
serializer.send(:has_many, :intercom_attributes) { }
end
if active_record_class.name == ForestLiana.integrations
.try(:[], :stripe).try(:[], :user_collection)
serializer.send(:has_many, :stripe_payments) { }
serializer.send(:has_many, :stripe_invoices) { }
serializer.send(:has_many, :stripe_cards) { }
end
SerializerFactory.define_serializer(active_record_class, serializer)
serializer
end
|