Skip to content

Commit 9eef985

Browse files
committed
Move hints into definition classes
Hints are static and read from the definition anyway. This way we can display hints without having an record to read this from.
1 parent ce9cb9d commit 9eef985

11 files changed

Lines changed: 41 additions & 41 deletions

File tree

app/models/alchemy/element_definition.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ def read_definitions_file
103103
def attributes
104104
super.with_indifferent_access
105105
end
106-
alias_method :definition, :attributes
107106

108107
def ingredients
109108
super.map { IngredientDefinition.new(**_1) }

app/models/alchemy/page.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444

4545
module Alchemy
4646
class Page < BaseRecord
47-
include Alchemy::Hints
4847
include Alchemy::Logger
4948
include Alchemy::Taggable
5049

@@ -164,6 +163,7 @@ class Page < BaseRecord
164163

165164
# site_name accessor
166165
delegate :name, to: :site, prefix: true, allow_nil: true
166+
delegate :has_hint?, :hint, to: :definition
167167

168168
# Class methods
169169
#

app/models/alchemy/page_definition.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module Alchemy
44
class PageDefinition
55
include ActiveModel::Model
66
include ActiveModel::Attributes
7+
include Alchemy::Hints
78

89
extend ActiveModel::Translation
910

@@ -104,5 +105,11 @@ def human_name
104105
def attributes
105106
super.with_indifferent_access
106107
end
108+
109+
private
110+
111+
def hint_translation_scope
112+
:page_hints
113+
end
107114
end
108115
end

lib/alchemy/hints.rb

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,27 +35,23 @@ module Hints
3535
# @return String
3636
#
3737
def hint
38-
hint = definition[:hint]
38+
hint = attributes[:hint]
3939
if hint == true
4040
Alchemy.t(hint_translation_attribute, scope: hint_translation_scope)
4141
else
4242
hint
4343
end
4444
end
4545

46-
# Returns true if the element has a hint defined
46+
# Returns true if the definition has a hint defined
4747
def has_hint?
48-
!!definition[:hint]
48+
!!attributes[:hint]
4949
end
5050

5151
private
5252

5353
def hint_translation_attribute
5454
name
5555
end
56-
57-
def hint_translation_scope
58-
"#{self.class.model_name.to_s.demodulize.downcase}_hints"
59-
end
6056
end
6157
end

spec/models/alchemy/element_definition_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@ module Alchemy
3030
it { is_expected.to_not allow_value("Article Element").for(:name) }
3131
end
3232

33+
it_behaves_like "having a hint" do
34+
let(:translation_key) { "article" }
35+
let(:translation_scope) { :element_hints }
36+
37+
let(:subject) do
38+
described_class.new(name: "article", **hint)
39+
end
40+
end
41+
3342
describe "#blank?" do
3443
subject { definition.blank? }
3544

spec/models/alchemy/element_spec.rb

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -760,11 +760,6 @@ module Alchemy
760760
end
761761
end
762762

763-
it_behaves_like "having a hint" do
764-
let(:definition_class) { ElementDefinition }
765-
let(:subject) { Element.new }
766-
end
767-
768763
describe "#nestable_elements" do
769764
let(:element) { Element.new }
770765

spec/models/alchemy/ingredient_definition_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ module Alchemy
3232
it { is_expected.to_not allow_value("Spree::Product").for(:type) }
3333
end
3434

35+
it_behaves_like "having a hint" do
36+
let(:translation_key) { "text" }
37+
let(:translation_scope) { :ingredient_hints }
38+
39+
let(:subject) do
40+
described_class.new(role: "text", **hint)
41+
end
42+
end
43+
3544
describe "#deprecation_notice" do
3645
subject { definition.deprecation_notice(element_name: element&.name) }
3746

spec/models/alchemy/ingredient_spec.rb

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,6 @@
1313
build(:alchemy_element, name: "article", autogenerate_ingredients: false)
1414
end
1515

16-
it_behaves_like "having a hint" do
17-
let(:definition_class) { Alchemy::IngredientDefinition }
18-
let(:subject) { Alchemy::Ingredients::Text.new(role: "headline", element: element) }
19-
end
20-
2116
describe "scopes" do
2217
let(:element) do
2318
build(:alchemy_element, name: "all_you_can_eat", autogenerate_ingredients: false)

spec/models/alchemy/page_definition_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ module Alchemy
3838
end
3939
end
4040

41+
it_behaves_like "having a hint" do
42+
let(:translation_key) { "standard" }
43+
let(:translation_scope) { :page_hints }
44+
45+
let(:subject) do
46+
described_class.new(name: "standard", **hint)
47+
end
48+
end
49+
4150
describe "validations" do
4251
it { is_expected.to validate_presence_of(:name) }
4352
it { is_expected.to allow_value("standard").for(:name) }

spec/models/alchemy/page_spec.rb

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1943,17 +1943,6 @@ module Alchemy
19431943
end
19441944
end
19451945

1946-
it_behaves_like "having a hint" do
1947-
let(:definition_class) { PageDefinition }
1948-
let(:subject) { Page.new }
1949-
end
1950-
1951-
it "keys hint translation by page_layout" do
1952-
page = Page.new(page_layout: :everything)
1953-
expect(page).to have_hint
1954-
expect(page.hint).to eq Alchemy.t("page_hints.everything")
1955-
end
1956-
19571946
describe "#layout_partial_name" do
19581947
let(:page) { Page.new(page_layout: "Standard Page") }
19591948

0 commit comments

Comments
 (0)