Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add srcset support #57

Merged
merged 2 commits into from
Nov 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions app/serializers/alchemy/json_api/essence_picture_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,27 @@ class EssencePictureSerializer
}
end

attribute :srcset do |essence|
essence.content.settings.fetch(:srcset, []).map do |src|
case src
when Hash
url = essence.picture_url(src)
size = src[:size]
else
url = essence.picture_url(size: src)
size = src
end
width, height = size.split("x", 2)

{
url: url,
desc: "#{width}w",
width: width,
height: height,
}
end
end

attribute :image_name do |essence|
essence.picture.name
end
Expand Down
21 changes: 21 additions & 0 deletions app/serializers/alchemy/json_api/ingredient_picture_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,27 @@ class IngredientPictureSerializer
}
end

attribute :srcset do |ingredient|
ingredient.settings.fetch(:srcset, []).map do |src|
case src
when Hash
url = ingredient.picture_url(src)
size = src[:size]
else
url = ingredient.picture_url(size: src)
size = src
end
width, height = size.split("x", 2)

{
url: url,
desc: "#{width}w",
width: width,
height: height,
}
end
end

attribute :image_name do |ingredient|
ingredient.picture.name
end
Expand Down
101 changes: 85 additions & 16 deletions spec/serializers/alchemy/json_api/essence_picture_serializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
title: "Picture",
content: content,
link: "/hello",
picture: picture
picture: picture,
)
end
let(:options) { {} }
Expand Down Expand Up @@ -43,37 +43,27 @@
end

context "with image" do
it do
expect(image_dimensions).to eq(width: 1, height: 1)
end
it { expect(image_dimensions).to eq(width: 1, height: 1) }

context "with content settings[:size]" do
before do
expect(content).to receive(:settings).at_least(:once) { size }
end

let(:size) do
{ size: "100x100" }
end
let(:size) { { size: "100x100" } }

it do
expect(image_dimensions).to eq(width: 100, height: 100)
end
it { expect(image_dimensions).to eq(width: 100, height: 100) }

context "without y dimension" do
let(:size) do
{ size: "100x" }
end
let(:size) { { size: "100x" } }

it "infers height from ratio" do
expect(image_dimensions).to eq(width: 100, height: 100)
end
end

context "without x dimension" do
let(:size) do
{ size: "x50" }
end
let(:size) { { size: "x50" } }

it "infers width from ratio" do
expect(image_dimensions).to eq(width: 50, height: 50)
Expand All @@ -82,6 +72,85 @@
end
end
end

describe "srcset" do
let(:srcset) { subject[:srcset] }

context "without image" do
let(:picture) { nil }

it { expect(srcset).to be_nil }
end

context "with srcset defined" do
before do
expect(content).to receive(:settings).at_least(:once) do
{
srcset: srcset_definition,
}
end
end

context "as strings" do
let(:srcset_definition) do
%w[100x100 200x100]
end

it "returns src sets objects" do
expect(srcset).to match_array(
[
{
url: instance_of(String),
desc: "100w",
width: "100",
height: "100",
},
{
url: instance_of(String),
desc: "200w",
width: "200",
height: "100",
},
]
)
end
end

context "as hash" do
let(:srcset_definition) do
[
{
size: "100x100",
crop: true,
},
{
size: "200x100",
format: "jpg",
},
]
end

it "returns src sets objects" do
expect(srcset).to match_array(
[
{
url: instance_of(String),
desc: "100w",
width: "100",
height: "100",
},
{
url: a_string_matching(%r{.jpg}),
desc: "200w",
width: "200",
height: "100",
},
]
)
end
end
end
end
end

context "With no picture set" do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,85 @@
end
end
end

describe "srcset" do
let(:srcset) { subject[:srcset] }

context "without image" do
let(:picture) { nil }

it { expect(srcset).to be_nil }
end

context "with srcset defined" do
before do
expect(ingredient).to receive(:settings).at_least(:once) do
{
srcset: srcset_definition,
}
end
end

context "as strings" do
let(:srcset_definition) do
%w[100x100 200x100]
end

it "returns src sets objects" do
expect(srcset).to match_array(
[
{
url: instance_of(String),
desc: "100w",
width: "100",
height: "100",
},
{
url: instance_of(String),
desc: "200w",
width: "200",
height: "100",
},
]
)
end
end

context "as hash" do
let(:srcset_definition) do
[
{
size: "100x100",
crop: true,
},
{
size: "200x100",
format: "jpg",
},
]
end

it "returns src sets objects" do
expect(srcset).to match_array(
[
{
url: instance_of(String),
desc: "100w",
width: "100",
height: "100",
},
{
url: a_string_matching(%r{.jpg}),
desc: "200w",
width: "200",
height: "100",
},
]
)
end
end
end
end
end

context "With no picture set" do
Expand Down