From 27b0385b7c4a5c62b423b92ca0734e06c2b52b8b Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Mon, 11 Apr 2022 21:07:09 +0200 Subject: [PATCH] Move can_be_cropped_to? into picture_thumbnails This method is the only one left after the PictureVariant and PictureThumb removal. --- app/models/alchemy/picture.rb | 1 - app/models/alchemy/picture/calculations.rb | 49 ------------------- .../concerns/alchemy/picture_thumbnails.rb | 18 +++++-- .../having_picture_thumbnails_examples.rb | 35 ++++++++++--- spec/models/alchemy/picture_spec.rb | 2 - spec/rails_helper.rb | 1 - spec/support/calculation_examples.rb | 36 -------------- 7 files changed, 40 insertions(+), 102 deletions(-) delete mode 100644 app/models/alchemy/picture/calculations.rb delete mode 100644 spec/support/calculation_examples.rb diff --git a/app/models/alchemy/picture.rb b/app/models/alchemy/picture.rb index 365e22fbde..2f2c54a284 100644 --- a/app/models/alchemy/picture.rb +++ b/app/models/alchemy/picture.rb @@ -43,7 +43,6 @@ class Picture < BaseRecord include Alchemy::NameConversions include Alchemy::Taggable include Alchemy::TouchElements - include Calculations has_many :essence_pictures, class_name: "Alchemy::EssencePicture", diff --git a/app/models/alchemy/picture/calculations.rb b/app/models/alchemy/picture/calculations.rb deleted file mode 100644 index a922cc5ba6..0000000000 --- a/app/models/alchemy/picture/calculations.rb +++ /dev/null @@ -1,49 +0,0 @@ -# frozen_string_literal: true - -module Alchemy - class Picture < BaseRecord - module Calculations - # An Image smaller than dimensions - # can not be cropped to given size - unless upsample is true. - # - def can_be_cropped_to?(string, upsample = false) - return true if upsample - - is_bigger_than? sizes_from_string(string) - end - - # Returns true if both dimensions of the base image are bigger than the dimensions hash. - # - def is_bigger_than?(dimensions) - image_file_width > dimensions[:width] && image_file_height > dimensions[:height] - end - - # Returns true is one dimension of the base image is smaller than the dimensions hash. - # - def is_smaller_than?(dimensions) - !is_bigger_than?(dimensions) - end - - # Given a string with an x, this function returns a Hash with point - # :width and :height. - # - def sizes_from_string(string) - width, height = string.to_s.split("x", 2).map(&:to_i) - - { - width: width, - height: height, - } - end - - # This function returns the :width and :height of the image file - # as a Hash - def image_size - { - width: image_file_width, - height: image_file_height, - } - end - end - end -end diff --git a/app/models/concerns/alchemy/picture_thumbnails.rb b/app/models/concerns/alchemy/picture_thumbnails.rb index ee2a95a701..79a421cfc8 100644 --- a/app/models/concerns/alchemy/picture_thumbnails.rb +++ b/app/models/concerns/alchemy/picture_thumbnails.rb @@ -102,15 +102,23 @@ def image_cropper_settings # Show image cropping link for content def allow_image_cropping? - settings[:crop] && picture && - picture.can_be_cropped_to?( - settings[:size], - settings[:upsample], - ) && !!picture.image_file.attached? + settings[:crop] && + picture&.image_file&.attached? && + can_be_cropped_to? end private + # An Image smaller than dimensions + # can not be cropped to given size - unless upsample is true. + # + def can_be_cropped_to? + return true if settings[:upsample] + + dimensions = inferred_dimensions_from_string(settings[:size]) + picture.image_file_width > dimensions[0] && picture.image_file_height > dimensions[1] + end + def default_crop_size return nil unless settings[:crop] && settings[:size] diff --git a/lib/alchemy/test_support/having_picture_thumbnails_examples.rb b/lib/alchemy/test_support/having_picture_thumbnails_examples.rb index dee185ecbc..7810258c46 100644 --- a/lib/alchemy/test_support/having_picture_thumbnails_examples.rb +++ b/lib/alchemy/test_support/having_picture_thumbnails_examples.rb @@ -588,10 +588,15 @@ let(:picture) { Alchemy::Picture.new } let(:image_file_width) { 400 } let(:image_file_height) { 300 } + let(:crop_size) { "400x300" } + let(:upsample) { false } before do allow(picture).to receive(:image_file_width) { image_file_width } allow(picture).to receive(:image_file_height) { image_file_height } + allow(record).to receive(:settings) do + { crop: true, size: crop_size, upsample: upsample } + end end subject { record.allow_image_cropping? } @@ -603,23 +608,37 @@ allow(record).to receive(:picture) { picture } end - it { is_expected.to be_falsy } + context "and image smaller or equal to crop size" do + context "if picture.image_file is nil" do + before do + expect(picture.image_file).to receive(:attached?) { false } + end - context "and with image larger than crop size" do - before do - allow(picture).to receive(:can_be_cropped_to?) { true } + it { is_expected.to be_falsy } end + context "if picture.image_file is present" do + before do + expect(picture.image_file).to receive(:attached?) { true } + end + + it { is_expected.to be_falsy } + + context "but with upsample set to true" do + let(:upsample) { true } + + it { is_expected.to be(true) } + end + end + end + + context "and with image larger than crop size" do let(:image_file_width) { 1201 } let(:image_file_height) { 481 } it { is_expected.to be_falsy } context "with crop set to true" do - before do - allow(record).to receive(:settings) { { crop: true } } - end - context "if picture.image_file is nil" do before do expect(picture.image_file).to receive(:attached?) { false } diff --git a/spec/models/alchemy/picture_spec.rb b/spec/models/alchemy/picture_spec.rb index de55bbd63d..13e78fe0a4 100644 --- a/spec/models/alchemy/picture_spec.rb +++ b/spec/models/alchemy/picture_spec.rb @@ -10,8 +10,6 @@ module Alchemy let(:picture) { build(:alchemy_picture, image_file: image_file) } - it_behaves_like "has image calculations" - it "is valid with valid attributes" do expect(picture).to be_valid end diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 6e7f446ecc..c1768412b2 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -28,7 +28,6 @@ require "alchemy/test_support/shared_contexts" require "alchemy/test_support/shared_uploader_examples" -require_relative "support/calculation_examples.rb" require_relative "support/hint_examples.rb" require_relative "support/capybara_helpers.rb" require_relative "support/custom_news_elements_finder" diff --git a/spec/support/calculation_examples.rb b/spec/support/calculation_examples.rb deleted file mode 100644 index eef6c135f9..0000000000 --- a/spec/support/calculation_examples.rb +++ /dev/null @@ -1,36 +0,0 @@ -# frozen_string_literal: true - -require "rails_helper" - -module Alchemy - shared_examples_for "has image calculations" do - describe "#can_be_cropped_to?" do - context "picture is 300x400 and shall be cropped to 200x100" do - it "should return true" do - allow(picture).to receive(:image_file_width) { 400 } - allow(picture).to receive(:image_file_height) { 300 } - - expect(picture.can_be_cropped_to?("200x100")).to be(true) - end - end - - context "picture is 300x400 and shall be cropped to 600x500" do - it "should return false" do - allow(picture).to receive(:image_file_width) { 400 } - allow(picture).to receive(:image_file_height) { 300 } - - expect(picture.can_be_cropped_to?("600x500")).to be(false) - end - end - - context "picture is 300x400 and shall be cropped to 600x500 with upsample set to true" do - it "should return true" do - allow(picture).to receive(:image_file_width) { 400 } - allow(picture).to receive(:image_file_height) { 300 } - - expect(picture.can_be_cropped_to?("600x500", true)).to be(true) - end - end - end - end -end