active_merge

Declares a service object and the #merge_all helper method for merging active record class instances.


License
MIT
Install
gem install active_merge -v 1.2.1

Documentation

ActiveMerge

Gem Version Build Status Dependency
Status Coverage Status

Declares the ActiveMerge module for extending ActiveRecord models.

Main usage

The module contains the merge_all class method for merging class instances into the first one.

When merging a list of instances:

  • all “has_many” relatives are reattached to the instance with the lowest id

  • all the instances except for the first one (with the lowest id) are deleted

Example

class Post < ActiveRecord
  extend ActiveMerge
  has_many :comments 
end

Post.all.merge_all
# This will merge all the posts into the first one.
# The other posts will be deleted after their comment are reattached
# to the first post.

Skipping validations and callbacks

You can use the validate: false option.

With this option set any activerecord validation and callback are skipped.

Post.all.merge_all validate: false

*Warning*

Only activerecord validations and callbacks are skipped! Raising other errors (by database etc.) will cause all changes to roll back as before.

Version changes

See the CHANGELOG.

License

This project rocks and uses MIT LICENSE[LICENSE].