From 5275048e1f969938bcd79bf606cc11a4aae41639 Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Mon, 3 Jun 2024 17:05:11 +0200 Subject: [PATCH] fix Ingredient Audio and Video boolean type casting Currently boolean values of ingredient video and audio are stored as strings ("0"/"1") in the data column JSON. Cast to Boolean before we save to make this booleans. (cherry picked from commit 0ec35d5eaa04cc3bec87a1c21acf604affedca78) --- app/models/alchemy/ingredients/audio.rb | 11 ++++ app/models/alchemy/ingredients/video.rb | 12 +++++ spec/models/alchemy/ingredients/audio_spec.rb | 40 +++++++++++++++ spec/models/alchemy/ingredients/video_spec.rb | 50 +++++++++++++++++++ 4 files changed, 113 insertions(+) diff --git a/app/models/alchemy/ingredients/audio.rb b/app/models/alchemy/ingredients/audio.rb index f1d60a0f98..b345226ca1 100644 --- a/app/models/alchemy/ingredients/audio.rb +++ b/app/models/alchemy/ingredients/audio.rb @@ -26,6 +26,17 @@ class Audio < Alchemy::Ingredient def preview_text(max_length = 30) name.to_s[0..max_length - 1] end + + %i[ + autoplay + controls + loop + muted + ].each do |method| + define_method(:"#{method}=") do |value| + super(ActiveModel::Type::Boolean.new.cast(value)) + end + end end end end diff --git a/app/models/alchemy/ingredients/video.rb b/app/models/alchemy/ingredients/video.rb index 13153f2baf..d1846433db 100644 --- a/app/models/alchemy/ingredients/video.rb +++ b/app/models/alchemy/ingredients/video.rb @@ -31,6 +31,18 @@ class Video < Alchemy::Ingredient def preview_text(max_length = 30) name.to_s[0..max_length - 1] end + + %i[ + autoplay + controls + loop + muted + playsinline + ].each do |method| + define_method(:"#{method}=") do |value| + super(ActiveModel::Type::Boolean.new.cast(value)) + end + end end end end diff --git a/spec/models/alchemy/ingredients/audio_spec.rb b/spec/models/alchemy/ingredients/audio_spec.rb index 1617a55726..d8cfe75acf 100644 --- a/spec/models/alchemy/ingredients/audio_spec.rb +++ b/spec/models/alchemy/ingredients/audio_spec.rb @@ -21,24 +21,64 @@ subject { audio_ingredient.autoplay } before { audio_ingredient.autoplay = false } it { is_expected.to eq(false) } + + context "when set to '0'" do + before { audio_ingredient.autoplay = "0" } + it { is_expected.to eq(false) } + end + + context "when set to 't'" do + before { audio_ingredient.autoplay = "t" } + it { is_expected.to eq(true) } + end end describe "#controls" do subject { audio_ingredient.controls } before { audio_ingredient.controls = true } it { is_expected.to eq(true) } + + context "when set to '0'" do + before { audio_ingredient.controls = "0" } + it { is_expected.to eq(false) } + end + + context "when set to 't'" do + before { audio_ingredient.controls = "t" } + it { is_expected.to eq(true) } + end end describe "#loop" do subject { audio_ingredient.loop } before { audio_ingredient.loop = false } it { is_expected.to eq(false) } + + context "when set to '0'" do + before { audio_ingredient.loop = "0" } + it { is_expected.to eq(false) } + end + + context "when set to 't'" do + before { audio_ingredient.loop = "t" } + it { is_expected.to eq(true) } + end end describe "#muted" do subject { audio_ingredient.muted } before { audio_ingredient.muted = true } it { is_expected.to eq(true) } + + context "when set to '0'" do + before { audio_ingredient.muted = "0" } + it { is_expected.to eq(false) } + end + + context "when set to 't'" do + before { audio_ingredient.muted = "t" } + it { is_expected.to eq(true) } + end end describe "#attachment" do diff --git a/spec/models/alchemy/ingredients/video_spec.rb b/spec/models/alchemy/ingredients/video_spec.rb index c72957872f..ef9fcc75e3 100644 --- a/spec/models/alchemy/ingredients/video_spec.rb +++ b/spec/models/alchemy/ingredients/video_spec.rb @@ -27,12 +27,32 @@ subject { video_ingredient.autoplay } before { video_ingredient.autoplay = false } it { is_expected.to eq(false) } + + context "when set to '0'" do + before { video_ingredient.autoplay = "0" } + it { is_expected.to eq(false) } + end + + context "when set to 't'" do + before { video_ingredient.autoplay = "t" } + it { is_expected.to eq(true) } + end end describe "#controls" do subject { video_ingredient.controls } before { video_ingredient.controls = true } it { is_expected.to eq(true) } + + context "when set to '0'" do + before { video_ingredient.controls = "0" } + it { is_expected.to eq(false) } + end + + context "when set to 't'" do + before { video_ingredient.controls = "t" } + it { is_expected.to eq(true) } + end end describe "#height" do @@ -45,18 +65,48 @@ subject { video_ingredient.loop } before { video_ingredient.loop = false } it { is_expected.to eq(false) } + + context "when set to '0'" do + before { video_ingredient.loop = "0" } + it { is_expected.to eq(false) } + end + + context "when set to 't'" do + before { video_ingredient.loop = "t" } + it { is_expected.to eq(true) } + end end describe "#muted" do subject { video_ingredient.muted } before { video_ingredient.muted = true } it { is_expected.to eq(true) } + + context "when set to '0'" do + before { video_ingredient.muted = "0" } + it { is_expected.to eq(false) } + end + + context "when set to 't'" do + before { video_ingredient.muted = "t" } + it { is_expected.to eq(true) } + end end describe "#playsinline" do subject { video_ingredient.playsinline } before { video_ingredient.playsinline = true } it { is_expected.to eq(true) } + + context "when set to '0'" do + before { video_ingredient.playsinline = "0" } + it { is_expected.to eq(false) } + end + + context "when set to 't'" do + before { video_ingredient.playsinline = "t" } + it { is_expected.to eq(true) } + end end describe "#preload" do