简体   繁体   English

Rails 应用程序中 gem package 的覆盖方法

[英]Override method from gem package in Rails application

I am using the package and initialise it in my rails application in config/initializers/prawn_rails.rb with include PrawnRailsForms我正在使用package并在我的 rails 应用程序中的 config/initializers/prawn_rails.rb 中使用include PrawnRailsForms对其进行初始化

I am trying to override a method to increase the font size of field.upcase with the code below我正在尝试使用以下代码覆盖增加 field.upcase 字体大小的方法

include PrawnRailsForms

DocumentExtensions.module_eval do
  def make_field_box(field)
    stroke_bounds
    bounds.add_left_padding 2
    move_down 2
    text field.upcase, size: 18
  end
end

But the font doesn't get affected in the pdf views even I restarted the server.但是即使我重新启动服务器,字体在 pdf 视图中也不会受到影响。

You need to do the monkey pathing here.你需要在这里做猴子路径

  1. Create a prawn-rails-forms directory in lib directory.lib目录中创建一个prawn-rails-forms目录。

  2. Create a document_extensions.rb file inside prawn-rails-forms directory.prawn-rails-forms目录中创建一个document_extensions.rb文件。

  3. Add below code to the document_extensions.rb将以下代码添加到document_extensions.rb

     module PrawnRailsForms module DocumentExtensions private def make_field_box(field) stroke_bounds bounds.add_left_padding 2 move_down 2 text field.upcase, size: 8 end end end
  4. Restart the server.重新启动服务器。

Extended扩展

If you find still not working after restarting the server then如果重新启动服务器后仍然无法正常工作,那么

  • Add config.autoload_paths += %W(#{config.root}/lib) to the config/application.rb ORconfig.autoload_paths += %W(#{config.root}/lib)添加到config/application.rb
  • Move prawn-rails-forms directory to the config/initializersprawn-rails-forms目录移动到config/initializers

Please let me know if it works for you.请让我知道它是否适合您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM