Created
December 10, 2014 13:31
-
-
Save robyurkowski/de33d5def627d21fe40f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def publish_form(slug_unique, publishable, document) | |
PublishFormRenderer.render(slug_unique: slug_unique, publishable: publishable, document: document, context: self) | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment