diff --git a/app/components/alchemy/ingredients/picture_view.rb b/app/components/alchemy/ingredients/picture_view.rb index 8e5a52bef4..4d5a8a7100 100644 --- a/app/components/alchemy/ingredients/picture_view.rb +++ b/app/components/alchemy/ingredients/picture_view.rb @@ -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 diff --git a/app/models/alchemy/ingredients/picture.rb b/app/models/alchemy/ingredients/picture.rb index 378dd694f5..54d76bae6a 100644 --- a/app/models/alchemy/ingredients/picture.rb +++ b/app/models/alchemy/ingredients/picture.rb @@ -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. diff --git a/app/views/alchemy/admin/ingredients/_picture_fields.html.erb b/app/views/alchemy/admin/ingredients/_picture_fields.html.erb index fb54429aac..f1278d769a 100644 --- a/app/views/alchemy/admin/ingredients/_picture_fields.html.erb +++ b/app/views/alchemy/admin/ingredients/_picture_fields.html.erb @@ -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: [ diff --git a/spec/components/alchemy/ingredients/picture_view_spec.rb b/spec/components/alchemy/ingredients/picture_view_spec.rb index 10c2ec200d..b8ebabf9e2 100644 --- a/spec/components/alchemy/ingredients/picture_view_spec.rb +++ b/spec/components/alchemy/ingredients/picture_view_spec.rb @@ -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 diff --git a/spec/models/alchemy/ingredients/picture_spec.rb b/spec/models/alchemy/ingredients/picture_spec.rb index da1b4182b0..aa037b4555 100644 --- a/spec/models/alchemy/ingredients/picture_spec.rb +++ b/spec/models/alchemy/ingredients/picture_spec.rb @@ -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 }