diff --git a/.rspec b/.rspec new file mode 100644 index 0000000..9fc14ad --- /dev/null +++ b/.rspec @@ -0,0 +1,2 @@ +--color +--backtrace \ No newline at end of file diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..966529e --- /dev/null +++ b/Gemfile @@ -0,0 +1,12 @@ +source "http://rubygems.org" + +gem "activerecord", ">= 3", :require => "active_record" +gem "sqlite3-ruby", :require => "sqlite3" +gem "paperclip", :require => "paperclip" +gem "paperclip-meta", :path => '.' + +if RUBY_VERSION < '1.9' + gem "ruby-debug", ">= 0.10.3" +end + +gem "rspec" diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..95e9d6e --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,54 @@ +PATH + remote: . + specs: + paperclip-meta (0.1) + paperclip + +GEM + remote: http://rubygems.org/ + specs: + activemodel (3.0.3) + activesupport (= 3.0.3) + builder (~> 2.1.2) + i18n (~> 0.4) + activerecord (3.0.3) + activemodel (= 3.0.3) + activesupport (= 3.0.3) + arel (~> 2.0.2) + tzinfo (~> 0.3.23) + activesupport (3.0.3) + arel (2.0.4) + builder (2.1.2) + columnize (0.3.2) + diff-lcs (1.1.2) + i18n (0.4.2) + linecache (0.43) + paperclip (2.3.6) + activerecord + activesupport + rspec (2.1.0) + rspec-core (~> 2.1.0) + rspec-expectations (~> 2.1.0) + rspec-mocks (~> 2.1.0) + rspec-core (2.1.0) + rspec-expectations (2.1.0) + diff-lcs (~> 1.1.2) + rspec-mocks (2.1.0) + ruby-debug (0.10.4) + columnize (>= 0.1) + ruby-debug-base (~> 0.10.4.0) + ruby-debug-base (0.10.4) + linecache (>= 0.3) + sqlite3-ruby (1.3.2) + tzinfo (0.3.23) + +PLATFORMS + ruby + +DEPENDENCIES + activerecord (>= 3) + paperclip + paperclip-meta! + rspec + ruby-debug (>= 0.10.3) + sqlite3-ruby diff --git a/Rakefile b/Rakefile index 73f18c6..64cd6a5 100644 --- a/Rakefile +++ b/Rakefile @@ -1,8 +1,34 @@ require 'rake' require 'rake/testtask' -require 'rake/rdoctask' +require 'rspec/core' +require 'rspec/core/rake_task' -desc "Build the gem into the current directory" -task :gem => :gemspec do - `gem build #{spec.name}.gemspec` +RSpec::Core::RakeTask.new(:spec) +task :default => :spec + +begin + include_files = ["README*", "LICENSE", "Rakefile", "init.rb", "{lib,test}/**/*"].map do |glob| + Dir[glob] + end.flatten + + require "jeweler" + Jeweler::Tasks.new do |s| + s.name = "paperclip-meta" + s.version = "0.1" + s.author = "Alexey Bondar" + s.email = "y8@ya.ru" + s.homepage = "http://github.com/y8/paperclip-meta" + s.description = "Add width, height and size methods to paperclip thumbnails" + s.summary = "Thumbnail dimensions for paperclip" + s.platform = Gem::Platform::RUBY + s.files = include_files + s.require_path = "lib" + s.rubyforge_project = "paperclip-meta" + s.has_rdoc = false + s.add_dependency 'paperclip' + end + + Jeweler::GemcutterTasks.new +rescue LoadError + puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler" end diff --git a/lib/paperclip-meta.rb b/lib/paperclip-meta.rb index b9d3b2e..b666342 100644 --- a/lib/paperclip-meta.rb +++ b/lib/paperclip-meta.rb @@ -1,7 +1,16 @@ module Paperclip class Attachment - alias :original_post_process_styles, :post_process_styles + alias :original_post_process_styles :post_process_styles + alias :original_save :save + # If attachment deleted - destroy meta data + def save + unless @queued_for_delete.empty? + instance_write(:meta, ActiveSupport::Base64.encode64(Marshal.dump({}))) if instance.respond_to?(:"#{name}_meta=") + end + original_save + end + # If model has #{name}_meta column we getting sizes of processed # thumbnails and saving it to #{name}_meta column. def post_process_styles @@ -11,8 +20,12 @@ def post_process_styles meta = {} @queued_for_write.each do |style, file| - geo = Geometry.from_file file - meta[style] = {:width => geo.width.to_i, :height => geo.height.to_i, :size => File.size(file) } + begin + geo = Geometry.from_file file + meta[style] = {:width => geo.width.to_i, :height => geo.height.to_i, :size => File.size(file) } + rescue NotIdentifiedByImageMagickError => e + meta[style] = {} + end end instance_write(:meta, ActiveSupport::Base64.encode64(Marshal.dump(meta))) @@ -38,6 +51,6 @@ def meta_read(style, item) meta.key?(style) ? meta[style][item] : nil end end - end + end end end \ No newline at end of file diff --git a/paperclip-meta.gemspec b/paperclip-meta.gemspec index 3944d46..e6e502b 100644 --- a/paperclip-meta.gemspec +++ b/paperclip-meta.gemspec @@ -1,19 +1,49 @@ -include_files = ["README*", "LICENSE", "Rakefile", "init.rb", "{lib,test}/**/*"].map do |glob| - Dir[glob] -end.flatten +# Generated by jeweler +# DO NOT EDIT THIS FILE DIRECTLY +# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command +# -*- encoding: utf-8 -*- + +Gem::Specification.new do |s| + s.name = %q{paperclip-meta} + s.version = "0.1" + + s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= + s.authors = ["Alexey Bondar"] + s.date = %q{2010-11-25} + s.description = %q{Add width, height and size methods to paperclip thumbnails} + s.email = %q{y8@ya.ru} + s.extra_rdoc_files = [ + "README.rdoc" + ] + s.files = [ + "README.rdoc", + "Rakefile", + "init.rb", + "lib/paperclip-meta.rb" + ] + s.homepage = %q{http://github.com/y8/paperclip-meta} + s.rdoc_options = ["--charset=UTF-8"] + s.require_paths = ["lib"] + s.rubyforge_project = %q{paperclip-meta} + s.rubygems_version = %q{1.3.7} + s.summary = %q{Thumbnail dimensions for paperclip} + s.test_files = [ + "spec/paperclip-meta/paperclip-meta_spec.rb", + "spec/schema.rb", + "spec/spec_helper.rb" + ] + + if s.respond_to? :specification_version then + current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION + s.specification_version = 3 + + if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then + s.add_runtime_dependency(%q, [">= 0"]) + else + s.add_dependency(%q, [">= 0"]) + end + else + s.add_dependency(%q, [">= 0"]) + end +end -spec = Gem::Specification.new do |s| - s.name = "paperclip-meta" - s.version = "0.1" - s.author = "Alexey Bondar" - s.email = "y8@ya.ru" - s.homepage = "http://github.com/y8/paperclip-meta" - s.description = "Add width, height and size methods to paperclip thumbnails" - s.summary = "Thumbnail dimensions for paperclip" - s.platform = Gem::Platform::RUBY - s.files = include_files - s.require_path = "lib" - s.rubyforge_project = "paperclip-meta" - s.has_rdoc = false - s.add_dependency 'paperclip' -end \ No newline at end of file diff --git a/spec/fixtures/big.jpg b/spec/fixtures/big.jpg new file mode 100644 index 0000000..bc7f1e9 Binary files /dev/null and b/spec/fixtures/big.jpg differ diff --git a/spec/fixtures/big.zip b/spec/fixtures/big.zip new file mode 100644 index 0000000..e4aec44 Binary files /dev/null and b/spec/fixtures/big.zip differ diff --git a/spec/fixtures/small.png b/spec/fixtures/small.png new file mode 100644 index 0000000..d5edc04 Binary files /dev/null and b/spec/fixtures/small.png differ diff --git a/spec/paperclip-meta/paperclip-meta_spec.rb b/spec/paperclip-meta/paperclip-meta_spec.rb new file mode 100644 index 0000000..457c133 --- /dev/null +++ b/spec/paperclip-meta/paperclip-meta_spec.rb @@ -0,0 +1,56 @@ +require 'spec_helper' + +describe "Geometry saver plugin" do + before(:each) do + small_path = File.join(File.dirname(__FILE__), '..', 'fixtures', 'small.png') + big_path = File.join(File.dirname(__FILE__), '..', 'fixtures', 'big.jpg') + not_path = File.join(File.dirname(__FILE__), '..', 'fixtures', 'big.zip') + @big_image = File.open(big_path) + @small_image = File.open(small_path) + @big_size = Paperclip::Geometry.from_file(big_path) + @small_size = Paperclip::Geometry.from_file(small_path) + @not_image = File.open(not_path) + end + + it "saves image geometry for original image" do + img = Image.new + img.small_image = @small_image + img.save! + + img.reload # Ensure that updates really saved to db + + img.small_image.width eq(@small_size.width) + img.small_image.height eq(@small_size.height) + end + + it "saves geometry for styles" do + img = Image.new + img.small_image = @small_image + img.big_image = @big_image + img.save! + + img.big_image.width(:small).should == 100 + img.big_image.height(:small).should == 100 + end + + it "clears geometry fields when image is destroyed" do + img = Image.new + img.small_image = @small_image + img.big_image = @big_image + img.save! + + img.big_image.width(:small).should == 100 + + img.big_image = nil + img.save! + + img.big_image.width(:small).should be_nil + end + + it "does not fails when file is not an image" do + img = Image.new + img.small_image = @not_image + lambda { img.save! }.should_not raise_error + img.small_image.width(:small).should be_nil + end +end \ No newline at end of file diff --git a/spec/schema.rb b/spec/schema.rb new file mode 100644 index 0000000..797d108 --- /dev/null +++ b/spec/schema.rb @@ -0,0 +1,15 @@ +ActiveRecord::Schema.define :version => 0 do + create_table "images", :force => true do |t| + t.string :small_image_file_name + t.string :small_image_content_type + t.integer :small_image_updated_at + t.integer :small_image_file_size + t.string :small_image_meta + + t.string :big_image_file_name + t.string :big_image_content_type + t.integer :big_image_updated_at + t.integer :big_image_file_size + t.string :big_image_meta + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..edb259e --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,36 @@ +$LOAD_PATH << "." unless $LOAD_PATH.include?(".") + +begin + require "bundler" + Bundler.setup +rescue Bundler::GemNotFound + raise RuntimeError, "Bundler couldn't find some gems." + + "Did you run `bundle install`?" +end + +Bundler.require +require 'logger' +Paperclip::Railtie.insert + +ActiveRecord::Base.establish_connection( + "adapter" => "sqlite3", + "database" => ":memory:" +) + +ActiveRecord::Base.logger = Logger.new(nil) + +load(File.dirname(__FILE__) + '/schema.rb') +$: << File.join(File.dirname(__FILE__), '..', 'lib') + +class Image < ActiveRecord::Base + has_attached_file :small_image, + :storage => :filesystem, + :path => "./spec/tmp/:style/:id.:extension", + :url => "./spec/tmp/:style/:id.extension" + + has_attached_file :big_image, + :storage => :filesystem, + :path => "./spec/tmp/fixtures/tmp/:style/:id.:extension", + :url => "./spec/tmp/fixtures/tmp/:style/:id.extension", + :styles => { :small => "100x100#" } +end \ No newline at end of file diff --git a/spec/tmp/fixtures/tmp/original/1.jpg b/spec/tmp/fixtures/tmp/original/1.jpg new file mode 100644 index 0000000..bc7f1e9 Binary files /dev/null and b/spec/tmp/fixtures/tmp/original/1.jpg differ diff --git a/spec/tmp/fixtures/tmp/original/2.jpg b/spec/tmp/fixtures/tmp/original/2.jpg new file mode 100644 index 0000000..bc7f1e9 Binary files /dev/null and b/spec/tmp/fixtures/tmp/original/2.jpg differ diff --git a/spec/tmp/fixtures/tmp/small/1.jpg b/spec/tmp/fixtures/tmp/small/1.jpg new file mode 100644 index 0000000..031667f Binary files /dev/null and b/spec/tmp/fixtures/tmp/small/1.jpg differ diff --git a/spec/tmp/fixtures/tmp/small/2.jpg b/spec/tmp/fixtures/tmp/small/2.jpg new file mode 100644 index 0000000..031667f Binary files /dev/null and b/spec/tmp/fixtures/tmp/small/2.jpg differ diff --git a/spec/tmp/original/1.png b/spec/tmp/original/1.png new file mode 100644 index 0000000..d5edc04 Binary files /dev/null and b/spec/tmp/original/1.png differ diff --git a/spec/tmp/original/2.png b/spec/tmp/original/2.png new file mode 100644 index 0000000..d5edc04 Binary files /dev/null and b/spec/tmp/original/2.png differ diff --git a/spec/tmp/original/3.png b/spec/tmp/original/3.png new file mode 100644 index 0000000..d5edc04 Binary files /dev/null and b/spec/tmp/original/3.png differ diff --git a/spec/tmp/original/3.zip b/spec/tmp/original/3.zip new file mode 100644 index 0000000..e4aec44 Binary files /dev/null and b/spec/tmp/original/3.zip differ diff --git a/spec/tmp/original/4.zip b/spec/tmp/original/4.zip new file mode 100644 index 0000000..e4aec44 Binary files /dev/null and b/spec/tmp/original/4.zip differ