paperclip-meta 0.2 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ spec/tmp/*/**
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --backtrace
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in paperclip-meta.gemspec
4
+ gemspec
@@ -9,7 +9,7 @@ Paperclip Meta will get image dimensions right after post_process_styles using p
9
9
  Add paperclip-meta to Gemfile:
10
10
 
11
11
  gem 'paperclip-meta'
12
-
12
+
13
13
  Create migration:
14
14
 
15
15
  class AddMetaToAvatar < ActiveRecord::Migration
@@ -23,7 +23,7 @@ Create migration:
23
23
  end
24
24
 
25
25
  Rebuild all thumbnails to fill meta column if you already have some attachments.
26
-
26
+
27
27
  Now you can use meta-magic:
28
28
 
29
29
  <%= image_tag @user.avatar.url, :size => @user.avatar.image_size %>
@@ -43,11 +43,11 @@ Meta column is simple hash:
43
43
  This hash will be marshaled and base64 encoded before writing to model attribute.
44
44
 
45
45
  Meta methods provided:
46
- - width
47
- - height
48
- - size
46
+ - width `@user.avatar.width(:thumb)`
47
+ - height `@user.avatar.height(:medium)`
48
+ - size `@user.avatar.size`
49
49
 
50
- You can pass thumbnail style to all this methods. If style not passed, default_style will be used.
50
+ You can pass thumbnail style to all these methods. If style not passed, default_style will be used.
51
51
 
52
52
  ==TODO
53
53
 
data/Rakefile CHANGED
@@ -1,34 +1,8 @@
1
- require 'rake'
2
- require 'rake/testtask'
3
- require 'rspec/core'
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core"
4
3
  require 'rspec/core/rake_task'
5
4
 
6
5
  RSpec::Core::RakeTask.new(:spec)
7
- task :default => :spec
8
6
 
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"
34
- end
7
+ # spec is default task
8
+ task :default => :spec
data/init.rb CHANGED
@@ -1 +1,2 @@
1
+ # FIXME: What this file for?
1
2
  require File.join(File.dirname(__FILE__), "lib", "paperclip-meta")
@@ -1,20 +1,30 @@
1
+ require "paperclip"
2
+ require "paperclip-meta/version"
3
+
1
4
  module Paperclip
5
+ module Meta
6
+ # TODO: Move most of paperclip-meta crap here.
7
+ end
8
+
9
+ # TODO: Make paperclip-meta less monkey-style.
2
10
  class Attachment
3
11
  alias :original_post_process_styles :post_process_styles
4
12
  alias :original_save :save
5
13
 
6
14
  # If attachment deleted - destroy meta data
7
15
  def save
8
- unless @queued_for_delete.empty?
9
- instance_write(:meta, ActiveSupport::Base64.encode64(Marshal.dump({}))) if instance.respond_to?(:"#{name}_meta=")
16
+ if (not @queued_for_delete.empty?) and @queued_for_write.empty?
17
+ instance_write(:meta, meta_encode({})) if instance.respond_to?(:"#{name}_meta=")
10
18
  end
11
19
  original_save
12
20
  end
13
-
21
+
14
22
  # If model has #{name}_meta column we getting sizes of processed
15
23
  # thumbnails and saving it to #{name}_meta column.
16
- def post_process_styles
17
- original_post_process_styles
24
+ def post_process_styles(*style_args)
25
+ # Check arity of :original_post_process_styles to maintain compatibility with
26
+ # Paperclip 2.3.9 and older.
27
+ method(:original_post_process_styles).arity == 0 ? original_post_process_styles : original_post_process_styles(*style_args)
18
28
 
19
29
  if instance.respond_to?(:"#{name}_meta=")
20
30
  meta = {}
@@ -28,11 +38,11 @@ module Paperclip
28
38
  end
29
39
  end
30
40
 
31
- instance_write(:meta, ActiveSupport::Base64.encode64(Marshal.dump(meta)))
41
+ instance_write(:meta, meta_encode(meta))
32
42
  end
33
43
  end
34
44
 
35
- # Meta access methods
45
+ # Define meta accessors methods
36
46
  [:width, :height, :size].each do |meth|
37
47
  define_method(meth) do |*args|
38
48
  style = args.first || default_style
@@ -40,17 +50,42 @@ module Paperclip
40
50
  end
41
51
  end
42
52
 
53
+ # Returns image dimesions ("WxH") for given style name. If style name not given,
54
+ # returns dimesions for default_style.
43
55
  def image_size(style = default_style)
44
56
  "#{width(style)}x#{height(style)}"
45
57
  end
46
58
 
47
59
  private
60
+ # Returns meta data for given style
48
61
  def meta_read(style, item)
49
62
  if instance.respond_to?(:"#{name}_meta") && instance_read(:meta)
50
- if meta = Marshal.load(ActiveSupport::Base64.decode64(instance_read(:meta)))
63
+ if meta = meta_decode(instance_read(:meta))
51
64
  meta.key?(style) ? meta[style][item] : nil
52
65
  end
53
66
  end
54
- end
67
+ end
68
+
69
+ # Return encoded metadata as String
70
+ def meta_encode(meta)
71
+ # Use Base64 class if aviliable, to prevent
72
+ # ActiveSupport deprecation warnings.
73
+ if Module.const_defined? "Base64"
74
+ ::Base64.encode64(Marshal.dump(meta))
75
+ else
76
+ ActiveSupport::Base64.encode64(Marshal.dump(meta))
77
+ end
78
+ end
79
+
80
+ # Return decoded metadata as Object
81
+ def meta_decode(meta)
82
+ # Use Base64 class if aviliable, to prevent
83
+ # ActiveSupport deprecation warnings.
84
+ if Module.const_defined? "Base64"
85
+ Marshal.load(::Base64.decode64(meta))
86
+ else
87
+ Marshal.load(ActiveSupport::Base64.decode64(meta))
88
+ end
89
+ end
55
90
  end
56
- end
91
+ end
@@ -0,0 +1,5 @@
1
+ module Paperclip
2
+ module Meta
3
+ VERSION = "0.3.1"
4
+ end
5
+ end
@@ -0,0 +1,30 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "paperclip-meta/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "paperclip-meta"
7
+ s.version = Paperclip::Meta::VERSION
8
+ s.authors = ["Alexey Bondar"]
9
+ s.email = ["y8@ya.ru"]
10
+ s.homepage = "http://github.com/y8/paperclip-meta"
11
+ s.summary = %q{Thumbnail dimensions for paperclip}
12
+ s.description = %q{Add width, height and size methods to paperclip thumbnails}
13
+
14
+ s.rubyforge_project = "paperclip-meta"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ # Development depensencies
22
+ s.add_development_dependency "bundler", ">= 1.0.0"
23
+ s.add_development_dependency "paperclip"
24
+ s.add_development_dependency "rspec"
25
+ s.add_development_dependency "activerecord"
26
+ s.add_development_dependency "sqlite3-ruby"
27
+
28
+ # Runtime dependencies
29
+ s.add_runtime_dependency "paperclip"
30
+ end
Binary file
Binary file
Binary file
@@ -1,26 +1,20 @@
1
- $LOAD_PATH << "." unless $LOAD_PATH.include?(".")
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ Bundler.require(:default, :development)
2
4
 
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
5
+ # Prepare activerecord
6
+ require "active_record"
14
7
 
8
+ # Connect to sqlite
15
9
  ActiveRecord::Base.establish_connection(
16
- "adapter" => "sqlite3",
10
+ "adapter" => "sqlite3",
17
11
  "database" => ":memory:"
18
12
  )
19
13
 
20
14
  ActiveRecord::Base.logger = Logger.new(nil)
15
+ load(File.join(File.dirname(__FILE__), 'schema.rb'))
21
16
 
22
- load(File.dirname(__FILE__) + '/schema.rb')
23
- $: << File.join(File.dirname(__FILE__), '..', 'lib')
17
+ Paperclip::Railtie.insert
24
18
 
25
19
  class Image < ActiveRecord::Base
26
20
  has_attached_file :small_image,
metadata CHANGED
@@ -1,87 +1,133 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: paperclip-meta
3
- version: !ruby/object:Gem::Version
4
- hash: 15
5
- prerelease: false
6
- segments:
7
- - 0
8
- - 2
9
- version: "0.2"
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.1
5
+ prerelease:
10
6
  platform: ruby
11
- authors:
7
+ authors:
12
8
  - Alexey Bondar
13
9
  autorequire:
14
10
  bindir: bin
15
11
  cert_chain: []
16
-
17
- date: 2010-11-30 00:00:00 +03:00
18
- default_executable:
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
12
+ date: 2012-01-25 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: &70281062647600 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 1.0.0
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70281062647600
25
+ - !ruby/object:Gem::Dependency
21
26
  name: paperclip
27
+ requirement: &70281062647060 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
22
34
  prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
35
+ version_requirements: *70281062647060
36
+ - !ruby/object:Gem::Dependency
37
+ name: rspec
38
+ requirement: &70281062646360 !ruby/object:Gem::Requirement
24
39
  none: false
25
- requirements:
26
- - - ">="
27
- - !ruby/object:Gem::Version
28
- hash: 3
29
- segments:
30
- - 0
31
- version: "0"
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70281062646360
47
+ - !ruby/object:Gem::Dependency
48
+ name: activerecord
49
+ requirement: &70281062645580 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70281062645580
58
+ - !ruby/object:Gem::Dependency
59
+ name: sqlite3-ruby
60
+ requirement: &70281062644620 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *70281062644620
69
+ - !ruby/object:Gem::Dependency
70
+ name: paperclip
71
+ requirement: &70281062643560 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
32
77
  type: :runtime
33
- version_requirements: *id001
78
+ prerelease: false
79
+ version_requirements: *70281062643560
34
80
  description: Add width, height and size methods to paperclip thumbnails
35
- email: y8@ya.ru
81
+ email:
82
+ - y8@ya.ru
36
83
  executables: []
37
-
38
84
  extensions: []
39
-
40
- extra_rdoc_files:
41
- - README.rdoc
42
- files:
85
+ extra_rdoc_files: []
86
+ files:
87
+ - .gitignore
88
+ - .rspec
89
+ - Gemfile
90
+ - Gemfile.lock
43
91
  - README.rdoc
44
92
  - Rakefile
45
93
  - init.rb
46
94
  - lib/paperclip-meta.rb
95
+ - lib/paperclip-meta/version.rb
96
+ - paperclip-meta.gemspec
97
+ - spec/fixtures/big.jpg
98
+ - spec/fixtures/big.zip
99
+ - spec/fixtures/small.png
47
100
  - spec/paperclip-meta/paperclip-meta_spec.rb
48
101
  - spec/schema.rb
49
102
  - spec/spec_helper.rb
50
- has_rdoc: true
51
103
  homepage: http://github.com/y8/paperclip-meta
52
104
  licenses: []
53
-
54
105
  post_install_message:
55
- rdoc_options:
56
- - --charset=UTF-8
57
- require_paths:
106
+ rdoc_options: []
107
+ require_paths:
58
108
  - lib
59
- required_ruby_version: !ruby/object:Gem::Requirement
109
+ required_ruby_version: !ruby/object:Gem::Requirement
60
110
  none: false
61
- requirements:
62
- - - ">="
63
- - !ruby/object:Gem::Version
64
- hash: 3
65
- segments:
66
- - 0
67
- version: "0"
68
- required_rubygems_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ! '>='
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
116
  none: false
70
- requirements:
71
- - - ">="
72
- - !ruby/object:Gem::Version
73
- hash: 3
74
- segments:
75
- - 0
76
- version: "0"
117
+ requirements:
118
+ - - ! '>='
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
77
121
  requirements: []
78
-
79
122
  rubyforge_project: paperclip-meta
80
- rubygems_version: 1.3.7
123
+ rubygems_version: 1.8.10
81
124
  signing_key:
82
125
  specification_version: 3
83
126
  summary: Thumbnail dimensions for paperclip
84
- test_files:
127
+ test_files:
128
+ - spec/fixtures/big.jpg
129
+ - spec/fixtures/big.zip
130
+ - spec/fixtures/small.png
85
131
  - spec/paperclip-meta/paperclip-meta_spec.rb
86
132
  - spec/schema.rb
87
133
  - spec/spec_helper.rb