Class: Railspress::Admin::ContentElementsController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/railspress/admin/content_elements_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



26
27
28
29
30
31
32
33
34
35
36
# File 'app/controllers/railspress/admin/content_elements_controller.rb', line 26

def create
  @content_element = ContentElement.new(content_element_params)
  @content_element.author_id = current_author&.id if authors_enabled?

  if @content_element.save
    redirect_to admin_content_element_path(@content_element), notice: "Content element '#{@content_element.name}' created."
  else
    @content_groups = ContentGroup.active.order(:name)
    render :new, status: :unprocessable_entity
  end
end

#destroyObject



84
85
86
87
88
89
90
91
92
# File 'app/controllers/railspress/admin/content_elements_controller.rb', line 84

def destroy
  if @content_element.soft_delete
    redirect_to admin_content_elements_path,
      notice: "Content element '#{@content_element.name}' deleted."
  else
    redirect_to admin_content_elements_path,
      alert: "Cannot delete '#{@content_element.name}' — it is a required element. To delete it, first unmark it as required."
  end
end

#editObject



38
39
40
# File 'app/controllers/railspress/admin/content_elements_controller.rb', line 38

def edit
  @content_groups = ContentGroup.active.order(:name)
end

#image_editorObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'app/controllers/railspress/admin/content_elements_controller.rb', line 107

def image_editor
  if params[:compact] == "true"
    render partial: "railspress/admin/shared/image_section_compact",
           locals: {
             record: @content_element,
             attachment_name: :image,
             label: "Element Image",
             editor_url: image_editor_admin_content_element_path(@content_element)
           }
  else
    focal_point = @content_element.image_focal_point
    focal_point.save! if focal_point.new_record?

    render partial: "railspress/admin/shared/image_section_editor",
           locals: {
             record: @content_element,
             attachment_name: :image,
             contexts: Railspress.image_contexts
           }
  end
end

#indexObject



8
9
10
11
12
13
14
# File 'app/controllers/railspress/admin/content_elements_controller.rb', line 8

def index
  scope = ContentElement.active
  scope = scope.where(content_group_id: params[:content_group_id]) if params[:content_group_id].present?
  @content_elements = scope.includes(:content_group)
                           .order(created_at: :desc)
  @content_groups = ContentGroup.active.order(:name)
end

#inlineObject



94
95
96
97
98
99
100
101
102
103
104
105
# File 'app/controllers/railspress/admin/content_elements_controller.rb', line 94

def inline
  if request.headers["Turbo-Frame"].present?
    render partial: "railspress/admin/content_elements/inline_form_frame",
           locals: {
             content_element: @content_element,
             form_frame_id: params[:form_frame_id],
             display_frame_id: params[:display_frame_id]
           }
  else
    redirect_to edit_admin_content_element_path(@content_element)
  end
end

#newObject



20
21
22
23
24
# File 'app/controllers/railspress/admin/content_elements_controller.rb', line 20

def new
  @content_element = ContentElement.new
  @content_groups = ContentGroup.active.order(:name)
  @content_element.content_group_id = params[:content_group_id] if params[:content_group_id].present?
end

#showObject



16
17
18
# File 'app/controllers/railspress/admin/content_elements_controller.rb', line 16

def show
  @versions = @content_element.versions.limit(10)
end

#updateObject



42
43
44
45
46
47
48
49
50
51
52
53
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
82
# File 'app/controllers/railspress/admin/content_elements_controller.rb', line 42

def update
  if @content_element.update(content_element_params)
    Railspress::CmsHelper.clear_cache if defined?(Railspress::CmsHelper)

    if request.headers["Turbo-Frame"].present?
      form_frame_id = params[:form_frame_id].presence || "cms_inline_editor_form_#{@content_element.id}"
      display_frame_id = params[:display_frame_id].presence

      streams = []
      streams << turbo_stream.replace(
        form_frame_id,
        partial: "railspress/admin/content_elements/inline_form_frame",
        locals: { content_element: @content_element, form_frame_id: form_frame_id, display_frame_id: display_frame_id }
      )
      if display_frame_id
        streams << turbo_stream.replace(
          display_frame_id,
          helpers.cms_element_display_frame(@content_element, display_frame_id)
        )
      end
      render turbo_stream: streams
    else
      redirect_to admin_content_element_path(@content_element), notice: "Content element '#{@content_element.name}' updated."
    end
  else
    @content_groups = ContentGroup.active.order(:name)

    if request.headers["Turbo-Frame"].present?
      form_frame_id = params[:form_frame_id].presence || "cms_inline_editor_form_#{@content_element.id}"
      display_frame_id = params[:display_frame_id].presence

      render turbo_stream: turbo_stream.replace(
        form_frame_id,
        partial: "railspress/admin/content_elements/inline_form_frame",
        locals: { content_element: @content_element, form_frame_id: form_frame_id, display_frame_id: display_frame_id }
      ), status: :unprocessable_entity
    else
      render :edit, status: :unprocessable_entity
    end
  end
end