Skip to content

Commit

Permalink
Remove elements_including_fixed relatin form Page
Browse files Browse the repository at this point in the history
This relation was only used in two internal methods and was never meant
to be public API.
  • Loading branch information
tvdeyen committed Jun 17, 2019
1 parent 015c228 commit e1b0dd1
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 36 deletions.
13 changes: 6 additions & 7 deletions app/models/alchemy/page/page_elements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ module Page::PageElements
has_many :elements,
-> { order(:position).not_nested.unfixed.available },
class_name: 'Alchemy::Element'
has_many :elements_including_fixed,
-> { order(:position).not_nested.not_trashed },
class_name: 'Alchemy::Element'
has_many :trashed_elements,
-> { Element.trashed.order(:position) },
class_name: 'Alchemy::Element'
Expand Down Expand Up @@ -46,7 +43,7 @@ module ClassMethods
#
def copy_elements(source, target)
new_elements = []
source.elements_including_fixed.each do |source_element|
source.all_elements.not_nested.not_trashed.order(:position).each do |source_element|
new_element = Element.copy(source_element, {
page_id: target.id
})
Expand Down Expand Up @@ -87,7 +84,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 @@ -192,9 +190,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
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
36 changes: 12 additions & 24 deletions spec/models/alchemy/page_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -558,8 +558,8 @@ module Alchemy
before { page.elements << create(:alchemy_element, :fixed) }

it "the copy should have source fixed elements" do
expect(subject.elements_including_fixed).not_to be_empty
expect(subject.elements_including_fixed.count).to eq(page.elements_including_fixed.count)
expect(subject.fixed_elements).not_to be_empty
expect(subject.fixed_elements.count).to eq(page.fixed_elements.count)
end
end

Expand Down Expand Up @@ -778,21 +778,15 @@ module Alchemy
describe '#available_element_definitions' do
subject { page.available_element_definitions }

let(:page) { build_stubbed(:alchemy_page, :public) }
let(:page) { create(:alchemy_page, :public) }

it "returns all element definitions of available elements" do
expect(subject).to be_an(Array)
expect(subject.collect { |e| e['name'] }).to include('header')
end

context "with unique elements already on page" do
let(:element) { build_stubbed(:alchemy_element, :unique) }

before do
allow(page).to receive(:elements_including_fixed) do
double(pluck: [element.name])
end
end
let!(:element) { create(:alchemy_element, :unique, page: page) }

it "does not return unique element definitions" do
expect(subject.collect { |e| e['name'] }).to include('article')
Expand All @@ -801,13 +795,15 @@ module Alchemy
end

context 'limited amount' do
let(:page) { build_stubbed(:alchemy_page, page_layout: 'columns') }
let(:unique_element) do
build_stubbed(:alchemy_element, :unique, name: 'unique_headline')
let(:page) { create(:alchemy_page, page_layout: 'columns') }

let!(:unique_element) do
create(:alchemy_element, :unique, name: 'unique_headline', page: page)
end
let(:element_1) { build_stubbed(:alchemy_element, name: 'column_headline') }
let(:element_2) { build_stubbed(:alchemy_element, name: 'column_headline') }
let(:element_3) { build_stubbed(:alchemy_element, name: 'column_headline') }

let!(:element_1) { create(:alchemy_element, name: 'column_headline', page: page) }
let!(:element_2) { create(:alchemy_element, name: 'column_headline', page: page) }
let!(:element_3) { create(:alchemy_element, name: 'column_headline', page: page) }

before do
allow(Element).to receive(:definitions).and_return([
Expand All @@ -828,14 +824,6 @@ module Alchemy
'elements' => ['column_headline', 'unique_headline'],
'autogenerate' => ['unique_headline', 'column_headline', 'column_headline', 'column_headline']
})
allow(page).to receive(:elements_including_fixed) do
double(pluck: [
unique_element.name,
element_1.name,
element_2.name,
element_3.name
])
end
end

it "should be readable" do
Expand Down

0 comments on commit e1b0dd1

Please sign in to comment.