Skip to content

Commit

Permalink
Merge pull request #1470 from tvdeyen/4.0-backports
Browse files Browse the repository at this point in the history
4.0 backports
  • Loading branch information
tvdeyen authored Sep 14, 2018
2 parents 5b2a3c9 + adcb288 commit d3141ef
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 9 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Change Log

## 4.0.5 (unreleased)

- Do not cache sitemap in Turbolinks [#1463](https://github.com/AlchemyCMS/alchemy_cms/pull/1463) ([tvdeyen](https://github.com/tvdeyen))
- Skip folded deeper levels when rendering page tree [#1324](https://github.com/AlchemyCMS/alchemy_cms/pull/1324) ([pascalj](https://github.com/pascalj))

## 4.0.4 (2018-09-05)

- Allow Kaminari 1.x [#1467](https://github.com/AlchemyCMS/alchemy_cms/pull/1467) ([tvdeyen](https://github.com/tvdeyen))
Expand Down
5 changes: 5 additions & 0 deletions app/models/alchemy/folded_page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,10 @@ class FoldedPage < ActiveRecord::Base
belongs_to :page, required: true
belongs_to :user, required: true,
class_name: Alchemy.user_class_name

def self.folded_for_user(user)
return none unless Alchemy.user_class < ActiveRecord::Base
where(user: user, folded: true)
end
end
end
19 changes: 10 additions & 9 deletions app/serializers/alchemy/page_tree_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,24 @@ def pages
tree = []
path = [{id: object.parent_id, children: tree}]
page_list = object.self_and_descendants
skip_branch = false
base_level = object.level - 1
# Load folded pages in advance
folded_user_pages = FoldedPage.folded_for_user(opts[:user]).pluck(:page_id)
folded_depth = Float::INFINITY

page_list.each_with_index do |page, i|
has_children = page_list[i + 1] && page_list[i + 1].parent_id == page.id
folded = has_children && page.folded?(opts[:user])
folded = has_children && folded_user_pages.include?(page.id)

if skip_branch
next if page.parent_id == path.last[:children].last[:id]

skip_branch = false
if page.depth > folded_depth
next
else
folded_depth = Float::INFINITY
end

# Do not walk my children if I'm folded and you don't need to have the
# full tree.
# If this page is folded, skip all pages that are on a higher level (further down the tree).
if folded && !opts[:full]
skip_branch = true
folded_depth = page.depth
end

if page.parent_id != path.last[:id]
Expand Down
4 changes: 4 additions & 0 deletions app/views/alchemy/admin/pages/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
<% content_for :javascript_includes do %>
<meta name="turbolinks-cache-control" content="no-cache">
<% end %>

<% content_for :toolbar do %>
<div class="toolbar_buttons">
<%= render 'alchemy/admin/partials/site_select' %>
Expand Down
38 changes: 38 additions & 0 deletions spec/models/alchemy/folded_page_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require "spec_helper"

module Alchemy
class NonArDummyUser; end

describe FoldedPage do
describe "folded_for_user" do
subject(:folded_for_user) { described_class.folded_for_user(user) }
let(:user) { create(:alchemy_dummy_user) }

context "with a non-AR user_class" do
around :each do |example|
before = Alchemy.user_class_name
Alchemy.user_class_name = "NonArDummyUser"
example.run
Alchemy.user_class_name = before
end
let(:user) { NonArDummyUser.new }

it "does not raise an error" do
expect {
folded_for_user
}.not_to raise_error
end
end

context "with folded pages" do
let(:page) { create(:alchemy_page) }
let(:other_user) { create(:alchemy_dummy_user) }
let!(:user_folded) { FoldedPage.create(user: user, page: page, folded: true) }
let!(:other_user_folded) { FoldedPage.create(user: other_user, page: page, folded: true) }

it { is_expected.to include(user_folded) }
it { is_expected.to_not include(other_user_folded) }
end
end
end
end

0 comments on commit d3141ef

Please sign in to comment.