Sha256: 2e7e922cb21fb139b6754f47a2abdb1b762ffd1531ae04d799a92d5238dda774

Contents?: true

Size: 1.68 KB

Versions: 15

Compression:

Stored size: 1.68 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
      interesting_nodes :method_add_arg, :assign
      interesting_files VIEW_FILES

      FINDERS = %w(find all first last)

      def url
        "http://rails-bestpractices.com/posts/24-move-code-into-controller"
      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

15 entries across 15 versions & 2 rubygems

Version Path
rails_best_practices-1.10.1 lib/rails_best_practices/reviews/move_code_into_controller_review.rb
rails_best_practices-gorgeouscode-1.0.0 lib/rails_best_practices/reviews/move_code_into_controller_review.rb
rails_best_practices-1.10.0 lib/rails_best_practices/reviews/move_code_into_controller_review.rb
rails_best_practices-1.9.1 lib/rails_best_practices/reviews/move_code_into_controller_review.rb
rails_best_practices-1.9.0 lib/rails_best_practices/reviews/move_code_into_controller_review.rb
rails_best_practices-1.8.0 lib/rails_best_practices/reviews/move_code_into_controller_review.rb
rails_best_practices-1.7.2 lib/rails_best_practices/reviews/move_code_into_controller_review.rb
rails_best_practices-1.7.1 lib/rails_best_practices/reviews/move_code_into_controller_review.rb
rails_best_practices-1.7.0 lib/rails_best_practices/reviews/move_code_into_controller_review.rb
rails_best_practices-1.6.0 lib/rails_best_practices/reviews/move_code_into_controller_review.rb
rails_best_practices-1.5.3 lib/rails_best_practices/reviews/move_code_into_controller_review.rb
rails_best_practices-1.5.2 lib/rails_best_practices/reviews/move_code_into_controller_review.rb
rails_best_practices-1.5.1 lib/rails_best_practices/reviews/move_code_into_controller_review.rb
rails_best_practices-1.5.0 lib/rails_best_practices/reviews/move_code_into_controller_review.rb
rails_best_practices-1.4.0 lib/rails_best_practices/reviews/move_code_into_controller_review.rb