paperclip_helpers 0.2

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.
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2011 Nick Ragaz
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/README.rdoc ADDED
@@ -0,0 +1,3 @@
1
+ = Paperclip Helpers
2
+
3
+ This project rocks and uses MIT-LICENSE.
data/Rakefile ADDED
@@ -0,0 +1,29 @@
1
+ # encoding: UTF-8
2
+ require 'rubygems'
3
+ begin
4
+ require 'bundler/setup'
5
+ rescue LoadError
6
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
7
+ end
8
+
9
+ require 'rake'
10
+ require 'rdoc/task'
11
+
12
+ require 'rake/testtask'
13
+
14
+ Rake::TestTask.new(:test) do |t|
15
+ t.libs << 'lib'
16
+ t.libs << 'test'
17
+ t.pattern = 'test/**/*_test.rb'
18
+ t.verbose = false
19
+ end
20
+
21
+ task :default => :test
22
+
23
+ Rake::RDocTask.new(:rdoc) do |rdoc|
24
+ rdoc.rdoc_dir = 'rdoc'
25
+ rdoc.title = 'Paperclip Helpers'
26
+ rdoc.options << '--line-numbers' << '--inline-source'
27
+ rdoc.rdoc_files.include('README.rdoc')
28
+ rdoc.rdoc_files.include('lib/**/*.rb')
29
+ end
@@ -0,0 +1,11 @@
1
+ Paperclip::Attachment.default_options[:convert_options] = {
2
+ all: "-strip -quality 40 -colors 256"
3
+ }
4
+
5
+ module Paperclip
6
+ class Attachment
7
+ def current_path(style=:original)
8
+ @queued_for_write[style].try(:path) || path(style)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,28 @@
1
+ module PaperclipHelpers::Attachments
2
+ extend ActiveSupport::Concern
3
+
4
+ private
5
+
6
+ def attach(obj, attachment, style, expected_format=:png, filename=nil, opts={})
7
+ return status_404 unless obj.send(attachment).file?
8
+
9
+ filename ||= "#{obj.id}_#{style}.#{expected_format}"
10
+
11
+ path = obj.send(attachment).path(style)
12
+ path.gsub!(/\.\w\w\w$/i, ".#{expected_format}")
13
+
14
+ disposition = opts[:disposition] ||
15
+ ( expected_format == :pdf ? 'attachment' : 'inline' )
16
+
17
+ return status_404 unless File.exist?(path)
18
+
19
+ send_file path,
20
+ type: MIME::Types.type_for(path).first.content_type,
21
+ disposition: disposition,
22
+ filename: filename
23
+ end
24
+
25
+ def status_404
26
+ render :file => "#{Rails.root}/public/404.html", :status => 404
27
+ end
28
+ end
@@ -0,0 +1,13 @@
1
+ module PaperclipHelpers::SetGeometry
2
+ # e.g. set_geometry_for :pdf, :thumbnail
3
+ def set_geometry_for(attachment, style, column=nil)
4
+ column ||= :"#{style}_size"
5
+ file_path = send(attachment).current_path(style)
6
+
7
+ if file_path && File.exist?(file_path)
8
+ output = `identify "#{file_path}"`.split(" ")[2]
9
+ send "#{column}=", output
10
+ output
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,38 @@
1
+ module PaperclipHelpers::TestHelpers
2
+ extend ActiveSupport::Concern
3
+
4
+ module ClassMethods
5
+ def should_have_attached_file(attachment)
6
+ klass = self.name.gsub(/Test$/, '').constantize
7
+
8
+ context "For an attachment named #{attachment}, #{klass}" do
9
+ should_have_db_column "#{attachment}_file_name", type: :string
10
+ should_have_db_column "#{attachment}_content_type", type: :string
11
+ should_have_db_column "#{attachment}_file_size", type: :integer
12
+ end
13
+
14
+ should "have an attachment named ##{attachment}" do
15
+ assert klass.new.respond_to?(attachment.to_sym),
16
+ "@#{klass.name.underscore} doesn't have a field named #{attachment}"
17
+ assert_equal Paperclip::Attachment,
18
+ klass.new.send(attachment.to_sym).class
19
+ end
20
+ end
21
+ end
22
+
23
+ def count_pages(pdf_file_path)
24
+ return nil unless pdf_file_path.last(4) == ".pdf"
25
+
26
+ output = `identify \"#{pdf_file_path}\"`
27
+ raise "Error counting pages in #{pdf_file_path}" unless $?.exitstatus == 0
28
+
29
+ output.split("\n").count
30
+ end
31
+
32
+ def get_image_size(image_file_path)
33
+ output = `identify \"#{image_file_path}\"`
34
+ raise "Error getting size of #{image_file_path}" unless $?.exitstatus == 0
35
+
36
+ output.split(" ")[2]
37
+ end
38
+ end
@@ -0,0 +1,15 @@
1
+ require 'active_support/dependencies'
2
+ require 'paperclip'
3
+
4
+ ActiveSupport.on_load(:action_controller) do
5
+ include PaperclipHelpers::Attachments
6
+ end
7
+
8
+ module PaperclipHelpers
9
+ autoload :Attachments, 'paperclip_helpers/attachments'
10
+ autoload :SetGeometry, 'paperclip_helpers/set_geometry'
11
+ autoload :TestHelpers, 'paperclip_helpers/test_helpers'
12
+
13
+ class Engine < Rails::Engine
14
+ end
15
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: paperclip_helpers
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.2'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Nick Ragaz
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-03-20 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: &70342046432080 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '3'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70342046432080
25
+ - !ruby/object:Gem::Dependency
26
+ name: paperclip
27
+ requirement: &70342046431680 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70342046431680
36
+ - !ruby/object:Gem::Dependency
37
+ name: sqlite3
38
+ requirement: &70342046431220 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70342046431220
47
+ description: Initializers and helper methods for using Paperclip.
48
+ email:
49
+ - nick.ragaz@gmail.com
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - lib/paperclip_helpers/attachments.rb
55
+ - lib/paperclip_helpers/set_geometry.rb
56
+ - lib/paperclip_helpers/test_helpers.rb
57
+ - lib/paperclip_helpers.rb
58
+ - config/initializers/paperclip.rb
59
+ - MIT-LICENSE
60
+ - Rakefile
61
+ - Gemfile
62
+ - README.rdoc
63
+ homepage:
64
+ licenses: []
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ! '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ! '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ requirements: []
82
+ rubyforge_project:
83
+ rubygems_version: 1.8.11
84
+ signing_key:
85
+ specification_version: 3
86
+ summary: Initializers and helper methods for using Paperclip.
87
+ test_files: []