Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Take Proc object as a value [Feature Request] #41

Open
anlek opened this issue Jul 21, 2014 · 2 comments
Open

Take Proc object as a value [Feature Request] #41

anlek opened this issue Jul 21, 2014 · 2 comments

Comments

@anlek
Copy link

anlek commented Jul 21, 2014

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
Copy link
Author

anlek commented Jul 21, 2014

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

@joshed-io
Copy link
Owner

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants