Sync changes to an ActiveRecord model to another model
Ruby
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib
.gitignore
Manifest
README.rdoc
Rakefile
init.rb
model_sync.gemspec

README.rdoc

model_sync

model_sync is a simple gem for pushing changes to one of your rails models to another.

class User < ActiveRecord::Base model_sync :sync_to => :student, :relationship => { :student_id => :s_stno }, :mappings => { :forename => :s_forename }, :mapping_block => Proc.new do |master, slave| slave.update_attribute(:s_sex, master.gender ? master.gender.code : nil) end end Assuming we have a Student model, the above code will add an after_save callback to the User model which will push changes to the forename value into the s_forename value of Student. The correct Student instance is found using the :relationship hash - student_id in User must equal s_stno in Student.

Although the above example only pushes changes to forename, you can add more mappings to the :mappings hash to push over as many values as you like.

When things are a bit more complicated than a simple like for like mapping you can use the :mapping_block option to pass a block which has access to the master and slave objects. Using these objects you can perform more complicated tasks than just making one value equal another.

Why use it?

I developed this gem as part of a web front end which I'm building for an old legacy system. The legacy system is a student records system which holds students which are booked on courses. However I don't want to add users of the website into the system as students until they have actually booked a course. So there needs to be a certain amount of separation between the online element and the legacy system, mainly because I can't make changes to the database structure of the legacy system.

Once a user books a course and becomes a student as well we've got a user and a student record we need to keep in sync because the user could change their address through the website, for example, which then needs feeding into the main system.

The model_sync gem will do all the syncing for me in a totally transparent way.

Installation

This gem lives at gemcutter.org (which is amazing by the way!). To use gemcutter is simplicity itself:

sudo gem install gemcutter
sudo gem tumble

And now with gemcutter as your preferred gem source you can type:

sudo gem install model_sync

Limitations

Currently the changes are pushed over with an after_save hook using update_attribute on the :sync_to model. This bypasses validations and any failures on update_attribute are currently not handled.

Also the instance methods which model_sync adds to a class are not available through associations. So if we did something like…

post.user.synced_with_student?

… it won't work because we are going through an association which actually uses a proxy behind the scenes. Instead you need to force the instantiation of the target:

post.user.target.synced_with_student?

I'm not totally sure how to get around that yet. It's something to do with an ActiveRecord association proxy being used instead of an actual instance of the target class (in this case the User class). If anyone knows how to fix that then please feel free to fork the project and patch.