sa77
sa77

Reputation: 3603

How to use two different method with the same "name" from two different gems on the same rails model?

The app (rails 4.2.7) i'm working on uses both carrierwave and paperclip for uploading image for two different fields on the same data model User (schema below).

create_table "users", force: :cascade do |t|
  t.string   "email"
  t.string   "first_name",             limit: 255
  t.string   "last_name",              limit: 255
  t.string   "avatar_file_name",       limit: 255
  t.string   "avatar_content_type",    limit: 255
  t.integer  "avatar_file_size"
  t.datetime "avatar_updated_at"
  t.string   "cv_file"
end

The avatar field is a paperclip object and cv_file is a carrierwave uploader.

Now, for background processing of cv_file field, i'm using carrierwave_backgrounder gem and for avatar field i'm using delayed_paperclip gem.

Both of these gems exposes process_in_background to process the image upload to background. So my User model looks like:

class User < ActiveRecord::Base
  # carrierwave
  mount_uploader :cv_file, CvFileUploader
  process_in_background :cv_file

  # paperclip
  has_attached_file :avatar, 
                    :default_url => "default-avatar.png",
                    :styles => {
                      :thumb => ['100x100#', :jpg, :quality => 80]
                    }
  process_in_background :avatar, processing_image_url: "default-avatar.png"

  # ...

end

I'm getting this error while trying to access any page on the app.

undefined method `remove_avatar?' for

Did you mean? remove_cv_file?

Any help will be greatly appreciated. Thank you!

Upvotes: 2

Views: 86

Answers (1)

Jason
Jason

Reputation: 539

It might be worthwhile to simply fork DelayedPaperclip and alter the name. Beyond that, you could potentially attempt to extend DelayedPaperclip and alias the method:

module AliasDelayedPaperclip extend DelayedPaperclip alias_method :paperclip_process_in_background, :process_in_background end

I haven't tested this, and it likely requires a bit of modification to actually work, but it's at least a suggestion of a path you could pursue.

Upvotes: 0

Related Questions