Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eager load relations in elements trash #1732

Merged
merged 1 commit into from
Feb 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion app/controllers/alchemy/admin/trash_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class TrashController < Alchemy::Admin::BaseController
authorize_resource class: false

def index
@elements = Element.trashed
@elements = Element.trashed.includes(*element_includes)
@page = Page.find(params[:page_id])
@allowed_elements = @page.available_element_definitions
end
Expand All @@ -18,6 +18,25 @@ def clear
@elements = Element.trashed
@elements.map(&:destroy)
end

private

def element_includes
[
{
contents: {
essence: :ingredient_association
},
all_nested_elements: [
{
contents: {
essence: :ingredient_association
}
}
]
}
]
end
end
end
end
6 changes: 6 additions & 0 deletions lib/alchemy/test_support/factories/element_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
name { 'header' }
end

trait :trashed do
after(:create) do |element|
element.update_column(:position, :null)
end
end

trait :with_nestable_elements do
name { 'slider' }
end
Expand Down
41 changes: 17 additions & 24 deletions spec/controllers/alchemy/admin/trash_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,59 +9,52 @@ module Admin

render_views

let(:alchemy_page) { create(:alchemy_page, :public) }
let(:element) { create(:alchemy_element, public: false, page: alchemy_page) }
let(:page) { create(:alchemy_page, :public) }
let!(:trashed) { create(:alchemy_element, :trashed, page: page) }
let!(:element) { create(:alchemy_element, page: page) }

before {
before do
authorize_user(:as_admin)
element.trash!
}
end

it "should hold trashed elements" do
get :index, params: {page_id: alchemy_page.id}
expect(response.body).to have_selector("[data-element-id=\"#{element.id}\"].element-editor")
it "lists trashed elements" do
get :index, params: {page_id: page.id}
expect(response.body).to have_selector("[data-element-id=\"#{trashed.id}\"].element-editor")
end

it "should not hold elements that are not trashed" do
element = create(:alchemy_element, page: alchemy_page, public: false)
get :index, params: {page_id: alchemy_page.id}
it "does not list elements that are not trashed" do
get :index, params: {page_id: page.id}
expect(response.body).not_to have_selector("[data-element-id=\"#{element.id}\"].element-editor")
end

context "with unique elements inside the trash" do
let(:trashed) { build_stubbed(:alchemy_element, :unique, position: nil, public: false, folded: true, page: alchemy_page) }
before { allow(Element).to receive(:trashed).and_return([trashed]) }
let!(:unique_trashed) { create(:alchemy_element, :trashed, :unique, page: page) }

context "and no unique elements on the page" do
before do
allow(alchemy_page).to receive(:elements).and_return double(not_trashed: double(pluck: []))
let!(:not_unique) do
create(:alchemy_element, page: page)
end

it "unique elements should be draggable" do
get :index, params: {page_id: alchemy_page.id}
expect(response.body).to have_selector("[data-element-id=\"#{trashed.id}\"].element-editor.draggable")
get :index, params: {page_id: page.id}
expect(response.body).to have_selector("[data-element-id=\"#{unique_trashed.id}\"].element-editor.draggable")
end
end

context "and with an unique element on the page" do
let!(:page) { create(:alchemy_page, :public) }
let!(:unique) { create(:alchemy_element, :unique, page: page) }

before do
allow(Page).to receive(:find).and_return(page)
end

it "unique elements should not be draggable" do
get :index, params: {page_id: page.id}
expect(response.body).to have_selector("[data-element-id=\"#{trashed.id}\"].element-editor.not-draggable")
expect(response.body).to have_selector("[data-element-id=\"#{unique_trashed.id}\"].element-editor.not-draggable")
end
end
end

describe "#clear" do
it "should destroy all containing elements" do
expect(Element.trashed).not_to be_empty
post :clear, params: {page_id: alchemy_page.id}, xhr: true
post :clear, params: {page_id: page.id}, xhr: true
expect(Element.trashed).to be_empty
end
end
Expand Down