Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/clowne-rb/clowne
A flexible gem for cloning models
https://github.com/clowne-rb/clowne
activerecord cloning hacktoberfest rails
Last synced: 2 months ago
JSON representation
A flexible gem for cloning models
- Host: GitHub
- URL: https://github.com/clowne-rb/clowne
- Owner: clowne-rb
- License: mit
- Created: 2017-10-24T14:39:39.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2023-11-08T22:19:37.000Z (about 1 year ago)
- Last Synced: 2024-11-15T05:12:05.446Z (2 months ago)
- Topics: activerecord, cloning, hacktoberfest, rails
- Language: Ruby
- Homepage: https://clowne.evilmartians.io
- Size: 1.15 MB
- Stars: 317
- Watchers: 6
- Forks: 18
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.txt
- Support: docs/supported_adapters.md
Awesome Lists containing this project
README
[![Gem Version](https://badge.fury.io/rb/clowne.svg)](https://badge.fury.io/rb/clowne)
[![Build Status](https://github.com/clowne-rb/clowne/actions/workflows/rspec.yml/badge.svg?branch=master)](https://github.com/clowne-rb/clowne/actions?query=branch%3Amaster+)
[![Docs](https://img.shields.io/badge/docs-link-brightgreen.svg)](https://clowne.evilmartians.io)# Clowne
A flexible gem for cloning your models. Clowne focuses on ease of use and provides the ability to connect various ORM adapters.
📖 Read [Evil Martians Chronicles](https://evilmartians.com/chronicles/clowne-clone-ruby-models-with-a-smile) to learn about possible use cases.
📑 [Documentation](https://clowne.evilmartians.io)
## Installation
To install Clowne with RubyGems:
```ruby
gem install clowne
```Or add this line to your application's Gemfile:
```ruby
gem "clowne"
```## Quick Start
Assume that you have the following model:
```ruby
class User < ActiveRecord::Base
# create_table :users do |t|
# t.string :login
# t.string :email
# t.timestamps null: false
# endhas_one :profile
has_many :posts
endclass Profile < ActiveRecord::Base
# create_table :profiles do |t|
# t.string :name
# end
endclass Post < ActiveRecord::Base
# create_table :posts
end
```Let's declare our cloners first:
```ruby
class UserCloner < Clowne::Cloner
adapter :active_recordinclude_association :profile, clone_with: SpecialProfileCloner
include_association :postsnullify :login
# params here is an arbitrary Hash passed into cloner
finalize do |_source, record, **params|
record.email = params[:email]
end
endclass SpecialProfileCloner < Clowne::Cloner
adapter :active_recordnullify :name
end
```Now you can use `UserCloner` to clone existing records:
```ruby
user = User.last
# => <#User id: 1, login: 'clown', email: '[email protected]'>operation = UserCloner.call(user, email: "[email protected]")
# => <#Clowne::Utils::Operation...>operation.to_record
# => <#User id: nil, login: nil, email: '[email protected]'>operation.persist!
# => truecloned = operation.to_record
# => <#User id: 2, login: nil, email: '[email protected]'>cloned.login
# => nil
cloned.email
# => "[email protected]"# associations:
cloned.posts.count == user.posts.count
# => true
cloned.profile.name
# => nil
```Take a look at our [documentation](https://clowne.evilmartians.io) for more info!
### Supported ORM adapters
Adapter |1:1 |*:1 | 1:M | M:M |
------------------------------------------|------------|------------|-------------|-------------------------|
[Active Record](https://clowne.evilmartians.io/#/active_record) | has_one | belongs_to | has_many | has_and_belongs_to|
[Sequel](https://clowne.evilmartians.io/#/sequel) | one_to_one | - | one_to_many | many_to_many |## Maintainers
- [Vladimir Dementyev](https://github.com/palkan)
- [Nikolay Sverchkov](https://github.com/ssnickolay)
## License
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).