Sha256: e78f8b5dc332f47aab845d0c56e3c1eb5ae308e01e20a17c5a6065d28b88c168

Contents?: true

Size: 1.19 KB

Versions: 34

Compression:

Stored size: 1.19 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]
      end
      
      def interesting_files
        /config\/routes.rb/
      end

      def initialize(options = {})
        super()
        @customize_count = options['customize_count'] || 3
      end
      
      def evaluate_start(node)
        if s(:lvar, :map) == node.subject and :resources == node.message
          add_error "overuse route customizations (customize_count > #{@customize_count})" if member_and_collection_count(node) > @customize_count
        end
      end

      private

      def member_and_collection_count(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

34 entries across 34 versions & 1 rubygems

Version Path
rails_best_practices-0.3.17 lib/rails_best_practices/checks/overuse_route_customizations_check.rb
rails_best_practices-0.3.16 lib/rails_best_practices/checks/overuse_route_customizations_check.rb
rails_best_practices-0.3.15 lib/rails_best_practices/checks/overuse_route_customizations_check.rb
rails_best_practices-0.3.14 lib/rails_best_practices/checks/overuse_route_customizations_check.rb
rails_best_practices-0.3.13 lib/rails_best_practices/checks/overuse_route_customizations_check.rb
rails_best_practices-0.3.12 lib/rails_best_practices/checks/overuse_route_customizations_check.rb
rails_best_practices-0.3.11 lib/rails_best_practices/checks/overuse_route_customizations_check.rb
rails_best_practices-0.3.10 lib/rails_best_practices/checks/overuse_route_customizations_check.rb
rails_best_practices-0.3.9 lib/rails_best_practices/checks/overuse_route_customizations_check.rb
rails_best_practices-0.3.8 lib/rails_best_practices/checks/overuse_route_customizations_check.rb
rails_best_practices-0.3.7 lib/rails_best_practices/checks/overuse_route_customizations_check.rb
rails_best_practices-0.3.6 lib/rails_best_practices/checks/overuse_route_customizations_check.rb
rails_best_practices-0.3.5 lib/rails_best_practices/checks/overuse_route_customizations_check.rb
rails_best_practices-0.3.4 lib/rails_best_practices/checks/overuse_route_customizations_check.rb
rails_best_practices-0.3.3 lib/rails_best_practices/checks/overuse_route_customizations_check.rb
rails_best_practices-0.3.2 lib/rails_best_practices/checks/overuse_route_customizations_check.rb
rails_best_practices-0.3.1 lib/rails_best_practices/checks/overuse_route_customizations_check.rb
rails_best_practices-0.3.0 lib/rails_best_practices/checks/overuse_route_customizations_check.rb
rails_best_practices-0.2.16 lib/rails_best_practices/checks/overuse_route_customizations_check.rb
rails_best_practices-0.2.15 lib/rails_best_practices/checks/overuse_route_customizations_check.rb