Module: Diffrent

Defined in:
lib/diffrent.rb

Instance Method Summary collapse

Instance Method Details

#diff_for(attr, old_version, new_version, options = {:format => :html}) ⇒ String

Get a diff between two arbitrary versions of an ActiveRecord object.

Parameters:

  • attr (Symbol)

    the attribute you want to diff.

  • old_version (Integer)

    the version you want to start your diff at.

  • new_version (Integer)

    the version you want to diff to.

  • options (Hash) (defaults to: {:format => :html})

    an options hash allowing you to pass a :format.

Returns:

  • (String)

    a diff string. If :format was nil, this could be a [Diffy::Diff].



10
11
12
13
14
15
16
17
# File 'lib/diffrent.rb', line 10

def diff_for(attr, old_version, new_version, options={:format => :html})
  changes = self.changes_between(new_version, old_version)
  if changes.key?(attr)
    Diffy::Diff.new(changes[attr].first, changes[attr].last).to_s(options[:format])
  else
    self.send(attr)
  end
end

#has_versions_after?(v) ⇒ Boolean

Are there later versions?

Parameters:

  • v (Integer)

    the version number to check.

Returns:

  • (Boolean)

    whether there are any versions after version v.



29
30
31
# File 'lib/diffrent.rb', line 29

def has_versions_after?(v)
  self.versions.at(v + 1)
end

#has_versions_before?(v) ⇒ Boolean

Are there previous versions?

Parameters:

  • v (Integer)

    the version number to check.

Returns:

  • (Boolean)

    whether there are any versions before version v.



22
23
24
# File 'lib/diffrent.rb', line 22

def has_versions_before?(v)
  self.versions.at(v - 1)
end