Class: RivetCms::ContentTypesController

Inherits:
ApplicationController show all
Includes:
InertiaProps
Defined in:
app/controllers/rivet_cms/content_types_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



51
52
53
54
55
56
57
58
59
60
# File 'app/controllers/rivet_cms/content_types_controller.rb', line 51

def create
  content_type = ContentType.new(content_type_params)

  if content_type.save
    audit "content_type.created", content_type
    redirect_to content_type_path(content_type), notice: "Content type created successfully"
  else
    redirect_to new_content_type_path, inertia: { errors: content_type.errors }
  end
end

#destroyObject



121
122
123
124
125
126
127
# File 'app/controllers/rivet_cms/content_types_controller.rb', line 121

def destroy
  @content_type.discard!
  audit "content_type.removed", @content_type
  # The type's own page dies with the removal; anywhere else goes back
  redirect_after_trash(content_type_path(@content_type), content_types_path,
                       notice: "#{@content_type.name} was removed. Its entries are kept and it can be restored from the trash.")
end

#indexObject



21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/controllers/rivet_cms/content_types_controller.rb', line 21

def index
  # Entry counts are content data, not schema; omit them without content read
  entry_counts = can?(:read, :content) ? Document.where(organization: Current.organization).in_visible_types.group(:content_type_id).count : nil

  render inertia: "ContentTypes/Index", props: {
    removed_count: ContentType.with_discarded.discarded.where(organization: Current.organization).count,
    content_types: permitted(Current.organization.content_types, :read, :schema).map { |ct|
      props = content_type_props(ct)
      entry_counts ? props.merge(documents_count: entry_counts.fetch(ct.id, 0)) : props
    }
  }
end

#newObject



47
48
49
# File 'app/controllers/rivet_cms/content_types_controller.rb', line 47

def new
  render inertia: "ContentTypes/New"
end

#purgeObject

The one place content is really destroyed. Only reachable from the trash, and only when the typed name matches, so it cannot be a slip.



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'app/controllers/rivet_cms/content_types_controller.rb', line 95

def purge
  unless params[:confirm].to_s.strip == @content_type.name.to_s.strip
    return redirect_back fallback_location: trash_content_types_path, allow_other_host: false,
                         status: :see_other, alert: "Type the name exactly to permanently delete it"
  end

  name = @content_type.name
  ContentType.transaction do
    # with_discarded: trashed entries still hold the FK and must go too
    documents = Document.with_discarded.where(content_type_id: @content_type.id)
    # Incoming links hold a foreign key, so they go before their targets
    Relation.where(target_document_id: documents.select(:id)).delete_all
    documents.find_each(&:destroy!)
    @content_type.destroy!
  end

  audit "content_type.purged", @content_type
  # Back to whichever trash (global or scoped) the purge came from
  redirect_back fallback_location: trash_content_types_path, allow_other_host: false,
                status: :see_other, notice: "#{name} and its entries were permanently deleted"
rescue ActiveRecord::ActiveRecordError => error
  Rails.logger&.error("[RivetCms] purge failed for content type #{@content_type.id}: #{error.class}")
  redirect_back fallback_location: trash_content_types_path, allow_other_host: false,
                status: :see_other, alert: "#{name} could not be deleted; nothing was removed"
end

#restoreObject



87
88
89
90
91
# File 'app/controllers/rivet_cms/content_types_controller.rb', line 87

def restore
  @content_type.undiscard!
  audit "content_type.restored", @content_type
  redirect_to content_type_path(@content_type), notice: "#{@content_type.name} was restored with its entries"
end

#showObject



34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/controllers/rivet_cms/content_types_controller.rb', line 34

def show
  fields = @content_type.fields.kept.ordered

  render inertia: "ContentTypes/Show", props: {
    content_type: content_type_props(@content_type),
    fields: fields.map { |f| field_props(f) },
    field_types: Field::FIELD_TYPE_LABELS,
    # Selectable targets for "reference" and "component" field options
    reference_targets: permitted(Current.organization.content_types.where.not(id: @content_type.id).order(:name), :read, :schema).map { |ct| { id: ct.id, name: ct.name } },
    embeddable_components: permitted(Current.organization.components.order(:name), :read, :schema).map { |c| { id: c.id, name: c.name } }
  }
end

#trashObject

Removed types are kept, so they need somewhere to be seen and restored



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'app/controllers/rivet_cms/content_types_controller.rb', line 72

def trash
  removed = permitted(ContentType.with_discarded.discarded.where(organization: Current.organization).order(:deleted_at), :read, :schema)
  entry_counts = can?(:read, :content) ? Document.with_discarded.where(content_type_id: removed.map(&:id)).group(:content_type_id).count : nil

  render inertia: "ContentTypes/Trash", props: {
    content_types: removed.map { |content_type|
      props = content_type_props(content_type).merge(
        removed_at: content_type.deleted_at.iso8601,
        paths: { restore: restore_content_type_path(content_type), purge: purge_content_type_path(content_type) }
      )
      entry_counts ? props.merge(documents_count: entry_counts.fetch(content_type.id, 0)) : props
    }
  }
end

#updateObject



62
63
64
65
66
67
68
69
# File 'app/controllers/rivet_cms/content_types_controller.rb', line 62

def update
  if @content_type.update(content_type_params)
    audit "content_type.updated", @content_type
    redirect_to content_type_path(@content_type), notice: "Content type updated successfully"
  else
    redirect_to content_type_path(@content_type), inertia: { errors: @content_type.errors }
  end
end