在范围控制器和应用程序控制器的过滤器之前设计优先级

时间:2016-04-07 06:25:55

标签: ruby-on-rails devise

我对过滤器之前的应用程序控制器和过滤器之前的用户控制器之间的过滤器执行顺序感到困惑。

class ApplicationController < ActionController::Base
  befor_filter :check_me

  def check_me
   puts "checked!"
  end
end


class UsersController < ApplicationController
  before_filter :authenticate_user!
end

我认为它应该执行check_me然后authenticate_user!。但事实并非如此。这里的正确顺序是什么?

1 个答案:

答案 0 :(得分:1)

当我们在层次结构中上升时,会附加回调。

这意味着<cfset result = {} /> <cftry> <cfset date1 = CREATEODBCDATETIME(form.StartDate & '00:00:00')> <cfset date2 = CREATEODBCDATETIME(form.EndDate & '23:59:59')> <cfquery datasource="#application.dsn#" name="GetEmployeeInfo"> SELECT * FROM cl_checklists WHERE date >= <cfqueryparam value="#date1#" cfsqltype="cf_sql_timestamp" /> AND date <= <cfqueryparam value="#date2#" cfsqltype="cf_sql_timestamp" /> AND trans_location IN ( <cfqueryparam value="#FORM.location#" cfsqltype="cf_sql_varchar" list="true" /> ) AND associate IN ( <cfqueryparam value="028,998,28" cfsqltype="cf_sql_varchar" list="true" /> ) </cfquery> <cfquery datasource="#application.dsn#" name="GetLocationInfo"> SELECT * FROM cl_checklists WHERE date >= <cfqueryparam value="#date1#" cfsqltype="cf_sql_timestamp" /> AND date <= <cfqueryparam value="#date2#" cfsqltype="cf_sql_timestamp" /> AND trans_location IN ( <cfqueryparam value="#FORM.location#" cfsqltype="cf_sql_varchar" list="true" /> ) </cfquery> <cffunction name="getop_id" access="public" returntype="string"> <cfargument name="associate" > <cfquery name="spitOutop_id" datasource="#application.userinfo_dsn#"> SELECT assoc_name FROM dbo.tco_associates WHERE assoc_id= #arguments.associate# </cfquery> <cfreturn spitOutop_id.assoc_name > </cffunction> <cfquery name="allAssociatesQry" dbtype="query"> SELECT DISTINCT associate, COUNT(*) AS associateCount FROM GetEmployeeInfo GROUP BY associate ORDER BY associate </cfquery> <table border="1" id="Checklist_Stats"> <thead> <th><strong>Associate Name</strong></th> <th><strong>Location</strong></th> <th><strong>Checklists Generated by Associate</strong></th> <th><strong>Checklists Generated by Selected Location(s)</strong></th> <th><strong>Associate Percentage of Location Total</strong></th> </thead> <tbody> <!--- aggregate variables ---> <cfset aggrAssociateChecklist = 0> <cfset aggrLocationChecklist = 0> <cfloop query="allAssociatesQry"> <!--- get Associate's name ---> <cfset thisAssociateCode = trim(allAssociatesQry.associate)> <cfset thisAssociateName = getop_id(thisAssociateCode) /> <!--- 1.1 get all trans_location code and total counts for the current Associate ---> <cfquery name="allLocCodeForAssociateQry" dbtype="query"> SELECT trans_location,count(trans_location) AS locCntr FROM GetEmployeeInfo WHERE associate='#thisAssociateCode#' GROUP BY trans_location ORDER BY trans_location </cfquery> <!--- 1.2 get the aggregate of checklist count generated by the current Associate for each location ---> <cfquery name="qTotalChecklistCountForAssociate" dbtype="query"> SELECT SUM(locCntr) AS totalAssocChecklist FROM allLocCodeForAssociateQry </cfquery> <!--- 2.1 get the total location checklist for each location available for the current Associate ---> <cfquery name="allLocChecklistForAssociateQry" dbtype="query"> SELECT trans_location,count(trans_location) AS totalLocCount FROM GetLocationInfo WHERE trans_location IN (#QuotedValueList(allLocCodeForAssociateQry.trans_location)#) GROUP BY trans_location ORDER BY trans_location </cfquery> <!--- 2.2 get the aggregate of location checklist generated by the current Associate ---> <cfquery name="qTotalLocChecklistForAssociate" dbtype="query"> SELECT SUM(totalLocCount) AS totalLocChecklist FROM allLocChecklistForAssociateQry </cfquery> <!--- display record for the current Associate ---> <cfoutput query="allLocCodeForAssociateQry"> <tr> <!---<td><strong>#thisAssociateCode#</strong></td>---> <td><strong>#thisAssociateName#</strong></td> <td>#allLocCodeForAssociateQry.trans_location#</td> <td>#allLocCodeForAssociateQry.locCntr#</td> <td>#allLocChecklistForAssociateQry['totalLocCount'][CurrentRow]#</td> <td>#NumberFormat((allLocCodeForAssociateQry.locCntr/allLocChecklistForAssociateQry['totalLocCount'][CurrentRow]) * 100, '9.99')#%</td> </tr> <cfset thisAssociateName = "" /> </cfoutput> <!--- 3.1 get sub total for each Associate group ---> <cfset totalAssocChecklist = qTotalChecklistCountForAssociate.totalAssocChecklist> <cfset totalLocChecklist = qTotalLocChecklistForAssociate.totalLocChecklist> <!--- 3.2 add to the aggregate ---> <cfset aggrAssociateChecklist += totalAssocChecklist> <cfset aggrLocationChecklist += totalLocChecklist> <!--- display sub total for each Associate group ---> <cfoutput> <tr> <td><strong>Subtotal</strong></td> <td></td> <td>#totalAssocChecklist#</td> <td>#totalLocChecklist#</td> <td>#NumberFormat((totalAssocChecklist/totalLocChecklist) * 100, '9.99')#%</td> </tr> </cfoutput> </cfloop> <!--- display calculated aggregate at the end of the result ---> <cfoutput> <tr> <td><strong>Total</strong></td> <td></td> <td>#aggrAssociateChecklist#</td> <td>#aggrLocationChecklist#</td> <td>#NumberFormat((aggrAssociateChecklist/aggrLocationChecklist) * 100, '9.99')#%</td> </tr> </cfoutput> </tbody> </table> <cfcatch type="any"> <cfset result.error = CFCATCH.message > <cfset result.detail = CFCATCH.detail > </cfcatch> </cftry> 回调首先出现,然后出现UsersController

执行顺序为:

  1. ApplicationController
  2. authenticate_user!
  3. 如果您想更改优先顺序,请添加check_me

    示例:prepend_before_filter

    参考:What order do before filters occur in?