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

def self.get_serializer_name(active_record_class)
  if active_record_class == ::Intercom::Conversation
    "ForestLiana::IntercomConversationSerializer"
  elsif active_record_class == ::Intercom::User
    "ForestLiana::IntercomAttributeSerializer"
  elsif active_record_class == Stripe::Charge
    "ForestLiana::StripePaymentSerializer"
  elsif active_record_class == Stripe::Card
    "ForestLiana::StripeCardSerializer"
  elsif 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::#{name}Serializer"
  end
end

Instance Method Details

#serializer_for(active_record_class) ⇒ Object



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

  # 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

  SchemaUtils.associations(active_record_class).each do |a|
    serializer.send(serializer_association(a), a.name)
  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

  SerializerFactory.define_serializer(active_record_class, serializer)

  serializer
end