Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Enable conditional default_url for Paperclip, to set default url with some method in the model
In some cases you need to set the default_url of paper clip base on some condition. e.g. You want to set default image on basis of gender of user. For this purpose this gist can make it possible to use a method as default_url. Just copy paper_clip_default_url_fix.rb to initializers folder, and use default_url as mentioned in model.rb
class User < ActiveRecord::Base
def set_picture_respect_to_gender
self.gender? ? '/images/male.jpg' : '/images/female.jpg'
end
has_attached_file :display_picture,
:styles => { :medium => "150x150>", :thumb => "100x100>", :small => "50x50>" },
:url => "/system/user/:id/:attachment/:style/:basename.:extension"
:default_url => :set_picture_respect_to_gender
end
module Paperclip
module Interpolations
def self.interpolate pattern, *args
if pattern.kind_of? Symbol
args.first.instance.send(pattern)
else
all.reverse.inject( pattern.dup ) do |result, tag|
result.gsub(/:#{tag}/) do |match|
send( tag, *args )
end
end
end
end
end
end
@edison

Know you how I can return :style too?

Like it:

  def set_picture_respect_to_gender
     self.gender? ? '/images/:style_male.jpg' : '/images/:style_female.jpg'
  end
@edison

Solved: Need to change paper_clip_default_url_fix.rb to this:

module Paperclip
  module Interpolations
    def self.interpolate pattern, *args
      pattern = args.first.instance.send(pattern) if pattern.kind_of? Symbol
      all.reverse.inject( pattern.dup ) do |result, tag|
        result.gsub(/:#{tag}/) do |match|
          send( tag, *args )
        end
      end
    end
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.