Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[B] Handle packaging paths better when exporting #3767

Merged
merged 1 commit into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# frozen_string_literal: true

module Packaging
module Exportation
class ExportTextToEpubV3Job < ApplicationJob
discard_on ActiveJob::DeserializationError, ActiveRecord::RecordNotFound
discard_on ActiveJob::DeserializationError, ActiveRecord::RecordNotFound, Utility::IndexMap::AlreadyStoredObjectError, StandardError

around_perform :advisory_locked!

Expand All @@ -15,6 +17,12 @@ class ExportTextToEpubV3Job < ApplicationJob
def perform(text, force: false)
Packaging::Exportation::ExportTextToEpubV3.run! text: text, force: force
end

# We should lock only on the text id.
# @return [(String)]
def lock_key_arguments
[arguments.first.try(:id)]
end
end
end
end
28 changes: 28 additions & 0 deletions api/app/models/text_section.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,34 @@ class TextSection < ApplicationRecord

friendly_id :slug_candidates, use: :scoped, scope: :text

def has_duplicate_source_identifier?
# :nocov:
return false if text.blank? || new_record?
# :nocov:

text.text_sections.where.not(id: id).exists?(source_identifier: source_identifier)
end

def has_unique_source_identifier?
# :nocov:
return false if text.blank? || new_record?
# :nocov:

!has_duplicate_source_identifier?
end

# @!attribute [r] packaging_base_path
# @return [String]
def packaging_base_path
"text/#{packaging_identifier}".gsub(/\A(.+)(?<!\.xhtml)\z/, '\1.xhtml')
end

# @!attribute [r] packaging_identifier
# @return [String]
def packaging_identifier
has_unique_source_identifier? ? source_identifier : slug
end

def slug_candidates
reserved_words = %w(all new edit session login logout users admin
stylesheets assets javascripts)
Expand Down
3 changes: 2 additions & 1 deletion api/app/services/packaging/epub_v3/text_section_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ def to_io

private

# @see TextSection#packaging_base_path
def build_base_path
"text/#{text_section.source_identifier}".gsub(/\A(.+)(?<!\.xhtml)\z/, '\1.xhtml')
text_section.packaging_base_path
end

def default_landmark
Expand Down
Loading