guidelines for where to put classes in Rails apps that don't fit anywhere

前端 未结 5 1657
忘掉有多难
忘掉有多难 2020-12-24 01:28

I\'m wondering if there are any best practices about where to put non-standard Ruby files in Rails apps, those that don\'t fit in any of the default directories (contr

5条回答
  •  粉色の甜心
    2020-12-24 01:50

    Good question - i don't have a concrete answer for you

    but I recommend checking out this post - http://blog.codeclimate.com/blog/2012/02/07/what-code-goes-in-the-lib-directory/ - be sure to read through all the comments

    on a current project i have a ton of non-ActiveRecord objects under app/models, it works but not ideal i put 're-useable' non application specific code under lib

    other alternatives I have tried on side projects (say we have a bunch of command objects) rails is a pain when it comes to namespaces under app, it loads everything up into the same namespace by default

    app/
      commands/
        products/create_command.rb         # Products::CreateCommand
        products/update_price_command.rb   # Products::UpdatePriceCommand
    

    alternate, everything besides rails under src or an app_name directory

    app/
      src/
        commands/
          create_product.rb         # Commands::CreateProduct
          update_product_price.rb   # Commands::UpdateProductPrice
    

    I haven't come across a good solution for this, ideally the 2nd one is better, but would be nice to not have the additional directory under app, that way you open app and see controllers, commands, models etc...

提交回复
热议问题