<commit_before># encoding: utf-8
require 'rails_best_practices/checks/check'

module RailsBestPractices
  module Checks
    # Check a view file to make sure there is no finder.
    #
    # Implementation: Check if view file contains finder, then the code should move to controller.
    class MoveCodeIntoControllerCheck < Check
      
      FINDER = [:find, :all, :first, :last]
      
      def interesting_nodes
        [:call]
      end
      
      def interesting_files
        VIEW_FILES
      end
      
      def evaluate_start(node)
        add_error "move code into controller" if finder?(node)
      end
      
      private

      def finder?(node)
        node.subject.node_type == :const && FINDER.include?(node.message)
      end
    end
  end
end<commit_msg>refactor and comment for move_code_into_controller_check<commit_after># encoding: utf-8
require 'rails_best_practices/checks/check'

module RailsBestPractices
  module Checks
    # Check a view file to make sure there is no finder, finder should be moved to controller.
    #
    # See the best practice details here http://rails-bestpractices.com/posts/24-move-code-into-controller.
    #
    # Implementation:
    #
    # Prepare process:
    #   none
    #
    # Review process:
    #   only check all view files to see if there are finders, then the finders should be moved to controller.
    class MoveCodeIntoControllerCheck < Check

      FINDERS = [:find, :all, :first, :last]

      def interesting_review_nodes
        [:call]
      end

      def interesting_review_files
        VIEW_FILES
      end

      # check call nodes in review process.
      #
      # if the subject of the call node is a constant,
      # and the message of the call node is one of the :find, :all, :first and :last,
      # then it is a finder and should be moved to controller.
      def review_start_call(node)
        add_error "move code into controller" if finder?(node)
      end

      private
        # check if the node is a finder call node.
        # e.g. the following call node is a finder
        #
        #     s(:call,
        #       s(:const, :Post),
        #       :find,
        #       s(:arglist, s(:lit, :all))
        #     )
        def finder?(node)
          :const == node.subject.node_type && FINDERS.include?(node.message)
        end
    end
  end
end
{'fraction_non_alphanumeric': 0.07495741056218058, 'fraction_numerical': 0.0017035775127768314, 'mean_word_length': 2.1700404858299596, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3436226', 'n_tokens_mistral': 704, 'n_tokens_neox': 676, 'n_words': 234}