Module: Ika::ClassMethods

Includes:
CarrierWave::Base64Uploader
Defined in:
lib/ika.rb

Instance Method Summary collapse

Methods included from CarrierWave::Base64Uploader

#base64_conversion, #split_base64

Instance Method Details

#export(options = {}, objects = nil) ⇒ Object



112
113
114
# File 'lib/ika.rb', line 112

def export(options = {}, objects = nil)
  ika_export(options, objects)
end

#ika_export(options = {}, objects = nil) ⇒ Object



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
# File 'lib/ika.rb', line 78

def ika_export(options = {}, objects = nil)
  CarrierWave::Uploader::Base.json_with_raw_data = true
  all_symbol = true
  options[:include] ||= []
  options[:include] = [options[:include]] unless options[:include].is_a?(Array)
  objects ||= self.includes(options[:include])
  options[:include].each do |opt|
    all_symbol = false unless opt.is_a?(Symbol)
  end

  if all_symbol
    json = objects.to_json(include: options[:include])
    CarrierWave::Uploader::Base.json_with_raw_data = false
    return json
  end

  whole_obj_arr = []
  objects.each do |object|
    obj_arr = JSON.parse(object.to_json)
    options[:include].each do |relation|
      if relation.is_a?(::Hash)
        relation.keys.each do |property|
          obj_arr[property] = JSON.parse(object.try(property).export({include: relation[property]}, object.try(property)))
        end
      elsif relation.is_a?(Symbol)
        obj_arr[relation] = JSON.parse(object.try(relation).present? ? object.try(relation).to_json : '[]')
      end
    end
    whole_obj_arr.push(obj_arr)
  end
  CarrierWave::Uploader::Base.json_with_raw_data = false
  JSON.generate(whole_obj_arr)
end

#ika_import(json_or_array, options = {}) ⇒ Object



13
14
15
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
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
# File 'lib/ika.rb', line 13

def ika_import(json_or_array, options = {})
  if json_or_array.is_a?(Array)
    objects = json_or_array
  else
    objects = JSON.parse(json_or_array)
    objects = [objects] unless objects.is_a?(Array)
  end
  json_or_array = nil

  ActiveRecord::Base.transaction do
    if options && options[:sync]
      remove_target_ids = all.pluck(:id)
    else
      remove_target_ids = []
    end
    objects.each do |object|
      record_exists = false
      if exists?(id: object['id'].to_i)
        record_exists = true
        exist_object = where(id: object['id'].to_i).first
      end

      object_params = {}
      object.keys.each do |key|
        if object[key].is_a?(Array)
          reflections[key].klass.import(object[key])
        else
          if new.try(key.to_sym).class.superclass == CarrierWave::Uploader::Base
            need_update = true
            obj_url = object[key]['url'] || object[key][:url]
            obj_md5 = object[key]['md5'] || object[key][:md5]
            obj_data = object[key]['data'] || object[key][:data]
            obj_name = object[key]['name'] || object[key][:name]
            if obj_url && File.exist?('public' + obj_url)
              md5 = Digest::MD5.file('public' + obj_url)
              need_update = false if md5 == obj_md5 && record_exists && obj_name == Pathname(exist_object.try(key.to_sym).to_s).basename.to_s
            end
            if obj_url && need_update
              object_params[key] = base64_conversion(obj_data, obj_name)
            elsif obj_url.blank?
              object_params[('remove_' + key).to_sym] = true
            end
          else
            object_params[key] = object[key]
          end
        end
        object.delete(key) if key != 'id'
      end
      if record_exists
        exist_object.attributes = object_params
        exist_object.save!(validate: false)
      else
        new(object_params).save!(validate: false)
      end
      remove_target_ids -= [object['id'].to_i]
      object.delete('id')
    end
    where(id: remove_target_ids).destroy_all
  end
end

#import(json_or_array, options = {}) ⇒ Object



74
75
76
# File 'lib/ika.rb', line 74

def import(json_or_array, options = {})
  ika_import(json_or_array, options)
end