Class: Yesterday::Changeset

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/yesterday/changeset.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.for_changed_object(object) ⇒ Object



13
14
15
# File 'lib/yesterday/changeset.rb', line 13

def self.for_changed_object(object)
  where(:changed_object_type => object.class.to_s, :changed_object_id => object.id)
end

.last_for(object) ⇒ Object



17
18
19
# File 'lib/yesterday/changeset.rb', line 17

def self.last_for(object)
  for_changed_object(object).order('created_at DESC').first
end

.version(version_number) ⇒ Object



9
10
11
# File 'lib/yesterday/changeset.rb', line 9

def self.version(version_number)
  where(:version_number => version_number)
end

.version_number_for(object) ⇒ Object



21
22
23
# File 'lib/yesterday/changeset.rb', line 21

def self.version_number_for(object)
  last_for(object).try(:version_number) || 0
end

Instance Method Details

#made_changesObject



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/yesterday/changeset.rb', line 29

def made_changes
  unless @made_changes
    compare_version_number ||= version_number > 1 ? (version_number - 1) : 1

    from_attributes   = Versioning.changeset_for(compare_version_number, changed_object).object_attributes
    to_attributes     = object_attributes
    diff              = Differ.new(from_attributes, to_attributes).diff

    @made_changes = VersionedObjectCreator.new(diff).to_object
  end

  @made_changes
end

#objectObject



25
26
27
# File 'lib/yesterday/changeset.rb', line 25

def object
  @object ||= VersionedObjectCreator.new(object_attributes).to_object if object_attributes.present?
end