Project

General

Profile

Refactor #5713

Updated by Petr Chalupa almost 10 years ago

Hi fellow Rubyists, 

 I saw methods defined directly in included block when using Concern several times. 

 <pre> 
 module AModule 
   extend ActiveSupport::Concern 

   
  
   included do 
    
     # this is BAD 
    
     def a_method 
     end 
   end 
 end 
 </pre> 

 It is a bad practice and it breaks things, all instance methods should go to the AModule, and all class methods to a ClassMethods module.  

 - It dynamically creates methods in each class, they are not shared through module, it is slow. 
 - It cannot be accessed for extension, there is no module to include to. 
 - it breaks `super` calls. 

 We should avoid using this and go for: 

 <pre> 
 module AModule 
   extend ActiveSupport::Concern 
  
   included do 
     # e.g. 
     has_one :user 
   end 

   module ClassMethods 
     def a_method 
     end 
   end 
 end 
 </pre> 

 Thanks. 

 I'll create issue to remove these bad definitions. 

 Petr

Back