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.
- #versioned? ⇒ Boolean
Instance Method Details
#current_versions ⇒ Object
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
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_current ⇒ Object
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
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
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
3 4 5 |
# File 'lib/versioned_record/class_methods.rb', line 3 def versioned? true end |