Module: VersionedRecord
- Defined in:
- lib/versioned_record.rb,
lib/versioned_record/version.rb,
lib/versioned_record/class_methods.rb,
lib/versioned_record/attribute_builder.rb
Defined Under Namespace
Modules: ClassMethods Classes: AttributeBuilder
Constant Summary collapse
- VERSION =
"0.2.0"
Class Method Summary collapse
Instance Method Summary collapse
-
#build_version(new_attrs = {}) ⇒ Object
Build (but do not save) a new version of the record This allows you to use the object in forms etc After the record is saved, all previous versions will be deprecated and this record will be marked as current.
-
#create_version(new_attrs = {}) ⇒ Object
Same as #create_version! but will not raise if the record is invalid.
-
#create_version!(new_attrs = {}) ⇒ Object
Create a new version of the existing record A new version can only be created once for a given record and subsequent versions must be created by calling create_version! on the latest version.
-
#deprecate_old_versions_after_create! ⇒ Object
Ensure that old versions are deprecated when we save (only applies on create).
-
#versions ⇒ Object
Retrieve all versions of this record Can be chained with other scopes.
Class Method Details
.included(model_class) ⇒ Object
12 13 14 15 16 |
# File 'lib/versioned_record.rb', line 12 def self.included(model_class) model_class.extend ClassMethods model_class.primary_keys = :id, :version model_class.after_save :ensure_version_deprecation!, on: :create end |
Instance Method Details
#build_version(new_attrs = {}) ⇒ Object
Build (but do not save) a new version of the record This allows you to use the object in forms etc After the record is saved, all previous versions will be deprecated and this record will be marked as current
58 59 60 61 62 |
# File 'lib/versioned_record.rb', line 58 def build_version(new_attrs = {}) new_version = self.class.new(new_version_attrs(new_attrs)).tap do |built| built.deprecate_old_versions_after_create! end end |
#create_version(new_attrs = {}) ⇒ Object
Same as #create_version! but will not raise if the record is invalid
42 43 44 45 46 |
# File 'lib/versioned_record.rb', line 42 def create_version(new_attrs = {}) create_operation do self.class.create(new_version_attrs(new_attrs)) end end |
#create_version!(new_attrs = {}) ⇒ Object
Create a new version of the existing record A new version can only be created once for a given record and subsequent versions must be created by calling create_version! on the latest version
Attributes that are not specified here will be copied to the new version from the previous version
This method will still fire ActiveRecord callbacks for save/create etc as per normal record creation
33 34 35 36 37 |
# File 'lib/versioned_record.rb', line 33 def create_version!(new_attrs = {}) create_operation do self.class.create!(new_version_attrs(new_attrs)) end end |
#deprecate_old_versions_after_create! ⇒ Object
Ensure that old versions are deprecated when we save (only applies on create)
77 78 79 |
# File 'lib/versioned_record.rb', line 77 def deprecate_old_versions_after_create! @deprecate_old_versions_after_create = true end |
#versions ⇒ Object
Retrieve all versions of this record Can be chained with other scopes
71 72 73 |
# File 'lib/versioned_record.rb', line 71 def versions self.class.where(id: self.id) end |