conditional convert options paperclip












6














After some research I was able to add styles based on my 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 as well. This 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 perfectly with styles but fails with convert_options?










share|improve this question
























  • I found a few solutions, but they actually reported exactly the same error as you see!
    – Richard Peck
    Apr 22 '14 at 14:14










  • Where'd you find them? And how is it a solution when throwing an error? :)
    – davegson
    Apr 22 '14 at 14:17






  • 1




    lol I didn't mean "solution", I meant another Q where someone found a similar error & attempted to resolve!
    – Richard Peck
    Apr 22 '14 at 14:18
















6














After some research I was able to add styles based on my 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 as well. This 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 perfectly with styles but fails with convert_options?










share|improve this question
























  • I found a few solutions, but they actually reported exactly the same error as you see!
    – Richard Peck
    Apr 22 '14 at 14:14










  • Where'd you find them? And how is it a solution when throwing an error? :)
    – davegson
    Apr 22 '14 at 14:17






  • 1




    lol I didn't mean "solution", I meant another Q where someone found a similar error & attempted to resolve!
    – Richard Peck
    Apr 22 '14 at 14:18














6












6








6


4





After some research I was able to add styles based on my 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 as well. This 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 perfectly with styles but fails with convert_options?










share|improve this question















After some research I was able to add styles based on my 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 as well. This 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 perfectly with styles but fails with convert_options?







ruby-on-rails paperclip






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 23 '17 at 12:17









Community

11




11










asked Apr 22 '14 at 13:33









davegson

5,44343459




5,44343459












  • I found a few solutions, but they actually reported exactly the same error as you see!
    – Richard Peck
    Apr 22 '14 at 14:14










  • Where'd you find them? And how is it a solution when throwing an error? :)
    – davegson
    Apr 22 '14 at 14:17






  • 1




    lol I didn't mean "solution", I meant another Q where someone found a similar error & attempted to resolve!
    – Richard Peck
    Apr 22 '14 at 14:18


















  • I found a few solutions, but they actually reported exactly the same error as you see!
    – Richard Peck
    Apr 22 '14 at 14:14










  • Where'd you find them? And how is it a solution when throwing an error? :)
    – davegson
    Apr 22 '14 at 14:17






  • 1




    lol I didn't mean "solution", I meant another Q where someone found a similar error & attempted to resolve!
    – Richard Peck
    Apr 22 '14 at 14:18
















I found a few solutions, but they actually reported exactly the same error as you see!
– Richard Peck
Apr 22 '14 at 14:14




I found a few solutions, but they actually reported exactly the same error as you see!
– Richard Peck
Apr 22 '14 at 14:14












Where'd you find them? And how is it a solution when throwing an error? :)
– davegson
Apr 22 '14 at 14:17




Where'd you find them? And how is it a solution when throwing an error? :)
– davegson
Apr 22 '14 at 14:17




1




1




lol I didn't mean "solution", I meant another Q where someone found a similar error & attempted to resolve!
– Richard Peck
Apr 22 '14 at 14:18




lol I didn't mean "solution", I meant another Q where someone found a similar error & attempted to resolve!
– Richard Peck
Apr 22 '14 at 14:18












5 Answers
5






active

oldest

votes


















8





+50









note: haven't verified that on working code



