Search code examples
ruby-on-railsrefactoringversioningvestal-versionsreversion

vestal_versions and htmldiff question of reversion


I'm guessing there's probably an easier way to do what I'm doing so that the code is less unwieldy.

I had trouble understanding how to use the revert_to method... i wanted something where i could call up two different versions at the same time, but this doesn't seem to be the way that vestal_versions works.

This code works, but I'm wondering if I'm making something harder than it needs to be and I'd like to find out before I delve deeper.

@article = Article.find(params[:id])

if params[:versions]
  v = params[:versions].split(',')
  @article.revert_to(v.first.to_i)
  @content1 = @article.content
  @article.revert_to(v.last.to_i)
  @content2 = @article.content
end

In case you're wondering, I'm using this in conjunction with HTMLDIFF to get the version changes.

<div id="content">
  <% if params[:versions] %>
    <%= Article.diff(@content1, @content2) %>
  <% else %>
    <%= @article.content %>
  <% end %>
</div>

Solution

  • I think that you are looking for the changes_between method that vestal_versions provides.

    @article = Article.find(params[:id])
    
    if params[:versions]
      v = params[:versions].split(',')
      @article_changes = @article.changes_between(v.first.to_i, v.last.to_i)
    end
    

    then @article_changes is a hash of the changes between the versions. Something like

    {"content" => ["first version content", "second version content"]}
    

    Maybe different depending on what you have versioned.