Skip to content

Commit

Permalink
Merge pull request #2911 from AlchemyCMS/backport/7.1-stable/pr-2909
Browse files Browse the repository at this point in the history
[7.1-stable] fix Ingredient Audio and Video boolean type casting
  • Loading branch information
tvdeyen committed Jun 4, 2024
2 parents 1c1310e + 5275048 commit 9419d6d
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 0 deletions.
11 changes: 11 additions & 0 deletions app/models/alchemy/ingredients/audio.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 12 additions & 0 deletions app/models/alchemy/ingredients/video.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
40 changes: 40 additions & 0 deletions spec/models/alchemy/ingredients/audio_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
50 changes: 50 additions & 0 deletions spec/models/alchemy/ingredients/video_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 9419d6d

Please sign in to comment.