Skip to content

Commit

Permalink
Use the picture description in picture ingredient
Browse files Browse the repository at this point in the history
Use the picture description to prefill the alt_tag if not given. This value is at the moment only available if the editor form is submitted.
  • Loading branch information
sascha-karnatz committed Feb 12, 2024
1 parent bd499e9 commit 1f9fca1
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/components/alchemy/ingredients/picture_view.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def srcset_options
end

def alt_text
ingredient.alt_tag.presence || html_options.delete(:alt) || ingredient.picture.name&.humanize
html_options.delete(:alt) || ingredient.alt_text
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions app/models/alchemy/ingredients/picture.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ class Picture < Alchemy::Ingredient
upsample
]

def alt_text
alt_tag.presence || picture&.description || picture&.name&.humanize
end

# The first 30 characters of the pictures name
#
# Used by the Element#preview_text method.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<%= f.input :caption, as: ingredient.settings[:caption_as_textarea] ? 'text' : 'string' %>
<%= f.input :title %>
<%= f.input :alt_tag %>
<%= f.input :alt_tag, as: :text, placeholder: ingredient.alt_text %>
<%- if ingredient.settings[:sizes].present? && ingredient.settings[:srcset].blank? -%>
<%= f.input :render_size,
collection: [
Expand Down
12 changes: 12 additions & 0 deletions spec/components/alchemy/ingredients/picture_view_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,18 @@
it "uses this as image alt text" do
expect(page).to have_selector('img[alt="Cute kittens"]')
end

context "with additional alt_tag" do
let(:ingredient) do
stub_model Alchemy::Ingredients::Picture,
picture: picture,
alt_tag: "not used alt text"
end

it "uses html option as image alt text" do
expect(page).to have_selector('img[alt="Cute kittens"]')
end
end
end

context "and not passed as html option" do
Expand Down
34 changes: 34 additions & 0 deletions spec/models/alchemy/ingredients/picture_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,40 @@
it { is_expected.to eq("A cute kitten") }
end

describe "alt_text" do
subject { picture_ingredient.alt_text }

context "with a alt_tag" do
before { picture_ingredient.alt_tag = "A cute kitten" }

it { is_expected.to eq("A cute kitten") }
end

context "with a picture description" do
before { picture.description = "Another cute kitten" }

it { is_expected.to eq("Another cute kitten") }

context "with a alt_tag" do
before { picture_ingredient.alt_tag = "A cute kitten" }

it { is_expected.to eq("A cute kitten") }
end
end

context "with a picture name" do
before { picture.name = "cute_kitten" }

it { is_expected.to eq("Cute kitten") }

context "with a picture description" do
before { picture.description = "Another cute kitten" }

it { is_expected.to eq("Another cute kitten") }
end
end
end

describe "css_class" do
before { picture_ingredient.css_class = "download" }
subject { picture_ingredient.css_class }
Expand Down

0 comments on commit 1f9fca1

Please sign in to comment.