Module: VersionedRecord::ClassMethods

Defined in:
lib/versioned_record/class_methods.rb

Instance Method Summary collapse

Instance Method Details

#current_versionsObject

Scope to limit records to only the current versions



4
5
6
# File 'lib/versioned_record/class_methods.rb', line 4

def current_versions
  where(is_current_version: true)
end

#exclude(record) ⇒ Object

Scope to exclude the given record from results. This is handy when retrieving all versions of a record except for one Say when we are viewing all other versions to a given record

Examples:


person = Person.find(1, 3)
other_versions = person.versions.exclude(person)


54
55
56
# File 'lib/versioned_record/class_methods.rb', line 54

def exclude(record)
  where(id: record.id).where('version != ?', record.version)
end

#exclude_currentObject

Scope to exclude current version from a query



9
10
11
# File 'lib/versioned_record/class_methods.rb', line 9

def exclude_current
  where(is_current_version: false)
end

#find(*args) ⇒ Object

Finds a record as per ActiveRecord::Base If only an ID is provided then it returns the current version for that ID Otherwise, both an ID and a version can be provided

Examples:

Single Argument


Model.find(1) => # The latest record

An ID and a version


Model.find(1, 0) => # The first version

See Also:



28
29
30
31
32
33
34
# File 'lib/versioned_record/class_methods.rb', line 28

def find(*args)
  if args.length == 1 && !args.first.kind_of?(Array)
    find_current(args.first)
  else
    super
  end
end

#find_current(id) ⇒ Object

Find the current version of the record with the given ID

Raises:

  • (ActiveRecord::RecordNotFound)

    if no record with the given ID is found



39
40
41
42
43
# File 'lib/versioned_record/class_methods.rb', line 39

def find_current(id)
  current_versions.where(id: id).first.tap do |record|
    raise ActiveRecord::RecordNotFound unless record.present?
  end
end