Skip to content

Commit

Permalink
Mutate I18n.load_path only the first time Paperclip::Glue is included (
Browse files Browse the repository at this point in the history
…#117)

* Add test for I18n.load_path mutation

* Only mutate I18n.load_path first time Paperclip::Glue is included
  • Loading branch information
jasonpenny committed Jul 20, 2023
1 parent 8e3cd82 commit 8bdf43f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/paperclip/glue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@

module Paperclip
module Glue
LOCALE_PATHS = Dir.glob("#{File.dirname(__FILE__)}/locales/*.{rb,yml}")

def self.included(base)
base.extend ClassMethods
base.send :include, Callbacks
base.send :include, Validators
base.send :include, Schema if defined? ActiveRecord::Base

locale_path = Dir.glob(File.dirname(__FILE__) + "/locales/*.{rb,yml}")
I18n.load_path += locale_path unless I18n.load_path.include?(locale_path)
I18n.load_path += LOCALE_PATHS unless (LOCALE_PATHS - I18n.load_path).empty?
end
end
end
21 changes: 21 additions & 0 deletions spec/paperclip/glue_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,25 @@
Object.send :remove_const, "NonActiveRecordModel"
end
end

describe "when included" do
it "does not mutate I18n.load_path more than once" do
before_load_path = I18n.load_path
I18n.load_path = []

# expect twice because the load_path is reset after creating the classes
expect(I18n.config).to receive(:load_path=).and_call_original.twice

FirstModel = Class.new
FirstModel.include Paperclip::Glue

SecondModel = Class.new
SecondModel.include Paperclip::Glue

ThirdModel = Class.new
ThirdModel.include Paperclip::Glue

I18n.load_path = before_load_path
end
end
end

0 comments on commit 8bdf43f

Please sign in to comment.