diff --git a/lib/mongoid_paperclip.rb b/lib/mongoid_paperclip.rb index 90787c5..67c8be2 100644 --- a/lib/mongoid_paperclip.rb +++ b/lib/mongoid_paperclip.rb @@ -6,8 +6,8 @@ case id = attachment.instance.id when Integer ("%09d".freeze % id).scan(/\d{3}/).join("/".freeze) - when String - id.scan(/.{4}/).join("/".freeze) + when String, BSON::ObjectId + id.to_s.scan(/.{4}/).join("/".freeze) else nil end diff --git a/spec/mongoid-paperclip_spec.rb b/spec/mongoid-paperclip_spec.rb index 626eb14..99cdaa4 100644 --- a/spec/mongoid-paperclip_spec.rb +++ b/spec/mongoid-paperclip_spec.rb @@ -27,6 +27,12 @@ it "stores fingerprint" do expect(user.avatar_fingerprint).to eq("2584a801e588b3fcf4aa074efff77e30") end + + it "interpolates path and url properly" do + id_partition = user.id.to_s.scan(/.{4}/).join("/") + expect(user.avatar.url).to eq("/system/users/#{id_partition}/avatar-original.png?#{Time.now.to_i}") + expect(user.avatar.path).to eq("#{__dir__}/public/system/users/#{id_partition}/avatar-original.png") + end end describe "multiple attachments" do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 6021228..09cff1e 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -22,7 +22,7 @@ class User include Mongoid::Document include Mongoid::Paperclip - has_mongoid_attached_file :avatar + has_mongoid_attached_file :avatar, url: "/system/:class/:id_partition/:basename-:style.:extension" validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\Z/ end