Tree Diff

Compare large object trees. Like a generic and standalone ActiveModel::Dirty, though completely ORM agnostic.

Installation

gem install tree_diff

Getting Started

  1. Define a diff class that inherits from TreeDiff. Pass an array of hashes and arrays to define your object tree to observe.

    This format is just like how sets of nested attributes are passed to strong params.

    class MyDiffClass < TreeDiff
      observe :status, :created_at, :user_id
        items: [:status, :description, :cost],
        starting_location: [:latitude, :longitude, :updated_at,
                            address: [:city, :state, :country]
    end
    
  2. Instantiate your diff class and pass it the object you want to compare just before mutating it. For example, controller usage:

    class ThingsController
      def update
        thing = Thing.find(params[:id])
        my_diff = MyDiffClass.new(thing)
    
        if thing.update(thing_params)
          handle_stuff if my_diff.saw_any_change?
          redirect_to thing, notice: 'Updated thing.'
        else
          # ...
        end
      end
    end