uploadbox 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c26f587ffb76e97589fabfeccf5d0ce8303ecb15
4
- data.tar.gz: 87df182c32e2947399ce4b6b019b4dab79a3266e
3
+ metadata.gz: 2c98593bc8ee77673827d0df2453c69faca92b38
4
+ data.tar.gz: 869dc96c133c8d37615901678a9c25fb2463daec
5
5
  SHA512:
6
- metadata.gz: 4e6ae3bb264c4c80ca0f4495e282372301b1d88a5213e5188bef00fe30e7e0e29a76c0cd860cabd4e49e1c254256a3980b42a40fbda8e542d6af0e188a78b3e9
7
- data.tar.gz: 47426e47bf77cd74207c2eb363d7b06e52144adf4ccff4ba7406efdaf4547a803e581c7e954bd5581f8e32d230f77b1a85474b027baf0b2d7ff8f927913f529d
6
+ metadata.gz: 1e230ef299e8a19bd3a58e98f9136c7e4bd41df72aeca4f3a53bcaa60f76d6dc6dbe072bf760031f890d4b9bbaf318cded27636b4d2f91f17f591c6f03d76c02
7
+ data.tar.gz: b4c694a45159cfd1785adfb26225d31b9313bccc23369e1a83b3e412dacff26a06b2d2815653912741c4ef4afaba720225383e031ba913869690c8a2bbcd1bc2
@@ -2,11 +2,18 @@ class @ImageUploader
2
2
  constructor: (@container) ->
3
3
  @preview = @container.find('[data-provides="fileupload"]')
4
4
  @fileInput = @container.find('input[type="file"]')
5
+ @typeInput = @container.find('input[name="image[imageable_type]"]')
6
+ @uploadNameInput = @container.find('input[name="image[upload_name]"]')
5
7
  @idInput = @container.find('[data-item="id"]')
6
8
  @container.find('a.btn.fileupload-exists').bind('ajax:success', @delete)
9
+ @thumbContainer = @container.find('.fileupload-preview.thumbnail')
7
10
  @fileInput.fileupload
8
11
  dataType: 'json'
9
- formData: {name: @fileInput.attr('name'), value: @fileInput.val()}
12
+ formData: [
13
+ {name: @fileInput.attr('name'), value: @fileInput.val()},
14
+ {name: @typeInput.attr('name'), value: @typeInput.val()}
15
+ {name: @uploadNameInput.attr('name'), value: @uploadNameInput.val()}
16
+ ]
10
17
  add: @add
11
18
  progress: @progress
12
19
  done: @done
@@ -25,11 +32,11 @@ class @ImageUploader
25
32
  @loader.detach()
26
33
  @idInput.val(image.id)
27
34
  @container.find('a.btn.fileupload-exists').attr('href', image.url)
28
- @container.find('.fileupload-preview.thumbnail img').detach()
35
+ @thumbContainer.find('img').detach()
29
36
  img = $('<img/>')
30
- img.attr('src', image.versions.regular)
31
- img.attr('width', 50)
32
- img.attr('height', 50)
37
+ img.attr('src', image.versions[@thumbContainer.data('version')])
38
+ img.attr('width', @thumbContainer.data('width'))
39
+ img.attr('height', @thumbContainer.data('height'))
33
40
  @container.find('.fileupload-preview.thumbnail').append(img)
34
41
  @container.find('.fileupload').removeClass('fileupload-new').addClass('fileupload-exists')
35
42
 
@@ -1,11 +1,11 @@
1
1
  module Uploadbox
2
2
  class ImagesController < ApplicationController
3
3
  def create
4
- @image = Image.create!(image_params)
4
+ @image = Image.create_upload(image_params)
5
5
  end
6
6
 
7
7
  def update
8
- @image = Image.create!(image_params)
8
+ @image = Image.create_upload(image_params)
9
9
  end
10
10
 
11
11
  def destroy
@@ -14,7 +14,7 @@ module Uploadbox
14
14
 
15
15
  private
16
16
  def image_params
