From 4012c4d95eadc135b7adb4efa69cb79ca825655e Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Thu, 21 Mar 2024 17:01:41 +0100 Subject: [PATCH 1/2] Allow HTML tags in ingredient picture view captions We allow a textarea to be used as image caption input. This produces br tags for new lines. The output needs to be html safe in order to allow that. --- .../alchemy/ingredients/picture_view.rb | 2 +- .../alchemy/ingredients/picture_view_spec.rb | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/app/components/alchemy/ingredients/picture_view.rb b/app/components/alchemy/ingredients/picture_view.rb index 2caa647fbe..d32ab0ab99 100644 --- a/app/components/alchemy/ingredients/picture_view.rb +++ b/app/components/alchemy/ingredients/picture_view.rb @@ -65,7 +65,7 @@ def call def caption return unless show_caption? - @_caption ||= content_tag(:figcaption, ingredient.caption) + @_caption ||= content_tag(:figcaption, ingredient.caption.html_safe) end def src diff --git a/spec/components/alchemy/ingredients/picture_view_spec.rb b/spec/components/alchemy/ingredients/picture_view_spec.rb index b8ebabf9e2..22d3c901a8 100644 --- a/spec/components/alchemy/ingredients/picture_view_spec.rb +++ b/spec/components/alchemy/ingredients/picture_view_spec.rb @@ -52,6 +52,22 @@ expect(page).to have_content("This is a cute cat") end + context "having html tags" do + let(:ingredient) do + stub_model Alchemy::Ingredients::Picture, + role: "image", + picture: picture, + data: { + caption: "This is a
cute cat" + } + end + + it "should show the caption" do + render_view + expect(page).to have_content("This is acute cat") + end + end + it "does not pass default options to picture url" do expect(ingredient).to receive(:picture_url).with({}) { picture_url } render_view From 7b8a43a0d4fb947714e1f9240123e8f5ddc2fc47 Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Thu, 21 Mar 2024 17:23:06 +0100 Subject: [PATCH 2/2] Allow to pass render format as symbol It is very common in Ruby to pass values as symbols as well. ie. image.url(format: :png) It was raising a (catched) error before that lead to cryptic undefined method url for nil errors. --- app/models/alchemy/picture_variant.rb | 2 +- spec/models/alchemy/picture_variant_spec.rb | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/app/models/alchemy/picture_variant.rb b/app/models/alchemy/picture_variant.rb index 64035ada22..8e2f291d75 100644 --- a/app/models/alchemy/picture_variant.rb +++ b/app/models/alchemy/picture_variant.rb @@ -43,7 +43,7 @@ def initialize(picture, options = {}) @picture = picture @options = options - @render_format = options[:format] || picture.default_render_format + @render_format = (options[:format] || picture.default_render_format).to_s end # Process a variant of picture diff --git a/spec/models/alchemy/picture_variant_spec.rb b/spec/models/alchemy/picture_variant_spec.rb index 7711f435f5..679c6164c5 100644 --- a/spec/models/alchemy/picture_variant_spec.rb +++ b/spec/models/alchemy/picture_variant_spec.rb @@ -231,6 +231,18 @@ end end end + + context "passed as symbol" do + let(:options) do + {format: :gif} + end + + it "converts the format" do + step = subject.steps[0] + expect(step.name).to eq(:encode) + expect(step.arguments).to include("gif") + end + end end context "requesting a not allowed format" do