Module: Ika

Extended by:
ActiveSupport::Concern
Defined in:
lib/ika.rb,
lib/ika/version.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

VERSION =
'1.0.6'

Instance Method Summary collapse

Instance Method Details

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



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

def export(options = {}, object = nil)
  CarrierWave::Uploader::Base.json_with_raw_data = true
  objects ||= self
  all_symbol = true
  options[:include] ||= []
  options[:include] = [options[:include]] unless options[:include].is_a?(Array)
  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

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