Class: Docsmith::Document

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/docsmith/document.rb

Overview

AR model backed by docsmith_documents. Serves as both a standalone versioned document and the shadow record auto-created when Docsmith::Versionable is included on any AR model.

Shadow record lifecycle:

include Docsmith::Versionable on Article → first save_version! call does:
Docsmith::Document.find_or_create_by!(subject: article_instance)
subject_type / subject_id link back to the originating record.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_record(record, field: nil) ⇒ Docsmith::Document

Find or create the shadow Document for an existing AR record.

Parameters:

  • record (ActiveRecord::Base)
  • field (Symbol, nil) (defaults to: nil)

    ignored — content_field comes from class config

Returns:



37
38
39
40
41
42
# File 'lib/docsmith/document.rb', line 37

def self.from_record(record, field: nil)
  find_or_create_by!(subject: record) do |doc|
    doc.content_type = "markdown"
    doc.title = record.respond_to?(:title) ? record.title.to_s : record.class.name
  end
end

Instance Method Details

#current_versionDocsmith::DocumentVersion?

Returns latest version by version_number.

Returns:



29
30
31
# File 'lib/docsmith/document.rb', line 29

def current_version
  document_versions.last
end