paperclip_upload 0.1.0 → 1.0.0

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: ffea8a1951c55d4cd81aab781355bca818cc572a
4
- data.tar.gz: 5e0ca02268e53c4eca741515bc6e41419938bcce
3
+ metadata.gz: 634b300e660876d996d1f717093f2cd65f4c6aba
4
+ data.tar.gz: f81e5cb3f748537e0c6d46fd4c8f3faeed23d29c
5
5
  SHA512:
6
- metadata.gz: 0a972c31c9b3fbf19dfa6d0f191cda2530a36a8af4d9d81dbc64d294401773382b7d4de79420615c900155216f7a59801500113ab076162d220b1e55e946ea65
7
- data.tar.gz: 80e73d1bf12564f43659dab0fbebd3c1df832f94901ad962770eacdb8716da0fedb667384bd007683784857342524566dff1e78cc03500b5af90e8dcca89c8ce
6
+ metadata.gz: ded439b53ccc9ab6a00953f1502476a64fb84fe3351fb6edf79a26db1ba2030079d3aaf5fd4dff4aeb4fb229578d6a1fe474b8d33899acdaabacfb5d673d41db
7
+ data.tar.gz: 9f0a12b84460e38c29e5d2845716c5913992f1696b27c376165b37438cf335559ad58d45341a55f182470b0acd55308c24dae36a5a6279a0f05a61b39e7287e8
@@ -3,10 +3,6 @@ module PaperclipUpload
3
3
  self.responder = PaperclipUploadResponder
4
4
  respond_to :json
5
5
 
6
- def show
7
- respond_with upload
8
- end
9
-
10
6
  def create
11
7
  respond_with PaperclipUpload::Upload.create(permitted_params), status: :created
12
8
  end
@@ -16,9 +12,5 @@ module PaperclipUpload
16
12
  def permitted_params
17
13
  params.permit(:file)
18
14
  end
19
-
20
- def upload
21
- @upload ||= Upload.find(params[:id])
22
- end
23
15
  end
24
16
  end
@@ -13,9 +13,26 @@
13
13
 
14
14
  module PaperclipUpload
15
15
  class Upload < ActiveRecord::Base
16
+ IDENTIFIER_LENGTH = 8
17
+
16
18
  has_attached_file :file, path: ':rails_root/tmp/uploads/:id/:filename'
17
19
 
18
20
  do_not_validate_attachment_file_type :file
19
21
  validates_attachment_presence :file
22
+
23
+ def identifier
24
+ raise "valid with saved instance only" if self.id.blank?
25
+ self.class.hashid.encode(self.id)
26
+ end
27
+
28
+ def self.identifier_to_id(_identifier)
29
+ self.hashid.decode(_identifier).first
30
+ end
31
+
32
+ private
33
+
34
+ def self.hashid
35
+ Hashids.new(PaperclipUpload.hash_salt, IDENTIFIER_LENGTH)
36
+ end
20
37
  end
21
38
  end
@@ -1,3 +1,3 @@
1
1
  class PaperclipUpload::UploadSerializer < ActiveModel::Serializer
2
- attributes :id
2
+ attributes :identifier
3
3
  end
data/config/routes.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  PaperclipUpload::Engine.routes.draw do
2
- resources :uploads, only: [:create, :show], defaults: { format: :json }
2
+ resources :uploads, only: [:create], defaults: { format: :json }
3
3
  end
@@ -1,10 +1,17 @@
1
1
  class PaperclipUpload::InstallGenerator < Rails::Generators::Base
2
2
  source_root File.expand_path('../templates', __FILE__)
3
3
 
4
+ def create_initializer
5
+ template "initializer.rb", "config/initializers/paperclip_upload.rb"
6
+ end
7
+
4
8
  def mount_routes
5
9
  line = "Rails.application.routes.draw do"
