Skip to content

Commit

Permalink
Move can_be_cropped_to? into picture_thumbnails
Browse files Browse the repository at this point in the history
This method is the only one left after the PictureVariant and PictureThumb removal.
  • Loading branch information
tvdeyen committed Apr 11, 2022
1 parent 22a26cd commit 27b0385
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 102 deletions.
1 change: 0 additions & 1 deletion app/models/alchemy/picture.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
49 changes: 0 additions & 49 deletions app/models/alchemy/picture/calculations.rb

This file was deleted.

18 changes: 13 additions & 5 deletions app/models/concerns/alchemy/picture_thumbnails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
35 changes: 27 additions & 8 deletions lib/alchemy/test_support/having_picture_thumbnails_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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? }
Expand All @@ -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 }
Expand Down
2 changes: 0 additions & 2 deletions spec/models/alchemy/picture_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
36 changes: 0 additions & 36 deletions spec/support/calculation_examples.rb

This file was deleted.

0 comments on commit 27b0385

Please sign in to comment.