rails_uploads 0.2.1 → 0.2.2

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
@@ -22,24 +22,24 @@ ImageMagick must be install, you can install it with homebrew:
22
22
 
23
23
  = Usage
24
24
 
25
- Mount the engine at the end of you routes.rb:
25
+ Mount the engine at the end of you routes.rb (local storage only):
26
26
  mount RailsUploads::Engine => '/' # Will be use to generate on the fly missing image presets
27
27
 
28
28
  Add the column to your table (just a string):
29
29
  create_table :models do |t|
30
- t.string :prop
30
+ t.string :attr
31
31
  end
32
32
 
33
33
  If you need a file:
34
34
  class Model < ActiveRecord::Base
35
- attr_accessible :prop
36
- attached_file :prop, :default => 'file.txt' # It's optional set a default file
35
+ attr_accessible :attr
36
+ attached_file :attr, :default => 'file.txt'
37
37
  end
38
38
 
39
39
  If you need a image:
40
40
  class Model < ActiveRecord::Base
41
- attr_accessible :prop
42
- attached_image :prop, :presets => [:small, :big], :default => 'assets/image.jpg' # It's optional set a default image
41
+ attr_accessible :attr
42
+ attached_image :attr, :presets => [:small, :big], :default => 'assets/image.jpg'
43
43
  end
44
44
 
45
45
  Define presets in your application.rb
@@ -51,27 +51,14 @@ Define presets in your application.rb
51
51
  config.uploads.default_presets = [:small] # Define the default presets for all models with attached images
52
52
  config.uploads.storage = :local # The default it's local, you can use :s3 as well
53
53
 
54
- If you want to use S3 create a s3.yml file in your config directory like this:
55
- development:
56
- bucket: development-bucket
57
- access_key_id: development_access_key_id
58
- secret_access_key: development_secret_access_key
59
-
60
- test:
61
- bucket: test-bucket
62
- access_key_id: test_access_key_id
63
- secret_access_key: test_secret_access_key
64
-
65
- production:
66
- bucket: production-bucket
67
- access_key_id: production_access_key_id
68
- secret_access_key: production_secret_access_key
54
+ If you want to use S3 create a s3.yml config file with this generator and complete it:
55
+ rails g uploads:s3:config
69
56
 
70
57
  The validation works very similar to paperclip:
71
58
  class Model < ActiveRecord::Base
72
- attr_accessible :prop
73
- attached_file :prop
74
- validates :prop, :attachment_presence => true, :attachment_size => { :in => 0..4.megabytes }, :attachment_content_type => { :in => ['txt'] }
59
+ attr_accessible :attr
60
+ attached_file :attr
61
+ validates :attr, :attachment_presence => true, :attachment_size => { :in => 0..4.megabytes }, :attachment_content_type => { :in => ['txt'] }
75
62
  end
76
63
 
77
64
  If you want to translate the errores the keys are:
@@ -87,24 +74,29 @@ In your views:
87
74
  a{ :href => record.image.url(:big) } # To get the a thumb
88
75
 
89
76
  In your forms:
90
- = f.file_field :prop
77
+ = f.file_field :attr
91
78
 
92
79
  = FAQ
93
80
 
94
81
  == How can I use a cdn with this plugin?
95
82
 
96
- Just define a base url in your application.rb:
83
+ Define a base url in your application.rb:
97
84
  config.uploads.base_url = 'http://cdn.example.com'
98
85
 
99
- == Can I automatically create buckets?
86
+ == How can I automatically create buckets?
100
87
 
101
- Yes, use this rake task after create the s3.yml file:
88
+ Use this rake task after create the s3.yml file:
102
89
  rake uploads:s3:buckets:create
103
90
 
104
91
  == How can I clean a preset?
105
92
 
106
- Just remove the corresponding folder in uploads/images manually or with this rake task:
107
- rake uploads:preset:clean NAME=preset
93
+ Use this rake task:
94
+ rake uploads:preset:clean MODEL=models PRESETS=first_preset,second_preset
95
+
96
+ == How can I refresh a preset?
97
+
98
+ Use this rake task:
99
+ rake uploads:preset:refresh MODEL=models PRESETS=first_preset,second_preset
108
100
 
109
101
  == How to migrate from versions before 0.1.0?
110
102
 
@@ -8,7 +8,7 @@ module RailsUploads
8
8
  image = RailsUploads::Types::Image.new(filename)
9
9
  if image.exists? and !image.exists?(preset)
10
10
  image.send :generate_preset, preset
11
- redirect_to request.url, :cb => Random.rand(100000)
11
+ redirect_to image.url(params[:preset]).to_s, :cb => Random.rand(100000)
12
12
  end
13
13
  end
14
14
  end
