Search code examples
ruby-on-railsruby-on-rails-3project-organization

Rails: where to place custom class injections?


i wrote some helper methods for my application like this

module Magick
  class Draw
    def qrcode(qrcode, left_corner, top_corner, right_corner, bottom_corner)
      size = qrcode.modules.length
      width = right_corner - left_corner
      height = bottom_corner - top_corner
      wset = width.to_f / size
      hset = height.to_f / size
...............

Where to place such code in rails ?


Solution

  • A common and easy way to load those patches is to create a file inside your ./config/initializers directory (ie: rmagick.rb) and put your code in this file.

    All files in this directory are loaded and executed on environment startup.

    You could also create a new file with your code in the lib/ directory (ie: rmagick_draw.rb) and add this line in ./config/application.rb:

    config.autoload_paths += %W(#{config.root}/lib)
    

    And then require the file anywhere you need it.