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 8, 2024
1 parent 5c14b4f commit e186411
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
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_tag
super.presence || picture&.description
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 %>
<%- if ingredient.settings[:sizes].present? && ingredient.settings[:srcset].blank? -%>
<%= f.input :render_size,
collection: [
Expand Down
18 changes: 16 additions & 2 deletions spec/models/alchemy/ingredients/picture_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e186411

Please sign in to comment.