lib/milton/is_uploadable.rb in citrusbyte-milton-0.1.8 vs lib/milton/is_uploadable.rb in citrusbyte-milton-0.1.9
- old
+ new
@@ -4,24 +4,22 @@
def self.included(base)
base.extend IsMethods
end
module IsMethods
- def is_uploadable(options = {})
- raise "Milton's is_uploadable requires a filename column on #{class_name} table" unless column_names.include?("filename")
-
+ def is_uploadable(options = {})
# TODO: implement size validations
# options[:min_size] ||= 1
# options[:max_size] ||= 4.megabytes
# options[:size] ||= (options[:min_size]..options[:max_size])
- options[:tempfile_path] ||= File.join(RAILS_ROOT, "tmp", "milton")
-
ensure_attachment_methods options
-
- UploadableFile.options = AttachableFile.options.merge(options)
+ options[:tempfile_path] ||= File.join(RAILS_ROOT, "tmp", "milton")
+
+ self.milton_options.merge!(options)
+
after_create :save_uploaded_file
extend Citrusbyte::Milton::IsUploadable::ClassMethods
include Citrusbyte::Milton::IsUploadable::InstanceMethods
end
@@ -75,14 +73,14 @@
class UploadableFile < AttachableFile
attr_reader :content_type, :filename, :size
class << self
- def write_to_temp_file(data_or_path)
- FileUtils.mkdir_p(self.options[:tempfile_path]) unless File.exists?(self.options[:tempfile_path])
+ def write_to_temp_file(data_or_path, options)
+ FileUtils.mkdir_p(options[:tempfile_path]) unless File.exists?(options[:tempfile_path])
- tempfile = Tempfile.new("#{rand(Time.now.to_i)}", self.options[:tempfile_path])
+ tempfile = Tempfile.new("#{rand(Time.now.to_i)}", options[:tempfile_path])
if data_or_path.is_a?(StringIO)
tempfile.binmode
tempfile.write data_or_path.read
tempfile.close
@@ -96,12 +94,12 @@
end
def initialize(attachment, data_or_path)
@has_been_saved = false
@content_type = data_or_path.content_type
- @filename = AttachableFile.sanitize_filename(data_or_path.original_filename) if respond_to?(:filename)
- @tempfile = UploadableFile.write_to_temp_file(data_or_path)
+ @filename = AttachableFile.sanitize_filename(data_or_path.original_filename, attachment.class.milton_options) if respond_to?(:filename)
+ @tempfile = UploadableFile.write_to_temp_file(data_or_path, attachment.class.milton_options)
@size = File.size(self.temp_path)
super attachment, filename
end
@@ -111,10 +109,10 @@
def save
return true if self.saved?
recreate_directory
File.cp(temp_path, path)
- File.chmod(self.class.options[:chmod], path)
+ File.chmod(milton_options[:chmod], path)
@has_been_saved = true
end
protected
def temp_path
\ No newline at end of file