comparison app/models/paste.rb @ 373:6e3e1e7b0212

Handle unique error in model instead of controller.
author nanaya <me@myconan.net>
date Sat, 06 Jun 2015 22:12:56 +0900
parents 96631607785f
children 02903dc8214d
comparison
equal deleted inserted replaced
372:117accbc72c3 373:6e3e1e7b0212
18 begin 18 begin
19 where(:secret => secret).find(id) 19 where(:secret => secret).find(id)
20 rescue ActiveRecord::RecordNotFound 20 rescue ActiveRecord::RecordNotFound
21 nil 21 nil
22 end 22 end
23 end
24
25 def self.graceful_create(params)
26 paste = self.new(params)
27 fresh = true
28 created = true
29
30 begin
31 created = paste.save
32 rescue ActiveRecord::RecordNotUnique
33 paste = self.find_by(:ip => paste.ip, :paste_hash => paste.paste_hash)
34 fresh = false
35 end
36
37 return [created, paste, fresh]
23 end 38 end
24 39
25 def paste_gzip=(paste) 40 def paste_gzip=(paste)
26 self.paste = ActiveSupport::Gzip.decompress paste 41 self.paste = ActiveSupport::Gzip.decompress paste
27 end 42 end