17
- params.require(:image).permit(:file)
17
+ params.require(:image).permit(:file, :imageable_type, :upload_name)
18
18
  end
19
19
  end
20
20
  end
@@ -0,0 +1,12 @@
1
+ module Uploadbox
2
+ module ImgHelper
3
+ def img(*args)
4
+ upload = args[0]
5
+ if upload.is_a? CarrierWave::Uploader::Base
6
+ image_tag(upload.url, width: upload.width, height: upload.height)
7
+ else
8
+ image_tag(*args)
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,8 @@
1
+ class Image < ActiveRecord::Base
2
+ belongs_to :imageable, polymorphic: true
3
+
4
+ def self.create_upload(attributes)
5
+ upload_class_name = attributes[:imageable_type] + attributes[:upload_name].camelize
6
+ Uploadbox.const_get(upload_class_name).create!(attributes)
7
+ end
8
+ end
@@ -0,0 +1,27 @@
1
+ module Uploadbox
2
+ class ImageProcessingUploader < CarrierWave::Uploader::Base
3
+ include CarrierWave::MimeTypes
4
+ include CarrierWave::MiniMagick
5
+ include CarrierWave::Processing::MiniMagick
6
+
7
+ process :set_content_type
8
+ process :strip
9
+
10
+ def store_dir
11
+ # "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
12
+ "uploads/image/#{mounted_as}/#{model.id}"
13
+ end
14
+
15
+ def extension_white_list
16
+ %w(jpg jpeg gif png)
17
+ end
18
+
19
+ def filename
20
+ if original_filename
21
+ extension = File.extname(original_filename)
22
+ name = File.basename(original_filename, extension).parameterize.dasherize
23
+ "#{name}#{extension}"
24
+ end
25
+ end
26
+ end
27
+ end
@@ -1,16 +1,18 @@
1
1
  = form.fields_for :image do
2
2
  div data-component="ImageUploader"
3
- input name="image_id" data-item="id" type="hidden"
4
- .fileupload data-provides="fileupload" class="fileupload-#{resource.image? ? 'exists' : 'new'}"
5
- .fileupload-preview.thumbnail style="width: 50px; height: 50px;"
6
- = image_tag resource.image.file.regular, width: 50, height: 50 if resource.image?
3
+ input name="#{upload_name}_id" data-item="id" type="hidden"
4
+ .fileupload data-provides="fileupload" class="fileupload-#{resource.send(upload_name).present? ? 'exists' : 'new'}"
5
+ .fileupload-preview.thumbnail data-version="#{version}" data-width="#{width}" data-height="#{height}" style="width: #{width}px; height: #{height}px;"
6
+ = img resource.send(upload_name).send(version) if resource.send(upload_name).present?
7
7
  div
8
8
  span.btn.btn-file
9
- span.fileupload-new Escolher imagem
9
+ span.fileupload-new Escolher
10
10
  span.fileupload-exists Alterar
11
11
  input type="file" data-url="#{uploadbox.images_path}" name="image[file]"
12
- - if resource.image?
13
- = link_to 'Excluir', uploadbox.image_path(resource.image), class: 'btn fileupload-exists', remote: true, method: :delete
12
+ input type="hidden" name="image[imageable_type]" value="#{resource.class}"
13
+ input type="hidden" name="image[upload_name]" value="#{upload_name}"
14
+ - if resource.send(upload_name).present?
15
+ = link_to 'Excluir', uploadbox.image_path(resource.send(upload_name)), class: 'btn fileupload-exists', remote: true, method: :delete
14
16
  - else
15
17
  = link_to 'Excluir', '#', class: 'btn fileupload-exists', remote: true, method: :delete
16
18
 
@@ -1,6 +1,7 @@
1
1
  json.(@image, :id)
2
2
  json.versions do
3
- json.original @image.file.url
4
- json.regular @image.file.regular.url
3
+ @image.class.versions.keys.each do |version|
4
+ json.set! version, @image.send(version).url
5
+ end
5
6
  end
6
7
  json.url image_path(@image)
File without changes
@@ -4,35 +4,26 @@ module Uploadbox
4
4
 
