diff --git a/app/models/alchemy/ingredients/picture.rb b/app/models/alchemy/ingredients/picture.rb index 378dd694f5..3f25b63abe 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_tag + super.presence || picture&.description + 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..64657533df 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 %> <%- if ingredient.settings[:sizes].present? && ingredient.settings[:srcset].blank? -%> <%= f.input :render_size, collection: [ diff --git a/spec/models/alchemy/ingredients/picture_spec.rb b/spec/models/alchemy/ingredients/picture_spec.rb index da1b4182b0..992940f9ab 100644 --- a/spec/models/alchemy/ingredients/picture_spec.rb +++ b/spec/models/alchemy/ingredients/picture_spec.rb @@ -18,10 +18,24 @@ end describe "alt_tag" do - before { picture_ingredient.alt_tag = "A cute kitten" } subject { picture_ingredient.alt_tag } - it { is_expected.to eq("A cute kitten") } + context "without a picture description" do + before { picture_ingredient.alt_tag = "A cute kitten" } + + it { is_expected.to eq("A cute kitten") } + end + + context "with a picture description" do + let(:picture) { build_stubbed(:alchemy_picture, description: "so cute") } + + it { is_expected.to eq("so cute") } + + it "uses the alt text if given" do + picture_ingredient.alt_tag = "A cute kitten" + expect(picture_ingredient.alt_tag).to eq("A cute kitten") + end + end end describe "css_class" do