mongoid_alize icon indicating copy to clipboard operation
mongoid_alize copied to clipboard

Take Proc object as a value [Feature Request]

Open anlek opened this issue 11 years ago • 2 comments

I'm trying to cache a URL for an image of my product in my project_products model So I'm trying something like this:

class ProjectProduct
  include Mongoid::Document
  include Mongoid::Timestamps
  include Mongoid::Alize

  field :quantity, type: Integer
  field :notes, type: String

  belongs_to :product
  embedded_in :project

  alize :product, :name, :slug, small_file: ->(p){p.url(:small)}
end

The key part is small_file: ->(p){p.url(:small)}. Since URL is actually computed based on the product_id and the size of the image (:small), Saving the file_name itself won't work. It would be great if mongoid_alize would take a proc object and eval it on save.

Thoughts?

anlek avatar Jul 21 '14 05:07 anlek

A workaround for this is to make a method that does the computation and use this:

class Product
  include Mongoid::Document
  include Mongoid::Timestamps

  mount_uploader :file, ImageUploader

  def small_file
    file.url(:small)
  end
end

and in the ProjectProduct I put alize :product, :name, :slug, :small_file

anlek avatar Jul 21 '14 05:07 anlek

Thanks for opening the issue. I'd be happy to support that in a pull request.

As far as a workaround today, have you tried overriding the callback?

joshed-io avatar Jul 21 '14 08:07 joshed-io