Understanding Activerecord::concern:
Reference: The difference between include and extend:
Https://www.cnblogs.com/chentianwei/p/9408963.html
The traditional module looks like this:
Module M def self.included (Base) Base (a Class) extends a module "Classmethods", and the base class method contains the methods in the "Classmethods" module. base.extendclassmethods Base added a: Disabled method. Base.class_eval do, {where (disabled:true)} end end module Classmethods ... EndEnd
Using Activesupport::concern:
' Active_support/concern ' Module M # m extends the module concern and can use the concern method. Extend Activesupport::concern # when M is included in a class, the class can use the method in the block. included do , {where (disabled:true)} end # When M is included in a class, the class method of this class expands,?? method is used as a class method. class_methods do ... EndEnd
Gem ' Name_of_person '
A small gem that adds many methods of invocation to the registered name of the English website user.
Https://github.com/basecamp/name_of_person/tree/master/lib/name_of_person
- After the gem has been loaded,
- ActiveRecord::Base contains the module Haspersonname, you can use the methods in LIB/NAME_OF_PERSON/HAS_PERSON_NAME.RB: class method Has_person _name.
- In the Rails app, APP/MODEL/USER.RB uses the Has_person_name method to include the module assignablein the include.
The instance of user has a new 2 instance method that invokes the method in module PersonName
- @user.name=: Call the Personname.full (name) method, @user the first_name, the last_name attribute is assigned a value.
- @user. Name: returns an personname.new object that can be used:
- Full | Initials | Familiar such as the method defined in the module personname.
- First | Last
How to use:
1. The user class must include First_Name, last_name2 properties, add Validates:first_name,: last_name, Presence:true
2. When instantiating a @user, the code internally calls the Name= method to First_Name, and the last_name attribute assigns the value!
(here is not very understanding, whether is devise this gem, when the discovery must verify first_name, last_name, automatically call name= method?) )
3. You can then use different combinations of first and last names by @user. Name.xxx .
Analysis: First look at three pictures:
Figure 2
Figure 3:
@user. Name's internal operating mechanism:
The first is a judgment:
if @user. first_name Nameofperson::P ersonname.new (@user. first_name, @user. last_name) End
If first_name exists, a new PersonName object is added, calling the Initialize method
def Initialize (first, last = nil) raise"firstname is required " unless first.present? = First , last super full end
Then call the full method to make the If judgment
def Full " #{first} #{last} " : First end
Analysis:
If @user.last_name exists (last.present?), the two name attributes of @user are merged and assigned to the @full object.
Finally, a PersonName object instance is returned, including @first, @full, and @last(as determined by @user)
@user. Name = "Dav Tom" Internal Run Analysis:
def name=(name) = Nameofperson::P ersonname.full (name) = full_name. Try(: first), Full_name. Try (: Last) end
First: Call the module PersonName class method full.
- Divides the passed string parameter into first, last variable
- Create a new PersonName object if the first variable is present
- Subsequent analysis and @ueser.name are the same.
def self.full (full_name) = Full_name.to_s.strip.split (/\s+/, 2) if first.present? End
Activesupport::concern and Gem ' Name_of_person ' (300?) internal operating mechanism analysis