Finding missing controllers and models
Ever get aggravated you didn't create a model or a base controller for your rails app.
I find its a common issue when large changes are afoot. Like when I create dozens
of tables at a sitting.
For those wondeful times, I've created a rake take that checks my models and controllers, and creates basic versions of the code.
http://2.bp.blogspot.com/_z5ltvMQPaa8/SjJXr_U2YBI/AAAAAAAAAAM/46OqEP32CJ...);padding:0px;color:#000000;text-align:left;line-height:20px;"> require 'rubygems'
require 'pp'
require 'find'
def controller_exists?(thefilename)
Find.find("./app/controllers") do |path|
if FileTest.directory?(path)
if File.basename(path)[0] == ?.
Find.prune
else
next
end
else
if path.include?(thefilename)
return TRUE
end
end
end
return FALSE
end
namespace :check do
desc "Check Model, and create missing ones"
task :modelfile => :environment do
pp ActiveRecord::Base.connection.tables
ActiveRecord::Base.connection.tables.each do |tname|
begin
themodel = tname.classify.constantize
model_exists = TRUE
rescue
model_exists = FALSE
end
case tname
when "schema_migrations"
model_exists = TRUE # Special table
when "tag_cross_ref"
model_exists = TRUE # Special table
end
if model_exists == FALSE # Dont exist
model_name = tname.classify
puts "Missing #{model_name} for table #{tname} - created\n"
mfilename = "./app/models/" + tname.singularize + ".rb"
if File.exists?(mfilename)
puts "#{mfilename} exists - Bad NEWS\n"
raise
end
mfile = open(mfilename,"w")
mfile.puts "class #{model_name} < ActiveRecord::Base\n"
mfile.puts "end\n"
mfile.close
else
puts "Exists #{model_name}(#{tname})\n"
end
end
end
desc "Check Controllers, and create missing ones"
task :controllerfile => :environment do
ActiveRecord::Base.connection.tables.each do |tname|
controller_file_name = tname.singularize + "_controller.rb"
ctl_missing = !controller_exists?(controller_file_name)
case tname
when "schema_migrations"
ctl_missing = FALSE #Special Table
when "tag_cross_ref"
ctl_missing = FALSE # Special table
end
if ctl_missing
controller_name = (tname.singularize + "_controller").camelize
puts "Missing #{controller_name} for table #{tname} - created\n"
cfilename = "./app/controllers/adminspace/" + controller_file_name
if File.exists?(cfilename)
puts "#{cfilename} exists - Bad NEWS\n"
raise
end
cfile = open(cfilename,"w")
cfile.puts "#generated by checker\n"
cfile.puts "class Adminspace::#{controller_name} < ApplicationController\n"
cfile.puts "layout 'tier1admin'\n"
cfile.puts "before_filter :login_required\n"
cfile.puts 'require_role "admin"' + "\n"
cfile.puts "\n"
cfile.puts " active_scaffold :#{tname.singularize} do |config|\n"
cfile.puts " config.actions = [:nested, :create, :update, :show, :list, :search]\n"
cfile.puts " end\n"
cfile.puts "\n"
cfile.puts "end\n"
cfile.close
else
#puts "Exists #{controller_name}(#{tname})\n"
end
end
end
end
- Technology:


Recent comments
25 weeks 9 hours ago
25 weeks 1 day ago
26 weeks 6 days ago
29 weeks 1 day ago
43 weeks 6 days ago
46 weeks 6 days ago
47 weeks 5 days ago
47 weeks 5 days ago
48 weeks 4 hours ago
50 weeks 1 day ago