paperclip-meta 0.1 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -6,6 +6,10 @@ Paperclip Meta will get image dimensions right after post_process_styles using p
6
6
 
7
7
  ==Quick Start
8
8
 
9
+ Add paperclip-meta to Gemfile:
10
+
11
+ gem 'paperclip-meta'
12
+
9
13
  Create migration:
10
14
 
11
15
  class AddMetaToAvatar < ActiveRecord::Migration
@@ -38,7 +42,7 @@ Meta column is simple hash:
38
42
 
39
43
  This hash will be marshaled and base64 encoded before writing to model attribute.
40
44
 
41
- New methods provided:
45
+ Meta methods provided:
42
46
  - width
43
47
  - height
44
48
  - size
data/Rakefile CHANGED
@@ -1,8 +1,34 @@
1
1
  require 'rake'
2
2
  require 'rake/testtask'
3
- require 'rake/rdoctask'
3
+ require 'rspec/core'
4
+ require 'rspec/core/rake_task'
4
5
 
5
- desc "Build the gem into the current directory"
6
- task :gem => :gemspec do
7
- `gem build #{spec.name}.gemspec`
6
+ RSpec::Core::RakeTask.new(:spec)
7
+ task :default => :spec
8
+
9
+ begin
10
+ include_files = ["README*", "LICENSE", "Rakefile", "init.rb", "{lib,test}/**/*"].map do |glob|
11
+ Dir[glob]
12
+ end.flatten
13
+
14
+ require "jeweler"
15
+ Jeweler::Tasks.new do |s|
16
+ s.name = "paperclip-meta"
17
+ s.version = "0.1"
18
+ s.author = "Alexey Bondar"
19
+ s.email = "y8@ya.ru"
20
+ s.homepage = "http://github.com/y8/paperclip-meta"
21
+ s.description = "Add width, height and size methods to paperclip thumbnails"
22
+ s.summary = "Thumbnail dimensions for paperclip"
23
+ s.platform = Gem::Platform::RUBY
24
+ s.files = include_files
25
+ s.require_path = "lib"
26
+ s.rubyforge_project = "paperclip-meta"
27
+ s.has_rdoc = false
28
+ s.add_dependency 'paperclip'
29
+ end
30
+
31
+ Jeweler::GemcutterTasks.new
32
+ rescue LoadError
33
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
8
34
  end
data/init.rb CHANGED
@@ -1 +1 @@
1
- require File.join(File.dirname(__FILE__), "lib", "paperclip_meta")
1
+ require File.join(File.dirname(__FILE__), "lib", "paperclip-meta")
@@ -1,7 +1,16 @@
1
1
  module Paperclip
2
2
  class Attachment
3
- alias original_post_process_styles post_process_styles
3
+ alias :original_post_process_styles :post_process_styles
4
+ alias :original_save :save
4
5
 
6
+ # If attachment deleted - destroy meta data
7
+ def save
8
+ unless @queued_for_delete.empty?
9
+ instance_write(:meta, ActiveSupport::Base64.encode64(Marshal.dump({}))) if instance.respond_to?(:"#{name}_meta=")
10
+ end
11
+ original_save
12
+ end
13
+
5
14
  # If model has #{name}_meta column we getting sizes of processed
6
15
  # thumbnails and saving it to #{name}_meta column.
7
16
  def post_process_styles
@@ -11,8 +20,12 @@ module Paperclip
11
20
  meta = {}
12
21
 
13
22
  @queued_for_write.each do |style, file|
14
- geo = Geometry.from_file file
15
- meta[style] = {:width => geo.width.to_i, :height => geo.height.to_i, :size => File.size(file) }
23
+ begin
24
+ geo = Geometry.from_file file
25
+ meta[style] = {:width => geo.width.to_i, :height => geo.height.to_i, :size => File.size(file) }
26
+ rescue NotIdentifiedByImageMagickError => e
27
+ meta[style] = {}
28
+ end
16
29
  end
17
30
 
18
31
  instance_write(:meta, ActiveSupport::Base64.encode64(Marshal.dump(meta)))
@@ -38,6 +51,6 @@ module Paperclip
38
51
  meta.key?(style) ? meta[style][item] : nil
39
52
  end
40
53
  end
41
- end
54
+ end
42
55
  end
43
56
  end
