John Merlino
2011-May-09 19:03 UTC
self.included like a constructor method that gets called when a module is included in a class?
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. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2011-May-09 19:20 UTC
Re: self.included like a constructor method that gets called when a module is included in a class?
On May 9, 8:03 pm, John Merlino <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hey all, > > Let''s say you have a controller class and you include a module: > > module AuthenticationSystem > def self.included(base) > base.send :helper_method, :current_user > end > end >The point of this is to call helper_method :current_user. You can look up what helper_method does in the rails docs. Fred> 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. > > -- > Posted viahttp://www.ruby-forum.com/.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
John Merlino
2011-May-09 19:35 UTC
Re: self.included like a constructor method that gets called when a module is included in a class?
> The point of this is to call helper_method :current_user. You can look > up what helper_method does in the rails docs. > > Fredok so basically this allows you to use current_user in your views. thanks for response -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.