paperclip-dimension 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gem "paperclip", '>= 2.4'
4
+
5
+ # Add dependencies to develop your gem here.
6
+ # Include everything needed to run rake, tests, features, etc.
7
+ group :development do
8
+ gem 'activerecord'
9
+ gem 'sqlite3'
10
+ gem 'rspec'
11
+ gem 'yard'
12
+ gem 'bundler'
13
+ gem 'jeweler'
14
+ end
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Aaron Qian
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.
@@ -0,0 +1,132 @@
1
+ paperclip-dimension
2
+ ===================
3
+
4
+ This gem is the improved version of `mongoid_paperclip_image_dimension` that is ORM agnostic.
5
+ If you are using `mongoid_paperclip_image_dimension`, you should upgrade to this gem.
6
+
7
+ This is a simple gem to persist image dimensions of originals and thumbnails.
8
+ It works for both s3 and filesystem storages.
9
+ This lib is based on [this stackoverflow.com article](http://stackoverflow.com/questions/4065295/paperclip-saving-the-images-dimentions-width-height).
10
+
11
+ Install
12
+ -------
13
+
14
+ You can simply install from rubygems:
15
+
16
+ ```ruby
17
+ gem install paperclip-dimension
18
+ require 'paperclip-dimension'
19
+ ```
20
+
21
+ or in Gemfile:
22
+
23
+ ```ruby
24
+ gem 'paperclip-dimension'
25
+ ```
26
+
27
+ Setup
28
+ -----
29
+
30
+ You need to make sure the following is in place:
31
+
32
+ * `Paperclip::Glue` is included in your model.
33
+ * `<attachment>_dimensions` attribute is requried to operate.
34
+
35
+
36
+ **Rails & ActiveRecord Migration**
37
+
38
+ `paperclip` gives you the ability to create migrations with `has_attached_file`.
39
+ For example:
40
+
41
+ ```ruby
42
+ class CreatePosts < ActiveRecord::Migration
43
+ def up
44
+ create_table :posts do |t|
45
+ t.has_attached_file :image
46
+ end
47
+ end
48
+ end
49
+ ```
50
+
51
+ This creates the following columns:
52
+
53
+ * image_file_name
54
+ * image_file_size
55
+ * image_content_type
56
+ * image_updated_at
57
+
58
+ `paperclip-dimension` will create an extra field for you: `image_dimensions` when the above migration is run.
59
+
60
+ **Mongoid**
61
+
62
+ Exmaple Mongoid setup:
63
+
64
+ ```ruby
65
+ class Post
66
+ include Mongoid::Document
67
+ include Paperclip::Glue
68
+
69
+ # The usual paperclip fields
70
+ field :image_file_name, :type => String
71
+ field :image_file_size, :type => Integer
72
+ field :image_content_type, :type => String
73
+ field :image_updated_at, :type => Time
74
+
75
+ # The extra paperclip-dimension field
76
+ field :image_dimensions, :type => Hash
77
+
78
+ # call #has_attached_file like usual
79
+ has_attached_file :image, :styles => {
80
+ :large => ['350x350>', :jpg],
81
+ :medium => ['150x150>', :jpg],
82
+ :small => ['30x30>', :jpg]
83
+ }
84
+ end
85
+ ```
86
+
87
+ Usage
88
+ -----
89
+
90
+ This gem gives your model a few extra helpers:
91
+
92
+ ```ruby
93
+ # suppose we used the example from above
94
+ p = Post.first
95
+
96
+ # retrieve image dimensions for all sytles
97
+ p.image_dimensions
98
+ # {
99
+ # 'original' => [2048, 1024],
100
+ # 'large' => [350, 175],
101
+ # 'medium' => [150, 75],
102
+ # 'small' => [30, 15]
103
+ # }
104
+
105
+ # get image dimension as an array
106
+ p.image_dimension # [2048, 1024]
107
+ p.image_dimension(:original) # [2048, 1024]
108
+ p.image_dimension(:large) # [350, 175]
109
+
110
+ # get it now as a string
111
+ p.image_dimension_str # "2048x1024"
112
+ p.image_dimension_str(:original) # "2048x1024"
113
+ p.image_dimension_str(:large) # "350x175"
114
+ ```
115
+
116
+ Contributing to paperclip-dimension
117
+ -----------------------------------
118
+
119
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
120
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
121
+ * Fork the project
122
+ * Start a feature/bugfix branch
123
+ * Commit and push until you are happy with your contribution
124
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
125
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
126
+
127
+ Copyright
128
+ ---------
129
+
130
+ Copyright (c) 2011 Aaron Qian. See LICENSE.txt for
131
+ further details.
132
+
@@ -0,0 +1,38 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ gem.name = "paperclip-dimension"
15
+ gem.homepage = "http://github.com/aq1018/paperclip-dimension"
16
+ gem.license = "MIT"
17
+ gem.summary = %Q{A simple plugin to persist image dimensions.}
18
+ gem.description = %Q{A simple plugin to persist image dimensions.}
19
+ gem.email = "aq1018@gmail.com"
20
+ gem.authors = ["Aaron Qian"]
21
+
22
+ # this directory is generated by running the test, and should be ignored...
23
+ gem.files.exclude 'public'
24
+ end
25
+
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ spec.rspec_opts = "--color --format progress"
33
+ end
34
+
35
+ task :default => :spec
36
+
37
+ require 'yard'
38
+ YARD::Rake::YardocTask.new
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,63 @@
1
+ require 'paperclip'
2
+
3
+ module Paperclip
4
+ module Dimension
5
+ def self.included(klass)
6
+ klass.extend Paperclip::Dimension::ClassMethods
7
+ end
8
+
9
+ module ClassMethods
10
+ # override has_attached_file to:
11
+ # 1). save dimensions on post process
12
+ # 2). create dimension accessors
13
+ def has_attached_file name, options={}
14
+ super
15
+
16
+ class_eval <<-END
17
+ # for ActiveRecord
18
+ serialize :#{name}_dimensions, Hash if respond_to?(:serialize)
19
+
20
+ def #{name}_dimension(style=:original)
21
+ self.#{name}_dimensions[style]
22
+ end
23
+
24
+ def #{name}_dimension_str(style=:original)
25
+ dim = #{name}_dimension(style)
26
+ dim ? dim.join('x') : nil
27
+ end
28
+ END
29
+
30
+ send "after_#{name}_post_process", lambda { save_dimensions_for(name) }
31
+ end
32
+ end
33
+
34
+ def save_dimensions_for(name)
35
+ opts = self.class.attachment_definitions[name]
36
+
37
+ styles = opts[:styles].keys + [:original]
38
+ dimension_hash = {}
39
+ styles.each do |style|
40
+ attachment = self.send name
41
+ geo = ::Paperclip::Geometry.from_file(attachment.queued_for_write[style])
42
+ dimension_hash[style] = [ geo.width.to_i, geo.height.to_i ]
43
+ end
44
+ self.send "#{name}_dimensions=", dimension_hash
45
+ end
46
+ end
47
+
48
+ module Glue
49
+ class << self
50
+ def included_with_dimension(klass)
51
+ included_without_dimension(klass)
52
+ klass.send :include, Paperclip::Dimension
53
+ end
54
+
55
+ alias :included_without_dimension :included
56
+ alias :included :included_with_dimension
57
+ end
58
+ end
59
+
60
+ module Schema
61
+ @@columns[:dimensions] = :string
62
+ end
63
+ end
@@ -0,0 +1,69 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "paperclip-dimension"
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Aaron Qian"]
12
+ s.date = "2012-01-28"
13
+ s.description = "A simple plugin to persist image dimensions."
14
+ s.email = "aq1018@gmail.com"
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".rspec",
22
+ "Gemfile",
23
+ "LICENSE.txt",
24
+ "README.md",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "lib/paperclip-dimension.rb",
28
+ "paperclip-dimension.gemspec",
29
+ "spec/paperclip-dimension_spec.rb",
30
+ "spec/ruby.png",
31
+ "spec/spec_helper.rb"
32
+ ]
33
+ s.homepage = "http://github.com/aq1018/paperclip-dimension"
34
+ s.licenses = ["MIT"]
35
+ s.require_paths = ["lib"]
36
+ s.rubygems_version = "1.8.10"
37
+ s.summary = "A simple plugin to persist image dimensions."
38
+
39
+ if s.respond_to? :specification_version then
40
+ s.specification_version = 3
41
+
42
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
43
+ s.add_runtime_dependency(%q<paperclip>, [">= 2.4"])
44
+ s.add_development_dependency(%q<activerecord>, [">= 0"])
45
+ s.add_development_dependency(%q<sqlite3>, [">= 0"])
46
+ s.add_development_dependency(%q<rspec>, [">= 0"])
47
+ s.add_development_dependency(%q<yard>, [">= 0"])
48
+ s.add_development_dependency(%q<bundler>, [">= 0"])
49
+ s.add_development_dependency(%q<jeweler>, [">= 0"])
50
+ else
51
+ s.add_dependency(%q<paperclip>, [">= 2.4"])
52
+ s.add_dependency(%q<activerecord>, [">= 0"])
53
+ s.add_dependency(%q<sqlite3>, [">= 0"])
54
+ s.add_dependency(%q<rspec>, [">= 0"])
55
+ s.add_dependency(%q<yard>, [">= 0"])
56
+ s.add_dependency(%q<bundler>, [">= 0"])
57
+ s.add_dependency(%q<jeweler>, [">= 0"])
58
+ end
59
+ else
60
+ s.add_dependency(%q<paperclip>, [">= 2.4"])
61
+ s.add_dependency(%q<activerecord>, [">= 0"])
62
+ s.add_dependency(%q<sqlite3>, [">= 0"])
63
+ s.add_dependency(%q<rspec>, [">= 0"])
64
+ s.add_dependency(%q<yard>, [">= 0"])
65
+ s.add_dependency(%q<bundler>, [">= 0"])
66
+ s.add_dependency(%q<jeweler>, [">= 0"])
67
+ end
68
+ end
69
+
@@ -0,0 +1,44 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe Paperclip::Dimension do
4
+ before(:each) do
5
+ @p = Post.create!({
6
+ :image =>File.open(File.dirname(__FILE__) + '/ruby.png'),
7
+ :another_image => File.open(File.dirname(__FILE__) + '/ruby.png')
8
+ })
9
+ @p.reload
10
+ end
11
+
12
+ it "should save dimensions" do
13
+ @p.image_dimensions.should_not be_nil
14
+ @p.another_image_dimensions.should_not be_nil
15
+ end
16
+
17
+ it "should retreive dimensions correctly" do
18
+ @p.image_dimension.should == [995, 996]
19
+ @p.image_dimension(:original).should == [995, 996]
20
+ @p.image_dimension(:large).should == [350, 350]
21
+ @p.image_dimension(:medium).should == [150, 150]
22
+ @p.image_dimension(:small).should == [30, 30]
23
+
24
+ @p.another_image_dimension.should == [995, 996]
25
+ @p.another_image_dimension(:original).should == [995, 996]
26
+ @p.another_image_dimension(:large).should == [350, 350]
27
+ @p.another_image_dimension(:medium).should == [150, 150]
28
+ @p.another_image_dimension(:small).should == [30, 30]
29
+ end
30
+
31
+ it "should retreive dimension strings correctly" do
32
+ @p.image_dimension_str.should == "995x996"
33
+ @p.image_dimension_str(:original).should == "995x996"
34
+ @p.image_dimension_str(:large).should == "350x350"
35
+ @p.image_dimension_str(:medium).should == "150x150"
36
+ @p.image_dimension_str(:small).should == "30x30"
37
+
38
+ @p.another_image_dimension_str.should == "995x996"
39
+ @p.another_image_dimension_str(:original).should == "995x996"
40
+ @p.another_image_dimension_str(:large).should == "350x350"
41
+ @p.another_image_dimension_str(:medium).should == "150x150"
42
+ @p.another_image_dimension_str(:small).should == "30x30"
43
+ end
44
+ end
Binary file
@@ -0,0 +1,61 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+
4
+ require 'rspec'
5
+ require 'active_record'
6
+ require 'paperclip'
7
+ require 'paperclip-dimension'
8
+ require 'paperclip/railtie'
9
+
10
+ # mocking Rails.root & Rails.env used in Paperclip::Interploration
11
+ module Rails
12
+ def self.root
13
+ File.dirname(__FILE__) + "/.."
14
+ end
15
+
16
+ def self.env
17
+ "test"
18
+ end
19
+
20
+ def self.logger
21
+ nil
22
+ end
23
+ end
24
+
25
+ # need to manually call this
26
+ Paperclip::Railtie.insert
27
+
28
+ # turn off logging
29
+ Paperclip.options[:log] = false
30
+
31
+ # use sqlite3 memory store
32
+ ActiveRecord::Base.establish_connection({
33
+ :adapter => 'sqlite3',
34
+ :database => ':memory:'
35
+ })
36
+
37
+ # create tables
38
+ ActiveRecord::Schema.define do
39
+ create_table :posts do |t|
40
+ t.has_attached_file :image
41
+ t.has_attached_file :another_image
42
+ end
43
+ end
44
+
45
+ # define model
46
+ class Post < ActiveRecord::Base
47
+ extend Paperclip::Dimension::ClassMethods
48
+ has_attached_file :image, :styles => {
49
+ :large => ['350x350>', :jpg],
50
+ :medium => ['150x150>', :jpg],
51
+ :small => ['30x30>', :jpg]
52
+ }
53
+
54
+ has_attached_file :another_image, :styles => {
55
+ :large => ['350x350>', :jpg],
56
+ :medium => ['150x150>', :jpg],
57
+ :small => ['30x30>', :jpg]
58
+ }
59
+ end
60
+
61
+
metadata ADDED
@@ -0,0 +1,139 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: paperclip-dimension
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Aaron Qian
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-01-28 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: paperclip
16
+ requirement: &20936760 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '2.4'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *20936760
25
+ - !ruby/object:Gem::Dependency
26
+ name: activerecord
27
+ requirement: &20935780 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *20935780
36
+ - !ruby/object:Gem::Dependency
37
+ name: sqlite3
38
+ requirement: &20934360 !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: *20934360
47
+ - !ruby/object:Gem::Dependency
48
+ name: rspec
49
+ requirement: &20932400 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *20932400
58
+ - !ruby/object:Gem::Dependency
59
+ name: yard
60
+ requirement: &20931660 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *20931660
69
+ - !ruby/object:Gem::Dependency
70
+ name: bundler
71
+ requirement: &20931000 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *20931000
80
+ - !ruby/object:Gem::Dependency
81
+ name: jeweler
82
+ requirement: &20930080 !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: *20930080
91
+ description: A simple plugin to persist image dimensions.
92
+ email: aq1018@gmail.com
93
+ executables: []
94
+ extensions: []
95
+ extra_rdoc_files:
96
+ - LICENSE.txt
97
+ - README.md
98
+ files:
99
+ - .document
100
+ - .rspec
101
+ - Gemfile
102
+ - LICENSE.txt
103
+ - README.md
104
+ - Rakefile
105
+ - VERSION
106
+ - lib/paperclip-dimension.rb
107
+ - paperclip-dimension.gemspec
108
+ - spec/paperclip-dimension_spec.rb
109
+ - spec/ruby.png
110
+ - spec/spec_helper.rb
111
+ homepage: http://github.com/aq1018/paperclip-dimension
112
+ licenses:
113
+ - MIT
114
+ post_install_message:
115
+ rdoc_options: []
116
+ require_paths:
117
+ - lib
118
+ required_ruby_version: !ruby/object:Gem::Requirement
119
+ none: false
120
+ requirements:
121
+ - - ! '>='
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ segments:
125
+ - 0
126
+ hash: -1625625660612208703
127
+ required_rubygems_version: !ruby/object:Gem::Requirement
128
+ none: false
129
+ requirements:
130
+ - - ! '>='
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ requirements: []
134
+ rubyforge_project:
135
+ rubygems_version: 1.8.10
136
+ signing_key:
137
+ specification_version: 3
138
+ summary: A simple plugin to persist image dimensions.
139
+ test_files: []