@@ -0,0 +1,56 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Geometry saver plugin" do
4
+ before(:each) do
5
+ small_path = File.join(File.dirname(__FILE__), '..', 'fixtures', 'small.png')
6
+ big_path = File.join(File.dirname(__FILE__), '..', 'fixtures', 'big.jpg')
7
+ not_path = File.join(File.dirname(__FILE__), '..', 'fixtures', 'big.zip')
8
+ @big_image = File.open(big_path)
9
+ @small_image = File.open(small_path)
10
+ @big_size = Paperclip::Geometry.from_file(big_path)
11
+ @small_size = Paperclip::Geometry.from_file(small_path)
12
+ @not_image = File.open(not_path)
13
+ end
14
+
15
+ it "saves image geometry for original image" do
16
+ img = Image.new
17
+ img.small_image = @small_image
18
+ img.save!
19
+
20
+ img.reload # Ensure that updates really saved to db
21
+
22
+ img.small_image.width eq(@small_size.width)
23
+ img.small_image.height eq(@small_size.height)
24
+ end
25
+
26
+ it "saves geometry for styles" do
27
+ img = Image.new
28
+ img.small_image = @small_image
29
+ img.big_image = @big_image
30
+ img.save!
31
+
32
+ img.big_image.width(:small).should == 100
33
+ img.big_image.height(:small).should == 100
34
+ end
35
+
36
+ it "clears geometry fields when image is destroyed" do
37
+ img = Image.new
38
+ img.small_image = @small_image
39
+ img.big_image = @big_image
40
+ img.save!
41
+
42
+ img.big_image.width(:small).should == 100
43
+
44
+ img.big_image = nil
45
+ img.save!
46
+
47
+ img.big_image.width(:small).should be_nil
48
+ end
49
+
50
+ it "does not fails when file is not an image" do
51
+ img = Image.new
52
+ img.small_image = @not_image
53
+ lambda { img.save! }.should_not raise_error
54
+ img.small_image.width(:small).should be_nil
55
+ end
56
+ end
@@ -0,0 +1,15 @@
1
+ ActiveRecord::Schema.define :version => 0 do
2
+ create_table "images", :force => true do |t|
3
+ t.string :small_image_file_name
4
+ t.string :small_image_content_type
5
+ t.integer :small_image_updated_at
6
+ t.integer :small_image_file_size
7
+ t.string :small_image_meta
8
+
9
+ t.string :big_image_file_name
10
+ t.string :big_image_content_type
11
+ t.integer :big_image_updated_at
12
+ t.integer :big_image_file_size
13
+ t.string :big_image_meta
14
+ end
15
+ end
@@ -0,0 +1,36 @@
1
+ $LOAD_PATH << "." unless $LOAD_PATH.include?(".")
2
+
3
+ begin
4
+ require "bundler"
5
+ Bundler.setup
6
+ rescue Bundler::GemNotFound
7
+ raise RuntimeError, "Bundler couldn't find some gems." +
8
+ "Did you run `bundle install`?"
9
+ end
10
+
11
+ Bundler.require
12
+ require 'logger'
13
+ Paperclip::Railtie.insert
14
+
15
+ ActiveRecord::Base.establish_connection(
16
+ "adapter" => "sqlite3",
17
+ "database" => ":memory:"
18
+ )
19
+
20
+ ActiveRecord::Base.logger = Logger.new(nil)
21
+
22
+ load(File.dirname(__FILE__) + '/schema.rb')
23
+ $: << File.join(File.dirname(__FILE__), '..', 'lib')
24
+
25
+ class Image < ActiveRecord::Base
26
+ has_attached_file :small_image,
27
+ :storage => :filesystem,
28
+ :path => "./spec/tmp/:style/:id.:extension",
29
+ :url => "./spec/tmp/:style/:id.extension"
30
+
31
+ has_attached_file :big_image,
32
+ :storage => :filesystem,
33
+ :path => "./spec/tmp/fixtures/tmp/:style/:id.:extension",
34
+ :url => "./spec/tmp/fixtures/tmp/:style/:id.extension",
35
+ :styles => { :small => "100x100#" }
36
+ end
metadata CHANGED
@@ -1,12 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: paperclip-meta
3
3
  version: !ruby/object:Gem::Version
4
- hash: 9
4
+ hash: 15
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 1
9
- version: "0.1"
8
+ - 2
9
+ version: "0.2"
10
10
  platform: ruby
11
11
  authors:
12
12
  - Alexey Bondar
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-10-03 00:00:00 +04:00
17
+ date: 2010-11-30 00:00:00 +03:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -37,20 +37,23 @@ executables: []
37
37
 
38
38
  extensions: []
39
39
 
40
- extra_rdoc_files: []
41
-
40
+ extra_rdoc_files:
41
+ - README.rdoc
42
42
  files:
43
- - README
43
+ - README.rdoc
44
44
  - Rakefile
45
45
  - init.rb
46
46
  - lib/paperclip-meta.rb
47
+ - spec/paperclip-meta/paperclip-meta_spec.rb
48
+ - spec/schema.rb
49
+ - spec/spec_helper.rb
47
50
  has_rdoc: true
48
51
  homepage: http://github.com/y8/paperclip-meta
49
52
  licenses: []
50
53
 
51
54
  post_install_message:
52
- rdoc_options: []
53
-
55
+ rdoc_options:
56
+ - --charset=UTF-8
54
57
  require_paths:
55
58
  - lib
56
59
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -78,5 +81,7 @@ rubygems_version: 1.3.7
78
81
  signing_key:
79
82
  specification_version: 3
80
83
  summary: Thumbnail dimensions for paperclip
81
- test_files: []
82
-
84
+ test_files:
85
+ - spec/paperclip-meta/paperclip-meta_spec.rb
86
+ - spec/schema.rb
87
+ - spec/spec_helper.rb