ruby on rails - How do I build a controller filter override helper -


in rails can add before_filters controllers. assuming base controller has following:

before_filter :my_base_filter

given this, assume have whole host of controllers inherit base controller, , norm behavior correct. have small handful of controllers following:

skip_before_filter :my_base_filter, only: [:method1, :method2] before_action only: [:method1, :method2]   my_secondary_filter(param1) end 

given code in few of controllers, , methods passed in (as whether or not uses only or except) different 1 controller next, ideally have single before_action or before_filter call run above code. call, in inherited controller, ideally like:

replace_filter(param1, {only: [:method1, :method2]}) 

the second parameter (the hash detailing methods apply to) should able accept empty value , apply methods. have created helper function (is written alongside these other filters and), syntactically , logically should this, can't seem invoke using before_action or before_filter without my_base_filter executing first. possible similar this, , if so, best way so?


application_controller.rb

class applicationcontroller < actioncontroller::base   include applicationhelper    before_action :my_base_filter   ... end 

inherited_controller.rb

class inheritedcontroller < applicationcontroller    # want replace these lines new helper function   skip_before_filter :my_base_filter, only: [:method1, :method2]   before_action only: [:method1, :method2]     my_secondary_filter(param1)   end   ... end 

application_helper.rb

class applicationhelper    def my_base_filter     # shit here normal behavior   end    def my_secondary_filter(param1)     # shit here specific functions instead     # of running normal base filter   end    # want able call function   # before_action or before_filter in order   # dry code   def replace_filter(param1, methods = {})     # run validation on parameters (including methods) here     # including raising exceptions if necessary     ...     # run following     skip_before_filter :my_base_filter, methods     before_action(methods)       my_secondary_filter(param1)     end   end end 

so... there's thing in rails called concern. it's intended way pull out modules of stuff in rails - while allowing whole bunch of neato things can use.

there's number of articles out there on , how use them. i'll let go explore.

i can't following fix problem, it's how i'd approach it.

part of problem - time running "replace_filter" method, bas_filter method has run.

what need able run replace_filter on first time applicationhelper included controller.

this activesupport::concern's included method comes aid.

give try:

# give meaningful name... class filterstuff < activesupport::concern    included     puts "i'm in included"     # note: not replace_filter, method call     if defined?(:replace_filter_method)        puts "this controller defines replace filter method i'm calling"       replace_filter_method     else       puts "this controller not define replace method , default base behaviour"       before_action :my_base_filter        end   end    def my_base_filter     puts "i'm in base filter"   end    def my_secondary_filter(param1)     puts "i'm in secondary filter param: #{param1}"   end    # making work problem...   def replace_filter(param1, methods = {})     puts "i'm in replace filter with: #{param1} , #{methods.inspect}"      # run validation on parameters (including methods) here     # including raising exceptions if necessary     ...     # run following     skip_before_filter :my_base_filter, methods     before_action(methods)       my_secondary_filter(param1)     end   end end class inheritedcontroller < applicationcontroller    include filterstuff     # actions go here...     private     # define method on controllers need    def replace_filter_method      puts "i'm in controllers replace filter method"      replace_filter(param1, only: [:method1, :method2])    end end 

i've added whole bunch of printf debugging - have run trhough , it'll tell being called when - , determine need proper stuff working.


Comments