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

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

  name = module_name if module_name
  name += class_name.demodulize

  ForestLiana::UserSpace.const_set("#{name}Serializer", serializer)
end

.get_serializer_name(active_record_class) ⇒ Object



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

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 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"
  else
    class_name = active_record_class.table_name.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



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

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 = {}

      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?
      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

  # 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

  # 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

  # ActsAsTaggable attribute
  if active_record_class.respond_to?(:acts_as_taggable) &&
    active_record_class.acts_as_taggable.try(: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

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

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

  ForestLiana::SerializerFactory.define_serializer(active_record_class,
                                                   serializer)

  serializer
end