ruby 如何根据配置添加has_one_attached?

ua4mk5z4  于 5个月前  发布在  Ruby
关注(0)|答案(1)|浏览(38)

我有一个gem,我想生成一个qrcode,如果配置启用了active_storage

app/config/initializers/cryptocoin_payable.rb

CryptocoinPayable.configure do |config|
  config.qrcode = :active_storage
end

个字符
它没有工作,因为

CryptoCoinPayable::Configuration is nil


我试着把它放在初始化之后

module CryptocoinPayable
end

if defined?(Rails)
  module CryptocoinPayable
    class Railtie < Rails::Railtie

      initializer 'cryptocoin_payable.active_storage' do
        ActiveSupport.on_load(:after_initialize) do
          require 'cryptocoin_payable/coin_payment'
          if (CryptocoinPayable.configuration == :qrcode)
            CryptocoinPayable::CoinPayment.has_one_attached(:qrcode)
           end
        end

      end

      rake_tasks do
        path = File.expand_path(__dir__)
        Dir.glob("#{path}/tasks/**/*.rake").each { |f| load f }
      end
    end
  end
end

require 'cryptocoin_payable/config'
require 'cryptocoin_payable/errors'
require 'cryptocoin_payable/version'
require 'cryptocoin_payable/adapters'


但它不工作“qrcode”是未定义的方法
如何根据配置设置has_*one_*attached
下面是响应:https://github.com/sbounmy/cryptocoin_payable

s1ag04yj

s1ag04yj1#

所以解决方案是:
请确保我们正确初始化after active_storage被反映。https://github.com/rails/rails/blob/9064735ec5cd3c679f8ffabc42532dd85223af58/activestorage/lib/active_storage/engine.rb#L171(如果你尝试后,附加它会引发一个不支持的宏错误)

initializer 'cryptocoin_payable.active_storage', after: 'active_storage.reflection' do
    require 'cryptocoin_payable/coin_payment'
    config.after_initialize do
      if CryptocoinPayable.configuration.qrcode?
        CryptocoinPayable::CoinPayment.has_one_attached(:qrcode)
      end
    end
  end

字符串
还确保rspec确实拿起railtie在我的情况下,我不得不要求'rails'在我的spec_helper之前cryptocoin_payable(定义?(Rails))

相关问题