It seems that the argument to the block passed in :convert_options is already an instance, not the attachment (as opposed to styles option, where it is an 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 that helps



Update:



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



it seems they recommend passing convert_options with styles, like in this spec: https://github.com/thoughtbot/paperclip/blob/263a498195d47563a6227be18cf4463c4c6e7903/spec/paperclip/style_spec.rb#L41



can you try this? so remove convert_options entirely, and in your configuration return hash like:



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





share|improve this answer























  • I can see the exception is coming out from console / irb. What do you call there that causes the exception?
    – Krzysztof
    Apr 28 '14 at 10:49










  • Thanks allot! The last update solved the problem. Well deserved bounty :)
    – davegson
    Apr 28 '14 at 11:44






  • 2




    I think this is not working anymore :( instance was replaced by the symbol (:all)
    – mrcaramori
    Nov 16 '14 at 16:32





















2














Apply it to all since that's what you're doing anyway?



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


failing that you might be looking at creating a Paperclip Processor






share|improve this answer





















  • Thanks! The exact convert options of the newly added cover isn't clear yet, so I changed it to enhanced as it will surely differ from the poster
    – davegson
    Apr 25 '14 at 9:45












  • what about putting this in decide_styles: styles[:thumb] = ["30x45!", :jpg, {:convert_options => {:all => "-flop"}}] I think you can define convert_options within an individual style
    – TomDunning
    Apr 25 '14 at 14:30












  • good idea, sadly didn't work...
    – davegson
    Apr 25 '14 at 14:57



















2














The approved answer doesn't work. It can be read in the comments and the author acknowledges that it hasn't been tested in code.



This code does work:



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 have convert_options inside the styles lambda; having it as a separate lambda does not work, at least for Paperclip version 4.1 and higher.



To keep my answer in one place, I've put all the code inline and omitted all styles beside thumb. Obviously to implement this you should keep the method decide_convert_options.






share|improve this answer





















  • the code worked for me... What didn't work at yours?
    – davegson
    Jan 14 '15 at 12:03










  • I kept getting NoMethodError: undefined method 'instance' for :all:Symbol until I changed it to the code above.
    – Joost Baaij
    Jan 14 '15 at 13:00












  • hmm strange, I do remember that it worked for me... but great to have another solution!
    – davegson
    Jan 14 '15 at 13:42



















0














Add the convert_options into the style. Here is an example for a generic rails Image model which contains two styles and corresponding booleans to enable 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: [/pngZ/, /jpe?gZ/]}



end





