From 1d2cb0363d95db69ed588b820d57d4b5a8577019 Mon Sep 17 00:00:00 2001 From: Yohta Kimura Date: Wed, 5 Jul 2023 07:18:02 +0900 Subject: [PATCH] lately resolve active versions when retrieving --- lib/carrierwave/uploader/versions.rb | 6 +++--- spec/uploader/versions_spec.rb | 28 ++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/lib/carrierwave/uploader/versions.rb b/lib/carrierwave/uploader/versions.rb index b233ce9cb..dd9b1a99c 100644 --- a/lib/carrierwave/uploader/versions.rb +++ b/lib/carrierwave/uploader/versions.rb @@ -133,7 +133,7 @@ def version(name, options = {}, &block) class_eval <<-RUBY, __FILE__, __LINE__ + 1 def #{name} - versions[:#{name}] + version_exists?(:#{name}) ? versions[:#{name}] : versions[:#{name}].class.new(model, mounted_as) end RUBY @@ -325,11 +325,11 @@ def remove_versions! end def retrieve_versions_from_cache!(cache_name) - active_versions.each { |name, v| v.retrieve_from_cache!(cache_name) } + versions.each { |name, v| v.retrieve_from_cache!(cache_name) } end def retrieve_versions_from_store!(identifier) - active_versions.each { |name, v| v.retrieve_from_store!(identifier) } + versions.each { |name, v| v.retrieve_from_store!(identifier) } end end # Versions diff --git a/spec/uploader/versions_spec.rb b/spec/uploader/versions_spec.rb index 1c77c8549..53f1ff57f 100644 --- a/spec/uploader/versions_spec.rb +++ b/spec/uploader/versions_spec.rb @@ -449,6 +449,13 @@ def move_to_cache expect(@uploader.thumb).to be_present expect(@uploader.preview).to be_blank end + + it "should evaluate the condition even the version is not called" do + @uploader_class.version(:preview, if: lambda{|record, args| record.false?(args[:file])}) + expect(@uploader).to receive(:false?).at_least(:once).and_return(false) + @uploader.store!(@file) + expect(@uploader.thumb).to be_present + end end context "when there is an 'unless' option" do @@ -483,6 +490,13 @@ def move_to_cache expect(@uploader.thumb).to be_present expect(@uploader.preview).to be_present end + + it "should evaluate the condition even the version is not called" do + @uploader_class.version(:preview, unless: lambda{|record, args| record.false?(args[:file])}) + expect(@uploader).to receive(:false?).at_least(:once).and_return(false) + @uploader.store!(@file) + expect(@uploader.thumb).to be_present + end end it "should not cache file twice when store! called with a file" do @@ -739,6 +753,13 @@ def shorten expect(@uploader.thumb).to be_present expect(@uploader.preview).to be_blank end + + it "should not evaluate the condition until version is called" do + @uploader_class.version(:preview, if: :false?) + expect(@uploader).not_to receive(:false?) + @uploader.retrieve_from_store!(@file) + expect(@uploader.thumb).to be_present + end end context "when there is an 'unless' option" do @@ -757,6 +778,13 @@ def shorten expect(@uploader.thumb).to be_present expect(@uploader.preview).to be_present end + + it "should not evaluate the condition until version is called" do + @uploader_class.version(:preview, unless: :false?) + expect(@uploader).not_to receive(:false?) + @uploader.retrieve_from_store!(@file) + expect(@uploader.thumb).to be_present + end end end end