Class: ActiveVersioning::VersionManager

Inherits:
Struct
  • Object
show all
Defined in:
lib/active_versioning/version_manager.rb

Constant Summary collapse

BLACKLISTED_ATTRIBUTES =
%w(
  created_at
  updated_at
  published
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#recordObject

Returns the value of attribute record

Returns:

  • (Object)

    the current value of record



2
3
4
# File 'lib/active_versioning/version_manager.rb', line 2

def record
  @record
end

Instance Method Details

#create_draft_from_version(id) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/active_versioning/version_manager.rb', line 9

def create_draft_from_version(id)
  version = record.versions.find(id)

  ensure_compatibility_with(version)

  new_version        = record.versions.draft.first_or_create(event: 'draft')
  new_version.object = version.object
  new_version.save
end

#ensure_compatibility_with(version) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/active_versioning/version_manager.rb', line 19

def ensure_compatibility_with(version)
  incompatible_attributes(version).tap do |incompatible_attrs|
    if incompatible_attrs.any?
      raise Errors::IncompatibleVersion.new(record, version), "The given version contains attributes that are no longer compatible with the current schema: #{incompatible_attrs.to_sentence}."
    end
  end
end

#incompatible_attributes(version) ⇒ Object



27
28
29
# File 'lib/active_versioning/version_manager.rb', line 27

def incompatible_attributes(version)
  version.object.keys - record.versioned_attributes.keys
end