Class: RelationalExporter::RecordWorker

Inherits:
Object
  • Object
show all
Includes:
Celluloid, Celluloid::Logger
Defined in:
lib/relational_exporter/record_worker.rb

Constant Summary collapse

@@MAX_ASSOCIATED =
{}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.csv_header_prefix_for_key(klass, key, index = nil) ⇒ Object



97
98
99
100
101
102
103
104
105
# File 'lib/relational_exporter/record_worker.rb', line 97

def self.csv_header_prefix_for_key(klass, key, index=nil)
  if klass.respond_to?(:active_model_serializer) && !klass.active_model_serializer.nil? && klass.active_model_serializer.respond_to?(:csv_header_prefix_for_key)
    header_prefix = klass.active_model_serializer.csv_header_prefix_for_key key.to_sym
  else
    header_prefix = klass.to_s
  end

  header_prefix + index.to_s + key.to_s.classify
end

.get_maxed_row_arr(records, fields, max_count = 0, &block) ⇒ Object



86
87
88
89
90
91
92
93
94
95
# File 'lib/relational_exporter/record_worker.rb', line 86

def self.get_maxed_row_arr(records, fields, max_count=0, &block)
  return if max_count.nil?
  max_count.times do |i|
    record = records[i].nil? ? {} : RecordWorker.serialized_attributes_for_object_or_class(records[i])
    fields.each do |field|
      val = record[field]
      yield val
    end
  end
end

.serialized_attributes_for_object_or_class(object) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/relational_exporter/record_worker.rb', line 107

def self.serialized_attributes_for_object_or_class(object)
  return {} if object.nil?

  klass, model = object.is_a?(Class) ? [object, object.first] : [object.class, object]

  return {} if model.nil?

  if model.respond_to?(:active_model_serializer) && !model.active_model_serializer.nil?
    serialized = model.active_model_serializer.new(model).as_json(root: false)
  end

  serialized = model.attributes if serialized.nil?
  serialized
end

.symbolize_options(options) ⇒ Object



122
123
124
125
126
127
128
129
# File 'lib/relational_exporter/record_worker.rb', line 122

def self.symbolize_options(options)
  options = options.as_json
  if options.is_a? Hash
    options.deep_symbolize_keys!
  elsif options.is_a? Array
    options.map { |val| RecordWorker.symbolize_options val }
  end
end

Instance Method Details

#actor_died(actor, reason) ⇒ Object



9
10
11
# File 'lib/relational_exporter/record_worker.rb', line 9

def actor_died(actor, reason)
  puts "Oh no! #{actor.inspect} has died because of a #{reason.class}" unless reason.nil?
end

#get_csv_row(record_sequence, record, associations, with_headers = false) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/relational_exporter/record_worker.rb', line 15

def get_csv_row(record_sequence, record, associations, with_headers=false)
  @record = record
  @associations = associations

  get_rows with_headers

  info "Queueing record #{record_sequence} <#{Actor.current}>…"
  Celluloid::Actor[:csv_builder].queue[record_sequence] = [@header_row, @value_row]
end

#get_rows(get_headers = false) ⇒ Object



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
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
# File 'lib/relational_exporter/record_worker.rb', line 25

def get_rows(get_headers=false)
  @header_row = []
  @value_row = []
  main_klass = @record.class

  RecordWorker.serialized_attributes_for_object_or_class(@record).each do |field, value|
    @header_row << RecordWorker.csv_header_prefix_for_key(main_klass, field) if get_headers
    @value_row << value
  end

  @associations.each do |association_accessor, association_options|
    association_accessor = association_accessor.to_s.to_sym
    association_klass = association_accessor.to_s.classify.constantize
    scope = RecordWorker.symbolize_options association_options.scope

    associated = @record.send association_accessor
    # TODO - this might suck for single associations (has_one) because they don't return an ar::associations::collectionproxy
    associated = associated.find_all_by_scope(scope) unless scope.blank? || !associated.respond_to?(:find_all_by_scope)

    if associated.is_a? Hash
      associated = [ associated ]
    elsif associated.blank?
      associated = []
    end

    foreign_key = main_klass.reflections[association_accessor].foreign_key rescue nil

    fields = RecordWorker.serialized_attributes_for_object_or_class(association_klass).keys

    fields.reject! {|v| v == foreign_key } if foreign_key

    if get_headers
      @@MAX_ASSOCIATED[association_accessor] ||= begin
        case main_klass.reflections[association_accessor].macro
        when :has_many
          max_associated = association_klass.find_all_by_scope(scope)
                                            .joins(main_klass.table_name.to_sym)
                                            .order('count_all desc')
                                            .group(foreign_key)
                                            .limit(1).count.flatten[1]
        when :has_one
          max_associated = 1
        end

        max_associated = 0 if max_associated.nil?
        max_associated
      end

      @@MAX_ASSOCIATED[association_accessor].times do |i|
        fields.each do |field|
          @header_row << RecordWorker.csv_header_prefix_for_key(association_klass, field, i+1)
        end
      end
    end

    RecordWorker.get_maxed_row_arr(associated, fields, @@MAX_ASSOCIATED[association_accessor]) do |field|
      @value_row << field
    end
  end
end