class RailsReview::Checker

Public Class Methods

app_directory_check() click to toggle source
# File lib/rails_review/checker.rb, line 40
def app_directory_check
  files = Dir.glob(File.join(["#{Rails.root}/app", '**', '*'])).reject { |f| File.directory?(f) || f.include?("/assets/") }

  files.each do |file|
    File.foreach(file).with_index do |line, index|
      @@errors[file] = [] unless @@errors.has_key?(file)
      @@errors[file] << {:time_now => index + 1} if line.include? 'Time.now'
      @@errors[file] << {:time_parse => index + 1} if line.include? 'Time.parse'

      # Controller Specific Checks
      if file.include?("/controllers/")
        formatted_line = line.gsub(' ', '')
        if formatted_line.include?("render:inline") || formatted_line.include?("renderinline:")
          @@errors[file] << {:render_inline => index + 1}
        end
        if formatted_line.include?("render:text") || formatted_line.include?("rendertext:")
          @@errors[file] << {:render_text => index + 1}
        end
        if formatted_line.include?("render:status") || formatted_line.include?("renderstatus:")
          status = formatted_line.gsub("render:status", '').gsub("renderstatus:", '').first
          @@errors[file] << {:render_status => index + 1} if status =~ /^[-+]?[1-9]([0-9]*)?$/
        end
      end

      # Checks for all sub-directories of app except views
      unless file.include?("/views/")
        @@errors[file] << {:find_by_sql => index + 1} if line.include?("find_by_sql") && !line.include?("<<")
        @@errors[file] << {:update_attribute => index + 1} if line.include?("update_attribute(")
        @@errors[file] << {:dot_all => index + 1} if line.include?(".all")
      end

      # Models specific checks
      if file.include?("/models/")
        @@errors[file] << {:table_name => index + 1} if line.include?("self.table_name")
        @@errors[file] << {:read_attribute => index + 1} if line.include?("read_attribute")
        @@errors[file] << {:write_attribute => index + 1} if line.include?("write_attribute")
        @@errors[file] << {:habtm => index + 1} if line.include?("has_and_belongs_to_many")
        formatted_line = line.gsub(" ", "")
        @@errors[file] << {:prepend_true => index + 1} if !(formatted_line.include?("prepend:true") || formatted_line.include?(":prepend=>true")) && line.include?("before_destroy")
        @@errors[file] << {:dependent_destroy => index + 1} if (line.include?('has_one') || line.include?('has_many')) && !line.include?('dependent')
      end

    end
  end
  print
end
custom_validator_directory_check() click to toggle source

Method to check if separate directory is being used for custom validators

# File lib/rails_review/checker.rb, line 34
def custom_validator_directory_check
  if Dir["#{Rails.root}/app/validators"].empty?
    @@errors["/validators"] = [{:validators => "not found"}]
  end
end
gemlock_check() click to toggle source

Method to check whether Gemfile.lock is present in .gitignore or not

# File lib/rails_review/checker.rb, line 7
def gemlock_check
  File.foreach('.gitignore').with_index do |line, index|
    if line.include? 'Gemfile.lock'
      @@errors["/.gitignore"] = [{:gemlock => index + 1}]
      break
    end
  end
end
print() click to toggle source

Print errors found

timezone_check() click to toggle source

Method to check if timezone has been configured

# File lib/rails_review/checker.rb, line 17
def timezone_check
  line_num = 0
  config_line  = ''
  File.foreach("#{Rails.root}/config/application.rb").with_index do |line, index |
    if line.include? "config.time_zone"
      line_num = index + 1
      config_line = line
    end
    break if line_num != 0
  end
  formatted_config_line = config_line.gsub(' ', '')
  if formatted_config_line.first == '#' || formatted_config_line.length == 19
    @@errors["/config/application.rb"] = [{:timezone => line_num}]
  end
end