5
5
  source_root File.expand_path('../templates', __FILE__)
6
6
 
7
- def create_initializer
7
+ def create_initializers
8
8
  copy_file 'initializers/carrierwave.rb', 'config/initializers/carrierwave.rb'
9
+ copy_file 'initializers/uploadbox.rb', 'config/initializers/uploadbox.rb'
9
10
  end
10
11
 
11
12
  def update_gitignore
12
13
  append_to_file '.gitignore', 'public/uploads'
13
14
  end
14
15
 
15
- def create_uploader
16
- copy_file 'uploaders/image_uploader.rb', 'app/uploaders/image_uploader.rb'
17
- end
18
-
19
- def create_model
20
- copy_file 'models/image.rb', 'app/models/image.rb'
21
- end
22
-
23
16
  def create_migration
24
17
  migration_template 'migrate/create_images.rb', 'db/migrate/create_images.rb'
25
18
  end
26
19
 
27
-
28
20
  private
29
-
30
- def self.next_migration_number(dirname)
31
- if ActiveRecord::Base.timestamped_migrations
32
- Time.now.utc.strftime('%Y%m%d%H%M%S')
33
- else
34
- '%.3d' % (current_migration_number(dirname) + 1)
21
+ def self.next_migration_number(dirname)
22
+ if ActiveRecord::Base.timestamped_migrations
23
+ Time.now.utc.strftime('%Y%m%d%H%M%S')
24
+ else
25
+ '%.3d' % (current_migration_number(dirname) + 1)
26
+ end
35
27
  end
36
- end
37
28
  end
38
29
  end
@@ -0,0 +1,4 @@
1
+ Uploadbox.retina = true
2
+ Uploadbox.retina_quality = 30
3
+ Uploadbox.image_quality = 70
4
+
@@ -3,6 +3,10 @@ class CreateImages < ActiveRecord::Migration
3
3
  create_table :images do |t|
4
4
  t.string :file
5
5
  t.references :imageable, polymorphic: true, index: true
6
+ t.integer :width
7
+ t.integer :height
8
+ t.boolean :retina, default: false
9
+ t.string :upload_name
6
10
 
7
11
  t.timestamps
8
12
  end
@@ -7,16 +7,31 @@ require 'carrierwave-processing'
7
7
  require 'mini_magick'
8
8
  require 'jbuilder'
9
9
 
10
-
11
10
  module Uploadbox
12
11
  class Engine < ::Rails::Engine
13
- config.autoload_paths << File.expand_path('../../../app/uploaders/concerns', __FILE__)
12
+ initializer 'uploadbox.action_controller' do |app|
13
+ ActiveSupport.on_load :action_controller do
14
+ helper Uploadbox::ImgHelper
15
+ end
16
+ end
14
17
  isolate_namespace Uploadbox
15
18
  end
16
19
  end
17
20
 
18
21
  class ActionView::Helpers::FormBuilder
19
- def image_uploader
20
- @template.render partial: 'uploadbox/images/uploader', locals: {resource: @object, form: self}
22
+ def uploader(upload_name, options={})
23
+ upload_model_class = "Uploadbox::#{@object.class.to_s + upload_name.to_s.camelize}".constantize
24
+ options.reverse_merge!(preview: upload_model_class.versions.keys.first)
25
+ dimensions = upload_model_class.versions[options[:preview]]
26
+ @template.render partial: 'uploadbox/images/uploader', locals: {
27
+ upload_name: upload_name,
28
+ resource: @object,
29
+ form: self,
30
+ version: options[:preview],
31
+ width: dimensions[0],
32
+ height: dimensions[1]
33
+ }
21
34
  end
22
35
  end
