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

Only return visible elements from Pages elements relations #1590

Merged
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
4 changes: 2 additions & 2 deletions app/controllers/alchemy/admin/elements_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ class ElementsController < Alchemy::Admin::BaseController

def index
@page = Page.find(params[:page_id])
@elements = @page.elements
@fixed_elements = @page.fixed_elements
@elements = @page.all_elements.not_nested.unfixed.not_trashed
@fixed_elements = @page.all_elements.fixed.not_trashed
end

def list
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/alchemy/elements_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ module ElementsHelper
#
# class MyCustomNewsArchive
# def elements(page:)
# news_page.elements.available.named('news').order(created_at: :desc)
# news_page.elements.named('news').order(created_at: :desc)
# end
#
# private
Expand Down
2 changes: 1 addition & 1 deletion app/models/alchemy/element.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class Element < BaseRecord
foreign_key: :parent_element_id,
dependent: :destroy

belongs_to :page, touch: true, inverse_of: :descendent_elements
belongs_to :page, touch: true, inverse_of: :all_elements

# A nested element belongs to a parent element.
belongs_to :parent_element,
Expand Down
42 changes: 17 additions & 25 deletions app/models/alchemy/page/page_elements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,19 @@ module Page::PageElements
included do
attr_accessor :autogenerate_elements

has_many :elements,
-> { order(:position).not_nested.unfixed.not_trashed },
has_many :all_elements,
-> { order(:position) },
class_name: 'Alchemy::Element'
has_many :elements_including_fixed,
-> { order(:position).not_nested.not_trashed },
has_many :elements,
-> { order(:position).not_nested.unfixed.available },
class_name: 'Alchemy::Element'
has_many :trashed_elements,
-> { Element.trashed.order(:position) },
class_name: 'Alchemy::Element'
has_many :fixed_elements,
-> { order(:position).fixed.not_trashed },
class_name: 'Alchemy::Element'
has_many :descendent_elements,
-> { order(:position).unfixed.not_trashed },
-> { order(:position).fixed.available },
class_name: 'Alchemy::Element'
has_many :contents, through: :elements
has_many :descendent_contents,
through: :descendent_elements,
class_name: 'Alchemy::Content',
source: :contents
has_and_belongs_to_many :to_be_swept_elements, -> { distinct },
class_name: 'Alchemy::Element',
join_table: ElementToPage.table_name
Expand All @@ -49,15 +42,12 @@ module ClassMethods
# @return [Array]
#
def copy_elements(source, target)
new_elements = []
source.elements_including_fixed.each do |source_element|
new_element = Element.copy(source_element, {
source_elements = source.all_elements.not_nested.not_trashed
source_elements.order(:position).map do |source_element|
Element.copy(source_element, {
page_id: target.id
})
new_element.move_to_bottom
new_elements << new_element
}).tap(&:move_to_bottom)
end
new_elements
end
end

Expand Down Expand Up @@ -91,7 +81,8 @@ def available_element_definitions(only_element_named = nil)

return [] if @_element_definitions.blank?

@_existing_element_names = elements_including_fixed.pluck(:name)
existing_elements = all_elements.not_nested.not_trashed
@_existing_element_names = existing_elements.pluck(:name)
delete_unique_element_definitions!
delete_outnumbered_element_definitions!

Expand Down Expand Up @@ -177,14 +168,14 @@ def element_definitions_by_name(names)
# feed_elements: [element_name, element_2_name]
#
def feed_elements
elements.available.named(definition['feed_elements'])
elements.named(definition['feed_elements'])
end

# Returns an array of all EssenceRichtext contents ids from not folded elements
#
def richtext_contents_ids
descendent_contents
.where(Element.table_name => {folded: false})
Alchemy::Content.joins(:element)
.where(Element.table_name => {page_id: id, folded: false})
.select(&:has_tinymce?)
.collect(&:id)
end
Expand All @@ -196,9 +187,10 @@ def richtext_contents_ids
# And if so, it generates them.
#
def generate_elements
elements_already_on_page = elements_including_fixed.pluck(:name)
existing_elements = all_elements.not_nested.not_trashed
existing_element_names = existing_elements.pluck(:name).uniq
definition.fetch('autogenerate', []).each do |element_name|
next if elements_already_on_page.include?(element_name)
next if existing_element_names.include?(element_name)
Element.create(page: self, name: element_name)
end
end
Expand Down
39 changes: 22 additions & 17 deletions spec/controllers/alchemy/admin/elements_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,39 @@ module Alchemy
before { authorize_user(:as_author) }

describe '#index' do
let(:alchemy_page) { build_stubbed(:alchemy_page) }

before do
expect(Page).to receive(:find).and_return alchemy_page
end
let!(:alchemy_page) { create(:alchemy_page) }
let!(:element) { create(:alchemy_element, page: alchemy_page) }
let!(:trashed_element) { create(:alchemy_element, page: alchemy_page).tap(&:trash!) }
let!(:nested_element) { create(:alchemy_element, :nested, page: alchemy_page) }
let!(:hidden_element) { create(:alchemy_element, page: alchemy_page, public: false) }

context 'with fixed elements' do
let(:fixed_element) { build_stubbed(:alchemy_element, :fixed, page: alchemy_page) }
let!(:fixed_element) do
create(:alchemy_element, :fixed,
page: alchemy_page)
end

before do
expect(alchemy_page).to receive(:fixed_elements).and_return [fixed_element]
let!(:fixed_hidden_element) do
create(:alchemy_element, :fixed,
public: false,
page: alchemy_page)
end

let!(:fixed_trashed_element) do
create(:alchemy_element, :fixed,
public: false,
page: alchemy_page).tap(&:trash!)
end

it "assigns fixed elements" do
get :index, params: {page_id: alchemy_page.id}
expect(assigns(:fixed_elements)).to eq([fixed_element])
expect(assigns(:fixed_elements)).to eq([fixed_element, fixed_hidden_element])
end
end

it "assigns page elements" do
expect(alchemy_page).to receive(:elements).and_return(double(not_trashed: []))
get :index, params: {page_id: alchemy_page.id}
expect(assigns(:elements)).to eq([element, hidden_element])
end
end

Expand Down Expand Up @@ -111,13 +122,7 @@ module Alchemy
end

context "untrashing" do
let(:trashed_element) { create(:alchemy_element) }

before do
# Because of a before_create filter it can not be created with a nil position
# and needs to be trashed here
trashed_element.trash!
end
let!(:trashed_element) { create(:alchemy_element).tap(&:trash!) }

it "sets a list of trashed element ids" do
post :order, params: {page_id: page.id, element_ids: [trashed_element.id]}, xhr: true
Expand Down
7 changes: 2 additions & 5 deletions spec/controllers/alchemy/admin/trash_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,11 @@ module Admin
end

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

before do
allow(Page).to receive(:find).and_return(page)
allow(page).to receive(:elements_including_fixed) do
double(pluck: [unique.name])
end
end

it "unique elements should not be draggable" do
Expand Down
Loading