Project

cloned

0.0
Repository is archived
No commit activity in last 3 years
No release in over 3 years
Simple lib for cloning rails models and associations.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.15
~> 10.0
~> 3.0

Runtime

 Project Readme

Cloned

Simple gem for copying trees of ActiveRecord models.

Installation

Add this line to your application's Gemfile:

gem 'cloned'

And then execute:

$ bundle

Or install it yourself as:

$ gem install cloned

Usage

First you need to define clonning stategy:

class CompanyCopyStrategy < Cloned::Strategy
  declare :company do
    before do |copied_company, _target_company|
      copied_company.copied_at = Time.zone.now
    end

    after do |copied_company, _target_company|
      copied_company.departments_count = copied_company.departments.size
    end

    nullify :created_at, :updated_at

    association(:departments)
  end

  declare :department do
    association :employees, if: ->(employee) { employee.salary > 2000 }
  end

  declare :employee do
    nullify :last_sickleave, :vacation_at
  end
end

Then you able to build or create clones:

CompanyCopyStrategy.make(target: Company.first, destination: Account.last.companies)

or

CompanyCopyStrategy.make(target: Company.first)

Cloning destination can be has_many association or nil.

Contributing

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

License

The gem is available as open source under the terms of the MIT License.