Sha256: 44b27354f113cd4a9f072aa2f55f72686fcd6101c5016652c1803c40316ea7a6

Contents?: true

Size: 1.73 KB

Versions: 5

Compression:

Stored size: 1.73 KB

Contents

# encoding: utf-8
require 'rails_best_practices/reviews/review'

module RailsBestPractices
  module Reviews
    # Review 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:
    #
    # Review process:
    #   only check all view files to see if there are finders, then the finders should be moved to controller.
    class MoveCodeIntoControllerReview < Review

      FINDERS = %w(find all first last)

      def url
        "http://rails-bestpractices.com/posts/24-move-code-into-controller"
      end

      def interesting_nodes
        [:method_add_arg, :assign]
      end

      def interesting_files
        VIEW_FILES
      end

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

      # check assign nodes.
      #
      # if the subject of the right value node is a constant,
      # and the message of the right value node is one of the find, all, first and last,
      # then it is a finder and should be moved to controller.
      def start_assign(node)
        add_error "move code into controller", node.file, node.right_value.line if finder?(node.right_value)
      end

      private
        # check if the node is a finder call node.
        def finder?(node)
          node.subject.const? && FINDERS.include?(node.message.to_s)
        end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rails_best_practices-1.3.0 lib/rails_best_practices/reviews/move_code_into_controller_review.rb
rails_best_practices-1.2.0 lib/rails_best_practices/reviews/move_code_into_controller_review.rb
rails_best_practices-1.1.0 lib/rails_best_practices/reviews/move_code_into_controller_review.rb
rails_best_practices-1.0.1 lib/rails_best_practices/reviews/move_code_into_controller_review.rb
rails_best_practices-1.0.0 lib/rails_best_practices/reviews/move_code_into_controller_review.rb