Skip to content

Commit

Permalink
Merge pull request #837 from alphagov/rename-specialist-document
Browse files Browse the repository at this point in the history
Rename specialist document
  • Loading branch information
chrislo committed Mar 14, 2017
2 parents fbd927e + b0aa02e commit 9b0ef01
Show file tree
Hide file tree
Showing 32 changed files with 192 additions and 201 deletions.
4 changes: 2 additions & 2 deletions app/lib/manual_document_renderer.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
require "markdown_attachment_processor"
require "specialist_document_header_extractor"
require "section_header_extractor"
require "govspeak_to_html_renderer"
require "footnotes_section_heading_renderer"

class ManualDocumentRenderer
def call(doc)
pipeline = [
MarkdownAttachmentProcessor.method(:new),
SpecialistDocumentHeaderExtractor.create,
SectionHeaderExtractor.create,
GovspeakToHTMLRenderer.create,
FootnotesSectionHeadingRenderer.create,
]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
require "markdown_attachment_processor"
require "specialist_document_header_extractor"
require "section_header_extractor"
require "govspeak_to_html_renderer"

class SpecialistDocumentRenderer
class SectionRenderer
def call(doc)
pipeline = [
MarkdownAttachmentProcessor.method(:new),
SpecialistDocumentHeaderExtractor.create,
SectionHeaderExtractor.create,
GovspeakToHTMLRenderer.create,
]

Expand Down
2 changes: 1 addition & 1 deletion app/models/attachment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Attachment
field :file_id, type: String
field :file_url, type: String

embedded_in :specialist_document_edition
embedded_in :section_edition

before_save :upload_file, if: :file_has_changed?

Expand Down
4 changes: 2 additions & 2 deletions app/models/document_factory_registry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
require "builders/manual_document_builder"
require "manual_with_documents"
require "slug_generator"
require "specialist_document"
require "section"

class DocumentFactoryRegistry
def self.validatable_document_factories
Expand Down Expand Up @@ -38,7 +38,7 @@ def manual_document_factory_factory