@@ -0,0 +1,13 @@
1
+ module Uploads
2
+ module S3
3
+ class ConfigGenerator < Rails::Generators::Base
4
+
5
+ source_root File.expand_path('../templates', __FILE__)
6
+
7
+ def install
8
+ copy_file 's3.yml', 'config/s3.yml'
9
+ end
10
+
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,16 @@
1
+
2
+ development:
3
+ bucket: development-bucket
4
+ access_key_id: development_access_key_id
5
+ secret_access_key: development_secret_access_key
6
+
7
+ test:
8
+ bucket: test-bucket
9
+ access_key_id: test_access_key_id
10
+ secret_access_key: test_secret_access_key
11
+
12
+ production:
13
+ bucket: production-bucket
14
+ access_key_id: production_access_key_id
15
+ secret_access_key: production_secret_access_key
16
+
@@ -16,7 +16,7 @@ module RailsUploads
16
16
  def check_changed_attachments
17
17
  @stored_attachments = []
18
18
  @deleted_attachments = []
19
- self.class.send(:attachments).each do |attr, options|
19
+ self.class.attachments.each do |attr, options|
20
20
  if changed_attributes.has_key? attr.to_s
21
21
  stored = attributes[attr.to_s]
22
22
  deleted = changed_attributes[attr.to_s]
@@ -27,7 +27,7 @@ module RailsUploads
27
27
  end
28
28
 
29
29
  def iterate_attachments
30
- self.class.send(:attachments).each do |attr, options|
30
+ self.class.attachments.each do |attr, options|
31
31
  next unless instance = send(attr)
32
32
  yield instance
33
33
  end
@@ -54,6 +54,8 @@ module RailsUploads
54
54
  end
55
55
 
56
56
  module ClassMethods
57
+
58
+ attr_reader :attachments
57
59
 
58
60
  def self.extended(base)
59
61
  [:image].each do |type|
@@ -76,8 +78,6 @@ module RailsUploads
76
78
  end
77
79
 
78
80
  protected
79
-
80
- attr_reader :attachments
81
81
 
82
82
  def define_attachment(*args)
83
83
  options = args.extract_options!
@@ -10,9 +10,9 @@ module RailsUploads
10
10
  initializer 'rails_uploads' do
11
11
  ::ActiveRecord::Base.send :include, RailsUploads::ActiveRecord::Base
12
12
  if config.uploads.storage == :s3
13
- require 'aws-sdk'
13
+ require 'aws-sdk'
14
14
  RailsUploads::Storages::S3.config = YAML.load_file(Rails.root.join('config', 's3.yml'))
15
- end
15
+ end
16
16
  end
17
17
 
18
18
  end
@@ -2,8 +2,8 @@ module RailsUploads
2
2
  module Storages
3
3
  class Local
4
4
 
5
- def initialize(tmp)
6
- @tmp = tmp
5
+ def initialize(default)
6
+ @tmp = (Rails.env == 'test' and not default)
7
7
  end
8
8
 
9
9
  def exists?(path)
@@ -5,7 +5,14 @@ module RailsUploads
5
5
  cattr_accessor :config
6
6
 
7
7
  def initialize(default)
8
- config = self.class.config[default ? Rails.env : 'production']
8
+ if (Rails.env == 'test' and not default)
9
+ env = 'test'
10
+ elsif default
11
+ env = 'production'
12
+ else
13
+ env = Rails.env
14
+ end
15
+ config = self.class.config[env]
9
16
  AWS.config access_key_id: config['access_key_id'], secret_access_key: config['secret_access_key']
10
17
  @bucket = AWS::S3.new.buckets[config['bucket']]
11
18
  @tmp = {}
@@ -37,15 +44,15 @@ module RailsUploads
37
44
  if upload.present?
38
45
  @tmp[source] = upload.path
39
46
  else
40
- tmp = Tempfile.open('s3', Rails.root.join('tmp')) do |file|
41
- object(source).read do |chunk|
42
- file.write(chunk)
43
- end
44
- end
45
- @tmp[source] = tmp.path
47
+ remote = Tempfile.new('s3')
48
+ remote.binmode
49
+ object(source).read { |chunk| remote.write(chunk) }
50
+ remote.close
51
+ remote.open
52
+ @tmp[source] = remote.path
46
53
  end
47
54
  end
48
- tmp = Tempfile.new('s3', Rails.root.join('tmp'))
55
+ tmp = Tempfile.new('s3')
49
56
  yield RailsUploads::Magick::Image.new(@tmp[source], tmp.path)
50
57
  store tmp, output
51
58
  end
@@ -25,6 +25,10 @@ module RailsUploads
25
25
  @options = options
26
26
  end
27
27
 
28
+ def is_default?
29
+ @default
30
+ end
31
+
28
32
  def exists?(*args)
