Skip to content

Commit

Permalink
Deprecate fallback option of elements finder
Browse files Browse the repository at this point in the history
Providing fallback to render_elements or the ElementsFinder class will be removed from Alchemy 6.0
  • Loading branch information
tvdeyen committed Dec 21, 2020
1 parent a10d119 commit 61f9f73
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 85 deletions.
18 changes: 0 additions & 18 deletions app/helpers/alchemy/elements_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,6 @@ module ElementsHelper
# <%= render_elements from_page: 'footer' %>
# </footer>
#
# === Fallback to elements from global page:
#
# You can use the fallback option as an override for elements that are stored on another page.
# So you can take elements from a global page and only if the user adds an element on current page the
# local one gets rendered.
#
# 1. You have to pass the the name of the element the fallback is for as <tt>for</tt> key.
# 2. You have to pass a <tt>page_layout</tt> name or {Alchemy::Page} from where the fallback elements is taken from as <tt>from</tt> key.
# 3. You can pass the name of element to fallback with as <tt>with</tt> key. This is optional (the element name from the <tt>for</tt> key is taken as default).
#
# <%= render_elements(fallback: {
# for: 'contact_teaser',
# from: 'sidebar',
# with: 'contact_teaser'
# }) %>
#
# === Custom elements finder:
#
# Having a custom element finder class:
Expand Down Expand Up @@ -76,8 +60,6 @@ module ElementsHelper
# The amount of elements to be rendered (begins with first element found)
# @option options [Number] :offset
# The offset to begin loading elements from
# @option options [Hash] :fallback
# Define elements that are rendered from another page.
# @option options [Boolean] :random (false)
# Randomize the output of elements
# @option options [Boolean] :reverse (false)
Expand Down
5 changes: 3 additions & 2 deletions lib/alchemy/elements_finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ class ElementsFinder
# Randomize the output of elements
# @option options [Boolean] :reverse (false)
# Reverse the load order
# @option options [Hash] :fallback
# Define elements that are loaded from another page if no element was found on given page.
def initialize(options = {})
@options = options
end
Expand Down Expand Up @@ -92,6 +90,9 @@ def get_page(page_or_layout)
end

def fallback_required?(elements)
if options[:fallback]
Alchemy::Deprecation.warn "Passing `fallback` options to `render_elements` is deprecated an will be removed with Alchemy 6.0."
end
options[:fallback] && elements
.where(Alchemy::Element.table_name => {name: options[:fallback][:for]})
.none?
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div id="header">
<div id="header_image">
<%= render_elements(:only => 'header', :fallback => {:for => 'header', :from => 'layout_header'}) %>
<%= render_elements(:only => 'header') %>
</div>
<%- claim = render_elements :only => ["claim"] -%>
<%- if !claim.blank? -%>
Expand Down
64 changes: 0 additions & 64 deletions spec/libraries/elements_finder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,69 +169,5 @@
end
end
end

context "with fallback options given" do
subject { finder.elements(page: page) }

let(:options) do
{
fallback: {
for: "download",
from: page_2,
},
}
end

context "and no element from that kind on current page" do
let(:page) { create(:alchemy_page, :public, page_layout: "standard") }

context "but element of that kind on fallback page" do
let(:page_2) { create(:alchemy_page, :public, page_layout: "standard") }
let!(:visible_element_2) { create(:alchemy_element, name: "download", public: true, page: page_2) }

it "loads elements from fallback page" do
is_expected.to eq([visible_element_2])
end
end

context "with fallback element defined" do
let(:options) do
{
fallback: {
for: "download",
with: "header",
from: page_2,
},
}
end

let(:page_2) { create(:alchemy_page, :public, page_layout: "standard") }
let!(:visible_element_2) { create(:alchemy_element, name: "header", public: true, page: page_2) }

it "loads fallback element from fallback page" do
is_expected.to eq([visible_element_2])
end
end

context "with fallback page defined as pagelayout name" do
let(:options) do
{
fallback: {
for: "download",
with: "text",
from: "everything",
},
}
end

let(:page_2) { create(:alchemy_page, :public, page_layout: "everything") }
let!(:visible_element_2) { create(:alchemy_element, name: "text", public: true, page: page_2) }

it "loads fallback element from fallback page" do
is_expected.to eq([visible_element_2])
end
end
end
end
end
end

0 comments on commit 61f9f73

Please sign in to comment.