rails_uploads 0.2.9 → 0.3.0
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.
- checksums.yaml +7 -0
- metadata +19 -371
- data/MIT-LICENSE +0 -20
- data/README.rdoc +0 -108
- data/Rakefile +0 -40
- data/app/controllers/rails_uploads/presets_controller.rb +0 -17
- data/config/locales/en.yml +0 -8
- data/config/locales/es.yml +0 -8
- data/config/routes.rb +0 -5
- data/lib/generators/uploads/s3/config_generator.rb +0 -13
- data/lib/generators/uploads/s3/templates/s3.yml +0 -16
- data/lib/rails_uploads.rb +0 -16
- data/lib/rails_uploads/active_record/base.rb +0 -139
- data/lib/rails_uploads/engine.rb +0 -7
- data/lib/rails_uploads/magick/image.rb +0 -83
- data/lib/rails_uploads/railtie.rb +0 -20
- data/lib/rails_uploads/storages/local.rb +0 -57
- data/lib/rails_uploads/storages/s3.rb +0 -68
- data/lib/rails_uploads/types/file.rb +0 -115
- data/lib/rails_uploads/types/image.rb +0 -45
- data/lib/rails_uploads/validators/attachment_content_type_validator.rb +0 -11
- data/lib/rails_uploads/validators/attachment_presence_validator.rb +0 -9
- data/lib/rails_uploads/validators/attachment_size_validator.rb +0 -27
- data/lib/rails_uploads/validators/base.rb +0 -13
- data/lib/rails_uploads/version.rb +0 -5
- data/lib/tasks/rails_uploads_tasks.rake +0 -89
- data/test/dummy/README.rdoc +0 -261
- data/test/dummy/Rakefile +0 -7
- data/test/dummy/app/assets/images/image.jpg +0 -0
- data/test/dummy/app/assets/javascripts/application.js +0 -15
- data/test/dummy/app/assets/stylesheets/application.css +0 -13
- data/test/dummy/app/controllers/application_controller.rb +0 -3
- data/test/dummy/app/helpers/application_helper.rb +0 -2
- data/test/dummy/app/models/file_upload.rb +0 -4
- data/test/dummy/app/models/image_upload.rb +0 -4
- data/test/dummy/app/models/validation_upload.rb +0 -17
- data/test/dummy/app/views/layouts/application.html.erb +0 -14
- data/test/dummy/config.ru +0 -4
- data/test/dummy/config/application.rb +0 -65
- data/test/dummy/config/boot.rb +0 -10
- data/test/dummy/config/database.yml +0 -25
- data/test/dummy/config/environment.rb +0 -5
- data/test/dummy/config/environments/development.rb +0 -37
- data/test/dummy/config/environments/production.rb +0 -67
- data/test/dummy/config/environments/test.rb +0 -37
- data/test/dummy/config/initializers/backtrace_silencers.rb +0 -7
- data/test/dummy/config/initializers/inflections.rb +0 -15
- data/test/dummy/config/initializers/mime_types.rb +0 -5
- data/test/dummy/config/initializers/secret_token.rb +0 -7
- data/test/dummy/config/initializers/session_store.rb +0 -8
- data/test/dummy/config/initializers/wrap_parameters.rb +0 -14
- data/test/dummy/config/locales/en.yml +0 -5
- data/test/dummy/config/routes.rb +0 -5
- data/test/dummy/config/s3.yml +0 -15
- data/test/dummy/db/development.sqlite3 +0 -0
- data/test/dummy/db/migrate/20130108201834_create_file_uploads.rb +0 -10
- data/test/dummy/db/migrate/20130110194721_create_validation_uploads.rb +0 -19
- data/test/dummy/db/migrate/20130124013431_create_image_uploads.rb +0 -10
- data/test/dummy/db/schema.rb +0 -43
- data/test/dummy/db/test.sqlite3 +0 -0
- data/test/dummy/log/development.log +0 -23
- data/test/dummy/log/test.log +0 -25090
- data/test/dummy/public/404.html +0 -26
- data/test/dummy/public/422.html +0 -26
- data/test/dummy/public/500.html +0 -25
- data/test/dummy/public/favicon.ico +0 -0
- data/test/dummy/public/uploads/files/file.txt +0 -1
- data/test/dummy/public/uploads/images/big/image.jpg +0 -0
- data/test/dummy/public/uploads/images/original/image.jpg +0 -0
- data/test/dummy/public/uploads/images/small/image.jpg +0 -0
- data/test/dummy/script/rails +0 -6
- data/test/dummy/test/fixtures/file.txt +0 -1
- data/test/dummy/test/fixtures/file_big.txt +0 -1
- data/test/dummy/test/fixtures/file_empty.txt +0 -1
- data/test/dummy/test/fixtures/image.jpg +0 -0
- data/test/dummy/test/fixtures/image_big.jpg +0 -0
- data/test/dummy/test/fixtures/image_empty.jpg +0 -0
- data/test/dummy/tmp/uploads/files/13687482983108968.jpg +0 -0
- data/test/dummy/tmp/uploads/files/13687483348482658.jpg +0 -0
- data/test/dummy/tmp/uploads/files/13687483563901100.jpg +0 -0
- data/test/dummy/tmp/uploads/files/13687484108914220.jpg +0 -0
- data/test/dummy/tmp/uploads/files/13687489381697390.jpg +0 -0
- data/test/dummy/tmp/uploads/files/13687490968011022.jpg +0 -0
- data/test/dummy/tmp/uploads/files/13687491524768450.jpg +0 -0
- data/test/dummy/tmp/uploads/files/13687494425396302.jpg +0 -0
- data/test/dummy/tmp/uploads/files/13687513450654020.jpg +0 -0
- data/test/dummy/tmp/uploads/files/13687514839373922.txt +0 -1
- data/test/dummy/tmp/uploads/files/13688181722986792.jpg +0 -0
- data/test/dummy/tmp/uploads/files/13688181729459798.jpg +0 -0
- data/test/dummy/tmp/uploads/images/big/13687484103672162.jpg +0 -0
- data/test/dummy/tmp/uploads/images/big/13687484106303082.jpg +0 -0
- data/test/dummy/tmp/uploads/images/big/13687489377129980.jpg +0 -0
- data/test/dummy/tmp/uploads/images/big/13687489379459820.jpg +0 -0
- data/test/dummy/tmp/uploads/images/big/13687490965463210.jpg +0 -0
- data/test/dummy/tmp/uploads/images/big/13687491522552230.jpg +0 -0
- data/test/dummy/tmp/uploads/images/big/13688181724596422.jpg +0 -0
- data/test/dummy/tmp/uploads/images/big/13688181727051510.jpg +0 -0
- data/test/dummy/tmp/uploads/images/big/13688259257147068.jpg +0 -0
- data/test/dummy/tmp/uploads/images/big/13688280071286788.jpg +0 -0
- data/test/dummy/tmp/uploads/images/big/13688281527981960.jpg +0 -0
- data/test/dummy/tmp/uploads/images/big/13688360981527310.jpg +0 -0
- data/test/dummy/tmp/uploads/images/big/13688360983686640.jpg +0 -0
- data/test/dummy/tmp/uploads/images/big/13688977138132322.jpg +0 -0
- data/test/dummy/tmp/uploads/images/big/13688977290079510.jpg +0 -0
- data/test/dummy/tmp/uploads/images/big/13688978966174670.jpg +0 -0
- data/test/dummy/tmp/uploads/images/original/13687482977916958.jpg +0 -0
- data/test/dummy/tmp/uploads/images/original/13687482979328390.jpg +0 -0
- data/test/dummy/tmp/uploads/images/original/13687482981277580.jpg +0 -0
- data/test/dummy/tmp/uploads/images/original/13687483343313990.jpg +0 -0
- data/test/dummy/tmp/uploads/images/original/13687483344710930.jpg +0 -0
- data/test/dummy/tmp/uploads/images/original/13687483346647770.jpg +0 -0
- data/test/dummy/tmp/uploads/images/original/13687483558679350.jpg +0 -0
- data/test/dummy/tmp/uploads/images/original/13687483560070410.jpg +0 -0
- data/test/dummy/tmp/uploads/images/original/13687483562001660.jpg +0 -0
- data/test/dummy/tmp/uploads/images/original/13687484103672162.jpg +0 -0
- data/test/dummy/tmp/uploads/images/original/13687484106303082.jpg +0 -0
- data/test/dummy/tmp/uploads/images/original/13687489377129980.jpg +0 -0
- data/test/dummy/tmp/uploads/images/original/13687489379459820.jpg +0 -0
- data/test/dummy/tmp/uploads/images/original/13687490965463210.jpg +0 -0
- data/test/dummy/tmp/uploads/images/original/13687491522552230.jpg +0 -0
- data/test/dummy/tmp/uploads/images/original/13688181723027610.jpg +0 -0
- data/test/dummy/tmp/uploads/images/original/13688181724596422.jpg +0 -0
- data/test/dummy/tmp/uploads/images/original/13688181727051510.jpg +0 -0
- data/test/dummy/tmp/uploads/images/original/13688259257147068.jpg +0 -0
- data/test/dummy/tmp/uploads/images/original/13688280071286788.jpg +0 -0
- data/test/dummy/tmp/uploads/images/original/13688281527981960.jpg +0 -0
- data/test/dummy/tmp/uploads/images/original/13688360981527310.jpg +0 -0
- data/test/dummy/tmp/uploads/images/original/13688360983686640.jpg +0 -0
- data/test/dummy/tmp/uploads/images/original/13688977138132322.jpg +0 -0
- data/test/dummy/tmp/uploads/images/original/13688977290079510.jpg +0 -0
- data/test/dummy/tmp/uploads/images/original/13688978966174670.jpg +0 -0
- data/test/dummy/tmp/uploads/images/small/13687484103672162.jpg +0 -0
- data/test/dummy/tmp/uploads/images/small/13687484106303082.jpg +0 -0
- data/test/dummy/tmp/uploads/images/small/13687489377129980.jpg +0 -0
- data/test/dummy/tmp/uploads/images/small/13687489379459820.jpg +0 -0
- data/test/dummy/tmp/uploads/images/small/13687490965463210.jpg +0 -0
- data/test/dummy/tmp/uploads/images/small/13687491522552230.jpg +0 -0
- data/test/dummy/tmp/uploads/images/small/13688181723027610.jpg +0 -0
- data/test/dummy/tmp/uploads/images/small/13688181724596422.jpg +0 -0
- data/test/dummy/tmp/uploads/images/small/13688181727051510.jpg +0 -0
- data/test/dummy/tmp/uploads/images/small/13688259257147068.jpg +0 -0
- data/test/dummy/tmp/uploads/images/small/13688280071286788.jpg +0 -0
- data/test/dummy/tmp/uploads/images/small/13688281527981960.jpg +0 -0
- data/test/dummy/tmp/uploads/images/small/13688360981527310.jpg +0 -0
- data/test/dummy/tmp/uploads/images/small/13688360983686640.jpg +0 -0
- data/test/dummy/tmp/uploads/images/small/13688977138132322.jpg +0 -0
- data/test/dummy/tmp/uploads/images/small/13688977290079510.jpg +0 -0
- data/test/dummy/tmp/uploads/images/small/13688978966174670.jpg +0 -0
- data/test/fixtures/file.txt +0 -1
- data/test/fixtures/file_big.txt +0 -1
- data/test/fixtures/file_empty.txt +0 -1
- data/test/fixtures/image.jpg +0 -0
- data/test/fixtures/image_big.jpg +0 -0
- data/test/fixtures/image_empty.jpg +0 -0
- data/test/integration/local_generate_test.rb +0 -25
- data/test/test_helper.rb +0 -50
- data/test/unit/local_file_record_test.rb +0 -20
- data/test/unit/local_file_string_test.rb +0 -23
- data/test/unit/local_file_upload_test.rb +0 -30
- data/test/unit/local_image_record_test.rb +0 -25
- data/test/unit/local_image_string_test.rb +0 -34
- data/test/unit/local_image_upload_test.rb +0 -27
- data/test/unit/local_records_test.rb +0 -52
- data/test/unit/local_validators_test.rb +0 -71
- data/test/unit/s3_file_record_tes.rb +0 -21
- data/test/unit/s3_file_string_tes.rb +0 -24
- data/test/unit/s3_file_upload_tes.rb +0 -31
- data/test/unit/s3_image_record_tes.rb +0 -26
- data/test/unit/s3_image_string_tes.rb +0 -31
- data/test/unit/s3_image_upload_tes.rb +0 -28
- data/test/unit/s3_records_tes.rb +0 -56
- data/test/unit/s3_validators_tes.rb +0 -71
data/README.rdoc
DELETED
@@ -1,108 +0,0 @@
|
|
1
|
-
=== NOTE: From version 0.1.5 and above defaults must be in public folder
|
2
|
-
=== NOTE: From version 0.2.0 and above the use of path is deprecated
|
3
|
-
|
4
|
-
---
|
5
|
-
|
6
|
-
{<img src="https://codeclimate.com/github/mattways/rails_uploads.png" />}[https://codeclimate.com/github/mattways/rails_uploads] {<img src="https://travis-ci.org/mattways/rails_uploads.png?branch=master" alt="Build Status" />}[https://travis-ci.org/mattways/rails_uploads] {<img src="https://gemnasium.com/mattways/rails_uploads.png" alt="Dependency Status" />}[https://gemnasium.com/mattways/rails_uploads]
|
7
|
-
|
8
|
-
= Rails Uploads
|
9
|
-
|
10
|
-
Minimalistic toolkit to handle file and images uploads using ActiveRecord.
|
11
|
-
|
12
|
-
= Install
|
13
|
-
|
14
|
-
Put this line in your Gemfile:
|
15
|
-
gem 'rails_uploads'
|
16
|
-
|
17
|
-
Then bundle:
|
18
|
-
$ bundle
|
19
|
-
|
20
|
-
ImageMagick must be install, you can install it with homebrew:
|
21
|
-
brew install imagemagick
|
22
|
-
|
23
|
-
= Usage
|
24
|
-
|
25
|
-
Mount the engine at the end of you routes.rb (local storage only):
|
26
|
-
mount RailsUploads::Engine => '/' # Will be use to generate on the fly missing image presets
|
27
|
-
|
28
|
-
Add the column to your table (just a string):
|
29
|
-
create_table :models do |t|
|
30
|
-
t.string :attr
|
31
|
-
end
|
32
|
-
|
33
|
-
If you need a file:
|
34
|
-
class Model < ActiveRecord::Base
|
35
|
-
attr_accessible :attr
|
36
|
-
attached_file :attr, :default => 'file.txt'
|
37
|
-
end
|
38
|
-
|
39
|
-
If you need a image:
|
40
|
-
class Model < ActiveRecord::Base
|
41
|
-
attr_accessible :attr
|
42
|
-
attached_image :attr, :presets => [:small, :big], :default => 'assets/image.jpg'
|
43
|
-
end
|
44
|
-
|
45
|
-
Define presets in your application.rb
|
46
|
-
config.uploads.presets = {
|
47
|
-
:big => { :method => :fit, :width => 1024, :height => 768 }, # Fit will scale the image until it fit in the space without cropping
|
48
|
-
:small => { :method => :fill, :width => 120, :height => :120 }, # Fill will scale the image to fill all the space and then crop
|
49
|
-
:custom => proc { |image| image.convert :resize => '100x100' } # ImageMagick wrapper to do whatever you want
|
50
|
-
}
|
51
|
-
config.uploads.default_presets = [:small] # Define the default presets for all models with attached images
|
52
|
-
config.uploads.storage = :local # The default it's local, you can use :s3 as well
|
53
|
-
|
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
|
56
|
-
|
57
|
-
The validation works very similar to paperclip:
|
58
|
-
class Model < ActiveRecord::Base
|
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'] }
|
62
|
-
end
|
63
|
-
|
64
|
-
If you want to translate the errores the keys are:
|
65
|
-
errors.messages.attachment_presence
|
66
|
-
errors.messages.attachment_size_in # :max and :min
|
67
|
-
errors.messages.attachment_size_less_than # :count
|
68
|
-
errors.messages.attachment_size_greater_than # :count
|
69
|
-
errors.messages.attachment_content_type # :types
|
70
|
-
|
71
|
-
In your views:
|
72
|
-
a{ :href => record.file.url } # To get the file url
|
73
|
-
a{ :href => record.image.url } # To get the original image
|
74
|
-
a{ :href => record.image.url(:big) } # To get the a thumb
|
75
|
-
|
76
|
-
In your forms:
|
77
|
-
= f.file_field :attr
|
78
|
-
|
79
|
-
If your file it's optional:
|
80
|
-
= f.check_box :delete_attr
|
81
|
-
|
82
|
-
= FAQ
|
83
|
-
|
84
|
-
== How can I use a cdn with this plugin?
|
85
|
-
|
86
|
-
Define a base url in your application.rb:
|
87
|
-
config.uploads.base_url = 'http://cdn.example.com'
|
88
|
-
|
89
|
-
== How can I automatically create buckets?
|
90
|
-
|
91
|
-
Use this rake task after create the s3.yml file:
|
92
|
-
rake uploads:s3:buckets:create
|
93
|
-
|
94
|
-
== How can I clean a preset?
|
95
|
-
|
96
|
-
Use this rake task:
|
97
|
-
rake uploads:presets:clean MODEL=models PRESETS=first_preset,second_preset
|
98
|
-
|
99
|
-
== How can I refresh a preset?
|
100
|
-
|
101
|
-
Use this rake task:
|
102
|
-
rake uploads:presets:refresh MODEL=models PRESETS=first_preset,second_preset
|
103
|
-
|
104
|
-
== How to migrate from versions before 0.1.0?
|
105
|
-
|
106
|
-
To migrate from versions before 0.1.0 you need to reorganize uploads with this task after define your presets in your application.rb:
|
107
|
-
rake uploads:migrate
|
108
|
-
|
data/Rakefile
DELETED
@@ -1,40 +0,0 @@
|
|
1
|
-
#!/usr/bin/env rake
|
2
|
-
begin
|
3
|
-
require 'bundler/setup'
|
4
|
-
rescue LoadError
|
5
|
-
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
6
|
-
end
|
7
|
-
begin
|
8
|
-
require 'rdoc/task'
|
9
|
-
rescue LoadError
|
10
|
-
require 'rdoc/rdoc'
|
11
|
-
require 'rake/rdoctask'
|
12
|
-
RDoc::Task = Rake::RDocTask
|
13
|
-
end
|
14
|
-
|
15
|
-
RDoc::Task.new(:rdoc) do |rdoc|
|
16
|
-
rdoc.rdoc_dir = 'rdoc'
|
17
|
-
rdoc.title = 'RailsUploads'
|
18
|
-
rdoc.options << '--line-numbers'
|
19
|
-
rdoc.rdoc_files.include('README.rdoc')
|
20
|
-
rdoc.rdoc_files.include('lib/**/*.rb')
|
21
|
-
end
|
22
|
-
|
23
|
-
APP_RAKEFILE = File.expand_path('../test/dummy/Rakefile', __FILE__)
|
24
|
-
load 'rails/tasks/engine.rake'
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
Bundler::GemHelper.install_tasks
|
29
|
-
|
30
|
-
require 'rake/testtask'
|
31
|
-
|
32
|
-
Rake::TestTask.new(:test) do |t|
|
33
|
-
t.libs << 'lib'
|
34
|
-
t.libs << 'test'
|
35
|
-
t.pattern = 'test/**/*_test.rb'
|
36
|
-
t.verbose = false
|
37
|
-
end
|
38
|
-
|
39
|
-
|
40
|
-
task :default => :test
|
@@ -1,17 +0,0 @@
|
|
1
|
-
module RailsUploads
|
2
|
-
class PresetsController < ActionController::Base
|
3
|
-
|
4
|
-
def generate
|
5
|
-
filename = "#{params[:image]}.#{params[:format]}"
|
6
|
-
preset = params[:preset].gsub('-', '_').to_sym
|
7
|
-
if Rails.application.config.uploads.presets.has_key? preset
|
8
|
-
image = RailsUploads::Types::Image.new(filename)
|
9
|
-
if image.exists? and !image.exists?(preset)
|
10
|
-
image.send :generate_preset, preset
|
11
|
-
redirect_to image.url(params[:preset]).to_s, cb: Random.rand(100000)
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
end
|
17
|
-
end
|
data/config/locales/en.yml
DELETED
@@ -1,8 +0,0 @@
|
|
1
|
-
en:
|
2
|
-
errors:
|
3
|
-
messages:
|
4
|
-
attachment_presence: "can't be blank"
|
5
|
-
attachment_content_type: "the allowed types are: %{types}"
|
6
|
-
attachment_size_less_than: "size must be less than %{count}"
|
7
|
-
attachment_size_greater_than: "size must be greater than %{count}"
|
8
|
-
attachment_size_in: "size must be between %{min} and %{max}"
|
data/config/locales/es.yml
DELETED
@@ -1,8 +0,0 @@
|
|
1
|
-
es:
|
2
|
-
errors:
|
3
|
-
messages:
|
4
|
-
attachment_presence: "no puede estar vacío"
|
5
|
-
attachment_content_type: "los formatos permitidos son: %{types}"
|
6
|
-
attachment_size_less_than: "debe pesar menos de %{count}"
|
7
|
-
attachment_size_greater_than: "debe pesar más de %{count}"
|
8
|
-
attachment_size_in: "debe pesar entre %{min} y %{max}"
|
data/config/routes.rb
DELETED
@@ -1,16 +0,0 @@
|
|
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
|
-
|
data/lib/rails_uploads.rb
DELETED
@@ -1,16 +0,0 @@
|
|
1
|
-
require 'rails_uploads/active_record/base'
|
2
|
-
require 'rails_uploads/magick/image'
|
3
|
-
require 'rails_uploads/storages/local'
|
4
|
-
require 'rails_uploads/storages/s3'
|
5
|
-
require 'rails_uploads/types/file'
|
6
|
-
require 'rails_uploads/types/image'
|
7
|
-
require 'rails_uploads/validators/base'
|
8
|
-
require 'rails_uploads/validators/attachment_content_type_validator'
|
9
|
-
require 'rails_uploads/validators/attachment_presence_validator'
|
10
|
-
require 'rails_uploads/validators/attachment_size_validator'
|
11
|
-
require 'rails_uploads/engine'
|
12
|
-
require 'rails_uploads/railtie'
|
13
|
-
require 'rails_uploads/version'
|
14
|
-
|
15
|
-
module RailsUploads
|
16
|
-
end
|
@@ -1,139 +0,0 @@
|
|
1
|
-
module RailsUploads
|
2
|
-
module ActiveRecord
|
3
|
-
module Base
|
4
|
-
|
5
|
-
def self.included(base)
|
6
|
-
base.extend ClassMethods
|
7
|
-
end
|
8
|
-
|
9
|
-
protected
|
10
|
-
|
11
|
-
def build_attachment_instance(source, options)
|
12
|
-
klass = options.has_key?(:type) ? options[:type].to_s.classify : 'File'
|
13
|
-
RailsUploads::Types.const_get(klass).new(source, options)
|
14
|
-
end
|
15
|
-
|
16
|
-
def check_changed_attachments
|
17
|
-
@stored_attachments = []
|
18
|
-
@deleted_attachments = []
|
19
|
-
self.class.attachments.each do |attr, options|
|
20
|
-
if changed_attributes.has_key? attr.to_s
|
21
|
-
stored = attributes[attr.to_s]
|
22
|
-
deleted = changed_attributes[attr.to_s]
|
23
|
-
add_changed_attachment stored, options, :stored if stored.present?
|
24
|
-
add_changed_attachment deleted, options, :deleted if deleted.present?
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
def iterate_attachments
|
30
|
-
self.class.attachments.each do |attr, options|
|
31
|
-
next unless instance = send(attr)
|
32
|
-
yield instance
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
def add_changed_attachment(source, options, type)
|
37
|
-
(type == :stored ? @stored_attachments : @deleted_attachments) << (source.is_a?(String) ? build_attachment_instance(source, options) : source)
|
38
|
-
end
|
39
|
-
|
40
|
-
def store_attachments
|
41
|
-
iterate_attachments { |a| a.store }
|
42
|
-
end
|
43
|
-
|
44
|
-
def delete_attachments
|
45
|
-
iterate_attachments { |a| a.delete }
|
46
|
-
end
|
47
|
-
|
48
|
-
def remove_stored_attachments
|
49
|
-
@stored_attachments.each { |a| a.delete }
|
50
|
-
end
|
51
|
-
|
52
|
-
def remove_deleted_attachments
|
53
|
-
@deleted_attachments.each { |a| a.delete }
|
54
|
-
end
|
55
|
-
|
56
|
-
module ClassMethods
|
57
|
-
|
58
|
-
attr_reader :attachments
|
59
|
-
|
60
|
-
def self.extended(base)
|
61
|
-
[:file, :image].each do |type|
|
62
|
-
base.send(:define_singleton_method, "attached_#{type}") do |*args|
|
63
|
-
options = args.extract_options!
|
64
|
-
options[:type] = type
|
65
|
-
define_attachment *args.append(options)
|
66
|
-
end
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
def inherited(subclass)
|
71
|
-
subclass.instance_variable_set(:@attachments, @attachments)
|
72
|
-
super
|
73
|
-
end
|
74
|
-
|
75
|
-
def is_attachable?
|
76
|
-
attachments.present?
|
77
|
-
end
|
78
|
-
|
79
|
-
protected
|
80
|
-
|
81
|
-
def define_attachment(*args)
|
82
|
-
options = args.extract_options!
|
83
|
-
make_attachable unless is_attachable?
|
84
|
-
args.each do |attr|
|
85
|
-
define_attachable_attribute_methods attr.to_sym, options
|
86
|
-
define_default_validations attr.to_sym, options
|
87
|
-
@attachments[attr.to_sym] = options
|
88
|
-
end
|
89
|
-
end
|
90
|
-
|
91
|
-
def make_attachable
|
92
|
-
before_save :store_attachments, :check_changed_attachments
|
93
|
-
after_save :remove_deleted_attachments
|
94
|
-
before_destroy :delete_attachments
|
95
|
-
after_rollback :remove_stored_attachments
|
96
|
-
@attachments = {}
|
97
|
-
end
|
98
|
-
|
99
|
-
def define_default_validations(attr, options)
|
100
|
-
default_validations = Rails.application.config.uploads.default_validations[options[:type]]
|
101
|
-
validates attr, default_validations.dup if default_validations.present? and not default_validations.empty?
|
102
|
-
end
|
103
|
-
|
104
|
-
def define_attachable_attribute_methods(attr, options)
|
105
|
-
['set', 'get'].each { |method| send "define_attachable_attribute_method_#{method}", attr, options }
|
106
|
-
end
|
107
|
-
|
108
|
-
def define_attachable_attribute_method_set(attr, options)
|
109
|
-
define_method "#{attr}=" do |value|
|
110
|
-
@attachments = {} if defined?(@attachments).nil?
|
111
|
-
if value.is_a? ActionDispatch::Http::UploadedFile or value.is_a? Rack::Test::UploadedFile or (value.is_a? String and value =~ /^[a-zA-Z0-9_-]+\.[a-zA-z]+$/)
|
112
|
-
@attachments[attr] = build_attachment_instance(value, options)
|
113
|
-
super(@attachments[attr].filename)
|
114
|
-
end
|
115
|
-
end
|
116
|
-
define_method "delete_#{attr}=" do |value|
|
117
|
-
if @attributes[attr.to_s].present? and value == '1'
|
118
|
-
send "#{attr}_will_change!"
|
119
|
-
@attachments[attr] = nil
|
120
|
-
@attributes[attr.to_s] = nil
|
121
|
-
end
|
122
|
-
instance_variable_set("@delete_#{attr}".to_sym, value)
|
123
|
-
end
|
124
|
-
end
|
125
|
-
|
126
|
-
def define_attachable_attribute_method_get(attr, options)
|
127
|
-
define_method attr do
|
128
|
-
@attachments = {} if defined?(@attachments).nil?
|
129
|
-
return @attachments[attr] if @attachments.has_key? attr
|
130
|
-
return nil if super().nil? and not options.has_key? :default
|
131
|
-
@attachments[attr] = build_attachment_instance(super(), options)
|
132
|
-
end
|
133
|
-
attr_reader "delete_#{attr}".to_sym
|
134
|
-
end
|
135
|
-
|
136
|
-
end
|
137
|
-
end
|
138
|
-
end
|
139
|
-
end
|
data/lib/rails_uploads/engine.rb
DELETED
@@ -1,83 +0,0 @@
|
|
1
|
-
module RailsUploads
|
2
|
-
module Magick
|
3
|
-
class Image
|
4
|
-
|
5
|
-
def initialize(source, output=nil)
|
6
|
-
@source = source
|
7
|
-
@output = output
|
8
|
-
end
|
9
|
-
|
10
|
-
def convert(args)
|
11
|
-
tokens = [convert? ? 'convert' : 'mogrify']
|
12
|
-
file_to_tokens tokens
|
13
|
-
args_to_tokens args, tokens
|
14
|
-
tokens << "\"#{@output}\"" if convert?
|
15
|
-
success, output = run(tokens)
|
16
|
-
if block_given?
|
17
|
-
yield success, output
|
18
|
-
else
|
19
|
-
success
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
def identify(args)
|
24
|
-
tokens = ['identify']
|
25
|
-
args_to_tokens args, tokens
|
26
|
-
file_to_tokens tokens
|
27
|
-
success, output = run(tokens)
|
28
|
-
if block_given?
|
29
|
-
yield success, output
|
30
|
-
else
|
31
|
-
success
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
def dimensions
|
36
|
-
identify(format: '%wx%h') do |success, output|
|
37
|
-
success ? output.chomp.split('x').map(&:to_i) : []
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
def width
|
42
|
-
dimensions[0]
|
43
|
-
end
|
44
|
-
|
45
|
-
def height
|
46
|
-
dimensions[1]
|
47
|
-
end
|
48
|
-
|
49
|
-
def resize_to_fill(max_width, max_height)
|
50
|
-
width, height = dimensions
|
51
|
-
scale = [max_width.to_f/width, max_height.to_f/height].max
|
52
|
-
convert resize: "#{(scale*width).ceil}x#{(scale*height).ceil}", gravity: 'center', crop: "#{max_width}x#{max_height}+0+0"
|
53
|
-
end
|
54
|
-
|
55
|
-
def resize_to_fit(max_width, max_height)
|
56
|
-
width, height = dimensions
|
57
|
-
scale = [max_width.to_f/width, max_height.to_f/height].reject{|s| s==0 }.min
|
58
|
-
convert resize: "#{(scale*width).ceil}x#{(scale*height).ceil}", gravity: 'center'
|
59
|
-
end
|
60
|
-
|
61
|
-
protected
|
62
|
-
|
63
|
-
def convert?
|
64
|
-
not (@output.nil? and ::File.exists?(@output))
|
65
|
-
end
|
66
|
-
|
67
|
-
def file_to_tokens(tokens)
|
68
|
-
tokens << (convert? ? "\"#{@source}\"" : @output)
|
69
|
-
end
|
70
|
-
|
71
|
-
def args_to_tokens(args, tokens)
|
72
|
-
args.each { |k, v| tokens << "-#{k} #{v}" }
|
73
|
-
end
|
74
|
-
|
75
|
-
def run(tokens)
|
76
|
-
output = `#{tokens.join(' ')}`
|
77
|
-
success = $?.exitstatus == 0
|
78
|
-
[success, output]
|
79
|
-
end
|
80
|
-
|
81
|
-
end
|
82
|
-
end
|
83
|
-
end
|