smarter_paperclip 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.rdoc ADDED
@@ -0,0 +1,4 @@
1
+ = Version 0.2.0
2
+ * Evolved to a gem from paperclip_extensions
3
+ = Version 0.1.0
4
+ * Gem existence started
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source "http://rubygems.org"
2
+ gemspec
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Maciej Mensfeld
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Manifest ADDED
@@ -0,0 +1,8 @@
1
+ CHANGELOG.rdoc
2
+ Gemfile
3
+ MIT-LICENSE
4
+ README.md
5
+ Rakefile
6
+ init.rb
7
+ lib/smarter_paperclip.rb
8
+ Manifest
data/README.md ADDED
@@ -0,0 +1,66 @@
1
+ # Smarter Paperclip
2
+
3
+ Collection of Paperclip extensions:
4
+
5
+ - `validates_attachment_minimum_resolution`
6
+ - `validates_attachment_if_included`
7
+ - `has_interpolated_attached_file`
8
+
9
+ Works with Ruby 1.8.7, 1.8.7 EE, 1.9.2
10
+ Works with Rails3
11
+
12
+ ## Install
13
+
14
+ gem install smarter_paperclip
15
+
16
+ and in your Gemfile:
17
+
18
+ gem 'smarter_paperclip'
19
+
20
+ ## Usage
21
+
22
+ `validates_attachment_minimum_resolution` - Picture minimal resolution validation. Parameters:
23
+
24
+ - width - minimal picture width
25
+ - height - minimal picture height
26
+ - message - error message - can be a Proc
27
+ - other params - std like for other validations (like if, unless, etc)
28
+
29
+
30
+ `validates_attachment_if_included` - It force attachment validation only if attachment (file) was send. If file was not send - it will not validate it (will pass without errors)
31
+
32
+ `has_interpolated_attached_file` - Allows you to use interpolations embed to object instance without worrying about file renaming. It rename them for you. Just use it instead of has_attached_file. Parameters same as with has_attached_file.
33
+
34
+ Of course you can use it instead of has_attached_file all the time, because with standard (not interpolated) models - it works same as has_attached_file.
35
+
36
+ ## Example
37
+
38
+ Interpolation
39
+
40
+ Paperclip.interpolates :instance_name do |attachment, style|
41
+ attachment.instance.name.to_url
42
+ end
43
+
44
+ Model
45
+
46
+ class Biography < ActiveRecord::Base
47
+ has_interpolated_attached_file :photo,
48
+ :url => "/images/models/biographies/:id/:instance_name_:style.:extension"
49
+
50
+ validates_attachment_if_included :photo,
51
+
52
+ validates_attachment_minimum_resolution :photo, :width => 500, :height => 500
53
+ end
54
+
55
+ ## Note on Patches/Pull Requests
56
+
57
+ * Fork the project.
58
+ * Make your feature addition or bug fix.
59
+ * Add tests for it. This is important so I don't break it in a future version unintentionally.
60
+ * Commit, do not mess with Rakefile, version, or history.
61
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
62
+ * Send me a pull request. Bonus points for topic branches.
63
+
64
+ ## Copyright
65
+
66
+ Copyright (c) 2011 Maciej Mensfeld. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'echoe'
4
+
5
+ Echoe.new('smarter_paperclip', '0.2.0') do |p|
6
+ p.description = "Collection of Paperclip extensions"
7
+ p.url = "https://github.com/mensfeld/smarter_paperclip"
8
+ p.author = "Maciej Mensfeld"
9
+ p.email = "maciej@mensfeld.pl"
10
+ p.ignore_pattern = ["tmp/*", "script/*"]
11
+ p.development_dependencies = ["rspec >=2.0.0", "paperclip"]
12
+ end
13
+
data/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ require 'smarter_paperclip'
2
+
@@ -0,0 +1,90 @@
1
+ require 'fileutils'
2
+ # Some usefull paperclip extensions
3
+ module Paperclip
4
+ module ClassMethods
5
+
6
+ # Picture minimal resolution validation
7
+ # Parameters
8
+ # - name - name of file field that should be validated
9
+ # - width - minimal picture width
10
+ # - height - minimal picture height
11
+ # - message - error message - can be a Proc
12
+ def validates_attachment_minimum_resolution name, options = {}
13
+ validation_options = options.dup
14
+ validates_each(name, validation_options) do |record, attr, value|
15
+ unless record.errors.include?(name)
16
+ m_width = options[:width]
17
+ m_height = options[:height]
18
+ message = options[:message] || "must be bigger."
19
+ message = message.call if message.is_a?(Proc)
20
+ image = record.send(name)
21
+ if image && image.queued_for_write[:original]
22
+ dimensions = Paperclip::Geometry.from_file(image.queued_for_write[:original])
23
+ if dimensions.width < m_width || dimensions.height < m_height
24
+ if record.errors.method(:add).arity == -2
25
+ record.errors.add(:"#{name}", message)
26
+ else
27
+ record.errors.add(:"#{name}", :inclusion, :default => options[:message], :value => value)
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+
35
+ # It force attachment validation only if attachment (file) was send
36
+ # if file was not send - it will not validate it (will pass without errors)
37
+ def validates_attachment_if_included name, options = {}
38
+ options[:if] = Proc.new { |imports| imports.send(name).file? }
39
+ validates_attachment_presence name, options
40
+ end
41
+
42
+ # Allows you to use interpolations embed to object instance without
43
+ # worrying about file renaming. It rename them for you.
44
+ # Just use it instead of has_attached_file
45
+ # Params:
46
+ # - same as has_attached_file
47
+ #
48
+ # Of course you can use it instead of has_attached_file all the time,
49
+ # because with standard (not interpolated) models - it works same as
50
+ # has_attached_file
51
+ def has_interpolated_attached_file name, options = {}
52
+
53
+ # Get old pathes to all files from file and save in instance variable
54
+ before_update do |record|
55
+ @interpolated_names = {} unless @interpolated_names
56
+ @interpolated_names[name] = {} unless @interpolated_names[name]
57
+ old_record = self.class.find(record.id)
58
+ (record.send(name).styles.keys+[:original]).each do |style|
59
+ @interpolated_names[name][style] = old_record.send(name).path(style)
60
+ end
61
+ end
62
+
63
+ # If validation has been passed - move files to a new location
64
+ after_update do |record|
65
+ (record.send(name).styles.keys+[:original]).each do |style|
66
+ orig_path = @interpolated_names[name][style]
67
+ dest_path = record.send(name).path(style)
68
+ if orig_path && dest_path && File.exist?(orig_path) && orig_path != dest_path
69
+ FileUtils.move(orig_path, dest_path)
70
+ end
71
+ end
72
+ end
73
+
74
+ # If renaming (or other callbacks) went wrong - restore old names to files
75
+ after_rollback do |record|
76
+ return unless @interpolated_names
77
+ (record.send(name).styles.keys+[:original]).each do |style|
78
+ dest_path = @interpolated_names[name][style]
79
+ orig_path = record.send(name).path(style)
80
+ if orig_path && dest_path && File.exist?(orig_path) && orig_path != dest_path
81
+ FileUtils.move(orig_path, dest_path)
82
+ end
83
+ end
84
+ end
85
+
86
+ has_attached_file name, options
87
+ end
88
+
89
+ end
90
+ end
@@ -0,0 +1,37 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{smarter_paperclip}
5
+ s.version = "0.2.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Maciej Mensfeld"]
9
+ s.cert_chain = ["/home/mencio/.cert_keys/gem-public_cert.pem"]
10
+ s.date = %q{2011-04-10}
11
+ s.description = %q{Collection of Paperclip extensions}
12
+ s.email = %q{maciej@mensfeld.pl}
13
+ s.extra_rdoc_files = ["CHANGELOG.rdoc", "README.md", "lib/smarter_paperclip.rb"]
14
+ s.files = ["CHANGELOG.rdoc", "Gemfile", "MIT-LICENSE", "README.md", "Rakefile", "init.rb", "lib/smarter_paperclip.rb", "Manifest", "smarter_paperclip.gemspec"]
15
+ s.homepage = %q{https://github.com/mensfeld/smarter_paperclip}
16
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Smarter_paperclip", "--main", "README.md"]
17
+ s.require_paths = ["lib"]
18
+ s.rubyforge_project = %q{smarter_paperclip}
19
+ s.rubygems_version = %q{1.5.2}
20
+ s.signing_key = %q{/home/mencio/.cert_keys/gem-private_key.pem}
21
+ s.summary = %q{Collection of Paperclip extensions}
22
+
23
+ if s.respond_to? :specification_version then
24
+ s.specification_version = 3
25
+
26
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
27
+ s.add_development_dependency(%q<rspec>, [">= 2.0.0"])
28
+ s.add_development_dependency(%q<paperclip>, [">= 0"])
29
+ else
30
+ s.add_dependency(%q<rspec>, [">= 2.0.0"])
31
+ s.add_dependency(%q<paperclip>, [">= 0"])
32
+ end
33
+ else
34
+ s.add_dependency(%q<rspec>, [">= 2.0.0"])
35
+ s.add_dependency(%q<paperclip>, [">= 0"])
36
+ end
37
+ end
data.tar.gz.sig ADDED
Binary file
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: smarter_paperclip
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.2.0
6
+ platform: ruby
7
+ authors:
8
+ - Maciej Mensfeld
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain:
12
+ - |
13
+ -----BEGIN CERTIFICATE-----
14
+ MIIDMjCCAhqgAwIBAgIBADANBgkqhkiG9w0BAQUFADA/MQ8wDQYDVQQDDAZtYWNp
15
+ ZWoxGDAWBgoJkiaJk/IsZAEZFghtZW5zZmVsZDESMBAGCgmSJomT8ixkARkWAnBs
16
+ MB4XDTExMDQwOTA5NDcyMloXDTEyMDQ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
+ default_executable:
36
+ dependencies:
37
+ - !ruby/object:Gem::Dependency
38
+ name: rspec
39
+ prerelease: false
40
+ requirement: &id001 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: 2.0.0
46
+ type: :development
47
+ version_requirements: *id001
48
+ - !ruby/object:Gem::Dependency
49
+ name: paperclip
50
+ prerelease: false
51
+ requirement: &id002 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ type: :development
58
+ version_requirements: *id002
59
+ description: Collection of Paperclip extensions
60
+ email: maciej@mensfeld.pl
61
+ executables: []
62
+
63
+ extensions: []
64
+
65
+ extra_rdoc_files:
66
+ - CHANGELOG.rdoc
67
+ - README.md
68
+ - lib/smarter_paperclip.rb
69
+ files:
70
+ - CHANGELOG.rdoc
71
+ - Gemfile
72
+ - MIT-LICENSE
73
+ - README.md
74
+ - Rakefile
75
+ - init.rb
76
+ - lib/smarter_paperclip.rb
77
+ - Manifest
78
+ - smarter_paperclip.gemspec
79
+ has_rdoc: true
80
+ homepage: https://github.com/mensfeld/smarter_paperclip
81
+ licenses: []
82
+
83
+ post_install_message:
84
+ rdoc_options:
85
+ - --line-numbers
86
+ - --inline-source
87
+ - --title
88
+ - Smarter_paperclip
89
+ - --main
90
+ - README.md
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: "0"
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ none: false
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: "1.2"
105
+ requirements: []
106
+
107
+ rubyforge_project: smarter_paperclip
108
+ rubygems_version: 1.5.2
109
+ signing_key:
110
+ specification_version: 3
111
+ summary: Collection of Paperclip extensions
112
+ test_files: []
113
+
metadata.gz.sig ADDED
Binary file