开发者

Vestal Versions changes_between not working for me

开发者 https://www.devze.com 2023-01-25 07:22 出处:网络
I was expecting it to be the union of all changes between 2 versions. (using vestal versions 1.0.2) ruby-1.8.7-p174 > contact.version

I was expecting it to be the union of all changes between 2 versions. (using vestal versions 1.0.2)

ruby-1.8.7-p174 > contact.version 
 => 12 
ruby-1.8.7-p174 > contact.latest_approved_version
 => 8 
ruby-1.8.7-p174 > contact.changes
 => {} 
ruby-1.8.7-p174 > contact.versions.last.changes
 => {"first_name"=>["I changed this one baby", "AdminF"]} 
ruby-1.8.7-p174 > contact.changes_between(8,12)
 => {} 
ruby-1.8.7-p174 > contact.changes_between(9,12)
 => {"deleted"=>[true, false]} 
ruby-1.8.7-p174 > contact.changes_between(10,12)
 => {} 
ruby-1.8.7-p174 > contact.changes_between(11,12)
 => {"first_name"=>["I changed this one baby", "AdminF"]} 

As you c开发者_开发百科an see no changes between 8 and 12, but changes between some of them.

The weird thing is I swear this was working yesterday!

Here is the method vestal is using, im not sure what the problem is:

 def changes_between(from, to)
    from_number, to_number = versions.number_at(from), versions.number_at(to)
    return {} if from_number == to_number
    chain = versions.between(from_number, to_number).reject(&:initial?)
    return {} if chain.empty?

    backward = from_number > to_number
    backward ? chain.pop : chain.shift unless from_number == 1 || to_number == 1

    chain.inject({}) do |changes, version|
      changes.append_changes!(backward ? version.changes.reverse_changes : version.changes)
    end
  end


It is possible that there is nothing wrong with the example you provided. If for example there are no differences between version 8 and 12. Even though there has been changes in version 9, 10 and 11, the method changes_between will not show any changes if the model's attributes has been reverted to the same values.

I think the easiest way to verify that is to check:

>> contact.revert_to(8)
=> "8"
>> contact.inspect
=> "#<Contact ... >"
>> contact.revert_to(12)
=> "12"
>> contact.inspect
=> "#<Contact ... >"

And then compare the output. I can't think of any other reason for the result you described.

By the way, the call you made to "last_approved_version", is that something you manually added to your model or is it something built-in in vestal_versions? Because I also use the 1.0.2 vestal_version and I can't find any reference to that. So if it really is something built-in then we probably have different forks of vestal_version and that might be the cause...

0

精彩评论

暂无评论...
验证码 换一张
取 消