PublishFormLocals = Struct.new(:warning, :notification, :disabled, :document) class PublishFormRenderer class << self def render(*args) new(*args).render end end attr_accessor :slug_unique, :slug_not_unique, :publishable, :not_publishable, :document, :context, :form_locals def initialize(slug_unique:, publishable:, document:, context:) self.slug_unique = slug_unique self.slug_not_unique = !slug_unique self.publishable = publishable self.not_publishable = !publishable self.document = document self.context = context self.form_locals = PublishFormLocals.new(disabled: false) end def render set_errors if current_user_cant_publish(document.document_type) || slug_not_unique || not_publishable set_warnings if publishable context.render(partial: "specialist_documents/publish_form", locals: form_locals.to_h) end def set_errors form_locals.disabled = true form_locals.warning = "You can't publish this document" form_locals.notification = if current_user_cant_publish(document.document_type) "You don't have permission to publish." elsif not_publishable "There's no changes to publish." elsif slug_not_unique "This document as a duplicate slug.
\nYou need to #{context.link_to "edit the document", [:edit, document]} and change the title to be able to be published".html_safe end end def set_warnings form_locals.notification = "This will email subscribers to #{context.current_finder[:title]}." if document_change_note_given? warn_major_change elsif minor_update? notify_minor_change end end def warn_major_change form_locals.warning = "You are about to publish a major edit with a public change note.".html_safe end def notify_minor_change form_locals.notification = "You are about to publish a minor edit.".html_safe end def document_change_note_given? document.change_note.present? end def minor_update? document.minor_update end end