29
33
  return false if is_deleted?
30
34
  storage.exists? path(*args)
@@ -78,19 +82,14 @@ module RailsUploads
78
82
  protected
79
83
 
80
84
  def build_storage(type=nil)
81
- tmp = (Rails.env == 'test' and not is_default?)
82
85
  type = (type or Rails.application.config.uploads.storage)
83
- RailsUploads::Storages.const_get(type.to_s.classify).new(tmp)
86
+ RailsUploads::Storages.const_get(type.to_s.classify).new(is_default?)
84
87
  end
85
88
 
86
89
  def storage
87
90
  @storage
88
91
  end
89
92
 
90
- def is_default?
91
- @default
92
- end
93
-
94
93
  def is_upload?
95
94
  @upload != false
96
95
  end
@@ -13,8 +13,6 @@ module RailsUploads
13
13
  def delete
14
14
  super { presets.each { |name| storage.delete path(name) if exists?(name) } if presets.any? }
15
15
  end
16
-
17
- protected
18
16
 
19
17
  def generate_preset(name)
20
18
  storage.magick destination_path, destination_path(name), @upload do |image|
@@ -32,6 +30,12 @@ module RailsUploads
32
30
  end
33
31
  end
34
32
 
33
+ def delete_preset(name)
34
+ storage.delete path(name)
35
+ end
36
+
37
+ protected
38
+
35
39
  def store_path(*args)
36
40
  ::File.join('images', (args[0] ? args[0].to_s.gsub('_', '-') : 'original'))
37
41
  end
@@ -1,7 +1,7 @@
1
1
  class AttachmentContentTypeValidator < RailsUploads::Validators::Base
2
2
 
3
3
  def validate_each(record, attribute, value)
4
- if value.present? and not value.send(:is_default?)
4
+ if value.present? and not value.is_default?
5
5
  unless options[:in].include? value.extname.from(1)
6
6
  add_error record, attribute, 'errors.messages.attachment_content_type', types: options[:in].join(', ')
7
7
  end
@@ -1,7 +1,7 @@
1
1
  class AttachmentSizeValidator < RailsUploads::Validators::Base
2
2
 
3
3
  def validate_each(record, attribute, value)
4
- if value.present? and not value.send(:is_default?)
4
+ if value.present? and not value.is_default?
5
5
  if options.has_key? :in
6
6
  unless options[:in].include? value.size
7
7
  add_error record, attribute, 'attachment_size_in', greater_than: options[:in].begin, less_than: options[:in].end
@@ -1,5 +1,5 @@
1
1
  module RailsUploads
2
2
 
3
- VERSION = '0.2.1'
3
+ VERSION = '0.2.2'
4
4
 
5
5
  end
@@ -1,4 +1,25 @@
1
1
 
2
+ module RailsUploads
3
+ module Task
4
+
5
+ def self.iterate_images
6
+ model = ENV['MODEL'].classify.constantize
7
+ presets = ENV['PRESETS'].split(',').map(&:to_sym)
8
+ model.find_each do |record|
9
+ model.attachments.each do |attr, options|
10
+ if options[:type] == :image
11
+ presets.each do |preset|
12
+ image = record.send(attr)
13
+ yield image, preset
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+
20
+ end
21
+ end
22
+
2
23
  namespace :uploads do
3
24
 
4
25
  task :migrate do
@@ -19,12 +40,21 @@ namespace :uploads do
19
40
  end
20
41
 
21
42
  namespace :presets do
43
+ desc 'Refresh preset'
44
+ task :refresh => :clean do
45
+ RailsUploads::Task.iterate_images do |image, preset|
46
+ puts "Generating preset #{image.url(preset)}."
47
+ image.generate_preset preset
48
+ end
49
+ puts "Presets refreshed successfully."
50
+ end
22
51
  desc 'Clean preset'
23
- task :clean, [:preset] do
24
- if ENV['NAME'].present?
25
- path = Rails.root.join('public', 'uploads', 'images', ENV['NAME'])
26
- ::FileUtils.rm_rf path if ::File.directory? path
52
+ task :clean => :environment do
53
+ RailsUploads::Task.iterate_images do |image, preset|
54
+ puts "Deleting preset #{image.url(preset)}."
55
+ image.delete_preset preset
27
56
  end
57
+ puts "Presets cleaned successfully."
28
58
  end
29
59
  end
30
60
 
@@ -20,3 +20,4 @@ Migrating to CreateImageUploads (20130124013431)
20
20
   (3.1ms) commit transaction
21
21
   (0.1ms) SELECT "schema_migrations"."version" FROM "schema_migrations" 
22
22
  Connecting to database specified by database.yml
23
+ Connecting to database specified by database.yml