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



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

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)


58
59
60
# File 'lib/versioned_record/class_methods.rb', line 58

def exclude(record)
  where(id: record._id).where.not(version: record.version)
end

#exclude_currentObject

Scope to exclude current version from a query



13
14
15
# File 'lib/versioned_record/class_methods.rb', line 13

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:



32
33
34
35
36
37
38
# File 'lib/versioned_record/class_methods.rb', line 32

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

Parameters:

  • id (Integer)

    the record’s ID

Raises:

  • (ActiveRecord::RecordNotFound)

    if no record with the given ID is found



43
44
45
46
47
# File 'lib/versioned_record/class_methods.rb', line 43

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

#versioned?Boolean

Returns:

  • (Boolean)


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

def versioned?
  true
end