Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix for Paperclip 2.7 - assign and instantiate instance with papercli…

…p-meta value, update code to be scoped by name and use a railtie
  • Loading branch information...
commit c31c74fa39358c8f330c6ff020e3c1f2359dfb07 1 parent 6d84eff
Tom Meier tommeier authored
5 .rvmrc
View
@@ -0,0 +1,5 @@
+rvm 1.9.2-p290@paperclip-meta --create
+
+if ! command -v bundle ; then
+ gem install bundler
+fi
6 init.rb
View
@@ -1,2 +1,4 @@
-# FIXME: What this file for?
-require File.join(File.dirname(__FILE__), "lib", "paperclip-meta")
+require File.join(File.dirname(__FILE__), "lib", "paperclip-meta")
+require 'paperclip-meta/railtie'
+
+Paperclip::Meta::Railtie.insert
91 lib/paperclip-meta.rb
View
@@ -1,91 +1,4 @@
require "paperclip"
require "paperclip-meta/version"
-
-module Paperclip
- module Meta
- # TODO: Move most of paperclip-meta crap here.
- end
-
- # TODO: Make paperclip-meta less monkey-style.
- class Attachment
- alias :original_post_process_styles :post_process_styles
- alias :original_save :save
-
- # If attachment deleted - destroy meta data
- def save
- if (not @queued_for_delete.empty?) and @queued_for_write.empty?
- instance_write(:meta, meta_encode({})) 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(*style_args)
- # Check arity of :original_post_process_styles to maintain compatibility with
- # Paperclip 2.3.9 and older.
- method(:original_post_process_styles).arity == 0 ? original_post_process_styles : original_post_process_styles(*style_args)
-
- if instance.respond_to?(:"#{name}_meta=")
- meta = {}
-
- @queued_for_write.each do |style, 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, meta_encode(meta))
- end
- end
-
- # Define meta accessors methods
- [:width, :height, :size].each do |meth|
- define_method(meth) do |*args|
- style = args.first || default_style
- meta_read(style, meth)
- end
- end
-
- # Returns image dimesions ("WxH") for given style name. If style name not given,
- # returns dimesions for default_style.
- def image_size(style = default_style)
- "#{width(style)}x#{height(style)}"
- end
-
- private
- # Returns meta data for given style
- def meta_read(style, item)
- if instance.respond_to?(:"#{name}_meta") && instance_read(:meta)
- if meta = meta_decode(instance_read(:meta))
- meta.key?(style) ? meta[style][item] : nil
- end
- end
- end
-
- # Return encoded metadata as String
- def meta_encode(meta)
- # Use Base64 class if aviliable, to prevent
- # ActiveSupport deprecation warnings.
- if Module.const_defined? "Base64"
- ::Base64.encode64(Marshal.dump(meta))
- else
- ActiveSupport::Base64.encode64(Marshal.dump(meta))
- end
- end
-
- # Return decoded metadata as Object
- def meta_decode(meta)
- # Use Base64 class if aviliable, to prevent
- # ActiveSupport deprecation warnings.
- if Module.const_defined? "Base64"
- Marshal.load(::Base64.decode64(meta))
- else
- Marshal.load(ActiveSupport::Base64.decode64(meta))
- end
- end
- end
-end
+require 'paperclip-meta/railtie'
+require 'paperclip-meta/attachment'
93 lib/paperclip-meta/attachment.rb
View
@@ -0,0 +1,93 @@
+module Paperclip
+ module Meta
+ module Attachment
+
+ def self.included(base)
+ base.send :include, InstanceMethods
+ base.alias_method_chain :save, :meta_data
+ base.alias_method_chain :post_process_styles, :meta_data
+ end
+
+ module InstanceMethods
+
+ def save_with_meta_data
+ if (not @queued_for_delete.empty?) and @queued_for_write.empty?
+ instance_write(:meta, meta_encode({})) if instance.respond_to?(:"#{name}_meta=")
+ end
+ save_without_meta_data
+ end
+
+ def post_process_styles_with_meta_data(*style_args)
+ post_process_styles_without_meta_data(*style_args)
+
+ if instance.respond_to?(:"#{name}_meta=")
+ meta = {}
+ @queued_for_write.each do |style, 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
+
+ unless meta == {}
+ instance.send("#{name}_meta=", meta_encode(meta))
+ instance.class.update_all({ "#{name}_meta" => meta_encode(meta) }, instance.class.primary_key => instance.id)
+ end
+ end
+ end
+
+ # Define meta accessors methods
+ [:width, :height, :size].each do |meth|
+ define_method(meth) do |*args|
+ style = args.first || default_style
+ meta_read(style, meth)
+ end
+ end
+
+ # Returns image dimesions ("WxH") for given style name. If style name not given,
+ # returns dimesions for default_style.
+ def image_size(style = default_style)
+ "#{width(style)}x#{height(style)}"
+ end
+
+ private
+
+ # Returns meta data for given style
+ def meta_read(style, item)
+ if instance.respond_to?(:"#{name}_meta") && instance_read(:meta)
+ if meta = meta_decode(instance_read(:meta))
+ meta.key?(style) ? meta[style][item] : nil
+ end
+ end
+ end
+
+ # Return encoded metadata as String
+ def meta_encode(meta)
+ # Use Base64 class if aviliable, to prevent
+ # ActiveSupport deprecation warnings.
+ if Module.const_defined? "Base64"
+ ::Base64.encode64(Marshal.dump(meta))
+ else
+ ActiveSupport::Base64.encode64(Marshal.dump(meta))
+ end
+ end
+
+ # Return decoded metadata as Object
+ def meta_decode(meta)
+ # Use Base64 class if aviliable, to prevent
+ # ActiveSupport deprecation warnings.
+ if Module.const_defined? "Base64"
+ Marshal.load(::Base64.decode64(meta))
+ else
+ Marshal.load(ActiveSupport::Base64.decode64(meta))
+ end
+ end
+
+ end
+
+ end
+
+ end
+end
24 lib/paperclip-meta/railtie.rb
View
@@ -0,0 +1,24 @@
+require 'paperclip-meta'
+
+module Paperclip
+ module Meta
+
+ if defined? Rails::Railtie
+ require 'rails'
+ class Railtie < Rails::Railtie
+ initializer 'paperclip_meta.insert_into_active_record' do
+ ActiveSupport.on_load :active_record do
+ Paperclip::Meta::Railtie.insert
+ end
+ end
+ end
+ end
+
+ class Railtie
+ def self.insert
+ Paperclip::Attachment.send(:include, Paperclip::Meta::Attachment)
+ end
+ end
+
+ end
+end
1  spec/spec_helper.rb
View
@@ -15,6 +15,7 @@
load(File.join(File.dirname(__FILE__), 'schema.rb'))
Paperclip::Railtie.insert
+Paperclip::Meta::Railtie.insert
class Image < ActiveRecord::Base
has_attached_file :small_image,
Please sign in to comment.
Something went wrong with that request. Please try again.