Skip to content

Instantly share code, notes, and snippets.

@cheshire137
Forked from tcocca/missing_spec_generator.rb
Last active July 7, 2016 15:04
Show Gist options
  • Save cheshire137/c8446ce500268752ac3d to your computer and use it in GitHub Desktop.
Save cheshire137/c8446ce500268752ac3d to your computer and use it in GitHub Desktop.
require 'erb'
# lib/missing_spec_generator.rb
class MissingSpecGenerator
RAILS_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
def spec_file spec_path, file_name, spec_template, namespace
spec_name = file_name.gsub('.rb', '') + '_spec.rb'
if File.exist?("#{spec_path}/#{spec_name}")
puts "#{spec_path}/#{spec_name} exists"
else
puts "#{spec_path}/#{spec_name} missing"
puts "\n"
spec_file = ERB.new(spec_template)
class_name = "#{namespace}#{file_name.gsub('.rb', '').camelcase}"
spec = spec_file.result(binding)
puts spec
FileUtils.mkdir_p(spec_path) unless File.exist?(spec_path)
File.open("#{spec_path}/#{spec_name}", 'w') {|f| f.write(spec) }
puts "\n"
end
end
def traverse_specs dir_name, spec_template, namespace=''
source_path = RAILS_ROOT + '/app/' + dir_name
unless File.directory?(source_path)
puts "No #{dir_name}"
return
end
Dir.open(source_path).each do |file_name|
unless file_name.match(/^\./) # skip hidden folders (.svn)
if File.directory?(RAILS_ROOT + '/app/' + dir_name + '/' + file_name)
traverse_specs("#{dir_name}/#{file_name}", spec_template,
"#{namespace}#{file_name.camelcase}::")
else
spec_file("#{RAILS_ROOT}/spec/#{dir_name}", file_name, spec_template,
namespace)
end
end
end
end
def generate_missing_helper_specs
helper_template = %q{require 'rails_helper'
RSpec.describe <%= class_name %>, type: :helper do
pending "add some examples to (or delete) #{__FILE__}"
end
}.gsub(/^ /, '')
traverse_specs('helpers', helper_template)
end
def generate_missing_controller_specs
controller_template = %q{require 'rails_helper'
RSpec.describe <%= class_name %>, type: :controller do
pending "add some examples to (or delete) #{__FILE__}"
end
}.gsub(/^ /, '')
traverse_specs('controllers', controller_template)
end
def generate_missing_worker_specs
worker_template = %q{require 'rails_helper'
RSpec.describe <%= class_name %>, type: :worker do
pending "add some examples to (or delete) #{__FILE__}"
end
}.gsub(/^ /, '')
traverse_specs('workers', worker_template)
end
def generate_missing_model_specs
model_template = %q{require 'rails_helper'
RSpec.describe <%= class_name %>, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end
}.gsub(/^ /, '')
traverse_specs('models', model_template)
end
end
namespace :my_project do
desc "Generate the missing rspec files from base templates"
task :generate_missing_specs do
require 'missing_spec_generator'
msg = MissingSpecGenerator.new
msg.generate_missing_helper_specs
msg.generate_missing_controller_specs
msg.generate_missing_model_specs
msg.generate_missing_worker_specs
end
end
@cheshire137
Copy link
Author

You can also just put the lib/missing_spec_generator.rb in your Rails app, then in a Rails console run:

require_relative 'lib/missing_spec_generator'
msg = MissingSpecGenerator.new
msg.generate_missing_helper_specs
msg.generate_missing_controller_specs
msg.generate_missing_model_specs
msg.generate_missing_worker_specs

@avdept
Copy link

avdept commented Jul 7, 2016

Assuming this is rails lib, you could do like

RAILS_ROOT = Rails.root or even replace RAILS_ROOT with Rails.root

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment