Reputation: 81520
How can I detect that I've got a before_filter
of a non-existent action in Rails?
class PostsController < ApplicationController
before_action :authorize_user, only: [:kreate]
def create
end
end
Upvotes: 0
Views: 91
Reputation: 4639
This was a straightup hack, but the only way I could think of checking a before filter would be on initialize. The following checks if a controller has a before/after filter that uses a non-existant action and raises an exception message if it does
# config/initializers/before_filters.rb
# require all controllers
Dir['app/controllers/*'].each do |path|
require path.split('/').last if path.include? '.rb'
end
# get array of all controllers
controllers = ApplicationController.descendants
controllers.each do |controller|
# get all filters for this controller
filters = controller._process_action_callbacks
# get all actions under this controller
actions = controller.action_methods.to_a
filters.each do |filter|
# get all action_conditions for this filter
action_conditions = filter.instance_variable_get(:@if)
# raise error message if action used in filter not in available controller actions
action_conditions.each do |action_condition|
if actions.none? {|action| action_condition.include? action }
message = "#{controller.name} has a #{filter.kind.to_s} filter with a non-existant action (#{action_condition.scan(/'([^']*)'/)[0][0]})"
raise message
end
end
end
end
Upvotes: 1
Reputation: 6438
You cannot.
Thats why you need to have test cases. In this case, they would have caught it as authorize_user
wouldn't have triggered as you applied it to a non-existent action due to a typo.
Upvotes: 0