ChangeNoteValidator.new(
ManualDocumentValidator.new(
SpecialistDocument.new(
Section.new(
slug_generator,
id,
editions,
Expand Down
4 changes: 2 additions & 2 deletions app/models/specialist_document.rb → app/models/section.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
require "active_model/conversion"
require "active_model/naming"

class SpecialistDocument
class Section
extend Forwardable

def self.edition_attributes
Expand All @@ -25,7 +25,7 @@ def self.edition_attributes

attr_reader :id, :editions, :latest_edition

def initialize(slug_generator, id, editions, edition_factory = SpecialistDocumentEdition.method(:new))
def initialize(slug_generator, id, editions, edition_factory = SectionEdition.method(:new))
@slug_generator = slug_generator
@id = id
@editions = editions
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
require "state_machine"

class SpecialistDocumentEdition
class SectionEdition
include Mongoid::Document
include Mongoid::Timestamps

store_in "specialist_document_editions"

field :document_id, type: String
field :version_number, type: Integer, default: 1
field :title, type: String
Expand Down
9 changes: 4 additions & 5 deletions app/repositories/repository_registry.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
require "specialist_document_repository"
require "section_repository"
require "manual_repository"
require "specialist_document_edition"
require "section_edition"
require "marshallers/document_association_marshaller"
require "marshallers/manual_publish_task_association_marshaller"
require "manual_publish_task"
require "manual_with_publish_tasks"
require "manual"
require "manual_record"
require "specialist_document_edition"
require "specialist_document_repository"


class RepositoryRegistry
def self.create
Expand Down Expand Up @@ -60,7 +59,7 @@ def manual_specific_document_repository_factory
->(manual) {
document_factory = entity_factories.manual_document_factory_factory.call(manual)

SpecialistDocumentRepository.new(
SectionRepository.new(
document_factory: document_factory,
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "fetchable"

class SpecialistDocumentRepository
class SectionRepository
include Fetchable

NotFoundError = Module.new
Expand All @@ -24,8 +24,7 @@ def all(limit = -1, offset = 0)
end

def [](id)
# TODO: add a method on SpecialistDocumentEdition to handle this
editions = specialist_document_editions
editions = section_editions
.where(document_id: id)
.order_by([:version_number, :desc])
.limit(2)
Expand All @@ -49,7 +48,7 @@ def search(query)
def slug_unique?(document)
# TODO: push this method down into persistence layer
if document.draft?
specialist_document_editions.where(
section_editions.where(
:slug => document.slug,
:document_id.ne => document.id,
:state => "published"
Expand All @@ -70,7 +69,7 @@ def store(document)
end

def count
specialist_document_editions.distinct(:document_id).count
section_editions.distinct(:document_id).count
end

private
Expand All @@ -95,7 +94,7 @@ def searchable_attributes

def all_document_ids_scoped(conditions)
only_document_ids_for(
specialist_document_editions
section_editions
.any_of(conditions)
)
end
Expand All @@ -108,15 +107,14 @@ def only_document_ids_for(collection)
.uniq
end

# TODO Add a method on SpecialistDocumentEdition to handle this
def all_document_ids
only_document_ids_for(
specialist_document_editions
section_editions
.all
)
end

def specialist_document_editions
SpecialistDocumentEdition.all
def section_editions
SectionEdition.all
end
end
10 changes: 5 additions & 5 deletions app/repositories/versioned_manual_repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,13 @@ def build_manual_for(manual_record, edition)

def get_latest_version_of_documents(document_ids)
(document_ids || []).map do |document_id|
build_specialist_document(document_id) { |editions| editions }
build_section(document_id) { |editions| editions }
end
end

def build_specialist_document(document_id)
all_editions = SpecialistDocumentEdition.where(document_id: document_id).order_by([:version_number, :desc]).to_a
SpecialistDocument.new(
def build_section(document_id)
all_editions = SectionEdition.where(document_id: document_id).order_by([:version_number, :desc]).to_a
Section.new(
->(_title) { raise RuntimeError, "read only manual" },
document_id,
yield(all_editions).take(2).reverse,
Expand All @@ -104,7 +104,7 @@ def build_specialist_document(document_id)

def get_published_version_of_documents(document_ids)
(document_ids || []).map do |document_id|
build_specialist_document(document_id) { |editions| editions.drop_while { |e| e.state != "published" } }
build_section(document_id) { |editions| editions.drop_while { |e| e.state != "published" } }
end
end
end
2 changes: 1 addition & 1 deletion app/services/abstract_manual_document_service_registry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def remove(context)
private

def document_renderer
SpecialistDocumentRenderer.new
SectionRenderer.new
end

def manual_document_builder
Expand Down
6 changes: 3 additions & 3 deletions bin/delete_documents_marked_for_delete
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def in_publishing_api?(content_id)
end

def marked_editions
SpecialistDocumentEdition.where(title: /\Axx/i)
SectionEdition.where(title: /\Axx/i)
end

def fetch_duplicated_editions
Expand Down Expand Up @@ -44,14 +44,14 @@ known_editions, unknown_editions = duplicated_editions.partition { |edition| in_
puts "The following #{unknown_editions.count} are unknown to Publishing API and are safe to delete:"
unknown_editions.each do |edition|
puts [edition[:slug], edition[:content_id], edition[:state], edition[:created_at]].join(",")
SpecialistDocumentEdition.where(document_id: edition[:content_id]).delete_all if ENV["DO_IT"].present?
SectionEdition.where(document_id: edition[:content_id]).delete_all if ENV["DO_IT"].present?
end

puts "The following #{known_editions.count} are known to Publishing API and will be deleted after the draft is discarded:"
known_editions.each do |edition|
puts [edition[:slug], edition[:content_id], edition[:state], edition[:created_at]].join(",")
if ENV["DO_IT"].present?
publishing_api.discard_draft(edition[:content_id])
SpecialistDocumentEdition.where(document_id: edition[:content_id]).delete_all
SectionEdition.where(document_id: edition[:content_id]).delete_all
end
end
2 changes: 1 addition & 1 deletion bin/find_duplicate_documents
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require File.expand_path("../../config/environment", __FILE__)

slug_hash = {}
SpecialistDocumentEdition.all.each do |edition|
SectionEdition.all.each do |edition|
slug_hash[edition.slug] ||= {}
slug_hash[edition.slug][edition.document_id] ||= { state: edition.state, created_at: edition.created_at, editions: 0 }
slug_hash[edition.slug][edition.document_id][:editions] += 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def self.down
end

def self.funds
SpecialistDocumentEdition.where(
SectionEdition.where(
:document_type => "international_development_fund",
# Some imported records don't have any extra_fields yet
:extra_fields.ne => {},
Expand Down
2 changes: 1 addition & 1 deletion lib/attachment_reporting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def create_organisation_attachment_count_hash
# the editions of these documents in version order to find unique PDF attachments and their
# publication times.
all_unique_document_ids_for_manual(manual).each do |document_id|
document_editions = SpecialistDocumentEdition.where(document_id: document_id).order(:version_number)
document_editions = SectionEdition.where(document_id: document_id).order(:version_number)

document_editions.each do |document_edition|
next if document_edition_never_published?(document_edition)
Expand Down
2 changes: 1 addition & 1 deletion lib/cli_manual_deleter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def complete_removal(manual_record)
discard_draft_from_publishing_api(manual_record.manual_id)

document_ids.each do |id|
SpecialistDocumentEdition.where(document_id: id).map(&:destroy)
SectionEdition.where(document_id: id).map(&:destroy)
end

manual_record.destroy
Expand Down
4 changes: 2 additions & 2 deletions lib/duplicate_draft_deleter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class DuplicateDraftDeleter
def call
duplicated_editions_not_in_publishing_api = duplicated_editions.reject { |data| in_publishing_api?(data[:content_id]) }
content_ids = duplicated_editions_not_in_publishing_api.map { |data| data[:content_id] }
editions_to_delete = SpecialistDocumentEdition.where(:document_id.in => content_ids)
editions_to_delete = SectionEdition.where(:document_id.in => content_ids)

puts "The following #{editions_to_delete.count} editions are unknown to Publishing API and will be deleted:"
editions_to_delete.each do |edition|
Expand Down Expand Up @@ -32,7 +32,7 @@ def in_publishing_api?(content_id)

def duplicated_editions
slug_hash = {}
SpecialistDocumentEdition.all.each do |edition|
SectionEdition.all.each do |edition|
slug_hash[edition.slug] ||= {}
slug_hash[edition.slug][edition.document_id] ||= { state: edition.state, created_at: edition.created_at, editions: 0, content_id: edition.document_id, slug: edition.slug }
slug_hash[edition.slug][edition.document_id][:editions] += 1
Expand Down
4 changes: 2 additions & 2 deletions lib/manual_publication_log_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def build_logs_for_all_other_suitable_document_editions
end

def document_editions_for_first_manual_edition
@document_editions_for_first_manual_edition ||= SpecialistDocumentEdition.where(:document_id.in => first_manual_edition.document_ids, :minor_update.nin => [true], version_number: 1).any_of({ state: "published" }, state: "archived")
@document_editions_for_first_manual_edition ||= SectionEdition.where(:document_id.in => first_manual_edition.document_ids, :minor_update.nin => [true], version_number: 1).any_of({ state: "published" }, state: "archived")
end

def first_manual_edition
Expand All @@ -79,6 +79,6 @@ def first_manual_edition
def document_editions_for_rebuild
ids_to_ignore = document_editions_for_first_manual_edition.map(&:_id)

SpecialistDocumentEdition.with_slug_prefix(@manual_slug).where(:minor_update.nin => [true], :_id.nin => ids_to_ignore).any_of({ state: "published" }, state: "archived")
SectionEdition.with_slug_prefix(@manual_slug).where(:minor_update.nin => [true], :_id.nin => ids_to_ignore).any_of({ state: "published" }, state: "archived")
end
end
2 changes: 1 addition & 1 deletion lib/manual_relocator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def most_recent_edition_of_section(document_id)
end

def all_editions_of_section(document_id)
SpecialistDocumentEdition.where(document_id: document_id).order_by([:version_number, :desc])
SectionEdition.where(document_id: document_id).order_by([:version_number, :desc])
end

def reslug
Expand Down
2 changes: 1 addition & 1 deletion lib/manual_section_reslugger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def current_section_in_content_store
end

def section_in_database(slug)
SpecialistDocumentEdition.where(slug: slug).last
SectionEdition.where(slug: slug).last
end

def section_in_content_store(slug)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
require "delegate"
require "active_support/core_ext/hash"

class SpecialistDocumentHeaderExtractor < SimpleDelegator
class SectionHeaderExtractor < SimpleDelegator
def self.create
->(doc) {
SpecialistDocumentHeaderExtractor.new(
SectionHeaderExtractor.new(
GovspeakHeaderExtractor.new,
doc,
)
Expand Down
13 changes: 3 additions & 10 deletions spec/factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,14 @@
organisation_slug "government-digital-service"
end

factory :specialist_document_edition do
factory :section_edition do
document_id { BSON::ObjectId.new }
sequence(:slug) { |n| "test-specialist-document-#{n}" }
sequence(:title) { |n| "Test Specialist Document #{n}" }
sequence(:slug) { |n| "test-section-edition-#{n}" }
sequence(:title) { |n| "Test Section Edition #{n}" }
summary "My summary"
body "My body"
end

factory :specialist_document do
slug_generator { "some" }
id { "some" }
editions { "s" }
initialize_with { new(slug_generator, id, editions) }
end

factory :publication_log do
sequence(:slug) { |n| "test-publication-log-#{n}" }
sequence(:title) { |n| "Test Publication Log #{n}" }
Expand Down
Loading

0 comments on commit 9b0ef01

Please sign in to comment.