Module: ActiveVersioning::Model::Versioned

Defined in:
lib/active_versioning/model/versioned.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



4
5
6
7
8
9
10
11
12
# File 'lib/active_versioning/model/versioned.rb', line 4

def self.included(base)
  base.class_eval do
    has_many :versions, -> { newest_first }, as: :versionable, dependent: :destroy

    after_create :create_version!

    attr_accessor :version_author
  end
end

Instance Method Details

#create_draft_from_version(version_id) ⇒ Object



47
48
49
# File 'lib/active_versioning/model/versioned.rb', line 47

def create_draft_from_version(version_id)
  version_manager.create_draft_from_version(version_id) and current_draft(true)
end

#current_draft(force_reload = false) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/active_versioning/model/versioned.rb', line 22

def current_draft(force_reload = false)
  unless persisted?
    raise ActiveVersioning::Errors::RecordNotPersisted.new("#{self} must be persisted to create a draft version")
  end

  @current_draft = nil if force_reload

  @current_draft ||= VersionProxy.new(versions.draft.first_or_create(
    object: versioned_attributes,
    event:  'draft'
  ))
end

#current_draft?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/active_versioning/model/versioned.rb', line 35

def current_draft?
  versions.draft.present?
end

#destroy_draftObject



39
40
41
# File 'lib/active_versioning/model/versioned.rb', line 39

def destroy_draft
  versions.draft.destroy_all
end

#live?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/active_versioning/model/versioned.rb', line 14

def live?
  true
end

#version?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/active_versioning/model/versioned.rb', line 18

def version?
  false
end

#version_managerObject



43
44
45
# File 'lib/active_versioning/model/versioned.rb', line 43

def version_manager
  @version_manager ||= VersionManager.new(self)
end

#versioned_attribute_namesObject



57
58
59
# File 'lib/active_versioning/model/versioned.rb', line 57

def versioned_attribute_names
  attribute_names - VersionManager::BLACKLISTED_ATTRIBUTES
end

#versioned_attributesObject



51
52
53
54
55
# File 'lib/active_versioning/model/versioned.rb', line 51

def versioned_attributes
  versioned_attribute_names.reduce(Hash.new) do |attrs, name|
    attrs.merge(name => send(name))
  end
end