Class: ForestLiana::SerializerFactory

Inherits:
Object
  • Object
show all
Defined in:
app/serializers/forest_liana/serializer_factory.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.define_serializer(active_record_class, serializer) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
# File 'app/serializers/forest_liana/serializer_factory.rb', line 6

def self.define_serializer(active_record_class, serializer)
  class_name = ForestLiana.name_for(active_record_class).classify
  module_name = class_name.deconstantize

  name = module_name if module_name
  name += class_name.demodulize

  # NOTICE: Create the serializer in the UserSpace to avoid conflicts with
  # serializer created from integrations, actions, segments, etc.
  ForestLiana::UserSpace.const_set("#{name}Serializer", serializer)
end

.get_serializer_name(active_record_class) ⇒ Object



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
# File 'app/serializers/forest_liana/serializer_factory.rb', line 18

def self.get_serializer_name(active_record_class)
  if defined?(::Intercom::Conversation) &&
    active_record_class == ::Intercom::Conversation
    "ForestLiana::IntercomConversationSerializer"
  elsif defined?(::Intercom::User) &&
    active_record_class == ::Intercom::User
    "ForestLiana::IntercomAttributeSerializer"
  elsif defined?(::Stripe::Charge) &&
    active_record_class == ::Stripe::Charge
    "ForestLiana::StripePaymentSerializer"
  elsif defined?(::Stripe::Card) &&
    active_record_class == ::Stripe::Card
    "ForestLiana::StripeCardSerializer"
  elsif defined?(::Stripe::Invoice) &&
    active_record_class == ::Stripe::Invoice
    "ForestLiana::StripeInvoiceSerializer"
  elsif defined?(::Stripe::Subscription) &&
    active_record_class == ::Stripe::Subscription
    "ForestLiana::StripeSubscriptionSerializer"
  elsif defined?(::Stripe::BankAccount) &&
    active_record_class == ::Stripe::BankAccount
    "ForestLiana::StripeBankAccountSerializer"
  elsif active_record_class == ForestLiana::Model::Stat
    "ForestLiana::StatSerializer"
  elsif active_record_class == ForestLiana::Model::Collection
    "ForestLiana::CollectionSerializer"
  elsif active_record_class == ForestLiana::Model::Action
    "ForestLiana::ActionSerializer"
  elsif active_record_class == ForestLiana::Model::Segment
    "ForestLiana::SegmentSerializer"
  else
    class_name = ForestLiana.name_for(active_record_class).classify
    module_name = class_name.deconstantize

    name = module_name if module_name
    name += class_name.demodulize

    "ForestLiana::UserSpace::#{name}Serializer"
  end
end

Instance Method Details

#serializer_for(active_record_class) ⇒ Object



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
# File 'app/serializers/forest_liana/serializer_factory.rb', line 59

def serializer_for(active_record_class)
  serializer = Class.new {
    include 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 = {}

      # Has many smart field
      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"
        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
  }

  attributes(active_record_class).each do |attribute|
    serializer.attribute(attribute)
  end

  # NOTICE: Format time type fields during the serialization.
  attributes_time(active_record_class).each do |attribute|
    serializer.attribute(attribute) do |x|
      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
    end
  end

  # NOTICE: Format serialized fields.
  attributes_serialized(active_record_class).each do |attribute, serialization|
    serializer.attribute(attribute) do |x|
      value = object.send(attribute)
      value ? value.to_json : nil
    end
  end

  # NOTICE: Format CarrierWave url attribute
  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

  # NOTICE: Format Paperclip url attribute
  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

  # NOTICE: Format ActsAsTaggable attribute
  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|
    begin
      if 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
      # NOTICE: Let this error silent, a bad association warning will be
      #         displayed in the schema adapter.
    end
  end

  # Intercom
  if has_intercom_integration?(active_record_class.name)
    serializer.send(:has_many, :intercom_conversations) { }
    serializer.send(:has_many, :intercom_attributes) { }
  end

  # Stripe
  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