What does # self.included (base) do in Ruby on Rails "Restful Authentication"?

I thought we would do

helper_method :current_user, :logged_in?, :authorized? 

so that these controller methods are available for use as helper methods in views. But in Restful Authentication lib/authenticated_system.rb I see:

 # Inclusion hook to make #current_user and #logged_in? # available as ActionView helper methods. def self.included(base) base.send :helper_method, :current_user, :logged_in?, :authorized? if base.respond_to? :helper_method end 

Why is this done so, and not alone? In addition, I do not see that included is called anywhere.

+42
ruby-on-rails restful-authentication
Mar 01 2018-11-11T00:
source share
5 answers

The self.included function self.included called when the module is turned on. It allows you to execute methods in the context of the database (where the module is included).

Additional information: ruby mixin tutorial .

+80
Mar 01 2018-11-11T00:
source share

When the AuthenticatedSystem method is enabled using the include method, the self.included method self.included started so that it is included in the base argument.

The code you showed calls helper_method and defines some useful helpers, but only if base has a helper_method method.

This is done so that, including the module, you can configure auxiliary methods, as well as add additional methods to the class.

+11
Mar 01 2018-11-11T00:
source share

For the same reason that Peter mentioned, I would like to add an example so that new developers can easily understand self.included (base) and self.extended (base):

 module Module1 def fun1 puts "fun1 from Module1" end def self.included(base) def fun2 puts "fun2 from Module1" end end end module Module2 def foo puts "foo from Module2" end def self.extended(base) def bar puts "bar from Module2" end end end class Test include Module1 extend Module2 def abc puts "abc form Test" end end 

Test.new.abC # => abc form Test

Test.new.fun1 # => fun1 from module 1

Test.new.fun2 # => fun2 from Module1

Test.foo # => foo from Module2

Test.bar # => bar from module 2

extend: methods will be available as class methods

include: methods will be available as instance methods

"base" in self.extended (base) /self.included (base):

The base parameter in the static extended method will be either an instance object or an object of a class of this class that extended the module depending on whether you are expanding the object or class accordingly.

When the class includes the module, the self.included method will be activated. The base parameter will be the class object for the class, which includes the module.

+6
Jan 03 '17 at 5:34 on
source share

Since this is the first result of a Google search of "self.included (base)", I will try to give a small example of how it works. I do not know how different it is from the rest of the authentication approach.

It is mainly used to create methods from one module, available in another module.

 module One def hello puts 'hello from module One' end end module Two def self.included(base) base.class_eval do include One end end end class ExampleClass include Two end ExampleClass.new.hello # => hello from module One 
+4
Aug 04 '16 at 8:07
source share

Want to dig into self.included and self.extended ?

Please see here: https://ruby-doc.org/core-2.2.1/Module.html#method-i-included

+1
Mar 24 '17 at 9:35
source share



All Articles