Module: SaveIdHashToFile

Instance Method Summary collapse

Methods included from SaveFile

#current_time_for_subfolder, #ensure_path, #folder_path, #full_file_path, #save_file

Instance Method Details

#get_exported_object(model, id) ⇒ Object



29
30
31
32
# File 'lib/backup/save_id_hash_to_file.rb', line 29

def get_exported_object(model, id)
  object = model.find(id)
  object.attributes
end

#save_id_hash_to_file(id_hash) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/backup/save_id_hash_to_file.rb', line 6

def save_id_hash_to_file(id_hash)
  id_hash.each do |name, ids|
    ids.sort.each_slice(@config.limit.to_i) do |ids_batch|
      save_ids_batch_to_file(name, ids_batch)
    end
  end
end

#save_ids_batch_to_file(name, ids_batch) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/backup/save_id_hash_to_file.rb', line 14

def save_ids_batch_to_file(name, ids_batch)
  model = Model.get_model(name)

  export = {}
  export[:table_name] = model.table_name
  export[:data] = ids_batch.map do |id|
    get_exported_object(model, id)
  end

  content = JSON.pretty_generate(export)
  file_name = "#{name}_#{ids_batch.first}-#{ids_batch.last}.json"
  file_path = @subfolder.present? ? "#{@subfolder}/#{file_name}" : file_name
  save_file(file_path, content)
end