36
+
37
+ require 'uploadbox/image_uploader'
@@ -0,0 +1,81 @@
1
+ module Uploadbox
2
+ module ImageUploader
3
+ def uploads_one(upload_name, options={})
4
+ default_options = {
5
+ default: false,
6
+ retina: Uploadbox.retina,
7
+ quality: Uploadbox.retina ? (Uploadbox.retina_quality || 40) : (Uploadbox.image_quality || 80)
8
+ }
9
+ options = options.reverse_merge(default_options)
10
+ upload_versions = options.select{ |key| default_options.keys.exclude? key }
11
+ options = options.select{ |key| default_options.keys.include? key }
12
+
13
+ imageable_type = self.to_s
14
+ upload_class_name = imageable_type + upload_name.to_s.camelize
15
+ upload_class = Class.new(Image)
16
+ Uploadbox.const_set(upload_class_name, upload_class)
17
+
18
+ # picture?
19
+ define_method("#{upload_name}?") { send(upload_name) and send(upload_name).file? }
20
+
21
+ # attach_picture
22
+ define_method("attach_#{upload_name}") do |upload_id|
23
+ if upload_id.present?
24
+ self.send("attach_#{upload_name}!", upload_id)
25
+ end
26
+ end
27
+
28
+ # attach_picture!
29
+ define_method("attach_#{upload_name}!") do |upload_id|
30
+ self.send("#{upload_name}=", upload_class.find(upload_id))
31
+ end
32
+
33
+ # Uploadbox::PostPicture < Image
34
+ upload_class.class_eval <<-RUBY, __FILE__, __LINE__ + 1
35
+ def self.versions
36
+ #{upload_versions}
37
+ end
38
+ RUBY
39
+
40
+ upload_class.instance_eval do
41
+ delegate *upload_versions.keys, to: :file
42
+
43
+ default_scope -> { where(imageable_type: imageable_type).where(upload_name: upload_name.to_s) }
44
+
45
+ # Uploabox::PostPictureUploader < UploadBox::ImgProcessing < CarrierWave
46
+ dynamic_uploader = Class.new(Uploadbox::ImageProcessingUploader)
47
+ Uploadbox.const_set(self.class.to_s + upload_name.to_s.camelize + 'Uploader', dynamic_uploader)
48
+ dynamic_uploader.class_eval do
49
+ upload_versions.each do |version_name, dimensions|
50
+ if options[:retina]
51
+ dimensions = dimensions.map{ |d| d * 2 }
52
+ end
53
+
54
+ version version_name do
55
+ process resize_to_fill: dimensions
56
+ process quality: quality = options[:quality]
57
+ end
58
+
59
+ def dimensions
60
+ model.class.versions[version_name]
61
+ end
62
+
63
+ def width
64
+ dimensions[0]
65
+ end
66
+
67
+ def height
68
+ dimensions[1]
69
+ end
70
+ end
71
+ end
72
+ mount_uploader :file, dynamic_uploader
73
+ end
74
+
75
+ has_one upload_name, as: :imageable, dependent: :destroy, class_name: "Uploadbox::#{self.to_s + upload_name.to_s.camelize}"
76
+ end
77
+
78
+ end
79
+ end
80
+ ActiveRecord::Base.extend Uploadbox::ImageUploader
81
+
@@ -1,3 +1,3 @@
1
1
  module Uploadbox
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/lib/uploadbox.rb CHANGED
@@ -1,4 +1,7 @@
1
1
  require "uploadbox/engine"
2
2
 
3
3
  module Uploadbox
4
+ mattr_accessor :retina
5
+ mattr_accessor :image_quality
6
+ mattr_accessor :retina_quality
4
7
  end
@@ -6,8 +6,10 @@
6
6
  </p>
7
7
 
8
8
  <p>
9
- <strong>Image:</strong>
10
- <%= image_tag @post.image.file.regular.url, width: 50, height: 50 if @post.image? %>
9
+ <strong>Regular:</strong>
10
+ <%= image_tag @post.image.file.regular.url if @post.image? %>
11
+ <strong>Thumb:</strong>
12
+ <%= image_tag @post.image.file.thumb.url if @post.image? %>
11
13
  </p>
12
14
 
13
15
  <p>
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: uploadbox
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Julio Protzek
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-06-29 00:00:00.000000000 Z
12
+ date: 2013-07-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -211,26 +211,27 @@ files:
211
211
  - app/controllers/uploadbox/application_controller.rb
