Skip to content

Commit

Permalink
Allow parent page change
Browse files Browse the repository at this point in the history
Backport pull request #2246 from tvdeyen/allow-parent-page-change
  • Loading branch information
tvdeyen committed Mar 9, 2022
1 parent 0367d09 commit 63613fe
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 18 deletions.
21 changes: 13 additions & 8 deletions app/assets/javascripts/alchemy/page_select.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
$.fn.alchemyPageSelect = function(options) {
$.fn.alchemyPageSelect = function (options) {
var pageTemplate = HandlebarsTemplates.page

return this.select2({
placeholder: options.placeholder,
allowClear: true,
allowClear: options.hasOwnProperty("allowClear")
? options.allowClear
: true,
minimumInputLength: 3,
initSelection: function (_$el, callback) {
if (options.initialSelection) {
Expand All @@ -12,13 +14,16 @@ $.fn.alchemyPageSelect = function(options) {
},
ajax: {
url: options.url,
datatype: 'json',
datatype: "json",
quietMillis: 300,
data: function (term, page) {
return {
q: $.extend({
name_cont: term
}, options.query_params),
q: $.extend(
{
name_cont: term
},
options.query_params
),
page: page
}
},
Expand All @@ -34,8 +39,8 @@ $.fn.alchemyPageSelect = function(options) {
formatSelection: function (page) {
return page.text || page.name
},
formatResult: function(page) {
return pageTemplate({page: page})
formatResult: function (page) {
return pageTemplate({ page: page })
}
})
}
1 change: 1 addition & 0 deletions app/controllers/alchemy/admin/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ def configure
def update
# stores old page_layout value, because unfurtunally rails @page.changes does not work here.
@old_page_layout = @page.page_layout
@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")
Expand Down
19 changes: 19 additions & 0 deletions app/views/alchemy/admin/pages/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
<%= alchemy_form_for [:admin, @page], class: 'edit_page' do |f| %>
<% unless @page.language_root? || @page.layoutpage %>
<%= f.input :parent_id, required: true, input_html: { class: 'alchemy_selectbox' } %>
<% end %>
<%= f.input :page_layout,
collection: @page_layouts,
label: page_layout_label(@page),
Expand Down Expand Up @@ -43,3 +47,18 @@

<%= f.submit Alchemy.t(:save) %>
<% end %>

<script>
$('#page_parent_id').alchemyPageSelect({
placeholder: "<%= Alchemy.t(:search_page) %>",
url: "<%= alchemy.api_pages_path %>",
allowClear: false,
<% if @page.parent %>
initialSelection: {
id: <%= @page.parent.id %>,
text: "<%= @page.parent.name %>",
url_path: "<%= @page.parent.url_path %>"
}
<% end %>
})
</script>
21 changes: 16 additions & 5 deletions app/views/alchemy/admin/pages/_new_page_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@
<%= f.hidden_field(:parent_id) %>
<% else %>
<% @page.parent = @current_language.root_page %>
<%= f.input :parent_id,
collection: @current_language.pages.contentpages,
label_method: :name,
value_method: :id,
input_html: { class: "alchemy_selectbox" } %>
<%= f.input :parent_id, as: :string, input_html: { class: 'alchemy_selectbox' } %>
<% end %>
<%= f.hidden_field(:language_id) %>
<%= f.hidden_field(:layoutpage) %>
Expand All @@ -21,3 +17,18 @@
<%= f.input :name %>
<%= f.submit Alchemy.t(:create) %>
<% end %>

<script>
$('input[type="text"]#page_parent_id').alchemyPageSelect({
placeholder: "<%= Alchemy.t(:search_page) %>",
url: "<%= alchemy.api_pages_path %>",
allowClear: false,
<% if @page.parent %>
initialSelection: {
id: <%= @page.parent.id %>,
text: "<%= @page.parent.name %>",
url_path: "<%= @page.parent.url_path %>"
}
<% end %>
})
</script>
7 changes: 7 additions & 0 deletions app/views/alchemy/admin/pages/update.js.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@
Alchemy.growl("<%= j @notice %>");
Alchemy.closeCurrentDialog();

<% elsif @page.parent_id != @old_parent_id -%>

Alchemy.closeCurrentDialog(function() {
Alchemy.growl("<%= j @notice %>");
Alchemy.currentSitemap.load(<%= @page.get_language_root.id %>);
});

<% else -%>

if (page) {
Expand Down
5 changes: 3 additions & 2 deletions spec/features/admin/page_creation_feature_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,21 @@
RSpec.describe "Page creation", type: :system do
before { authorize_user(:as_admin) }

describe "parent selection" do
describe "parent selection", :js do
let!(:homepage) { create(:alchemy_page, :language_root) }

context "without having a parent id in the params" do
it "contains a parent select" do
visit new_admin_page_path
expect(page).to have_select("Parent", selected: homepage.name)
expect(page).to have_css("#s2id_page_parent_id")
end
end

context "with having a parent id in the params" do
it "contains a hidden parent_id field" do
visit new_admin_page_path(parent_id: homepage)
expect(page).to have_field("page_parent_id", type: "hidden")
expect(page).to_not have_css("#s2id_page_parent_id")
end
end
end
Expand Down
23 changes: 20 additions & 3 deletions spec/features/admin/page_editing_feature_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,14 @@ class FooPreviewSource < Alchemy::Admin::PreviewUrl; end
before { authorize_user(:as_admin) }
let!(:a_page) { create(:alchemy_page) }

before do
visit alchemy.admin_pages_path
find(".sitemap_page[name='#{a_page.name}'] .icon.fa-cog").click
expect(page).to have_selector(".alchemy-dialog-overlay.open")
end

context "when updating the name" do
it "saves the name" do
visit alchemy.admin_pages_path
find(".sitemap_page[name='#{a_page.name}'] .icon.fa-cog").click
expect(page).to have_selector(".alchemy-dialog-overlay.open")
within(".alchemy-dialog.modal") do
find("input#page_name").set("name with some %!x^)'([@!{}]|/?\:# characters")
find(".submit button").click
Expand All @@ -189,6 +192,20 @@ class FooPreviewSource < Alchemy::Admin::PreviewUrl; end
expect(page).to have_selector("#sitemap a.sitemap_pagename_link", text: "name with some %!x^)'([@!{}]|/?\:# characters")
end
end

describe "changing parent" do
let!(:new_parent) { create(:alchemy_page) }

it "can change page parent" do
within(".alchemy-dialog.modal") do
expect(page).to have_css("#s2id_page_parent_id")
select2_search(new_parent.name, from: "Parent")
find(".submit button").click
end
expect(page).to_not have_selector(".alchemy-dialog-overlay.open")
expect(page).to have_selector("#sitemap .sitemap_url", text: "/#{new_parent.urlname}/#{a_page.urlname}")
end
end
end

describe "fixed attributes" do
Expand Down

0 comments on commit 63613fe

Please sign in to comment.