Commit a5b37281bca8497128972d2cc2c12a7a592ecffd

Authored by Rodrigo Souto
1 parent 393b6332

Making the sanitizer an active record extension

app/models/image.rb
@@ -4,7 +4,7 @@ class Image < ActiveRecord::Base @@ -4,7 +4,7 @@ class Image < ActiveRecord::Base
4 Image.attachment_options[:max_size] 4 Image.attachment_options[:max_size]
5 end 5 end
6 6
7 - before_create { |file| file.filename = Environment.verify_filename(file.filename) } 7 + sanitize_filename
8 8
9 has_attachment :content_type => :image, 9 has_attachment :content_type => :image,
10 :storage => :file_system, 10 :storage => :file_system,
app/models/thumbnail.rb
@@ -3,7 +3,7 @@ class Thumbnail < ActiveRecord::Base @@ -3,7 +3,7 @@ class Thumbnail < ActiveRecord::Base
3 :content_type => :image, :max_size => 5.megabytes 3 :content_type => :image, :max_size => 5.megabytes
4 validates_as_attachment 4 validates_as_attachment
5 5
6 - before_create { |file| file.filename = Environment.verify_filename(file.filename) } 6 + sanitize_filename
7 7
8 postgresql_attachment_fu 8 postgresql_attachment_fu
9 end 9 end
app/models/uploaded_file.rb
@@ -18,9 +18,10 @@ class UploadedFile < Article @@ -18,9 +18,10 @@ class UploadedFile < Article
18 18
19 validates_size_of :title, :maximum => 60, :if => (lambda { |file| !file.title.blank? }) 19 validates_size_of :title, :maximum => 60, :if => (lambda { |file| !file.title.blank? })
20 20
  21 + sanitize_filename
  22 +
21 before_create do |uploaded_file| 23 before_create do |uploaded_file|
22 uploaded_file.is_image = true if uploaded_file.image? 24 uploaded_file.is_image = true if uploaded_file.image?
23 - uploaded_file.filename = Environment.verify_filename(uploaded_file.filename)  
24 end 25 end
25 26
26 def thumbnail_path 27 def thumbnail_path
config/initializers/active_record_extensions.rb 0 → 100644
@@ -0,0 +1 @@ @@ -0,0 +1 @@
  1 +require 'upload_sanitizer'
lib/upload_sanitizer.rb 0 → 100644
@@ -0,0 +1,13 @@ @@ -0,0 +1,13 @@
  1 +module UploadSanitizer
  2 + def self.included(base)
  3 + base.extend(ClassMethods)
  4 + end
  5 +
  6 + module ClassMethods
  7 + def sanitize_filename
  8 + before_create { |file| file.filename = Environment.verify_filename(file.filename) }
  9 + end
  10 + end
  11 +end
  12 +
  13 +ActiveRecord::Base.send(:include, UploadSanitizer)