Class: RivetCms::DocumentsController
- Inherits:
-
ApplicationController
- Object
- ApplicationController
- RivetCms::DocumentsController
- Includes:
- InertiaProps
- Defined in:
- app/controllers/rivet_cms/documents_controller.rb
Instance Method Summary collapse
- #create ⇒ Object
- #destroy ⇒ Object
- #edit ⇒ Object
- #index ⇒ Object
- #new ⇒ Object
-
#publish ⇒ Object
Publishes what's on screen: the submitted values are written to the draft first, so unsaved edits are validated instead of the stale draft.
-
#purge ⇒ Object
Permanently destroys one entry and its revisions.
- #restore ⇒ Object
- #trash ⇒ Object
- #update ⇒ Object
Instance Method Details
#create ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'app/controllers/rivet_cms/documents_controller.rb', line 53 def create document = @content_type.documents.new(slug: params[:slug]) document.save! draft = document.revisions.create!(state: :draft, **) document.update!(draft_revision: draft) writer = DraftWriter.new(draft).tap { |w| w.write(values_param) } audit "entry.created", document redirect_to edit_content_type_document_path(@content_type, document), notice: write_notice(writer, "Draft saved") rescue ActiveRecord::RecordInvalid => e redirect_to new_content_type_document_path(@content_type), inertia: { errors: e.record.errors } end |
#destroy ⇒ Object
95 96 97 98 99 100 101 102 103 |
# File 'app/controllers/rivet_cms/documents_controller.rb', line 95 def destroy @document.discard! audit "entry.trashed", @document # Back to whichever list the delete came from, except the entry's own # editor, whose URL just died with the trashing dead = edit_content_type_document_path(@content_type, @document) redirect_after_trash(dead, content_type_documents_path(@content_type), notice: "#{@document.slug} was moved to the trash") end |
#edit ⇒ Object
66 67 68 |
# File 'app/controllers/rivet_cms/documents_controller.rb', line 66 def edit render inertia: "Documents/Edit", props: editor_props(@document) end |
#index ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'app/controllers/rivet_cms/documents_controller.rb', line 31 def index documents = @content_type.documents.recent documents = documents.search(params[:q]) if params[:q].present? page = documents.includes(:draft_revision).page(params[:page]).per(25) visible = permitted(page, :read, :content) titles = document_titles(visible) render inertia: "Documents/Index", props: { content_type: content_type_props(@content_type), q: params[:q].presence, trashed_count: @content_type.documents.with_discarded.discarded.count, trash_path: trash_content_type_documents_path(@content_type), documents: visible.map { |document| document_list_props(document, titles) }, pagination: { page: page.current_page, total_pages: page.total_pages } } end |
#new ⇒ Object
49 50 51 |
# File 'app/controllers/rivet_cms/documents_controller.rb', line 49 def new render inertia: "Documents/Edit", props: editor_props(nil) end |
#publish ⇒ Object
Publishes what's on screen: the submitted values are written to the draft first, so unsaved edits are validated instead of the stale draft.
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'app/controllers/rivet_cms/documents_controller.rb', line 79 def publish draft = @document.draft_revision writer = nil if params.key?(:values) draft.update!() writer = DraftWriter.new(draft).tap { |w| w.write(values_param) } end # The snapshot records who published, which is not always the last editor draft.publish!(publisher: [:author], publisher_name: [:author_name]) audit "entry.published", @document redirect_to edit_content_type_document_path(@content_type, @document), notice: write_notice(writer, "Published") rescue ContentInvalidError => e redirect_to edit_content_type_document_path(@content_type, @document), inertia: { errors: e.errors.group_by(&:field_key).transform_values { |group| group.map(&:message) } } end |
#purge ⇒ Object
Permanently destroys one entry and its revisions. Smaller blast radius than purging a type, so a plain confirmation rather than a typed name.
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'app/controllers/rivet_cms/documents_controller.rb', line 132 def purge slug = @document.slug Document.transaction do Relation.where(target_document_id: @document.id).delete_all @document.destroy! end audit "entry.purged", @document # Back to whichever trash (global or scoped) the purge came from redirect_back fallback_location: trash_content_type_documents_path(@content_type), allow_other_host: false, status: :see_other, notice: "#{slug} was permanently deleted" rescue ActiveRecord::ActiveRecordError => error Rails.logger&.error("[RivetCms] purge failed for document #{@document.id}: #{error.class}") redirect_back fallback_location: trash_content_type_documents_path(@content_type), allow_other_host: false, status: :see_other, alert: "#{slug} could not be deleted; nothing was removed" end |
#restore ⇒ Object
123 124 125 126 127 128 |
# File 'app/controllers/rivet_cms/documents_controller.rb', line 123 def restore @document.undiscard! audit "entry.restored", @document redirect_to edit_content_type_document_path(@content_type, @document), notice: "#{@document.slug} was restored" end |
#trash ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'app/controllers/rivet_cms/documents_controller.rb', line 105 def trash documents = permitted(@content_type.documents.with_discarded.discarded.order(:deleted_at), :read, :content) titles = document_titles(documents) render inertia: "Documents/Trash", props: { content_type: content_type_props(@content_type), documents: documents.map { |document| document_list_props(document, titles).merge( trashed_at: document.deleted_at.iso8601, paths: { restore: restore_content_type_document_path(@content_type, document), purge: purge_content_type_document_path(@content_type, document) } ) } } end |
#update ⇒ Object
70 71 72 73 74 75 |
# File 'app/controllers/rivet_cms/documents_controller.rb', line 70 def update @document.draft_revision.update!() writer = DraftWriter.new(@document.draft_revision).tap { |w| w.write(values_param) } audit "entry.updated", @document redirect_to edit_content_type_document_path(@content_type, @document), notice: write_notice(writer, "Draft saved") end |