Hey all,
Let's say you have a controller class and you include a module:
class ApplicationController < ActionController::Base include AuthenticationSystem
def default_page case when cu.group_is?(:a) then a_path when cu.group_is?(:b) then b_path when cu.group_is?(:c) then c_path end end end
Notice how this method makes a call to method cu. Now in order for cu to exist, that means the module methods must be copied into ApplicationController before any of its methods are called by subclasses:
class DashboardController < ApplicationController def root redirect_to default_page end end
If the module methods were not included prior to default_page being called in ApplicationController, since default_page calls method cu defined in the module AuthenticationSystem, it would throw error.
That part is clear. But what is unclear is what's the whole purpose of this:
module AuthenticationSystem def self.included(base) base.send :helper_method, :current_user end end
If, for example, current_user setter/getter are already declared in module:
def current_user @current_user = User.find_by_id(session[:user_id]) end
alias :cu :current_user
def current_user=(new_user) session[:user_id] = new_user ? new_user.id : nil @current_user = new_user end
and thus will be copied into the ApplicationController class, then what's the point of sending it to the class using base.send when it will already be copied into the class?
Or is base.send like a contructor method that gets executed before anything else in the module and therefore when its included in a class, it acts as consutrcotr of class and gets called prior to any methods of the class being called?
Thanks for response.