Class: Docsmith::DocumentVersion

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

Overview

Immutable content snapshot. Table is docsmith_versions. Class name is DocumentVersion (not Version) to avoid colliding with lib/docsmith/version.rb which holds the Docsmith::VERSION constant.

Instance Method Summary collapse

Instance Method Details

#export(**options) ⇒ Hash

The JSON export envelope as a Hash, for embedding in a larger API response without a JSON round-trip. Same shape as render(:json).

Note this is deliberately NOT as_json. DocumentVersion is an ActiveRecord::Base, and overriding as_json would silently change what render json: @version returns for every app already relying on standard attribute serialization.

Parameters:

  • options (Hash)

    :include_parsed adds "data" with the parsed document

Returns:

  • (Hash)

    string-keyed, JSON-ready

Raises:



61
62
63
# File 'lib/docsmith/document_version.rb', line 61

def export(**options)
  Rendering::JsonRenderer.new.to_h(self, **options)
end

#previous_versionDocsmith::DocumentVersion?

Returns:



30
31
32
33
34
# File 'lib/docsmith/document_version.rb', line 30

def previous_version
  document.document_versions
          .where("version_number < ?", version_number)
          .last
end

#render(format, **options) ⇒ String

Renders this version's content in the given output format.

Parameters:

  • format (Symbol)

    :html or :json

  • options (Hash)

    passed through to the renderer

Returns:

  • (String)

Raises:

  • (ArgumentError)

    for unknown formats



42
43
44
45
46
47
48
# File 'lib/docsmith/document_version.rb', line 42

def render(format, **options)
  case format.to_sym
  when :html then Rendering::HtmlRenderer.new.render(self, **options)
  when :json then Rendering::JsonRenderer.new.render(self, **options)
  else raise ArgumentError, "Unknown render format: #{format}. Supported: :html, :json"
  end
end