Sha256: 7ee03623d35b2b2e077d31ab7f86cecf4046afc7a885addfa4420c9c412d67d4

Contents?: true

Size: 1.18 KB

Versions: 16

Compression:

Stored size: 1.18 KB

Contents

require 'rails_best_practices/checks/check'

module RailsBestPractices
  module Checks
    # Check a controller file to make sure to use before_filter to remove duplicate call in different action.
    #
    # Implementation: Check all methods' first call, if they are duplicate, then should use before_filter.
    class UseBeforeFilterCheck < Check

      def interesting_nodes
        [:class]
      end

      def interesting_files
        CONTROLLER_FILES
      end

      def evaluate_start(node)
        @methods = {}
        node.grep_nodes({:node_type => :defn}).each { |method_node| remember_method(method_node) }
        @methods.each do |first_call, method_nodes|
          if method_nodes.size > 1
            add_error "use before_filter for #{method_nodes.collect{|method_node| method_node.message_name}.join(',')}", 
              node.file, method_nodes.collect{|method_node| method_node.line}.join(',')
          end
        end
      end

      private

      def remember_method(method_node)
        first_call = method_node.body[1]
        unless first_call == s(:nil)
          @methods[first_call] ||= []
          @methods[first_call] << method_node
        end
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
rails_best_practices-0.4.2 lib/rails_best_practices/checks/use_before_filter_check.rb
rails_best_practices-0.4.1 lib/rails_best_practices/checks/use_before_filter_check.rb
rails_best_practices-0.4.0 lib/rails_best_practices/checks/use_before_filter_check.rb
rails_best_practices-0.3.27 lib/rails_best_practices/checks/use_before_filter_check.rb
rails_best_practices-0.3.26 lib/rails_best_practices/checks/use_before_filter_check.rb
rails_best_practices-0.3.25 lib/rails_best_practices/checks/use_before_filter_check.rb
rails_best_practices-0.3.24 lib/rails_best_practices/checks/use_before_filter_check.rb
rails_best_practices-0.3.23 lib/rails_best_practices/checks/use_before_filter_check.rb
rails_best_practices-0.3.22 lib/rails_best_practices/checks/use_before_filter_check.rb
rails_best_practices-0.3.21 lib/rails_best_practices/checks/use_before_filter_check.rb
rails_best_practices-0.3.20 lib/rails_best_practices/checks/use_before_filter_check.rb
rails_best_practices-0.3.19 lib/rails_best_practices/checks/use_before_filter_check.rb
rails_best_practices-0.3.18 lib/rails_best_practices/checks/use_before_filter_check.rb
rails_best_practices-0.3.17 lib/rails_best_practices/checks/use_before_filter_check.rb
rails_best_practices-0.3.16 lib/rails_best_practices/checks/use_before_filter_check.rb
rails_best_practices-0.3.15 lib/rails_best_practices/checks/use_before_filter_check.rb