Class: Alchemy::Admin::PagesController

Inherits:
ResourcesController show all
Includes:
Clipboard, CurrentLanguage, OnPageLayout::CallbacksRunner
Defined in:
app/controllers/alchemy/admin/pages_controller.rb

Constant Summary

Constants included from ResourceFilter

ResourceFilter::COMMON_SEARCH_FILTER_EXCLUDES

Instance Method Summary collapse

Methods included from Clipboard

#clipboard_empty?

Methods included from ResourceName

#controller_path_array, #resource_array, #resource_model_name, #resource_name, #resources_name

Methods included from ResourcesHelper

#contains_relations?, #edit_resource_path, #new_resource_path, #render_attribute, #resource_attribute_field_options, #resource_has_tags, #resource_instance_variable, #resource_model, #resource_path, #resource_relations_names, #resource_scope, #resource_url_proxy, #resource_window_size, #resources_instance_variable, #resources_path

Methods inherited from BaseController

#leave

Methods included from Modules

included, #module_definition_for, register_module

Methods included from Alchemy::AbilityHelper

#current_ability

Methods included from ConfigurationMethods

#configuration, #multi_language?, #multi_site?, #prefix_locale?

Instance Method Details

#configureObject

Set page configuration like page names, meta tags and states.



130
131
# File 'app/controllers/alchemy/admin/pages_controller.rb', line 130

def configure
end

#copy_language_treeObject



218
219
220
221
222
# File 'app/controllers/alchemy/admin/pages_controller.rb', line 218

def copy_language_tree
  language_root_to_copy_from.copy_children_to(copy_of_language_root)
  flash[:notice] = Alchemy.t(:language_pages_copied)
  redirect_to admin_pages_path
end

#createObject



95
96
97
98
99
100
101
102
103
104
# File 'app/controllers/alchemy/admin/pages_controller.rb', line 95

def create
  @page = paste_from_clipboard || Page.new(page_params)
  if @page.save
    flash[:notice] = Alchemy.t("Page created", name: @page.name)
    do_redirect_to(redirect_path_after_create_page)
  else
    new
    render :new
  end
end

#destroyObject

Fetches page via before filter, destroys it and redirects to page tree



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'app/controllers/alchemy/admin/pages_controller.rb', line 152

def destroy
  if @page.destroy
    flash[:notice] = Alchemy.t("Page deleted", name: @page.name)

    # Remove page from clipboard
    remove_resource_from_clipboard(@page)
  else
    flash[:warning] = @page.errors.full_messages.to_sentence
  end

  if @page.layoutpage?
    redirect_to alchemy.admin_layoutpages_path
  else
    redirect_to alchemy.admin_pages_path
  end
end

#editObject

Edit the content of the page and all its elements and ingredients.

Locks the page to current user to prevent other users from editing it meanwhile.



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

def edit
  # fetching page via before filter
  if page_is_locked?
    flash[:warning] = Alchemy.t("This page is locked", name: @page.locker_name)
    redirect_to admin_pages_path
  elsif page_needs_lock?
    @page.lock_to!(current_alchemy_user)
  end
  @preview_urls = Alchemy.config.preview_sources.map do |klass|
    [
      klass.model_name.human,
      klass.new(routes: Alchemy::Engine.routes).url_for(@page)
    ]
  end
  @preview_url = @preview_urls.first.last
  @layoutpage = @page.layoutpage?
  Alchemy::Current.language = @page.language
end

#flushObject



224
225
226
227
228
229
230
231
# File 'app/controllers/alchemy/admin/pages_controller.rb', line 224

def flush
  @current_language.pages.flushables.update_all(published_at: Time.current)
  # We need to ensure, that also all layoutpages get the +published_at+ timestamp set,
  # but not set to public true, because the cache_key for an element is +published_at+
  # and we don't want the layout pages to be present in +Page.published+ scope.
  @current_language.pages.flushable_layoutpages.update_all(published_at: Time.current)
  respond_to { |format| format.turbo_stream }
