paperclip-staging 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NTlkZDYzOTkwYzFiMDU3YTdjZjRlZjhkZWQ0MjNhNGE4ODVlZTNhOA==
5
+ data.tar.gz: !binary |-
6
+ MGZiYTRiNzUxMTY2NGRjNzYzYjVlYjJhZTRhNWY4ZWVmZDRhOWY5NA==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ MWVmOWE4Mjc4ZmY5MWM2YmE0YzJkYjY1NWM5ODM1ZjVjMDY5MzI4MjY1NWFj
10
+ NGI4NGM3YWFjYWVlODg0MmE2ZWM2NTQ5ZWNhZjQ3MDUxZjgzNmQwMTdhYjJi
11
+ ZDE0NjllYTRlNzk4OTQyZDI3MjA4ODdhNjNiMGQwYmU2ZjY1YjU=
12
+ data.tar.gz: !binary |-
13
+ MDQ3MjY4OTNiYWQzMmVkNDk5ZjI4ZjIwY2UwYWMxZTNjMTcyY2MwYTI1NTNk
14
+ MzNhMzk1YWM3OGI4ZjM3YjlmN2MxYzU0NjdiYmM2YWJkNjgzNzY2MzZlYWUx
15
+ MGNjN2Y4ZGM0YWFkMzlkZjgxNGQ2YzY0MGUwYWUwMmM0MTVjMDU=
data/.gitignore ADDED
@@ -0,0 +1,16 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+ *.gem
16
+ /.ruby-*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in paperclip-staging.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Tomasz Dąbrowski
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,53 @@
1
+ # Paperclip::Staging
2
+
3
+ Allow [Paperclip](https://github.com/thoughtbot/paperclip) to pass attachments as data-uri on unsaved records. Useful when dealing with forms and validation errors.
4
+
5
+ Imagine you have two fields on a model - `name` and `attachment`. Let's say that `name` has a validation. If you edit an object and change both `name` and `attachment`, but the new name doesn't meet the validation, then you lose the new `attachment`.
6
+
7
+ With paperclip-staging, the attachment is preserved using a hidden field. It is also available with `staged_url(style_name)` (instead of regular `url(style_name)`) calls, using data-uri encoding - which is useful if you want to display current value for the attachment/image.
8
+
9
+ You can check the [example project](https://github.com/dabroz/paperclip-staging-example).
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'paperclip-staging'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install paperclip-staging
26
+
27
+ ## Usage
28
+
29
+ 1. Add a new hidden field in the form, named `<attachment_name>_staging`.
30
+
31
+ `f.hidden_field :file_staging`
32
+
33
+ or
34
+
35
+ `f.input :file_staging, as: :hidden` for `simple_form`
36
+
37
+ 2. Add this field as a valid form param:
38
+
39
+ `attr_accessible :file_staging`
40
+
41
+ or
42
+
43
+ `params.require(xxx).permit(xxx, :file_staging)` for Rails 4
44
+
45
+ 3. That's it!
46
+
47
+ ## Contributing
48
+
49
+ 1. Fork it ( https://github.com/dabroz/paperclip-staging/fork )
50
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
51
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
52
+ 4. Push to the branch (`git push origin my-new-feature`)
53
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,92 @@
1
+ require "paperclip/staging/version"
2
+
3
+ module Paperclip
4
+ module Staging
5
+ end
6
+ end
7
+
8
+ class NamedStringIO < StringIO
9
+ attr_accessor :original_filename
10
+ attr_accessor :content_type
11
+
12
+ def initialize(text, filename, type)
13
+ super(text)
14
+ self.original_filename = filename
15
+ self.content_type = type
16
+ end
17
+ end
18
+
19
+ # module UrlGeneratorExtensions
20
+ # def for(style_name, options)
21
+ # end
22
+ # end
23
+
24
+ module AttachmentExtensions
25
+ def staged_url(style_name = default_style, options = {})
26
+ queued = queued_for_write[style_name]
27
+ if queued
28
+ data = Base64.strict_encode64(File.read(queued.path))
29
+ "data:#{queued.content_type};base64,#{data}"
30
+ else
31
+ url(style_name, options)
32
+ end
33
+ end
34
+ end
35
+
36
+ module HasAttachedFileExtensions
37
+ def define
38
+ super
39
+ define_accessor_staging
40
+ end
41
+
42
+ def define_accessor_staging
43
+ name = @name
44
+ staging_getter_name = "#{name.to_s}_staging".to_sym
45
+ staging_setter_name = "#{name.to_s}_staging=".to_sym
46
+
47
+ already_updated_ivar = "@attachment_#{name}_already_updated".to_sym
48
+
49
+ @klass.send :define_method, staging_getter_name do
50
+ ivar = "@attachment_#{name}"
51
+ attachment = instance_variable_get(ivar)
52
+ return nil unless attachment
53
+ queued = attachment.queued_for_write[:original]
54
+ if queued
55
+ filename = attachment.original_filename
56
+ type = attachment.content_type
57
+ body = File.read(queued.path)
58
+ [filename, type, body].map{|text|Base64.strict_encode64(text)}.join('|')
59
+ end
60
+ end
61
+
62
+ old_attachment_setter = "#{name.to_s}_original_setter".to_sym
63
+ @klass.send :alias_method, old_attachment_setter, "#{@name}="
64
+
65
+ @klass.send :define_method, "#{@name}=" do |file|
66
+ instance_variable_set(already_updated_ivar, true)
67
+ send(old_attachment_setter, file)
68
+ end
69
+
70
+ @klass.send :define_method, staging_setter_name do |value|
71
+ return if value.blank?
72
+ already_updated = instance_variable_get(already_updated_ivar)
73
+ return if already_updated
74
+ filename, type, body = value.split('|').map{|text|Base64.strict_decode64(text)}
75
+ send(name).assign(NamedStringIO.new(body, filename, type))
76
+ end
77
+ end
78
+ end
79
+
80
+ module Paperclip
81
+ class Attachment
82
+ prepend AttachmentExtensions
83
+ end
84
+
85
+ # class UrlGenerator
86
+ # prepend UrlGeneratorExtensions
87
+ # end
88
+
89
+ class HasAttachedFile
90
+ prepend HasAttachedFileExtensions
91
+ end
92
+ end
@@ -0,0 +1,5 @@
1
+ module Paperclip
2
+ module Staging
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'paperclip/staging/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "paperclip-staging"
8
+ spec.version = Paperclip::Staging::VERSION
9
+ spec.authors = ["Tomasz Dąbrowski"]
10
+ spec.email = ["t.dabrowski@rock-hard.eu"]
11
+ spec.summary = %q{Allow Paperclip to pass attachments as data-uri on unsaved records.}
12
+ spec.description = %q{Allow Paperclip to pass attachments as data-uri on unsaved records. Useful when dealing with forms and validation errors.}
13
+ spec.homepage = "https://github.com/dabroz/paperclip-staging"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_runtime_dependency 'paperclip', '~> 4.2'
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.7"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: paperclip-staging
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Tomasz Dąbrowski
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-04-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: paperclip
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '4.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '4.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ description: Allow Paperclip to pass attachments as data-uri on unsaved records. Useful
56
+ when dealing with forms and validation errors.
57
+ email:
58
+ - t.dabrowski@rock-hard.eu
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - .gitignore
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - lib/paperclip/staging.rb
69
+ - lib/paperclip/staging/version.rb
70
+ - paperclip-staging.gemspec
71
+ homepage: https://github.com/dabroz/paperclip-staging
72
+ licenses:
73
+ - MIT
74
+ metadata: {}
75
+ post_install_message:
76
+ rdoc_options: []
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ! '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ! '>='
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ requirements: []
90
+ rubyforge_project:
91
+ rubygems_version: 2.4.3
92
+ signing_key:
93
+ specification_version: 4
94
+ summary: Allow Paperclip to pass attachments as data-uri on unsaved records.
95
+ test_files: []