Sha256: 2b8b643955261956d07788210f4e1bdae39667c7a1e52e9e86e8c2c88638b145

Contents?: true

Size: 1.02 KB

Versions: 2

Compression:

Stored size: 1.02 KB

Contents

require 'rails_best_practices/checks/check'

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

      def interesting_nodes
        [:class]
      end

      def interesting_files
        /_controller.rb$/
      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_names|
          add_error "use filter for #{first_call.to_ruby} in #{method_names.join(',')}" if method_names.size > 1
        end
      end

      private

      def remember_method(method_node)
        method_name = method_node.message_name
        first_call = method_node.body[1]
        @methods[first_call] ||= []
        @methods[first_call] << method_name
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rails_best_practices-0.2.13 lib/rails_best_practices/checks/use_filter_check.rb
rails_best_practices-0.2.12 lib/rails_best_practices/checks/use_filter_check.rb