paperclip-meta 0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/README +50 -0
  2. data/Rakefile +8 -0
  3. data/init.rb +1 -0
  4. data/lib/paperclip-meta.rb +43 -0
  5. metadata +82 -0
data/README ADDED
@@ -0,0 +1,50 @@
1
+ =Paperclip Meta
2
+
3
+ Simple gem to let paperclip play nice with thumbnails width, height and size.
4
+
5
+ Paperclip Meta will get image dimensions right after post_process_styles using paperclips own Geometry.from_file. This should make paperclip-meta storage independent.
6
+
7
+ ==Quick Start
8
+
9
+ Create migration:
10
+
11
+ class AddMetaToAvatar < ActiveRecord::Migration
12
+ def self.up
13
+ add_column :users, :avatar_meta, :text
14
+ end
15
+
16
+ def self.down
17
+ remove_column :users, :avatar_meta
18
+ end
19
+ end
20
+
21
+ Rebuild all thumbnails to fill meta column if you already have some attachments.
22
+
23
+ Now you can use meta-magic:
24
+
25
+ <%= image_tag @user.avatar.url, :size => @user.avatar.image_size %>
26
+ <%= image_tag @user.avatar.url(:medium), :size => @user.avatar.image_size(:medium) %>
27
+ <%= image_tag @user.avatar.url(:thumb), :size => @user.avatar.image_size(:thumb) %>
28
+
29
+ ==Internals
30
+
31
+ Meta column is simple hash:
32
+
33
+ :style => {
34
+ :width => 100,
35
+ :height => 100,
36
+ :size => 42000
37
+ }
38
+
39
+ This hash will be marshaled and base64 encoded before writing to model attribute.
40
+
41
+ New methods provided:
42
+ - width
43
+ - height
44
+ - size
45
+
46
+ You can pass thumbnail style to all this methods. If style not passed, default_style will be used.
47
+
48
+ ==TODO
49
+
50
+ * It will be nice to write some tests. :D
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc "Build the gem into the current directory"
6
+ task :gem => :gemspec do
7
+ `gem build #{spec.name}.gemspec`
8
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.join(File.dirname(__FILE__), "lib", "paperclip_meta")
@@ -0,0 +1,43 @@
1
+ module Paperclip
2
+ class Attachment
3
+ alias original_post_process_styles post_process_styles
4
+
5
+ # If model has #{name}_meta column we getting sizes of processed
6
+ # thumbnails and saving it to #{name}_meta column.
7
+ def post_process_styles
8
+ original_post_process_styles
9
+
10
+ if instance.respond_to?(:"#{name}_meta=")
11
+ meta = {}
12
+
13
+ @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) }
16
+ end
17
+
18
+ instance_write(:meta, ActiveSupport::Base64.encode64(Marshal.dump(meta)))
19
+ end
20
+ end
21
+
22
+ # Meta access methods
23
+ [:width, :height, :size].each do |meth|
24
+ define_method(meth) do |*args|
25
+ style = args.first || default_style
26
+ meta_read(style, meth)
27
+ end
28
+ end
29
+
30
+ def image_size(style = default_style)
31
+ "#{width(style)}x#{height(style)}"
32
+ end
33
+
34
+ private
35
+ def meta_read(style, item)
36
+ if instance.respond_to?(:"#{name}_meta") && instance_read(:meta)
37
+ if meta = Marshal.load(ActiveSupport::Base64.decode64(instance_read(:meta)))
38
+ meta.key?(style) ? meta[style][item] : nil
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: paperclip-meta
3
+ version: !ruby/object:Gem::Version
4
+ hash: 9
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ version: "0.1"
10
+ platform: ruby
11
+ authors:
12
+ - Alexey Bondar
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-10-03 00:00:00 +04:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: paperclip
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ description: Add width, height and size methods to paperclip thumbnails
35
+ email: y8@ya.ru
36
+ executables: []
37
+
38
+ extensions: []
39
+
40
+ extra_rdoc_files: []
41
+
42
+ files:
43
+ - README
44
+ - Rakefile
45
+ - init.rb
46
+ - lib/paperclip-meta.rb
47
+ has_rdoc: true
48
+ homepage: http://github.com/y8/paperclip-meta
49
+ licenses: []
50
+
51
+ post_install_message:
52
+ rdoc_options: []
53
+
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ hash: 3
62
+ segments:
63
+ - 0
64
+ version: "0"
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ hash: 3
71
+ segments:
72
+ - 0
73
+ version: "0"
74
+ requirements: []
75
+
76
+ rubyforge_project: paperclip-meta
77
+ rubygems_version: 1.3.7
78
+ signing_key:
79
+ specification_version: 3
80
+ summary: Thumbnail dimensions for paperclip
81
+ test_files: []
82
+