Class: RailsBestPractices::Checks::AlwaysAddDbIndexCheck

### Home Classes Methods

### In Files

- lib/rails_best_practices/checks/always_add_db_index_check.rb

### Parent

Check

### Methods

- ::new
- #evaluate_start
- #interesting_files
- #interesting_nodes

### Files

- LICENSE

### Class/Module Index

Quicksearch

- RailsBestPractices
- RailsBestPractices::Checks
- RailsBestPractices::Checks::AddModelVirtualAttributeCheck
- RailsBestPractices::Checks::AlwaysAddDbIndexCheck
- RailsBestPractices::Checks::Check
- RailsBestPractices::Checks::IsolateSeedDataCheck
- RailsBestPractices::Checks::KeepFindersOnTheirOwnModelCheck
- RailsBestPractices::Checks::LawOfDemeterCheck
- RailsBestPractices::Checks::ManyToManyCollectionCheck
- RailsBestPractices::Checks::MoveCodeIntoControllerCheck
- RailsBestPractices::Checks::MoveCodeIntoHelperCheck
- RailsBestPractices::Checks::MoveCodeIntoModelCheck
- RailsBestPractices::Checks::MoveFinderToNamedScopeCheck
- RailsBestPractices::Checks::MoveModelLogicIntoModelCheck
- RailsBestPractices::Checks::NeedlessDeepNestingCheck
- RailsBestPractices::Checks::NestedModelFormsCheck
- RailsBestPractices::Checks::NotUseDefaultRouteCheck
- RailsBestPractices::Checks::OveruseRouteCustomizationsCheck
- RailsBestPractices::Checks::ReplaceComplexCreationWithFactoryMethodCheck
- RailsBestPractices::Checks::ReplaceInstanceVariableWithLocalVariableCheck
- RailsBestPractices::Checks::UseBeforeFilterCheck
- RailsBestPractices::Checks::UseModelAssociationCheck
- RailsBestPractices::Checks::UseModelCallbackCheck
- RailsBestPractices::Checks::UseObserverCheck
- RailsBestPractices::Checks::UseScopeAccessCheck
- RailsBestPractices::Core
- RailsBestPractices::Core::CheckingVisitor
- RailsBestPractices::Core::Error
- RailsBestPractices::Core::Runner
- Enumerable
- NilClass
- Sexp
- String

No matching classes.

# RailsBestPractices::Checks::AlwaysAddDbIndexCheck

Check db/schema.rb file to make sure every reference key has a database index.

Implementation: read all add_index method calls to get the indexed columns in table, then read integer method call in create_table block to get the reference columns in tables, compare with indexed columns, if not in the indexed columns, then it violates always_add_db_index_check.

### Public Class Methods

new() click to toggle source

    # File lib/rails_best_practices/checks/always_add_db_index_check.rb, line 18
    def initialize
      super
      @index_columns = []
    end

### Public Instance Methods

evaluate_start(node) click to toggle source

    # File lib/rails_best_practices/checks/always_add_db_index_check.rb, line 23
    def evaluate_start(node)
      if :block == node.node_type
        find_index_columns(node)
      elsif :call == node.node_type
        case node.message
        when :create_table
          @table_name = node.arguments[1].to_ruby_string
        when :integer
          column_name = node.arguments[1].to_ruby_string
          if column_name =~ /_id$/ and !indexed?(@table_name, column_name)
            add_error "always add db index (#@table_name => #{column_name})", node.file, node.line
          end
        end
      end
    end

interesting_files() click to toggle source

    # File lib/rails_best_practices/checks/always_add_db_index_check.rb, line 14
    def interesting_files
      /db\/schema.rb/
    end

interesting_nodes() click to toggle source

    # File lib/rails_best_practices/checks/always_add_db_index_check.rb, line 10
    def interesting_nodes
      [:block, :call]
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.