Permalink
Browse files

First version

  • Loading branch information...
1 parent b357190 commit 1cb5f218a6c4bac3892a6a71f3ecb9c9970c5e7c @y8 y8 committed Oct 3, 2010
Showing with 121 additions and 0 deletions.
  1. +50 −0 README
  2. +8 −0 Rakefile
  3. +1 −0 init.rb
  4. +43 −0 lib/paperclip_meta.rb
  5. +19 −0 paperclip-meta.gemspec
View
50 README
@@ -0,0 +1,50 @@
+=Paperclip Meta
+
+Simple gem to let paperclip play nice with thumbnails width, height and size.
+
+Paperclip Meta will get image dimensions right after post_process_styles using paperclips own Geometry.from_file. This should make paperclip-meta storage independent.
+
+==Quick Start
+
+Create migration:
+
+ class AddMetaToAvatar < ActiveRecord::Migration
+ def self.up
+ add_column :users, :avatar_meta, :text
+ end
+
+ def self.down
+ remove_column :users, :avatar_meta
+ end
+ end
+
+Rebuild all thumbnails to fill meta column if you already have some attachments.
+
+Now you can use meta-magic:
+
+ <%= image_tag @user.avatar.url, :size => @user.avatar.image_size %>
+ <%= image_tag @user.avatar.url(:medium), :size => @user.avatar.image_size(:medium) %>
+ <%= image_tag @user.avatar.url(:thumb), :size => @user.avatar.image_size(:thumb) %>
+
+==Internals
+
+Meta column is simple hash:
+
+ :style => {
+ :width => 100,
+ :height => 100,
+ :size => 42000
+ }
+
+This hash will be marshaled and base64 encoded before writing to model attribute.
+
+New methods provided:
+ - width
+ - height
+ - size
+
+You can pass thumbnail style to all this methods. If style not passed, default_style will be used.
+
+==TODO
+
+* It will be nice to write some tests. :D
View
@@ -0,0 +1,8 @@
+require 'rake'
+require 'rake/testtask'
+require 'rake/rdoctask'
+
+desc "Build the gem into the current directory"
+task :gem => :gemspec do
+ `gem build #{spec.name}.gemspec`
+end
View
@@ -0,0 +1 @@
+require File.join(File.dirname(__FILE__), "lib", "paperclip_meta")
View
@@ -0,0 +1,43 @@
+module Paperclip
+ class Attachment
+ alias original_post_process_styles post_process_styles
+
+ # If model has #{name}_meta column we getting sizes of processed
+ # thumbnails and saving it to #{name}_meta column.
+ def post_process_styles
+ original_post_process_styles
+
+ if instance.respond_to?(:"#{name}_meta=")
+ 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) }
+ end
+
+ instance_write(:meta, ActiveSupport::Base64.encode64(Marshal.dump(meta)))
+ end
+ end
+
+ # Meta access methods
+ [:width, :height, :size].each do |meth|
+ define_method(meth) do |*args|
+ style = args.first || default_style
+ meta_read(style, meth)
+ end
+ end
+
+ def image_size(style = default_style)
+ "#{width(style)}x#{height(style)}"
+ end
+
+ private
+ def meta_read(style, item)
+ if instance.respond_to?(:"#{name}_meta")
+ if meta = Marshal.load(ActiveSupport::Base64.decode64(instance_read(:meta)))
+ meta.key?(style) ? meta[style][item] : nil
+ end
+ end
+ end
+ end
+end
View
@@ -0,0 +1,19 @@
+include_files = ["README*", "LICENSE", "Rakefile", "init.rb", "{lib,test}/**/*"].map do |glob|
+ Dir[glob]
+end.flatten
+
+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

0 comments on commit 1cb5f21

Please sign in to comment.