Home paperclip resize not working for dynamic columns on attachment model
Reply: 0

paperclip resize not working for dynamic columns on attachment model

opensource-developer
1#
opensource-developer Published in 2017-12-06 16:59:47Z

I have a two columns on my attachment model on which the user sets the dimension to which they want to resize the image.

However, the variables when the resize happens are nil, but set to actual values after the resize happens.

below is the code

  has_attached_file :file, :styles => lambda { |a|
                          { :logo => ["200x50>",:png],
                            :user_defined => ["#{a.instance.custom_width}x#{a.instance.custom_height}>",:png] }
                        }

the custom_width & custom_height are nil when conversion happens however the logo conversion works as expected.

I am using ruby 2.2.4p230 & Rails 4.2.4

below is the full mode code

class Attachment < ActiveRecord::Base
  belongs_to :model_one

  belongs_to :attachable, polymorphic: true
  #has_attached_file :file, styles: { logo: ['200x50>',:png] }
  has_attached_file :file, styles: lambda { |attachment| attachment.instance.styles }

  def styles
    Rails.logger.info self.inspect
    Rails.logger.info self.attachable
    styles = {}
    m = "200x50>"
    l = "#{self.custom_width}x#{self.custom_height}>"
    styles[:logo] = [m, :png]
    styles[:user_defined] = [l, :png]
    styles
  end

end

Can anyone please help and let me know if i am doing something wrong?

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.304506 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO