Skip to content

Instantly share code, notes, and snippets.

@robyurkowski
Created December 10, 2014 13:31
Show Gist options
  • Select an option

  • Save robyurkowski/de33d5def627d21fe40f to your computer and use it in GitHub Desktop.

Select an option

Save robyurkowski/de33d5def627d21fe40f to your computer and use it in GitHub Desktop.

Revisions

  1. robyurkowski created this gist Dec 10, 2014.
    3 changes: 3 additions & 0 deletions application_helper.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    def publish_form(slug_unique, publishable, document)
    PublishFormRenderer.render(slug_unique: slug_unique, publishable: publishable, document: document, context: self)
    end
    82 changes: 82 additions & 0 deletions publish_form_renderer.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,82 @@
    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.<br />\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 <strong>major edit</strong> with a public change note.".html_safe
    end


    def notify_minor_change
    form_locals.notification = "You are about to publish a <strong>minor edit</strong>.".html_safe
    end


    def document_change_note_given?
    document.change_note.present?
    end


    def minor_update?
    document.minor_update
    end
    end