From f0fd035c40bb77cdef13d0fe6e74e2cdcf393607 Mon Sep 17 00:00:00 2001 From: Martin Meyerhoff Date: Thu, 13 May 2021 15:12:04 +0200 Subject: [PATCH] Pass crop parameter in default EssencePicture#picture_url_options (#2098) Prior to this commit, calling `my_essence.picture_url` without parameters would return an uncropped image be default. In order to get the cropped picture, one would have to add `my_essence.picture_url(crop: true)`. This is unintuitive: For an EssencePicture that I have cropped as a user (crop values are present), `essence.picture_url` as well as `essence.ingredient` should return the cropped version of the image. --- app/models/alchemy/essence_picture.rb | 3 ++ spec/models/alchemy/essence_picture_spec.rb | 35 +++++++++++++++------ 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/app/models/alchemy/essence_picture.rb b/app/models/alchemy/essence_picture.rb index 2e3aa29e9d..6e9a73ea36 100644 --- a/app/models/alchemy/essence_picture.rb +++ b/app/models/alchemy/essence_picture.rb @@ -74,8 +74,11 @@ def picture_url(options = {}) def picture_url_options return {} if picture.nil? + crop = crop_values_present? || content.settings[:crop] + { format: picture.default_render_format, + crop: !!crop, crop_from: crop_from.presence, crop_size: crop_size.presence, size: content.settings[:size], diff --git a/spec/models/alchemy/essence_picture_spec.rb b/spec/models/alchemy/essence_picture_spec.rb index b0d3c227b9..f919d6df90 100644 --- a/spec/models/alchemy/essence_picture_spec.rb +++ b/spec/models/alchemy/essence_picture_spec.rb @@ -85,9 +85,8 @@ module Alchemy end context "when crop sizes are present" do - before do - expect(essence).to receive(:crop_size).and_return("200x200") - expect(essence).to receive(:crop_from).and_return("10x10") + let(:essence) do + create(:alchemy_essence_picture, :with_content, picture: picture, crop_size: "200x200", crop_from: "10x10") end it "passes these crop sizes to the picture's url method." do @@ -150,33 +149,39 @@ module Alchemy end context "with crop sizes present" do - before do - expect(essence).to receive(:crop_size) { "200x200" } - expect(essence).to receive(:crop_from) { "10x10" } + let(:essence) do + create(:alchemy_essence_picture, :with_content, picture: picture, crop_size: "200x200", crop_from: "10x10") end it "includes these crop sizes.", :aggregate_failures do expect(picture_url_options[:crop_from]).to eq "10x10" expect(picture_url_options[:crop_size]).to eq "200x200" end + + it "includes {crop: true}" do + expect(picture_url_options[:crop]).to be true + end end # Regression spec for issue #1279 context "with crop sizes being empty strings" do - before do - expect(essence).to receive(:crop_size) { "" } - expect(essence).to receive(:crop_from) { "" } + let(:essence) do + create(:alchemy_essence_picture, :with_content, picture: picture, crop_size: "", crop_from: "") end it "does not include these crop sizes.", :aggregate_failures do expect(picture_url_options[:crop_from]).to be_nil expect(picture_url_options[:crop_size]).to be_nil end + + it "includes {crop: false}" do + expect(picture_url_options[:crop]).to be false + end end context "with content having size setting" do before do - expect(essence.content).to receive(:settings) { {size: "30x70"} } + expect(essence.content).to receive(:settings).twice { {size: "30x70"} } end it "includes this size." do @@ -184,6 +189,16 @@ module Alchemy end end + context "with content having crop setting" do + before do + expect(essence.content).to receive(:settings).twice { {crop: true} } + end + + it "includes this setting" do + expect(picture_url_options[:crop]).to be true + end + end + context "without picture assigned" do let(:picture) { nil }