On Sunday, May 3, 2015 at 3:02:58 PM UTC-7, Andrew Burleson wrote:
>
> I'm working on doing some versioning on a sequel model. It's simple enough 
> to do something like: `Model.one_to_many :versions` and then use an 
> `after_save` hook to create a version which serializes the state of the 
> model.
>
> However, I'm interested in adding some optional field on the version 
> table, like user (who made the changes) and notes (why the changes were 
> made). I'd love to create a simple syntax for accessing these fields that 
> exist on the version table and not the original model, something like:
>
> a_model.update({name: "foo", amount: 123}) do |version|
>   version.user = current_user
>   version.note = "Updated model amount for some reason"
> end
>
> In the example above the idea is the model version will be created 
> regardless, but if a block is given the not yet saved version is yielded to 
> the block so the caller can access it (to add change notes etc).
>
> I've been able to make something like this work by overriding `save` and 
> `update`, but it's not dry, and it doesn't seem like this would cover all 
> the ways you can save a model (e.g. `update_all`). Meanwhile doing this via 
> a hook doesn't seem viable as I see no way to forward the optional block 
> from the various save-triggering methods.
>
> Any suggestions? Is there a standardized or better approach to 
> implementing versioning using sequel?
>

The best way to do versioning in general is via a database trigger, but 
that isn't going to support the user/notes you want.

For what you want, I would add a method that stores information to an 
instance variable, and have the after_save hook check that instance 
variable, allowing for an API such as:

  a_model.update_reasons(:user=>current_user, :note=>"Updated model amount 
for some reason").
    update(name: "foo", amount: 123)

Thanks,
Jeremy

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.

Reply via email to