Skip to content

Commit

Permalink
Remove deprecation warnings for modern ActiveSupport.
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexey Bondar committed Jan 25, 2012
1 parent 095fd86 commit 71431c1
Showing 1 changed file with 33 additions and 6 deletions.
39 changes: 33 additions & 6 deletions lib/paperclip-meta.rb
Expand Up @@ -6,14 +6,16 @@ class Attachment
# If attachment deleted - destroy meta data
def save
if (not @queued_for_delete.empty?) and @queued_for_write.empty?
instance_write(:meta, ActiveSupport::Base64.encode64(Marshal.dump({}))) if instance.respond_to?(:"#{name}_meta=")
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=")
Expand All @@ -28,29 +30,54 @@ def post_process_styles(*style_args)
end
end

instance_write(:meta, ActiveSupport::Base64.encode64(Marshal.dump(meta)))
instance_write(:meta, meta_encode(meta))
end
end

# Meta access methods
# 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 = Marshal.load(ActiveSupport::Base64.decode64(instance_read(:meta)))
if meta = meta_decode(instance_read(:meta))
meta.key?(style) ? meta[style][item] : nil
end
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

0 comments on commit 71431c1

Please sign in to comment.