0.0
No commit activity in last 3 years
No release in over 3 years
Provide export / import to ActiveRecord
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.13
~> 10.0
~> 3.0

Runtime

 Project Readme

ActiveArchiver

Provide export / import to ActiveRecord and support CarrierWave.

Installation

Add this line to your application's Gemfile:

gem 'active_archiver', github:'zaru/active_archiver'

And then execute:

$ bundle

Usage

sample code

export = User.find(1).export

User.find(1).destroy
User.find(1)
=> ActiveRecord::RecordNotFound: Couldn't find User with 'id'=1

User.import(export)
User.find(1)
=> #<User id: 1, ...>

export

Returns the specified model object as a hash. Images uploaded with CarrierWave are encoded in Base64.

export = User.last.export

=> {:attributes=>
  {"id"=>3,
   "last_name"=>"hoge",
   "first_name"=>"piyo",
   "email"=>"hogepiyo@example.com",
   "created_at"=>Sun, 06 Nov 2016 21:04:11 JST +09:00,
   "updated_at"=>Tue, 22 Nov 2016 12:16:28 JST +09:00},
 :associations=>[]}

If you want to export related models as well

export = User.last.export(includes: [:photos])

=> {:attributes=>
  {"id"=>3,
   "last_name"=>"hoge",
   "first_name"=>"piyo",
   "email"=>"hogepiyo@example.com",
   "created_at"=>Sun, 06 Nov 2016 21:04:11 JST +09:00,
   "updated_at"=>Tue, 22 Nov 2016 12:16:28 JST +09:00},
 :associations=>
   [{:model_name=>"Photo",
     :association_name=>:photos,
     :attributes=>
      {"id"=>57,
       "user_id"=>3,
       "image"=>
        {:url=>"https://example.com/uploads/hoge.png",
         :file_name=>"hoge.png",
         :blob=>
          "data:image/png;base64,iVBORw...

To specify a nested model.

class User < ApplicationRecord
  has_many :photos
  has_many :pets
end

class Pet < ApplicationRecord
  has_many :pet_photos
end
export = User.last.export(includes: [:photos, pets:[:pet_photos]])

import

User.import(export)

archive

Write the temporary file.

Hoge.find(1).archive

=> #<File:/tmp/20161125-12571-ak10w5.json (closed)>

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/zaru/active_archiver.