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 CHANGED
@@ -1,4 +1,4 @@
1
- === NOTE: From version 0.1.3 and above RailsUploads::Engine renamed Rails::Uploads::Engine
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 Rails::Uploads::Engine => '/' # Will be use to generate on the fly missing image presets
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, the path it's relative to the public folder (used to generate the url)
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, the path it's relative to the publica folder (used to generate the url)
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 ::File.exists?(Rails.root.join('public', 'uploads', 'images', 'original', filename)) and Rails.application.config.uploads.presets.has_key?(preset)
8
- RailsUploads::Types::Image.new(filename).send :generate_preset, preset
9
- redirect_to request.url, :cb => Random.rand(100000)
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
@@ -1,5 +1,5 @@
1
1
  RailsUploads::Engine.routes.draw do
2
2
 
3
- match 'uploads/images/:preset/:image' => 'presets#generate', :status => 404
3
+ match 'uploads/images/:preset/:image.:format' => 'presets#generate', :status => 404
4
4
 
5
5
  end
@@ -1,5 +1,5 @@
1
1
  module RailsUploads
2
- class Engine < ::Rails::Engine
2
+ class Engine < Rails::Engine
3
3
 
4
4
  isolate_namespace RailsUploads
5
5
 
@@ -22,7 +22,7 @@ module RailsUploads
22
22
  @options = options
23
23
  end
24
24
 
25
- def has_default?
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
- is_deleted? ? false : ::File.exists?(realpath(*args))
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
- (exists? ? ::File.size(realpath(*args)) : 0)
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('', public_path(*args))
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(Rails.application.config.uploads.base_url, path(*args))
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
- check_store_dir
78
+ create_dir
79
+ @upload.rewind # Hack to avoid empty files
82
80
  ::File.open(destination_path, 'wb') do |file|
83
- file.write(@upload.read)
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 has_default? and is_stored? and exists?
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 check_store_dir(*args)
102
- dir = Rails.root.join('public', 'uploads', store_path(*args))
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
- Rails.root.join('public', public_path(*args))
111
+ base_path.join public_path(*args)
108
112
  end
109
113
 
110
114
  def public_path(*args)
111
- has_default? ? default_path(*args) : ::File.join('uploads', store_path(*args), filename)
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
- (Rails.application.config.uploads.default_presets | (@options[:presets] or []))
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
- check_store_dir(name)
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 :prop, :attachment_content_type => { :in => ['png', 'gif'] }
3
+ # validates :attr, :attachment_content_type => { :in => ['png', 'gif'] }
4
4
 
5
- def validate_each(record, attribute, value)
6
- if !has_default?(record, attribute) and value
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 :prop, :attachment_presence => true
3
+ # validates :attr, :attachment_presence => true
4
4
 
5
5
  def validate_each(record, attribute, value)
6
- if !has_default?(record, attribute) and not (value.kind_of? RailsUploads::Types::File and value.exists?)
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 :prop, :attachment_size => { :in => 0..4.megabytes }
3
+ # validates :attr, :attachment_size => { :in => 0..4.megabytes }
4
4
 
5
5
  def validate_each(record, attribute, value)
6
- if !has_default?(record, attribute) and value
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,5 +1,5 @@
1
1
  module RailsUploads
2
2
 
3
- VERSION = '0.1.4'
3
+ VERSION = '0.1.5'
4
4
 
5
5
  end
@@ -1,4 +1,4 @@
1
1
  class FileUpload < ActiveRecord::Base
2
2
  attr_accessible :file
3
- attached_file :file, :default => 'file.txt'
3
+ attached_file :file, :default => 'default.txt'
4
4
  end
@@ -1,4 +1,4 @@
1
1
  class ImageUpload < ActiveRecord::Base
2
2
  attr_accessible :image
3
- attached_image :image, :presets => [:small, :big], :default => 'assets/image.jpg'
3
+ attached_image :image, :presets => [:small, :big], :default => 'default.jpg'
4
4
  end
@@ -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 => 'file.txt'
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 => 'assets/image.jpg'
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 }