Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Untitled #2

Merged
5 commits merged into from Nov 29, 2010
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
spec/tmp/*/**
pkg
2 changes: 2 additions & 0 deletions .rspec
@@ -0,0 +1,2 @@
--color
--backtrace
12 changes: 12 additions & 0 deletions 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"
54 changes: 54 additions & 0 deletions 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
34 changes: 30 additions & 4 deletions 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
21 changes: 17 additions & 4 deletions 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
Expand All @@ -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)))
Expand All @@ -38,6 +51,6 @@ def meta_read(style, item)
meta.key?(style) ? meta[style][item] : nil
end
end
end
end
end
end
66 changes: 48 additions & 18 deletions 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<paperclip>, [">= 0"])
else
s.add_dependency(%q<paperclip>, [">= 0"])
end
else
s.add_dependency(%q<paperclip>, [">= 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
Binary file added spec/fixtures/big.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added spec/fixtures/big.zip
Binary file not shown.
Binary file added spec/fixtures/small.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
56 changes: 56 additions & 0 deletions 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
15 changes: 15 additions & 0 deletions 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
36 changes: 36 additions & 0 deletions 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