rails_uploads 0.1.4 → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +4 -4
- data/app/controllers/rails_uploads/presets_controller.rb +8 -3
- data/config/routes.rb +1 -1
- data/lib/rails_uploads/engine.rb +1 -1
- data/lib/rails_uploads/types/file.rb +20 -16
- data/lib/rails_uploads/types/image.rb +2 -6
- data/lib/rails_uploads/validators/attachment_content_type_validator.rb +3 -3
- data/lib/rails_uploads/validators/attachment_presence_validator.rb +4 -4
- data/lib/rails_uploads/validators/attachment_size_validator.rb +2 -2
- data/lib/rails_uploads/validators/base.rb +0 -5
- data/lib/rails_uploads/version.rb +1 -1
- data/test/dummy/app/models/file_upload.rb +1 -1
- data/test/dummy/app/models/image_upload.rb +1 -1
- data/test/dummy/app/models/validation_upload.rb +2 -2
- data/test/dummy/log/test.log +3258 -0
- data/test/dummy/public/uploads/files/default.txt +1 -0
- data/test/dummy/public/uploads/files/file.txt +1 -0
- data/test/dummy/public/uploads/images/original/default.jpg +0 -0
- data/test/dummy/public/uploads/images/original/image.jpg +0 -0
- data/test/file_string_test.rb +6 -5
- data/test/file_upload_test.rb +5 -5
- data/test/{controller_test.rb → generate_test.rb} +3 -3
- data/test/image_presets_test.rb +4 -4
- data/test/image_string_test.rb +10 -10
- data/test/image_upload_test.rb +4 -4
- data/test/rails_uploads_test.rb +1 -1
- data/test/records_test.rb +10 -10
- data/test/validators_test.rb +4 -4
- metadata +12 -4
data/README.rdoc
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
=== NOTE: From version 0.1.
|
1
|
+
=== NOTE: From version 0.1.5 and above defaults must be put in public/uploads folder
|
2
2
|
|
3
3
|
---
|
4
4
|
|
@@ -19,7 +19,7 @@ Then bundle:
|
|
19
19
|
= Usage
|
20
20
|
|
21
21
|
Mount the engine at the end of you routes.rb:
|
22
|
-
mount
|
22
|
+
mount RailsUploads::Engine => '/' # Will be use to generate on the fly missing image presets
|
23
23
|
|
24
24
|
Add the column to your table (just a string):
|
25
25
|
create_table :models do |t|
|
@@ -29,13 +29,13 @@ Add the column to your table (just a string):
|
|
29
29
|
If you need a file:
|
30
30
|
class Model < ActiveRecord::Base
|
31
31
|
attr_accessible :prop
|
32
|
-
attached_file :prop, :default => 'file.txt' # It's optional set a default file
|
32
|
+
attached_file :prop, :default => 'file.txt' # It's optional set a default file
|
33
33
|
end
|
34
34
|
|
35
35
|
If you need a image:
|
36
36
|
class Model < ActiveRecord::Base
|
37
37
|
attr_accessible :prop
|
38
|
-
attached_image :prop, :presets => [:small, :big], :default => 'assets/image.jpg' # It's optional set a default image
|
38
|
+
attached_image :prop, :presets => [:small, :big], :default => 'assets/image.jpg' # It's optional set a default image
|
39
39
|
end
|
40
40
|
|
41
41
|
Define presets in your application.rb
|
@@ -1,12 +1,17 @@
|
|
1
1
|
module RailsUploads
|
2
2
|
class PresetsController < ApplicationController
|
3
3
|
|
4
|
+
layout false
|
5
|
+
|
4
6
|
def generate
|
5
7
|
filename = "#{params[:image]}.#{params[:format]}"
|
6
8
|
preset = params[:preset].gsub('-', '_').to_sym
|
7
|
-
if
|
8
|
-
RailsUploads::Types::Image.new(filename)
|
9
|
-
|
9
|
+
if Rails.application.config.uploads.presets.has_key?(preset)
|
10
|
+
image = RailsUploads::Types::Image.new(filename)
|
11
|
+
if image.exists? and !image.exists?(preset)
|
12
|
+
image.send :generate_preset, preset
|
13
|
+
redirect_to request.url, :cb => Random.rand(100000) and return
|
14
|
+
end
|
10
15
|
end
|
11
16
|
end
|
12
17
|
|
data/config/routes.rb
CHANGED
data/lib/rails_uploads/engine.rb
CHANGED
@@ -22,7 +22,7 @@ module RailsUploads
|
|
22
22
|
@options = options
|
23
23
|
end
|
24
24
|
|
25
|
-
def
|
25
|
+
def is_default?
|
26
26
|
@default
|
27
27
|
end
|
28
28
|
|
@@ -38,17 +38,14 @@ module RailsUploads
|
|
38
38
|
@deleted
|
39
39
|
end
|
40
40
|
|
41
|
-
def default_path(*args)
|
42
|
-
@options[:default]
|
43
|
-
end
|
44
|
-
|
45
41
|
def exists?(*args)
|
46
|
-
|
42
|
+
return false if is_deleted?
|
43
|
+
::File.exists? realpath(*args)
|
47
44
|
end
|
48
45
|
|
49
46
|
def size(*args)
|
50
47
|
return 0 if is_deleted?
|
51
|
-
|
48
|
+
::File.size realpath(*args)
|
52
49
|
end
|
53
50
|
|
54
51
|
def extname
|
@@ -63,12 +60,12 @@ module RailsUploads
|
|
63
60
|
|
64
61
|
def path(*args)
|
65
62
|
return nil if is_deleted?
|
66
|
-
::File.join
|
63
|
+
::File.join '', public_path(*args)
|
67
64
|
end
|
68
65
|
|
69
66
|
def url(*args)
|
70
67
|
return nil if is_deleted?
|
71
|
-
::File.join
|
68
|
+
::File.join Rails.application.config.uploads.base_url, path(*args)
|
72
69
|
end
|
73
70
|
|
74
71
|
def realpath(*args)
|
@@ -78,9 +75,12 @@ module RailsUploads
|
|
78
75
|
|
79
76
|
def store
|
80
77
|
if not is_stored? and is_upload?
|
81
|
-
|
78
|
+
create_dir
|
79
|
+
@upload.rewind # Hack to avoid empty files
|
82
80
|
::File.open(destination_path, 'wb') do |file|
|
83
|
-
|
81
|
+
while chunk = @upload.read(16 * 1024)
|
82
|
+
file.write(chunk)
|
83
|
+
end
|
84
84
|
end
|
85
85
|
@stored = true
|
86
86
|
yield if block_given?
|
@@ -88,7 +88,7 @@ module RailsUploads
|
|
88
88
|
end
|
89
89
|
|
90
90
|
def delete
|
91
|
-
if not
|
91
|
+
if not is_default? and is_stored? and exists?
|
92
92
|
::File.delete realpath
|
93
93
|
yield if block_given?
|
94
94
|
@stored = false
|
@@ -98,17 +98,21 @@ module RailsUploads
|
|
98
98
|
|
99
99
|
protected
|
100
100
|
|
101
|
-
def
|
102
|
-
|
101
|
+
def base_path
|
102
|
+
Rails.root.join (Rails.env == 'test' and not is_default?) ? 'tmp' : 'public'
|
103
|
+
end
|
104
|
+
|
105
|
+
def create_dir(*args)
|
106
|
+
dir = base_path.join('uploads', store_path(*args))
|
103
107
|
FileUtils.mkdir_p dir unless ::File.directory? dir
|
104
108
|
end
|
105
109
|
|
106
110
|
def destination_path(*args)
|
107
|
-
|
111
|
+
base_path.join public_path(*args)
|
108
112
|
end
|
109
113
|
|
110
114
|
def public_path(*args)
|
111
|
-
|
115
|
+
::File.join 'uploads', store_path(*args), filename
|
112
116
|
end
|
113
117
|
|
114
118
|
def store_path(*args)
|
@@ -7,11 +7,7 @@ module RailsUploads
|
|
7
7
|
end
|
8
8
|
|
9
9
|
def presets
|
10
|
-
|
11
|
-
end
|
12
|
-
|
13
|
-
def default_path(*args)
|
14
|
-
args.any? ? super().reverse.sub('.', "-#{args[0].to_s.gsub('_', '-')}.".reverse).reverse : super()
|
10
|
+
Rails.application.config.uploads.default_presets | (@options[:presets] or [])
|
15
11
|
end
|
16
12
|
|
17
13
|
def store
|
@@ -37,7 +33,7 @@ module RailsUploads
|
|
37
33
|
protected
|
38
34
|
|
39
35
|
def generate_preset(name)
|
40
|
-
|
36
|
+
create_dir(name)
|
41
37
|
image = RailsUploads::Magick::Image.new(realpath, realpath(name))
|
42
38
|
settings = Rails.application.config.uploads.presets[name]
|
43
39
|
if settings.is_a? Proc
|
@@ -1,9 +1,9 @@
|
|
1
1
|
class AttachmentContentTypeValidator < RailsUploads::Validators::Base
|
2
2
|
|
3
|
-
# validates :
|
3
|
+
# validates :attr, :attachment_content_type => { :in => ['png', 'gif'] }
|
4
4
|
|
5
|
-
def validate_each(record, attribute, value)
|
6
|
-
if
|
5
|
+
def validate_each(record, attribute, value)
|
6
|
+
if value.present? and not value.is_default?
|
7
7
|
unless options[:in].include? value.extname.from(1)
|
8
8
|
add_error record, attribute, 'errors.messages.attachment_content_type', :types => options[:in].join(', ')
|
9
9
|
end
|
@@ -1,11 +1,11 @@
|
|
1
1
|
class AttachmentPresenceValidator < RailsUploads::Validators::Base
|
2
2
|
|
3
|
-
# validates :
|
3
|
+
# validates :attr, :attachment_presence => true
|
4
4
|
|
5
5
|
def validate_each(record, attribute, value)
|
6
|
-
|
7
|
-
add_error record, attribute, 'errors.messages.attachment_presence'
|
8
|
-
end
|
6
|
+
unless value.present? and value.exists?
|
7
|
+
add_error record, attribute, 'errors.messages.attachment_presence'
|
8
|
+
end
|
9
9
|
end
|
10
10
|
|
11
11
|
end
|
@@ -1,9 +1,9 @@
|
|
1
1
|
class AttachmentSizeValidator < RailsUploads::Validators::Base
|
2
2
|
|
3
|
-
# validates :
|
3
|
+
# validates :attr, :attachment_size => { :in => 0..4.megabytes }
|
4
4
|
|
5
5
|
def validate_each(record, attribute, value)
|
6
|
-
if
|
6
|
+
if value.present? and not value.is_default?
|
7
7
|
if options.has_key? :in
|
8
8
|
unless options[:in].include? value.size
|
9
9
|
add_error record, attribute, 'attachment_size_in', :greater_than => options[:in].begin, :less_than => options[:in].end
|
@@ -3,11 +3,6 @@ module RailsUploads
|
|
3
3
|
class Base < ActiveModel::EachValidator
|
4
4
|
|
5
5
|
protected
|
6
|
-
|
7
|
-
def has_default?(record, attribute)
|
8
|
-
options = record.class.instance_variable_get('@attachments')[attribute]
|
9
|
-
options.has_key?(:default)
|
10
|
-
end
|
11
6
|
|
12
7
|
def add_error(record, attribute, type, options={})
|
13
8
|
record.errors[attribute] << (options[:message] || I18n.t(type, options))
|
@@ -1,9 +1,9 @@
|
|
1
1
|
class ValidationUpload < ActiveRecord::Base
|
2
2
|
attr_accessible :file_presence, :file_content_type, :file_size, :file_all, :image_presence, :image_content_type, :image_size, :image_all
|
3
3
|
attached_file :file_presence, :file_content_type, :file_size, :file_all
|
4
|
-
attached_file :file_default, :default => '
|
4
|
+
attached_file :file_default, :default => 'default.txt'
|
5
5
|
attached_image :image_presence, :image_content_type, :image_size, :image_all, :presets => [:big]
|
6
|
-
attached_image :image_default, :presets => [:big, :small], :default => '
|
6
|
+
attached_image :image_default, :presets => [:big, :small], :default => 'default.jpg'
|
7
7
|
validates :file_presence, :attachment_presence => true
|
8
8
|
validates :file_content_type, :attachment_content_type => { :in => ['txt'] }
|
9
9
|
validates :file_size, :attachment_size => { :less_than => 15.kilobytes, :greater_than => 10.bytes }
|