end

#foldObject



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'app/controllers/alchemy/admin/pages_controller.rb', line 173

def fold
  # @page is fetched via before filter
  was_folded = @page.folded?(current_alchemy_user.id)
  @page.fold!(current_alchemy_user.id, !was_folded)

  respond_to do |format|
    format.turbo_stream do
      if was_folded
        @page = PageTreePreloader.new(
          page: @page,
          user: current_alchemy_user,
          admin_includes: true
        ).call
      else
        head 200
      end
    end
  end
end

#indexObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/controllers/alchemy/admin/pages_controller.rb', line 49

def index
  @query = @current_language.pages.contentpages.ransack(search_filter_params[:q])

  if @view == "list"
    @query.sorts = default_sort_order if @query.sorts.empty?
    items = @query.result

    if search_filter_params[:tagged_with].present?
      items = items.tagged_with(search_filter_params[:tagged_with])
    end

    items = items.page(params[:page] || 1).per(items_per_page)
    @pages = items
  elsif @current_language.root_page
    @root_page = @current_language.root_page
  end
end

#infoObject



86
87
88
# File 'app/controllers/alchemy/admin/pages_controller.rb', line 86

def info
  render layout: !request.xhr?
end


169
170
171
# File 'app/controllers/alchemy/admin/pages_controller.rb', line 169

def link
  render LinkDialog::Tabs.new(**link_dialog_params)
end

#newObject



90
91
92
93
# File 'app/controllers/alchemy/admin/pages_controller.rb', line 90

def new
  @page ||= Page.new(layoutpage: params[:layoutpage] == "true", parent_id: params[:parent_id])
  @page_layouts = Page.layouts_for_select(@current_language.id, layoutpages: @page.layoutpage?)
end

#publishObject

Sets the page public and updates the published_at attribute that is used as cache_key



213
214
215
216
# File 'app/controllers/alchemy/admin/pages_controller.rb', line 213

def publish
  # fetching page via before filter
  @page.publish!
end

#showObject

Used by page preview iframe in Page#edit view.



77
78
79
80
81
82
83
84
# File 'app/controllers/alchemy/admin/pages_controller.rb', line 77

def show
  authorize! :edit_content, @page

  Current.preview_page = @page
  # Setting the locale to pages language, so the page content has it's correct translations.
  ::I18n.locale = @page.language.locale
  render(layout: Alchemy.config.admin_page_preview_layout || "application")
end

#treeObject



67
68
69
70
71
72
73
# File 'app/controllers/alchemy/admin/pages_controller.rb', line 67

def tree
  @root_page = Alchemy::PageTreePreloader.new(
    page: @current_language.root_page,
    user: current_alchemy_user,
    admin_includes: true
  ).call
end

#unlockObject

Leaves the page editing mode and unlocks the page for other users



194
195
196
197
198
199
200
201
202
203
204
205
# File 'app/controllers/alchemy/admin/pages_controller.rb', line 194

def unlock
  # fetching page via before filter
  @page.unlock!
  flash[:notice] = Alchemy.t(:unlocked_page, name: @page.name)
  @pages_locked_by_user = Page.from_current_site.locked_by(current_alchemy_user)
  respond_to do |format|
    format.js
    format.html do
      redirect_to(unlock_redirect_path, allow_other_host: true)
    end
  end
end

#unlock_redirect_pathObject



207
208
209
# File 'app/controllers/alchemy/admin/pages_controller.rb', line 207

def unlock_redirect_path
  safe_redirect_path(fallback: admin_pages_path)
end

#updateObject

Updates page

  • fetches page via before filter



137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'app/controllers/alchemy/admin/pages_controller.rb', line 137

def update
  @old_parent_id = @page.parent_id
  if @page.update(page_params)
    @notice = Alchemy.t("Page saved", name: @page.name)
    @while_page_edit = request.referer.include?("edit")

    if @view == "list"
      flash[:notice] = @notice
    end
  else
    render :configure, status: 422
  end
end