212
212
  - app/controllers/uploadbox/images_controller.rb
213
213
  - app/helpers/uploadbox/application_helper.rb
214
- - app/models/concerns/image_uploadable.rb
215
- - app/models/concerns/uploadable.rb
216
- - app/uploaders/concerns/image_processing.rb
214
+ - app/helpers/uploadbox/img_helper.rb
215
+ - app/models/image.rb
216
+ - app/uploaders/uploadbox/image_processing_uploader.rb
217
217
  - app/views/layouts/uploadbox/application.html.erb
218
218
  - app/views/uploadbox/images/_uploader.html.slim
219
219
  - app/views/uploadbox/images/create.json.jbuilder
220
220
  - config/routes.rb
221
+ - lib/form_builder.rb
221
222
  - lib/generators/uploadbox/image/image_generator.rb
222
223
  - lib/generators/uploadbox/image/templates/initializers/carrierwave.rb
224
+ - lib/generators/uploadbox/image/templates/initializers/uploadbox.rb
223
225
  - lib/generators/uploadbox/image/templates/migrate/create_images.rb
224
226
  - lib/generators/uploadbox/image/templates/models/image.rb
225
- - lib/generators/uploadbox/image/templates/uploaders/image_uploader.rb
226
227
  - lib/generators/uploadbox/image/USAGE
227
228
  - lib/tasks/uploadbox_tasks.rake
228
229
  - lib/uploadbox/engine.rb
230
+ - lib/uploadbox/image_uploader.rb
229
231
  - lib/uploadbox/version.rb
230
232
  - lib/uploadbox.rb
231
233
  - MIT-LICENSE
232
234
  - Rakefile
233
- - README.rdoc
234
235
  - test/dummy/app/assets/javascripts/application.coffee
235
236
  - test/dummy/app/assets/stylesheets/application.sass
236
237
  - test/dummy/app/assets/stylesheets/scaffold.css
@@ -300,7 +301,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
300
301
  version: '0'
301
302
  requirements: []
302
303
  rubyforge_project:
303
- rubygems_version: 2.0.3
304
+ rubygems_version: 2.0.4
304
305
  signing_key:
305
306
  specification_version: 4
306
307
  summary: Ajax file uploader for Rails applications.
data/README.rdoc DELETED
@@ -1,3 +0,0 @@
1
- = Uploadbox
2
-
3
- This project rocks and uses MIT-LICENSE.
@@ -1,11 +0,0 @@
1
- module ImageUploadable
2
- extend ActiveSupport::Concern
3
-
4
- included do
5
- has_one :image, as: :imageable, dependent: :destroy
6
- end
7
-
8
- def image?
9
- image and image.file?
10
- end
11
- end
@@ -1,9 +0,0 @@
1
- module Uploadable
2
- extend ActiveSupport::Concern
3
-
4
- included do
5
- belongs_to :imageable, polymorphic: true
6
- mount_uploader :file, ImageUploader
7
- delegate :regular, to: :file
8
- end
9
- end
@@ -1,33 +0,0 @@
1
- module ImageProcessing
2
- extend ActiveSupport::Concern
3
-
4
- include CarrierWave::MimeTypes
5
- include CarrierWave::MiniMagick
6
- include CarrierWave::Processing::MiniMagick
7
-
8
- included do
9
- process :set_content_type
10
- process :strip
11
-
12
- version :regular do
13
- process resize_to_fill: [100, 100]
14
- process quality: 30
15
- end
16
- end
17
-
18
- def store_dir
19
- "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
20
- end
21
-
22
- def extension_white_list
23
- %w(jpg jpeg gif png)
24
- end
25
-
26
- def filename
27
- if original_filename
28
- extension = File.extname(original_filename)
29
- name = File.basename(original_filename, extension).parameterize.dasherize
30
- "#{name}#{extension}"
31
- end
32
- end
33
- end
@@ -1,3 +0,0 @@
1
- class ImageUploader < CarrierWave::Uploader::Base
2
- include ImageProcessing
3
- end