Sha256: e72195a4736927fbeb1de38906baa77c1d743fdac66a8af1311133d5f207e0be

Contents?: true

Size: 1.84 KB

Versions: 3

Compression:

Stored size: 1.84 KB

Contents

require 'rails_best_practices/checks/check'

module RailsBestPractices
  module Checks
    # Check config/routes.rb to make sure there are no much route customizations.
    #
    # Implementation: check member and collection route count, if more than customize_count, then it is overuse route customizations.
    class OveruseRouteCustomizationsCheck < Check
      
      def interesting_nodes
        [:call, :iter]
      end
      
      def interesting_files
        /config\/routes.rb/
      end

      def initialize(options = {})
        super()
        @customize_count = options['customize_count'] || 3
      end
      
      def evaluate_start(node)
        add_error "overuse route customizations (customize_count > #{@customize_count})", node.file, node.subject.line if member_and_collection_count(node) > @customize_count
      end

      private
        def member_and_collection_count(node)
          if :resources == node.message
            member_and_collection_count_for_rails2(node)
          elsif :iter == node.node_type and :resources == node.subject.message
            member_and_collection_count_for_rails3(node)
          end
        end

        # this is the checker for rails3 style routes
        def member_and_collection_count_for_rails3(node)
          get_nodes = node.grep_nodes(:node_type => :call, :message => :get)
          post_nodes = node.grep_nodes(:node_type => :call, :message => :post)
          get_nodes.size + post_nodes.size
        end
        
        # this is the checker for rails2 style routes
        def member_and_collection_count_for_rails2(node)
          hash_nodes = node.grep_nodes(:node_type => :hash)
          return 0 if hash_nodes.empty?
          customize_hash = eval(hash_nodes.first.to_ruby)
          (customize_hash[:member].size || 0) + (customize_hash[:collection].size || 0)
        end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rails_best_practices-0.4.2 lib/rails_best_practices/checks/overuse_route_customizations_check.rb
rails_best_practices-0.4.1 lib/rails_best_practices/checks/overuse_route_customizations_check.rb
rails_best_practices-0.4.0 lib/rails_best_practices/checks/overuse_route_customizations_check.rb