Class: Plum::Cp::EntriesController

Inherits:
BaseController show all
Defined in:
app/controllers/plum/cp/entries_controller.rb

Constant Summary collapse

ALLOWED_RICH_TEXT_TAGS =
%w[
  a action-text-attachment blockquote br code em figcaption figure
  h1 h2 h3 hr img li ol p pre s strong table tbody td th thead tr ul
].freeze
ALLOWED_RICH_TEXT_ATTRIBUTES =
%w[
  alt caption content-type filename filesize height href presentation
  rel sgid src target title url width
].freeze

Instance Method Summary collapse

Instance Method Details

#createObject



26
27
28
29
30
31
32
33
34
35
36
# File 'app/controllers/plum/cp/entries_controller.rb', line 26

def create
  @entry = @content_type.entries.build(entry_params)
  @entry.site = current_site
  @entry.author = current_user if current_user.is_a?(Plum::User)

  if save_entry(@entry)
    redirect_to edit_cp_content_type_entry_path(@content_type, @entry), notice: "Entry created"
  else
    render :new, status: :unprocessable_entity
  end
end

#destroyObject



131
132
133
134
# File 'app/controllers/plum/cp/entries_controller.rb', line 131

def destroy
  @entry.destroy
  redirect_to cp_content_type_entries_path(@content_type), notice: "Entry deleted"
end

#diffObject

Git-style review of draft vs. live content.



104
105
106
107
108
109
110
111
# File 'app/controllers/plum/cp/entries_controller.rb', line 104

def diff
  unless @entry.has_draft?
    return redirect_to edit_cp_content_type_entry_path(@content_type, @entry),
                       notice: "No draft changes to review"
  end

  @diff = Plum::DraftDiff.new(@entry)
end

#discard_draftObject



125
126
127
128
129
# File 'app/controllers/plum/cp/entries_controller.rb', line 125

def discard_draft
  @entry.discard_draft!
  redirect_back fallback_location: edit_cp_content_type_entry_path(@content_type, @entry),
                notice: "Draft discarded"
end

#editObject



38
39
# File 'app/controllers/plum/cp/entries_controller.rb', line 38

def edit
end

#image_fieldObject

Persists one image association without submitting (and potentially overwriting) the other edited fields on the page.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/controllers/plum/cp/entries_controller.rb', line 85

def image_field
  handle = params[:field_handle].to_s
  field = image_fields.find { |candidate| candidate["handle"].to_s == handle }
  return render json: { error: "Unknown image field" }, status: :unprocessable_entity unless field

  asset_id = params[:asset_id].presence
  if asset_id && !current_site.assets.exists?(id: asset_id)
    return render json: { error: "Image is not available" }, status: :unprocessable_entity
  end

  data = @entry.data.to_h.merge(handle => asset_id&.to_i)
  if @entry.update(data: data)
    render json: { saved: true, asset_id: data[handle] }
  else
    render json: { error: @entry.errors.full_messages.to_sentence }, status: :unprocessable_entity
  end
end

#indexObject



18
19
20
# File 'app/controllers/plum/cp/entries_controller.rb', line 18

def index
  @entries = @content_type.entries.order(updated_at: :desc)
end

#newObject



22
23
24
# File 'app/controllers/plum/cp/entries_controller.rb', line 22

def new
  @entry = @content_type.entries.build(status: :draft, site: current_site, locale: current_site.default_locale)
end

#publish_draftObject



113
114
115
116
117
118
119
120
121
122
123
# File 'app/controllers/plum/cp/entries_controller.rb', line 113

def publish_draft
  published = @entry.publish_draft!(editor: current_user)

  respond_to do |format|
    format.html do
      redirect_back fallback_location: edit_cp_content_type_entry_path(@content_type, @entry),
                    notice: published ? "Draft published" : "No draft changes to publish"
    end
    format.json { render json: { published: published, published_at: Time.current.iso8601 } }
  end
end

#translateObject



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'app/controllers/plum/cp/entries_controller.rb', line 136

def translate
  locale = params[:locale].to_s
  unless current_site.locales.include?(locale)
    return redirect_to edit_cp_content_type_entry_path(@content_type, @entry), alert: "Locale is not configured"
  end

  source = @entry.origin || @entry
  translation = source.translations.find_or_initialize_by(locale: locale)
  if translation.new_record?
    translation.assign_attributes(
      site: current_site,
      content_type: @content_type,
      author: current_user.is_a?(Plum::User) ? current_user : nil,
      title: @entry.title,
      slug: @entry.slug,
      status: :draft,
      data: @entry.data.to_h.deep_dup
    )
    translation.save!
  end
  redirect_to edit_cp_content_type_entry_path(@content_type, translation), notice: "#{locale} translation ready"
end

#updateObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/controllers/plum/cp/entries_controller.rb', line 54

def update
  # Writing-mode saves on a live entry become a working draft so the
  # published version (and its cached pages) stay untouched.
  return save_write_mode_draft if write_mode? && @entry.published?

  @entry.assign_attributes(entry_params)
  # The full form displays draft values when a draft exists, so a form
  # save publishes what the editor saw — the draft is no longer pending.
  @entry.draft_data = nil unless write_mode?

  if save_entry(@entry)
    respond_to do |format|
      format.html do
        if write_mode?
          redirect_to write_cp_content_type_entry_path(@content_type, @entry), notice: "Saved"
        else
          redirect_to edit_cp_content_type_entry_path(@content_type, @entry), notice: "Entry updated"
        end
      end
      format.json { render json: { saved: true, saved_at: Time.current.iso8601 } }
    end
  else
    respond_to do |format|
      format.html { render :edit, status: :unprocessable_entity }
      format.json { render json: { saved: false, errors: @entry.errors.full_messages }, status: :unprocessable_entity }
    end
  end
end

#writeObject

Distraction-free writing surface for the entry's first rich text field. Saves go through #update with write_mode, which merges the submitted fields into the existing data instead of replacing it.



44
45
46
47
48
49
50
51
52
# File 'app/controllers/plum/cp/entries_controller.rb', line 44

def write
  @field = @content_type.fields.find { |field| field["type"] == "rich_text" && field["handle"].present? }
  unless @field
    return redirect_to edit_cp_content_type_entry_path(@content_type, @entry),
                       alert: "This content type has no rich text field to write in"
  end

  render layout: "plum/write"
end