SuperModule allows defining class methods and method invocations the same way a super class does without using def included(base).
This succeeds ActiveSupport::Concern by offering lighter purely idiomatic Ruby syntax and simpler module dependency support.
1. Just include SuperModule at the top of a module definition:
module UserIdentifiable include SuperModule belongs_to :user validates :user_id, presence: true def self.most_active_user User.find_by_id(select('count(id) as head_count, user_id').group('user_id').order('count(id) desc').first.user_id) end def slug "#{self.class.name}_#{user_id}" end end
2. Mix newly defined module into a class or another super module
class ClubParticipation < ActiveRecord::Base include UserIdentifiable end class CourseEnrollment < ActiveRecord::Base include UserIdentifiable end module Accountable include SuperModule include UserIdentifiable end class Activity < ActiveRecord::Base include Accountable end
3. And start using by invoking class methods or instance methods
CourseEnrollment.most_active_user ClubParticipation.most_active_user Activity.last.slug ClubParticipation.create(club_id: club.id, user_id: user.id).slug CourseEnrollment.new(course_id: course.id).valid?
More details and examples are available over here: https://github.com/AndyObtiva/super_module
And the SuperModule RubyGem lives over here: http://rubygems.org/gems/super_module
Enjoy!!