share|improve this answer





























    0














    You can also specify convert_options in styles:



    class Image < ActiveRecord::Base
    has_attached_file :image, styles: -> (attachment) { attachment.instance.paperclip_styles }

    private

    def image_ratio
    # logic to define the image ratio
    end

    def paperclip_styles
    vitrina_geometry = image_ratio > 1.2 ? '268x156>' : '268x156^'
    vitrina_convert_options = if image_ratio > 1.2
    "-quality 75 -gravity center -crop '268x156+0+0'"
    else
    "-quality 75 -strip -gravity center -background '#FFFFFF' -extent 268x156"
    end

    {
    medium: {
    geometry: '500x500>',
    convert_options: '-quality 75 -strip'
    },
    thumb: {
    geometry: '256x148>',
    convert_options: '-quality 75 -strip'
    },
    small: {
    geometry: '120x120>',
    convert_options: '-quality 75 -strip'
    },
    course_thumb: {
    geometry: '395x220^',
    convert_options: '-quality 75 -gravity center -crop '395x220+0+0''
    },
    :vitrina => {
    geometry: vitrina_geometry,
    convert_options: vitrina_convert_options
    }
    }
    end
    end





    share|improve this answer





















      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f23221167%2fconditional-convert-options-paperclip%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      5 Answers
      5






      active

      oldest

      votes








      5 Answers
      5






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      8





      +50









      note: haven't verified that on working code



      It seems that the argument to the block passed in :convert_options is already an instance, not the attachment (as opposed to styles option, where it is an 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 that helps



      Update:



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



      it seems they recommend passing convert_options with styles, like in this spec: https://github.com/thoughtbot/paperclip/blob/263a498195d47563a6227be18cf4463c4c6e7903/spec/paperclip/style_spec.rb#L41



      can you try this? so remove convert_options entirely, and in your configuration return hash like:



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





      share|improve this answer























      • I can see the exception is coming out from console / irb. What do you call there that causes the exception?
        – Krzysztof
        Apr 28 '14 at 10:49










      • Thanks allot! The last update solved the problem. Well deserved bounty :)
        – davegson
        Apr 28 '14 at 11:44






      • 2




        I think this is not working anymore :( instance was replaced by the symbol (:all)
        – mrcaramori
        Nov 16 '14 at 16:32


















      8





      +50









      note: haven't verified that on working code



      It seems that the argument to the block passed in :convert_options is already an instance, not the attachment (as opposed to styles option, where it is an 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 that helps



      Update:



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



      it seems they recommend passing convert_options with styles, like in this spec: https://github.com/thoughtbot/paperclip/blob/263a498195d47563a6227be18cf4463c4c6e7903/spec/paperclip/style_spec.rb#L41



      can you try this? so remove convert_options entirely, and in your configuration return hash like:



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





      share|improve this answer























      • I can see the exception is coming out from console / irb. What do you call there that causes the exception?
        – Krzysztof
        Apr 28 '14 at 10:49










      • Thanks allot! The last update solved the problem. Well deserved bounty :)
        – davegson
        Apr 28 '14 at 11:44






      • 2




        I think this is not working anymore :( instance was replaced by the symbol (:all)
        – mrcaramori
        Nov 16 '14 at 16:32
















      8





      +50







      8





      +50



      8




      +50




      note: haven't verified that on working code



      It seems that the argument to the block passed in :convert_options is already an instance, not the attachment (as opposed to styles option, where it is an 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 that helps



      Update:



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



      it seems they recommend passing convert_options with styles, like in this spec: https://github.com/thoughtbot/paperclip/blob/263a498195d47563a6227be18cf4463c4c6e7903/spec/paperclip/style_spec.rb#L41



      can you try this? so remove convert_options entirely, and in your configuration return hash like:



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





      share|improve this answer














      note: haven't verified that on working code



      It seems that the argument to the block passed in :convert_options is already an instance, not the attachment (as opposed to styles option, where it is an 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 that helps



      Update:



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



      it seems they recommend passing convert_options with styles, like in this spec: https://github.com/thoughtbot/paperclip/blob/263a498195d47563a6227be18cf4463c4c6e7903/spec/paperclip/style_spec.rb#L41



      can you try this? so remove convert_options entirely, and in your configuration return hash like:



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






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Apr 28 '14 at 11:27

























      answered Apr 26 '14 at 14:58









      Krzysztof

      43924




      43924












      • I can see the exception is coming out from console / irb. What do you call there that causes the exception?
        – Krzysztof
        Apr 28 '14 at 10:49










      • Thanks allot! The last update solved the problem. Well deserved bounty :)
        – davegson
        Apr 28 '14 at 11:44






      • 2




        I think this is not working anymore :( instance was replaced by the symbol (:all)
        – mrcaramori
        Nov 16 '14 at 16:32




















      • I can see the exception is coming out from console / irb. What do you call there that causes the exception?
        – Krzysztof
        Apr 28 '14 at 10:49










      • Thanks allot! The last update solved the problem. Well deserved bounty :)
        – davegson
        Apr 28 '14 at 11:44






      • 2




        I think this is not working anymore :( instance was replaced by the symbol (:all)
        – mrcaramori
        Nov 16 '14 at 16:32


















      I can see the exception is coming out from console / irb. What do you call there that causes the exception?
      – Krzysztof
      Apr 28 '14 at 10:49




      I can see the exception is coming out from console / irb. What do you call there that causes the exception?
      – Krzysztof
      Apr 28 '14 at 10:49












      Thanks allot! The last update solved the problem. Well deserved bounty :)
      – davegson
      Apr 28 '14 at 11:44




      Thanks allot! The last update solved the problem. Well deserved bounty :)
      – davegson
      Apr 28 '14 at 11:44




      2




      2




      I think this is not working anymore :( instance was replaced by the symbol (:all)
      – mrcaramori
      Nov 16 '14 at 16:32






      I think this is not working anymore :( instance was replaced by the symbol (:all)
      – mrcaramori
      Nov 16 '14 at 16:32















      2














      Apply it to all since that's what you're doing anyway?



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


      failing that you might be looking at creating a Paperclip Processor






      share|improve this answer





















      • Thanks! The exact convert options of the newly added cover isn't clear yet, so I changed it to enhanced as it will surely differ from the poster
        – davegson
        Apr 25 '14 at 9:45












      • what about putting this in decide_styles: styles[:thumb] = ["30x45!", :jpg, {:convert_options => {:all => "-flop"}}] I think you can define convert_options within an individual style
        – TomDunning
        Apr 25 '14 at 14:30












      • good idea, sadly didn't work...
        – davegson
        Apr 25 '14 at 14:57
















      2














      Apply it to all since that's what you're doing anyway?



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


      failing that you might be looking at creating a Paperclip Processor






      share|improve this answer





















      • Thanks! The exact convert options of the newly added cover isn't clear yet, so I changed it to enhanced as it will surely differ from the poster
        – davegson
        Apr 25 '14 at 9:45












      • what about putting this in decide_styles: styles[:thumb] = ["30x45!", :jpg, {:convert_options => {:all => "-flop"}}] I think you can define convert_options within an individual style
        – TomDunning
        Apr 25 '14 at 14:30












      • good idea, sadly didn't work...
        – davegson
        Apr 25 '14 at 14:57














      2












      2








      2






      Apply it to all since that's what you're doing anyway?



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


      failing that you might be looking at creating a Paperclip Processor






      share|improve this answer












      Apply it to all since that's what you're doing anyway?



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


      failing that you might be looking at creating a Paperclip Processor







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Apr 25 '14 at 9:37









      TomDunning

      3,47411626




      3,47411626












      • Thanks! The exact convert options of the newly added cover isn't clear yet, so I changed it to enhanced as it will surely differ from the poster
        – davegson
        Apr 25 '14 at 9:45












      • what about putting this in decide_styles: styles[:thumb] = ["30x45!", :jpg, {:convert_options => {:all => "-flop"}}] I think you can define convert_options within an individual style
        – TomDunning
        Apr 25 '14 at 14:30












      • good idea, sadly didn't work...
        – davegson
        Apr 25 '14 at 14:57


















      • Thanks! The exact convert options of the newly added cover isn't clear yet, so I changed it to enhanced as it will surely differ from the poster
        – davegson
        Apr 25 '14 at 9:45












      • what about putting this in decide_styles: styles[:thumb] = ["30x45!", :jpg, {:convert_options => {:all => "-flop"}}] I think you can define convert_options within an individual style
        – TomDunning
        Apr 25 '14 at 14:30












      • good idea, sadly didn't work...
        – davegson
        Apr 25 '14 at 14:57
















      Thanks! The exact convert options of the newly added cover isn't clear yet, so I changed it to enhanced as it will surely differ from the poster
      – davegson
      Apr 25 '14 at 9:45






      Thanks! The exact convert options of the newly added cover isn't clear yet, so I changed it to enhanced as it will surely differ from the poster
      – davegson
      Apr 25 '14 at 9:45














      what about putting this in decide_styles: styles[:thumb] = ["30x45!", :jpg, {:convert_options => {:all => "-flop"}}] I think you can define convert_options within an individual style
      – TomDunning
      Apr 25 '14 at 14:30






      what about putting this in decide_styles: styles[:thumb] = ["30x45!", :jpg, {:convert_options => {:all => "-flop"}}] I think you can define convert_options within an individual style
      – TomDunning
      Apr 25 '14 at 14:30














      good idea, sadly didn't work...
      – davegson
      Apr 25 '14 at 14:57




      good idea, sadly didn't work...
      – davegson
      Apr 25 '14 at 14:57











      2














      The approved answer doesn't work. It can be read in the comments and the author acknowledges that it hasn't been tested in code.



      This code does work:



      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 have convert_options inside the styles lambda; having it as a separate lambda does not work, at least for Paperclip version 4.1 and higher.



      To keep my answer in one place, I've put all the code inline and omitted all styles beside thumb. Obviously to implement this you should keep the method decide_convert_options.






      share|improve this answer





















      • the code worked for me... What didn't work at yours?
        – davegson
        Jan 14 '15 at 12:03










      • I kept getting NoMethodError: undefined method 'instance' for :all:Symbol until I changed it to the code above.
        – Joost Baaij
        Jan 14 '15 at 13:00












      • hmm strange, I do remember that it worked for me... but great to have another solution!
        – davegson
        Jan 14 '15 at 13:42
















      2














      The approved answer doesn't work. It can be read in the comments and the author acknowledges that it hasn't been tested in code.



      This code does work:



      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 have convert_options inside the styles lambda; having it as a separate lambda does not work, at least for Paperclip version 4.1 and higher.



      To keep my answer in one place, I've put all the code inline and omitted all styles beside thumb. Obviously to implement this you should keep the method decide_convert_options.






      share|improve this answer





















      • the code worked for me... What didn't work at yours?
        – davegson
        Jan 14 '15 at 12:03










      • I kept getting NoMethodError: undefined method 'instance' for :all:Symbol until I changed it to the code above.
        – Joost Baaij
        Jan 14 '15 at 13:00












      • hmm strange, I do remember that it worked for me... but great to have another solution!
        – davegson
        Jan 14 '15 at 13:42














      2












      2








      2






      The approved answer doesn't work. It can be read in the comments and the author acknowledges that it hasn't been tested in code.



      This code does work:



      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 have convert_options inside the styles lambda; having it as a separate lambda does not work, at least for Paperclip version 4.1 and higher.



      To keep my answer in one place, I've put all the code inline and omitted all styles beside thumb. Obviously to implement this you should keep the method decide_convert_options.






      share|improve this answer












      The approved answer doesn't work. It can be read in the comments and the author acknowledges that it hasn't been tested in code.



      This code does work:



      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 have convert_options inside the styles lambda; having it as a separate lambda does not work, at least for Paperclip version 4.1 and higher.



      To keep my answer in one place, I've put all the code inline and omitted all styles beside thumb. Obviously to implement this you should keep the method decide_convert_options.







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Jan 14 '15 at 11:39









      Joost Baaij

      6,49722630




      6,49722630












      • the code worked for me... What didn't work at yours?
        – davegson
        Jan 14 '15 at 12:03










      • I kept getting NoMethodError: undefined method 'instance' for :all:Symbol until I changed it to the code above.
        – Joost Baaij
        Jan 14 '15 at 13:00












      • hmm strange, I do remember that it worked for me... but great to have another solution!
        – davegson
        Jan 14 '15 at 13:42


















      • the code worked for me... What didn't work at yours?
        – davegson
        Jan 14 '15 at 12:03










      • I kept getting NoMethodError: undefined method 'instance' for :all:Symbol until I changed it to the code above.
        – Joost Baaij
        Jan 14 '15 at 13:00












      • hmm strange, I do remember that it worked for me... but great to have another solution!
        – davegson
        Jan 14 '15 at 13:42
















      the code worked for me... What didn't work at yours?
      – davegson
      Jan 14 '15 at 12:03




      the code worked for me... What didn't work at yours?
      – davegson
      Jan 14 '15 at 12:03












      I kept getting NoMethodError: undefined method 'instance' for :all:Symbol until I changed it to the code above.
      – Joost Baaij
      Jan 14 '15 at 13:00






      I kept getting NoMethodError: undefined method 'instance' for :all:Symbol until I changed it to the code above.
      – Joost Baaij
      Jan 14 '15 at 13:00














      hmm strange, I do remember that it worked for me... but great to have another solution!
      – davegson
      Jan 14 '15 at 13:42




      hmm strange, I do remember that it worked for me... but great to have another solution!
      – davegson
      Jan 14 '15 at 13:42











      0














      Add the convert_options into the style. Here is an example for a generic rails Image model which contains two styles and corresponding booleans to enable 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: [/pngZ/, /jpe?gZ/]}



      end





      share|improve this answer


























        0














        Add the convert_options into the style. Here is an example for a generic rails Image model which contains two styles and corresponding booleans to enable 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: [/pngZ/, /jpe?gZ/]}



        end





        share|improve this answer
























          0












          0








          0






          Add the convert_options into the style. Here is an example for a generic rails Image model which contains two styles and corresponding booleans to enable 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: [/pngZ/, /jpe?gZ/]}



          end





          share|improve this answer












          Add the convert_options into the style. Here is an example for a generic rails Image model which contains two styles and corresponding booleans to enable 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: [/pngZ/, /jpe?gZ/]}



          end






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 7 '15 at 22:31









          MattW.

          9,85944459




          9,85944459























              0














              You can also specify convert_options in styles:



              class Image < ActiveRecord::Base
              has_attached_file :image, styles: -> (attachment) { attachment.instance.paperclip_styles }

              private

              def image_ratio
              # logic to define the image ratio
              end

              def paperclip_styles
              vitrina_geometry = image_ratio > 1.2 ? '268x156>' : '268x156^'
              vitrina_convert_options = if image_ratio > 1.2
              "-quality 75 -gravity center -crop '268x156+0+0'"
              else
              "-quality 75 -strip -gravity center -background '#FFFFFF' -extent 268x156"
              end

              {
              medium: {
              geometry: '500x500>',
              convert_options: '-quality 75 -strip'
              },
              thumb: {
              geometry: '256x148>',
              convert_options: '-quality 75 -strip'
              },
              small: {
              geometry: '120x120>',
              convert_options: '-quality 75 -strip'
              },
              course_thumb: {
              geometry: '395x220^',
              convert_options: '-quality 75 -gravity center -crop '395x220+0+0''
              },
              :vitrina => {
              geometry: vitrina_geometry,
              convert_options: vitrina_convert_options
              }
              }
              end
              end





              share|improve this answer


























                0














                You can also specify convert_options in styles:



                class Image < ActiveRecord::Base
                has_attached_file :image, styles: -> (attachment) { attachment.instance.paperclip_styles }

                private

                def image_ratio
                # logic to define the image ratio
                end

                def paperclip_styles
                vitrina_geometry = image_ratio > 1.2 ? '268x156>' : '268x156^'
                vitrina_convert_options = if image_ratio > 1.2
                "-quality 75 -gravity center -crop '268x156+0+0'"
                else
                "-quality 75 -strip -gravity center -background '#FFFFFF' -extent 268x156"
                end

                {
                medium: {
                geometry: '500x500>',
                convert_options: '-quality 75 -strip'
                },
                thumb: {
                geometry: '256x148>',
                convert_options: '-quality 75 -strip'
                },
                small: {
                geometry: '120x120>',
                convert_options: '-quality 75 -strip'
                },
                course_thumb: {
                geometry: '395x220^',
                convert_options: '-quality 75 -gravity center -crop '395x220+0+0''
                },
                :vitrina => {
                geometry: vitrina_geometry,
                convert_options: vitrina_convert_options
                }
                }
                end
                end





                share|improve this answer
























                  0












                  0








                  0






                  You can also specify convert_options in styles:



                  class Image < ActiveRecord::Base
                  has_attached_file :image, styles: -> (attachment) { attachment.instance.paperclip_styles }

                  private

                  def image_ratio
                  # logic to define the image ratio
                  end

                  def paperclip_styles
                  vitrina_geometry = image_ratio > 1.2 ? '268x156>' : '268x156^'
                  vitrina_convert_options = if image_ratio > 1.2
                  "-quality 75 -gravity center -crop '268x156+0+0'"
                  else
                  "-quality 75 -strip -gravity center -background '#FFFFFF' -extent 268x156"
                  end

                  {
                  medium: {
                  geometry: '500x500>',
                  convert_options: '-quality 75 -strip'
                  },
                  thumb: {
                  geometry: '256x148>',
                  convert_options: '-quality 75 -strip'
                  },
                  small: {
                  geometry: '120x120>',
                  convert_options: '-quality 75 -strip'
                  },
                  course_thumb: {
                  geometry: '395x220^',
                  convert_options: '-quality 75 -gravity center -crop '395x220+0+0''
                  },
                  :vitrina => {
                  geometry: vitrina_geometry,
                  convert_options: vitrina_convert_options
                  }
                  }
                  end
                  end





                  share|improve this answer












                  You can also specify convert_options in styles:



                  class Image < ActiveRecord::Base
                  has_attached_file :image, styles: -> (attachment) { attachment.instance.paperclip_styles }

                  private

                  def image_ratio
                  # logic to define the image ratio
                  end

                  def paperclip_styles
                  vitrina_geometry = image_ratio > 1.2 ? '268x156>' : '268x156^'
                  vitrina_convert_options = if image_ratio > 1.2
                  "-quality 75 -gravity center -crop '268x156+0+0'"
                  else
                  "-quality 75 -strip -gravity center -background '#FFFFFF' -extent 268x156"
                  end

                  {
                  medium: {
                  geometry: '500x500>',
                  convert_options: '-quality 75 -strip'
                  },
                  thumb: {
                  geometry: '256x148>',
                  convert_options: '-quality 75 -strip'
                  },
                  small: {
                  geometry: '120x120>',
                  convert_options: '-quality 75 -strip'
                  },
                  course_thumb: {
                  geometry: '395x220^',
                  convert_options: '-quality 75 -gravity center -crop '395x220+0+0''
                  },
                  :vitrina => {
                  geometry: vitrina_geometry,
                  convert_options: vitrina_convert_options
                  }
                  }
                  end
                  end






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 20 at 20:17









                  Kadu Diógenes

                  358315




                  358315






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f23221167%2fconditional-convert-options-paperclip%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      404 Error Contact Form 7 ajax form submitting

                      How to know if a Active Directory user can login interactively

                      Refactoring coordinates for Minecraft Pi buildings written in Python