Skip to content

Commit f7c6fe1

Browse files
committed
Move deprecation notice to element definition
The notice is static per element definition, not different per element record. We can display the notice where we just display the definition that way.
1 parent b05b531 commit f7c6fe1

5 files changed

Lines changed: 79 additions & 97 deletions

File tree

app/decorators/alchemy/element_editor.rb

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -77,49 +77,6 @@ def respond_to?(*args, **kwargs)
7777
super
7878
end
7979

80-
# Returns a deprecation notice for elements marked deprecated
81-
#
82-
# You can either use localizations or pass a String as notice
83-
# in the element definition.
84-
#
85-
# == Custom deprecation notices
86-
#
87-
# Use general element deprecation notice
88-
#
89-
# - name: old_element
90-
# deprecated: true
91-
#
92-
# Add a translation to your locale file for a per element notice.
93-
#
94-
# en:
95-
# alchemy:
96-
# element_deprecation_notices:
97-
# old_element: Foo baz widget is deprecated
98-
#
99-
# or use the global translation that apply to all deprecated elements.
100-
#
101-
# en:
102-
# alchemy:
103-
# element_deprecation_notice: Foo baz widget is deprecated
104-
#
105-
# or pass string as deprecation notice.
106-
#
107-
# - name: old_element
108-
# deprecated: This element will be removed soon.
109-
#
110-
def deprecation_notice
111-
case definition.deprecated
112-
when String
113-
definition.deprecated
114-
when TrueClass
115-
Alchemy.t(
116-
name,
117-
scope: :element_deprecation_notices,
118-
default: Alchemy.t(:element_deprecated)
119-
)
120-
end
121-
end
122-
12380
private
12481

12582
def find_or_create_ingredient(definition)

app/models/alchemy/element.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ class Element < BaseRecord
106106
scope :not_nested, -> { where(parent_element_id: nil) }
107107

108108
delegate :restricted?, to: :page, allow_nil: true
109-
delegate :has_hint?, :hint, to: :definition
109+
delegate :deprecation_notice, :has_hint?, :hint, to: :definition
110110

111111
# Concerns
112112
include Definitions

app/models/alchemy/element_definition.rb

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,49 @@ def ingredients
109109
super.map(&:with_indifferent_access)
110110
end
111111

112+
# Returns a deprecation notice for elements marked deprecated
113+
#
114+
# You can either use localizations or pass a String as notice
115+
# in the element definition.
116+
#
117+
# == Custom deprecation notices
118+
#
119+
# Use general element deprecation notice
120+
#
121+
# - name: old_element
122+
# deprecated: true
123+
#
124+
# Add a translation to your locale file for a per element notice.
125+
#
126+
# en:
127+
# alchemy:
128+
# element_deprecation_notices:
129+
# old_element: Foo baz widget is deprecated
130+
#
131+
# or use the global translation that apply to all deprecated elements.
132+
#
133+
# en:
134+
# alchemy:
135+
# element_deprecation_notice: Foo baz widget is deprecated
136+
#
137+
# or pass string as deprecation notice.
138+
#
139+
# - name: old_element
140+
# deprecated: This element will be removed soon.
141+
#
142+
def deprecation_notice
143+
case deprecated
144+
when String
145+
deprecated
146+
when TrueClass
147+
Alchemy.t(
148+
name,
149+
scope: :element_deprecation_notices,
150+
default: Alchemy.t(:element_deprecated)
151+
)
152+
end
153+
end
154+
112155
private
113156

114157
def hint_translation_scope

spec/decorators/alchemy/element_editor_spec.rb

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -185,57 +185,4 @@
185185

186186
it { is_expected.to be(false) }
187187
end
188-
189-
describe "deprecation_notice" do
190-
subject { element_editor.deprecation_notice }
191-
192-
context "when element is not deprecated" do
193-
let(:element) { build(:alchemy_element, name: "article") }
194-
195-
it { is_expected.to be_nil }
196-
end
197-
198-
context "when element is deprecated" do
199-
let(:element) { build(:alchemy_element, name: "old") }
200-
201-
context "with custom element translation" do
202-
it { is_expected.to eq("Old element is deprecated") }
203-
end
204-
205-
context "without custom element translation" do
206-
let(:element) { build(:alchemy_element, name: "old_too") }
207-
208-
before do
209-
allow(element).to receive(:definition) do
210-
Alchemy::ElementDefinition.new(
211-
name: "old_too",
212-
deprecated: true
213-
)
214-
end
215-
end
216-
217-
it do
218-
is_expected.to eq(
219-
"WARNING! This element is deprecated and will be removed soon. " \
220-
"Please do not use it anymore."
221-
)
222-
end
223-
end
224-
225-
context "with String as deprecation" do
226-
let(:element) { build(:alchemy_element, name: "old_string") }
227-
228-
before do
229-
allow(element).to receive(:definition) do
230-
Alchemy::ElementDefinition.new(
231-
name: "old_string",
232-
deprecated: "Foo baz widget"
233-
)
234-
end
235-
end
236-
237-
it { is_expected.to eq("Foo baz widget") }
238-
end
239-
end
240-
end
241188
end

spec/models/alchemy/element_definition_spec.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,41 @@ module Alchemy
4646
end
4747
end
4848

49+
describe "#deprecation_notice" do
50+
subject { definition.deprecation_notice }
51+
52+
context "when element is not deprecated" do
53+
let(:definition) { described_class.new(name: "article") }
54+
55+
it { is_expected.to be_nil }
56+
end
57+
58+
context "when element is deprecated" do
59+
let(:definition) { described_class.new(name: "old", deprecated: true) }
60+
61+
context "with custom element translation" do
62+
it { is_expected.to eq("Old element is deprecated") }
63+
end
64+
65+
context "without custom element translation" do
66+
let(:definition) { described_class.new(name: "old_too", deprecated: true) }
67+
68+
it do
69+
is_expected.to eq(
70+
"WARNING! This element is deprecated and will be removed soon. " \
71+
"Please do not use it anymore."
72+
)
73+
end
74+
end
75+
76+
context "with String as deprecation" do
77+
let(:definition) { described_class.new(name: "old_string", deprecated: "Foo baz widget") }
78+
79+
it { is_expected.to eq("Foo baz widget") }
80+
end
81+
end
82+
end
83+
4984
describe ".all" do
5085
# skip memoization
5186
before { ElementDefinition.instance_variable_set(:@definitions, nil) }

0 commit comments

Comments
 (0)