code analysis - Detect before_filter of a nonexistent action in Rails -
how can detect i've got before_filter
of non-existent action in rails?
class postscontroller < applicationcontroller before_action :authorize_user, only: [:kreate] def create end end
this straightup hack, way think of checking before filter on initialize. following checks if controller has before/after filter uses non-existant action , raises exception message if does
# config/initializers/before_filters.rb # require controllers dir['app/controllers/*'].each |path| require path.split('/').last if path.include? '.rb' end # array of controllers controllers = applicationcontroller.descendants controllers.each |controller| # filters controller filters = controller._process_action_callbacks # actions under controller actions = controller.action_methods.to_a filters.each |filter| # action_conditions filter action_conditions = filter.instance_variable_get(:@if) # raise error message if action used in filter not in available controller actions action_conditions.each |action_condition| if actions.none? {|action| action_condition.include? action } message = "#{controller.name} has #{filter.kind.to_s} filter non-existant action (#{action_condition.scan(/'([^']*)'/)[0][0]})" raise message end end end end
Comments
Post a Comment