Conditional paperclip conversion options

After some research, I managed to add styles based on the image_class column.

Model.rb

 has_attached_file :image, :styles => lambda { |attachment| attachment.instance.decide_styles } def decide_styles styles = {} case self.image_class when "poster" styles[:thumb] = ["30x45!", :jpg] styles[:standard] = ["185x278!", :jpg] styles[:expanded] = ["372x559!", :jpg] styles[:big] = ["600x900!", :jpg] when "cover" styles[:thumb] = ["30x45!", :jpg] styles[:standard] = ["300x1200!", :jpg] end styles end 

This works smoothly, now I wanted to add conditional convert_options . It somehow fails.

 has_attached_file :image, :styles => lambda { |attachment| attachment.instance.decide_styles }, :convert_options => lambda { |attachment| attachment.instance.decide_convert_options } def decide_styles ... end def decide_convert_options opshunz = {} case self.image_class when "poster" opshunz[:thumb] = "-flop" opshunz[:standard] = "-flop" opshunz[:expanded] = "-flop" opshunz[:big] = "-flop" when "cover" opshunz[:thumb] = "-enhance" opshunz[:standard] = "-enhance" end opshunz end 

Error:

 NoMethodError: undefined method `instance' for :all:Symbol from /Users/AnsPoluke/Sites/nulike/app/models/movie_image.rb:8:in `block in <class:MovieImage>' from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/paperclip-4.1.1/lib/paperclip/attachment.rb:431:in `[]' from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/paperclip-4.1.1/lib/paperclip/attachment.rb:431:in `process_options' from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/paperclip-4.1.1/lib/paperclip/attachment.rb:423:in `extra_options_for' from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/paperclip-4.1.1/lib/paperclip/style.rb:56:in `convert_options' from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/paperclip-4.1.1/lib/paperclip/style.rb:79:in `block in processor_options' from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/paperclip-4.1.1/lib/paperclip/style.rb:78:in `each' from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/paperclip-4.1.1/lib/paperclip/style.rb:78:in `processor_options' from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/paperclip-4.1.1/lib/paperclip/attachment.rb:462:in `block in post_process_style' from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/paperclip-4.1.1/lib/paperclip/attachment.rb:461:in `each' from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/paperclip-4.1.1/lib/paperclip/attachment.rb:461:in `inject' from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/paperclip-4.1.1/lib/paperclip/attachment.rb:461:in `post_process_style' from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/paperclip-4.1.1/lib/paperclip/attachment.rb:454:in `block in post_process_styles' from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/paperclip-4.1.1/lib/paperclip/attachment.rb:453:in `each' from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/paperclip-4.1.1/lib/paperclip/attachment.rb:453:in `post_process_styles' from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/paperclip-4.1.1/lib/paperclip/attachment.rb:445:in `block (2 levels) in post_process' from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/activesupport-4.0.2/lib/active_support/callbacks.rb:393:in `_run__3861360263242897910__image_post_process__callbacks' from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/activesupport-4.0.2/lib/active_support/callbacks.rb:80:in `run_callbacks' from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/paperclip-4.1.1/lib/paperclip/callbacks.rb:36:in `run_paperclip_callbacks' from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/paperclip-4.1.1/lib/paperclip/attachment.rb:443:in `block in post_process' from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/activesupport-4.0.2/lib/active_support/callbacks.rb:383:in `_run__3861360263242897910__post_process__callbacks' from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/activesupport-4.0.2/lib/active_support/callbacks.rb:80:in `run_callbacks' from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/paperclip-4.1.1/lib/paperclip/callbacks.rb:36:in `run_paperclip_callbacks' from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/paperclip-4.1.1/lib/paperclip/attachment.rb:442:in `post_process' from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/paperclip-4.1.1/lib/paperclip/attachment.rb:114:in `assign' from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/paperclip-4.1.1/lib/paperclip/has_attached_file.rb:66:in `block in define_setter' from (irb):2 from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/railties-4.0.2/lib/rails/commands/console.rb:90:in `start' from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/railties-4.0.2/lib/rails/commands/console.rb:9:in `start' from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/railties-4.0.2/lib/rails/commands.rb:62:in `<top (required)>' from bin/rails:4:in `require' 

Any ideas why it works fine with styles but with convert_options error?

+6
source share
4 answers

note: did not confirm that on working code

It seems that the block argument is passed: convert_options is already an instance, not an attachment (unlike the styles option, where is that attachment)

Try:

 convert_options: lambda { |instance| instance.decide_convert_options } 

Btw your code would look much better if you extract the configuration data, for example:

 has_attached_file :image, styles: lambda { |attachment| attachment.instance.image_options[:styles] }, convert_options: lambda { |instance| instance.image_options[:convert_options] } IMAGE_OPTIONS = { poster: { styles: { thumb: ["30x45!", :jpg], standard: ["185x278!", :jpg], expanded: ["372x559!", :jpg] big: ["600x900!", :jpg] }, convert_options: { thumb: "-flop", standard: "-flop", expanded: "-flop", big: = "-flop" } }, cover: { styles: { thumb: ["30x45!", :jpg], standard: ["300x1200!", :jpg] }, convert_options: { thumb: "-enhance", standard: "-enhance" } } } def image_options IMAGE_OPTIONS[self.image_class] end 

I hope this helps

Update:

It looks like your convert_options are not installed here: https://github.com/thoughtbot/paperclip/blob/a93dfc773b4fd649db4d1281b42a2a71b1ae72ff/lib/paperclip/style.rb#L55

They seem to recommend passing convert_options with styles like in this specification: https://github.com/thoughtbot/paperclip/blob/263a498195d47563a6227be18cf4463c4c6e7903/spec/paperclip/style_spec.rb#L41

Can you try this? therefore, completely remove convert_options, and in your configuration, return the hash, for example:

 IMAGE_OPTIONS = { poster: { styles: { thumb: { geometry: "30x45!", format: :jpg, convert_options: '-flop', }, standard: {...} expanded: {...} big: {...} } }, cover: { styles: {...} 
+6
source

Apply it to everyone, so how do you do it anyway?

 :convert_options => {:all => "-flop"} 

if you do not want to create a paper clip processor

+2
source

The approved answer does not work. It can be read in the comments, and the author acknowledges that it has not been tested in the code.

This code works :

 has_attached_file :image, :styles => lambda { |attachment| thumb_convert_options = case attachment.instance.image_class when "poster" "-flop" when "cover" "-enhance" end { thumb: { convert_options: thumb_convert_options } } } 

The correct approach is to convert_options inside styles lambda; having it as a separate lambda does not work, at least for Paperclip version 4.1 and higher.

To save my answer in one place, I put all the code in a line and skipped all the styles next to thumb . Obviously, to implement this, you must save the decide_convert_options method.

+2
source

Add convert_options to the style. The following is an example of a general rail image model that contains two styles and associated logical values ​​to include these styles.

 # == Schema Information # # Table name: images # # id :integer not null, primary key # image_file_name :string(255) # image_content_type :string(255) # image_file_size :integer # hero_style :boolean # thumb_style :boolean # image_updated_at :datetime # created_at :datetime not null # updated_at :datetime not null # class Image < ActiveRecord::Base # These are the postprocessing options. # The boolean <stylename>_style? attributes controls which styles are created. STYLES = { hero: {geometry: "2500x800#", convert_options: "-quality 75 -strip", source_file_options: ""}, thumb: {geometry: "312x100#", convert_options: "-quality 75 -strip", source_file_options: ""} } has_attached_file :image, styles: lambda { |file| r = {} STYLES.keys.each do |stylename| r[stylename] = STYLES[stylename] if file.instance.method("%s_style?" % stylename).call end return r } validates_attachment :image, :presence => true, content_type: { content_type: ["image/jpeg", "image/png"] }, file_name: {matches: [/png\Z/, /jpe?g\Z/]} end 
0
source

All Articles