ruby on rails - Dynamically created method? _one_time_conditions_valid_718? -
ruby on rails - Dynamically created method? _one_time_conditions_valid_718? -
i'm new ror , jumping big ror project. used railroady create diagram of of models , controllers. i've noticed many of controllers begin many 5 methods in form
_one_time_conditions_valid_xxx?
where xxx ranges 200 1116. however, these methods don't appear in actual code. these methods automagically generated gem? can't find reference anywhere.
okay here reference. these methods defined in activesupport::callbacks::callback
in method called #_compile_per_key_options
line 159. looks this
def _compile_per_key_options key_options = _compile_options(@per_key) @klass.class_eval <<-ruby_eval, __file__, __line__ + 1 def _one_time_conditions_valid_#{@callback_id}? true if #{key_options} end ruby_eval end
it used supplying info before
, after
, around
filters through 2 different methods called #start
, #end
.
both #start
, #end
check these methods so
return if key && !object.send("_one_time_conditions_valid_#{@callback_id}?")
from looks whole purpose of these methods determine if callback been defined , if compile appropriate hooks.
these hooks clear names. before
hooks run before defined action , access info before action gets it, after
hooks run after defined action , access info after action gets it, , around
hooks wrap action , triggers event yield
. can define own such as:
class record include activesupport::callbacks define_callbacks :magic def magic run_callbacks :magic puts "abracadabra" end end end class magician < record set_callback :magic, :before, :perform def perform puts "the magician perform trick" end set_callback :magic, :after |object| puts "tada" end end
this shown
magician = magician.new magician.magic #output: # magician perform trick #(before callback) # abracadabra #(actual event) # tada #(after callback)
this means if controllers have "as many five" of these there equal amount of filters in form of before_filter
, after_filter
, before_action
, around_action
, etc. (the list of available callbacks pretty long)
ruby-on-rails ruby validation
Comments
Post a Comment