Skip to content

Commit dc1e81d

Browse files
committed
Allow to set element definition icon
Useful if you want to distingiush elements visually.
1 parent 605cc2e commit dc1e81d

File tree

8 files changed

+62
-3
lines changed

8 files changed

+62
-3
lines changed

app/assets/builds/alchemy/admin.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/assets/builds/alchemy/admin.css.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 0 deletions
Loading

app/models/alchemy/element.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class Element < BaseRecord
3737
"compact",
3838
"deprecated",
3939
"hint",
40+
"icon",
4041
"ingredients",
4142
"message",
4243
"nestable_elements",

app/models/alchemy/element_definition.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,18 @@ class ElementDefinition
2121
attribute :message
2222
attribute :warning
2323
attribute :hint
24+
attribute :icon
2425

2526
validates :name,
2627
presence: true,
2728
format: {
2829
with: /\A[a-z_-]+\z/
2930
}
3031

32+
validates :icon,
33+
format: {with: /\A[\w-]+\z/i},
34+
if: -> { icon.is_a?(String) }
35+
3136
delegate :blank?, to: :name
3237

3338
class << self
@@ -151,8 +156,20 @@ def deprecation_notice
151156
end
152157
end
153158

159+
def icon_file
160+
@_icon_file ||= File.read(icons_root_path.join("#{icon_name}.svg")).html_safe
161+
end
162+
163+
def icon_name
164+
(icon == true) ? name : icon
165+
end
166+
154167
private
155168

169+
def icons_root_path
170+
Rails.root.join("app", "assets", "images", "alchemy", "element_icons")
171+
end
172+
156173
def hint_translation_scope
157174
:element_hints
158175
end

app/stylesheets/alchemy/admin/page_definitions.scss

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,15 @@
5757
header {
5858
display: flex;
5959
width: 100%;
60+
height: 18px;
6061
align-items: center;
6162
justify-content: space-between;
62-
gap: var(--spacing-1);
63+
gap: var(--spacing-2);
64+
65+
svg {
66+
width: 18px;
67+
height: 18px;
68+
}
6369

6470
.labels {
6571
display: flex;

app/views/alchemy/admin/page_definitions/_element.html.erb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
<details>
44
<summary>
55
<header>
6+
<% if element.icon %>
7+
<%= element.icon_file %>
8+
<% else %>
9+
<%= render_icon('draggable', style: false, class: 'element', fixed_width: false) %>
10+
<% end %>
611
<%= Alchemy::Element.display_name_for(element.name) %>
712
<span class="labels">
813
<% if element.unique %>

spec/models/alchemy/element_definition_spec.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ module Alchemy
1010
subject { definition.attributes }
1111

1212
it { is_expected.to have_key(:name) }
13+
it { is_expected.to have_key(:icon) }
1314
it { is_expected.to have_key(:unique) }
1415
it { is_expected.to have_key(:amount) }
1516
it { is_expected.to have_key(:taggable) }
@@ -28,6 +29,14 @@ module Alchemy
2829
it { is_expected.to validate_presence_of(:name) }
2930
it { is_expected.to allow_value("article").for(:name) }
3031
it { is_expected.to_not allow_value("Article Element").for(:name) }
32+
33+
it { is_expected.to allow_value(true).for(:icon) }
34+
it { is_expected.to allow_value("article").for(:icon) }
35+
it { is_expected.to allow_value("Article_2").for(:icon) }
36+
it { is_expected.to allow_value("article2-line").for(:icon) }
37+
it { is_expected.to_not allow_value("Article Icon").for(:icon) }
38+
it { is_expected.to_not allow_value("article.svg").for(:icon) }
39+
it { is_expected.to_not allow_value("Article.png").for(:icon) }
3140
end
3241

3342
it_behaves_like "having a hint" do
@@ -90,6 +99,26 @@ module Alchemy
9099
end
91100
end
92101

102+
describe "#icon_name" do
103+
subject(:icon_name) { definition.icon_name }
104+
105+
context "with icon attribute being true" do
106+
let(:definition) { described_class.new(name: "article", icon: true) }
107+
108+
it "returns the name attribute" do
109+
expect(icon_name).to eq("article")
110+
end
111+
end
112+
113+
context "with icon attribute being a string" do
114+
let(:definition) { described_class.new(icon: "article-line") }
115+
116+
it "returns the icon attribute" do
117+
expect(icon_name).to eq("article-line")
118+
end
119+
end
120+
end
121+
93122
describe "#ingredients" do
94123
let(:definition) { described_class.new }
95124

0 commit comments

Comments
 (0)