How to use concerns in Rails 4

后端 未结 6 1809
甜味超标
甜味超标 2020-11-22 06:57

The default Rails 4 project generator now creates the directory \"concerns\" under controllers and models. I have found some explanations about how to use routing concerns,

6条回答
  •  不思量自难忘°
    2020-11-22 07:25

    It's worth to mention that using concerns is considered bad idea by many.

    1. like this guy
    2. and this one

    Some reasons:

    1. There is some dark magic happening behind the scenes - Concern is patching include method, there is a whole dependency handling system - way too much complexity for something that's trivial good old Ruby mixin pattern.
    2. Your classes are no less dry. If you stuff 50 public methods in various modules and include them, your class still has 50 public methods, it's just that you hide that code smell, sort of put your garbage in the drawers.
    3. Codebase is actually harder to navigate with all those concerns around.
    4. Are you sure all members of your team have same understanding what should really substitute concern?

    Concerns are easy way to shoot yourself in the leg, be careful with them.

提交回复
热议问题