我正在为Rails创建一个链轮预处理器,在资源管道中找到.png.rb文件,并使用它们在我的应用程序中生成各种页面的png截图.
我已经阅读了这个主题,但似乎找不到任何简单的文档,如何获得这个设置.请帮助?
这是我到目前为止
/initializers/sprockets.rb:
require 'screenshot_preprocessor' Rails.application.assets.register_mime_type('screenshot/png','.png.rb') Rails.application.assets.register_preprocessor('screenshot/png',ScreenshotPreprocessor)
/lib/screenshot_preprocessor.rb:
class ScreenshotPreprocessor # What API do I need to provide here? # - What methods do I need to provide? # - What parameters does Sprockets pass me? # - What do I need to return to Sprockets? end
解决方法
好的,我仍然不知道在哪里可以找到文档.但是,通过阅读Sprockets的源代码,使用撬动调试器,并从与Sprockets完成类似操作的人员阅读博客帖子,我可以想出这一点:
/initializers/sprockets.rb:
require 'screenshot_generator' Rails.application.assets.register_engine('.screenshot',ScreenshotGenerator)
/lib/screenshot_generator.rb:
require_relative 'capybara_screenshot' # Don't worry about this,it's not # relevant to this question. class ScreenshotGenerator < Sprockets::Processor def evaluate(context,locals) generator_class = ScreenshotGenerator.get_generator_class(context.pathname) return generator_class.new.generate end private def self.get_generator_class(generator_file) # This evaluates the Ruby code in the given file and returns a class that # can generate a binary string containing an image file. # (Code omitted for brevity) end end
这对我来说现在工作正常,但是我更希望看到一些关于Sprockets预处理器,后处理器和引擎如何工作的真实文档.如果有人发现任何这样的文件,请发表一个答案.