Module

What is the preferred way to add methods in activerecord base?

Let's say you need to create a module which provides common methods to the classes inherited from activerecord base.

From the two solutions below, which one is the preferred way to handle this? Explain your answer.

Solution A
module Commentable
def self.extended(base)
    base.class_eval do
        include InstanceMethods
        extend ClassMethods
    end
end
module ClassMethods
    def test_commentable_classmethod
        puts 'test class method'
    end
end
module InstanceMethods
    def test_commentable_instance_method
        puts 'test instance method'
    end
end
end
ActiveRecord::Base.extend(Commentable)
 
Solution B
module Commentable
def self.included(base)
    base.extend(ClassMethods)
end
module ClassMethods
    def test_commentable_classmethod
        puts 'test class method'
    end
end
def test_commentable_instance_method
    puts 'test instance methods'
end
end
ActiveRecord::Base.send(:include, Commentable)
 
Log in to accept this challenge