Skip to content

Instantly share code, notes, and snippets.

@hiveer
Created April 25, 2018 07:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hiveer/991c2c045305d48b8824f1b73bde268d to your computer and use it in GitHub Desktop.
Save hiveer/991c2c045305d48b8824f1b73bde268d to your computer and use it in GitHub Desktop.
code climate base on 'rails_best_practices'
#!/usr/bin/env ruby
REFERENCE = {
"add model virtual attribute" => "https://rails-bestpractices.com/posts/2010/07/21/add-model-virtual-attribute/",
"always add db index" => "https://rails-bestpractices.com/posts/2010/07/24/always-add-db-index/",
"check return value of save otherwise use save" => "https://rails-bestpractices.com/posts/2012/11/02/check-the-return-value-of-save-otherwise-use-save/",
"default_scope is evil" => "https://rails-bestpractices.com/posts/2013/06/15/default_scope-is-evil/",
"dry bundler in capistrano" => "https://rails-bestpractices.com/posts/2010/09/02/dry-bundler-in-capistrano/",
"isolating seed data" => "https://rails-bestpractices.com/posts/2010/07/24/isolating-seed-data/",
"keep finders on their own model" => "https://rails-bestpractices.com/posts/2010/07/23/keep-finders-on-their-own-model/",
"law of demeter" => "https://rails-bestpractices.com/posts/2010/07/24/the-law-of-demeter/",
"move code into controller" => "https://rails-bestpractices.com/posts/2010/07/24/move-code-into-controller/",
"move code into helper" => "https://rails-bestpractices.com/posts/2010/07/24/move-code-into-helper/",
"move code into model" => "https://rails-bestpractices.com/posts/2010/07/24/move-code-into-model/",
"move finder to named_scope" => "https://rails-bestpractices.com/posts/2010/07/14/move-finder-to-named_scope/",
"move model logic into model" => "https://rails-bestpractices.com/posts/2010/07/21/move-model-logic-into-the-model/",
"needless deep nesting" => "https://rails-bestpractices.com/posts/2010/07/22/needless-deep-nesting/",
"don't rescue exception" => "https://rails-bestpractices.com/posts/2012/11/01/don-t-rescue-exception-rescue-standarderror/",
"not use default route if you use restful design" => "https://rails-bestpractices.com/posts/2010/07/22/not-use-default-route-if-you-use-restful-design/",
"not use time_ago_in_words" => "https://rails-bestpractices.com/posts/2012/02/10/not-use-time_ago_in_words/",
"overuse route customizations" => "https://rails-bestpractices.com/posts/2010/07/22/overuse-route-customizations/",
"protect mass assignment" => "https://rails-bestpractices.com/posts/2012/03/06/protect-mass-assignment/",
"remove empty helpers" => "https://rails-bestpractices.com/posts/2011/04/09/remove-empty-helpers/",
"replace complex creation wit factory method" => "https://rails-bestpractices.com/posts/2010/07/21/replace-complex-creation-with-factory-method/",
"replace instance variable with local variable" => "https://rails-bestpractices.com/posts/2010/07/24/replace-instance-variable-with-local-variable/",
"restrict auto generated routes" => "https://rails-bestpractices.com/posts/2011/08/19/restrict-auto-generated-routes/",
"simplify render in controllers" => "https://rails-bestpractices.com/posts/2010/12/12/simplify-render-in-controllers/",
"simplify render in views" => "https://rails-bestpractices.com/posts/2010/12/04/simplify-render-in-views/",
"use before_filter" => "https://rails-bestpractices.com/posts/2010/07/24/use-before_filter/",
"use model assignment" => "https://rails-bestpractices.com/posts/2010/07/19/use-model-association/",
"use multipart alternative as content_type of email" => "https://rails-bestpractices.com/posts/2010/08/05/use-multipart-alternative-as-content_type-of-email/",
"use observer" => "https://rails-bestpractices.com/posts/2010/07/24/use-observer/",
"use query attribute" => "https://rails-bestpractices.com/posts/2010/10/03/use-query-attribute/",
"use say and say_with_time in migrations to make a useful migration log" => "https://rails-bestpractices.com/posts/2010/08/19/use-say-and-say_with_time-in-migrations-to-make-a-useful-migration-log/",
"use scope access" => "https://rails-bestpractices.com/posts/2010/07/20/use-scope-access/",
"speed up assets precompile with turbo sprockets rails3" => "https://rails-bestpractices.com/posts/2012/11/23/speed-up-assets-precompile-with-turbo-sprockets-rails3/"
}
def get_article_name str
reg = /([a-zA-Z0-9 _]+)\(?.*/
key = if reg.match str
$1.strip.downcase
else
""
end
REFERENCE[key]
end
def generate_html
puts "Generating the code climate ..."
`rails_best_practices . > rails_best_practices_output.html`
f = File.open("rails_best_practices_output.html")
of = File.open("code_climate.html", 'w')
reg = /.*\[31m(.*):([0-9]+) - (.*)\e.*/
f.each_line do |line|
next unless reg.match(line)
match_line = $2
match_file = $1
article_name = $3
if get_article_name(article_name)
of.puts("<h2><a target='_blank' href=#{get_article_name(article_name)}>#{article_name}</a></h2>")
else
of.puts("<h2>#{article_name}</h2>")
end
of.puts("<p>#{match_file.gsub(Dir.pwd, '')}:<strong style=\"background-color:darkseagreen\">#{match_line}</strong></p>")
ffa = File.open(match_file).to_a
of.puts("<div style=\"background-color:lightgrey;margin-left:50\">")
start = match_line.to_i - 5 > 0 ? match_line.to_i - 5 : 0
(start .. match_line.to_i + 5).each do |index|
if ffa[index] && (index + 1) == match_line.to_i
of.puts("<code style=\"background-color:darkseagreen\">#{index+1} - #{ffa[index].gsub('<', '&lt;').gsub('>', '&gt;')}</code><br>")
elsif ffa[index]
of.puts("<code>#{index+1} - #{ffa[index].gsub('<', '&lt;').gsub('>', '&gt;')}</code><br>")
end
end
of.puts("</div>")
end
f.close
of.close
puts "Open file in Chrome"
system("open code_climate.html")
`rm rails_best_practices_output.html`
`rm code_climate.html`
end
if `compgen -c | grep rails_best_practices` == ''
puts 'We will install the gem `rails_best_practices` first'
`gem install rails_best_practices`
generate_html
else
generate_html
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment