Module: VersionedRecord::ClassMethods
- Defined in:
- lib/versioned_record/class_methods.rb
Instance Method Summary collapse
-
#current_versions ⇒ Object
Scope to limit records to only the current versions.
-
#exclude(record) ⇒ Object
Scope to exclude the given record from results.
-
#exclude_current ⇒ Object
Scope to exclude current version from a query.
-
#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.
-
#find_current(id) ⇒ Object
Find the current version of the record with the given ID.
Instance Method Details
#current_versions ⇒ Object
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
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_current ⇒ Object
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
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
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 |