6
10
  gsub_file "config/routes.rb", /(#{Regexp.escape(line)})/mi do |match|
7
- "#{match}\n mount PaperclipUpload::Engine => '/'\n"
11
+ <<-HERE.gsub(/^ {9}/, '')
12
+ #{match}
13
+ mount PaperclipUpload::Engine => '/'
14
+ HERE
8
15
  end
9
16
  end
10
17
 
@@ -0,0 +1,5 @@
1
+ PaperclipUpload.setup do |config|
2
+ # The upload module uses a salt string to generate an unique hash for each instance.
3
+ # A salt string can be defined here to replace the default and increase the module's security.
4
+ # config.hash_salt = "A new and improved string"
5
+ end
@@ -0,0 +1,14 @@
1
+ class UploadController < ApplicationController
2
+ self.responder = PaperclipUploadResponder
3
+ respond_to :json
4
+
5
+ def create
6
+ respond_with PaperclipUpload::Upload.create(permitted_params), status: :created
7
+ end
8
+
9
+ private
10
+
11
+ def permitted_params
12
+ params.permit(:file)
13
+ end
14
+ end
@@ -0,0 +1,29 @@
1
+ class PaperclipUpload::UploadControllerGenerator < Rails::Generators::NamedBase
2
+ source_root File.expand_path('../templates', __FILE__)
3
+ argument :base_controller, type: :string, :default => "application"
4
+
5
+ def generate_controller
6
+ generate "controller #{name}"
7
+ end
8
+
9
+ def replace_controller_with_template
10
+ copy_file "controller.rb", "app/controllers/#{name}_controller.rb", force: true
11
+ end
12
+
13
+ def customize_controller
14
+ line = "class UploadController < ApplicationController"
15
+ gsub_file "app/controllers/#{name}_controller.rb", /(#{Regexp.escape(line)})/mi do |match|
16
+ "class #{name.classify.pluralize}Controller < #{base_controller.classify}Controller"
17
+ end
18
+ end
19
+
20
+ def add_routes
21
+ line = "Rails.application.routes.draw do"
22
+ gsub_file "config/routes.rb", /(#{Regexp.escape(line)})/mi do |match|
23
+ <<-HERE.gsub(/^ {9}/, '')
24
+ #{match}
25
+ post "#{name}", to: "#{name}#create", defaults: { format: :json }
26
+ HERE
27
+ end
28
+ end
29
+ end
@@ -4,11 +4,14 @@ module PaperclipUpload
4
4
 
5
5
  class_methods do
6
6
  def has_attached_upload(_paperclip_attr_name, _options = {})
7
- attr_accessor :upload_id
7
+ attr_accessor :upload_identifier
8
8
  attr_accessor :upload
9
9
 
10
10
  before_validation do
11
- self.upload = PaperclipUpload::Upload.find(self.upload_id) if self.upload_id
11
+ if self.upload_identifier
12
+ decoded_id = PaperclipUpload::Upload.identifier_to_id(self.upload_identifier)
13
+ self.upload = PaperclipUpload::Upload.find(decoded_id)
14
+ end
12
15
 
13
16
  if self.upload
14
17
  if !self.upload.is_a? PaperclipUpload::Upload
@@ -1,3 +1,3 @@
1
1
  module PaperclipUpload
2
- VERSION = "0.1.0"
2
+ VERSION = "1.0.0"
3
3
  end
@@ -1,8 +1,22 @@
1
1
  require "paperclip"
2
2
  require "responders"
3
+ require 'hashids'
3
4
  require "active_model_serializers"
4
5
  require "paperclip_upload/active_record_extension"
5
6
  require "paperclip_upload/engine"
6
7
 
7
8
  module PaperclipUpload
9
+ extend self
10
+
11
+ attr_writer :hash_salt
12
+
13
+ def hash_salt
14
+ return "default" unless @hash_salt
15
+ @hash_salt
16
+ end
17
+
18
+ def setup
19
+ yield self
20
+ require "paperclip_upload"
21
+ end
8
22
  end
@@ -0,0 +1 @@
1
+ # HI! I'm just a dummy app
@@ -0,0 +1,5 @@
1
+ PaperclipUpload.setup do |config|
2
+ # The upload module uses a salt string to generate an unique hash for each instance.
3
+ # A salt string can be defined here to replace the default and increase the module's security.
4
+ # config.hash_salt = "A new and improved string"
5
+ end
@@ -1,4 +1,3 @@
1
1
  Rails.application.routes.draw do
2
-
3
- mount PaperclipUpload::Engine => "/"
2
+ mount PaperclipUpload::Engine => '/'
4
3
  end
@@ -5106,3 +5106,23 @@ Migrating to AddAttachmentPhotoToPromotions (20150613212609)
5106
5106
   (0.1ms) begin transaction
5107
5107
  SQL (0.4ms) INSERT INTO "promotions" ("created_at", "updated_at") VALUES (?, ?) [["created_at", "2015-06-13 22:09:43.053785"], ["updated_at", "2015-06-13 22:09:43.053785"]]
5108
5108
   (1.6ms) commit transaction
5109
+ PaperclipUpload::Upload Load (0.6ms) SELECT "paperclip_upload_uploads".* FROM "paperclip_upload_uploads" WHERE "paperclip_upload_uploads"."id" = ? LIMIT 1 [["id", 1]]
5110
+ PaperclipUpload::Upload Load (0.1ms) SELECT "paperclip_upload_uploads".* FROM "paperclip_upload_uploads" WHERE "paperclip_upload_uploads"."id" = ? LIMIT 1 [["id", 0]]
5111
+ PaperclipUpload::Upload Load (0.1ms) SELECT "paperclip_upload_uploads".* FROM "paperclip_upload_uploads" WHERE "paperclip_upload_uploads"."id" = ? LIMIT 1 [["id", 0]]
5112
+ PaperclipUpload::Upload Load (0.2ms) SELECT "paperclip_upload_uploads".* FROM "paperclip_upload_uploads" WHERE "paperclip_upload_uploads"."id" IN (1, 2)
5113
+ PaperclipUpload::Upload Load (0.2ms) SELECT "paperclip_upload_uploads".* FROM "paperclip_upload_uploads" WHERE "paperclip_upload_uploads"."id" IN (1, 33)
5114
+
5115
+
5116
+ Started GET "/" for ::1 at 2015-06-26 10:03:48 -0300
5117
+ ActiveRecord::SchemaMigration Load (0.5ms) SELECT "schema_migrations".* FROM "schema_migrations"
5118
+ Processing by Rails::WelcomeController#index as HTML
5119
+ Rendered /opt/rubies/2.0.0-p645/lib/ruby/gems/2.0.0/gems/railties-4.2.1/lib/rails/templates/rails/welcome/index.html.erb (1.7ms)
5120
+ Completed 200 OK in 8ms (Views: 8.1ms | ActiveRecord: 0.0ms)
5121
+
5122
+
5123
+ Started GET "/" for ::1 at 2015-06-26 10:13:19 -0300
5124
+ Processing by Rails::WelcomeController#index as HTML
5125
+ Rendered /opt/rubies/2.0.0-p645/lib/ruby/gems/2.0.0/gems/railties-4.2.1/lib/rails/templates/rails/welcome/index.html.erb (0.1ms)
5126
+ Completed 200 OK in 2ms (Views: 1.6ms | ActiveRecord: 0.0ms)
5127
+ DEPRECATION WARNING: Defining a route where `to` is a controller without an action is deprecated. Please change `to: :api/uploads/create` to `controller: :api/uploads/create`. (called from block in <top (required)> at /Users/leandro/src/paperclip_upload/spec/dummy/config/routes.rb:2)
5128
+ DEPRECATION WARNING: Defining a route where `to` is a controller without an action is deprecated. Please change `to: :api/uploads/create` to `controller: :api/uploads/create`. (called from block in <top (required)> at /Users/leandro/src/paperclip_upload/spec/dummy/config/routes.rb:2)