条件转换选项回形针 [英] conditional convert options paperclip

查看:40
本文介绍了条件转换选项回形针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过一些研究之后能够根据我的 image_class 列添加样式.

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

这很顺利,现在我也想添加条件 convert_options.这以某种方式失败了.

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

错误:

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'

任何想法为什么它与 styles 完美配合,但在 convert_options 中失败?

Any ideas why it works perfectly with styles but fails with convert_options?

推荐答案

注意:尚未在工作代码中验证这一点

note: haven't verified that on working code

似乎传入 :convert_options 的块的参数已经是一个实例,而不是附件(与样式选项相反,它是一个附件)

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)

试试:

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

希望能帮到你

更新:

您的 convert_options 似乎没有在此处设置:https://github.com/thoughtbot/paperclip/blob/a93dfc773b4fd649db4d1281b42a2a71b1ae72ff/lib/paperclip/style.rb#L55

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

似乎他们建议传递带有样式的 convert_options,例如在此规范中:https://github.com/thoughtbot/paperclip/blob/263a498195d47563a6227be18cf4463c4c6e7903/spec/paperclip/style_spec.rb#L41

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

你可以试试这个吗?所以完全删除 convert_options,并在您的配置中返回哈希值,如:

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: {...}

这篇关于条件转换选项回形针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