/lib/rails_refactor.rb

https://github.com/tricon/rails_refactor · Ruby · 189 lines · 152 code · 35 blank · 2 comment · 11 complexity · 9e323cbec759405c88de89b7d953860e MD5 · raw file

  1. #!/usr/bin/env ruby
  2. require 'active_support/inflector'
  3. require 'active_support/core_ext/string/inflections'
  4. begin
  5. File.exist? './config/environment.rb'
  6. rescue LoadError
  7. puts "*** rails_refactor needs to be run from the root of a Rails 3 webapp ***"
  8. exit
  9. end
  10. class Renamer
  11. def initialize(from, to)
  12. @from, @to = from, to
  13. end
  14. def model_rename
  15. to_model_file = @to.underscore + ".rb"
  16. `mv app/models/#{@from.underscore}.rb app/models/#{to_model_file}`
  17. replace_in_file("app/models/#{to_model_file}", @from, @to)
  18. to_spec_file = @to.underscore + "_spec.rb"
  19. `mv spec/models/#{@from.underscore}_spec.rb spec/models/#{to_spec_file}`
  20. replace_in_file("spec/models/#{to_spec_file}", @from, @to)
  21. Dir["db/migrate/*_create_#{@from.underscore.pluralize}.rb"].each do |file|
  22. timestamp_and_path = file.split('_')[0]
  23. to_migration_path = "#{timestamp_and_path}_create_#{@to.underscore.pluralize}.rb"
  24. `mv #{file} #{to_migration_path}`
  25. replace_in_file(to_migration_path, "Create#{@from.pluralize}", "Create#{@to.pluralize}")
  26. replace_in_file(to_migration_path, @from.underscore.pluralize, @to.underscore.pluralize)
  27. end
  28. end
  29. def controller_rename
  30. setup_for_controller_rename
  31. to_controller_path = "app/controllers/#{@to.underscore}.rb"
  32. to_resource_name = @to.gsub(/Controller$/, "")
  33. to_resource_path = to_resource_name.underscore
  34. `mv app/controllers/#{@from.underscore}.rb #{to_controller_path}`
  35. replace_in_file(to_controller_path, @from, @to)
  36. # TODO: Use cross-platform move commands.
  37. if File.exist?("spec/controllers/#{@from.underscore}_spec.rb")
  38. to_spec = "spec/controllers/#{to_resource_path}_controller_spec.rb"
  39. `mv spec/controllers/#{@from.underscore}_spec.rb #{to_spec}`
  40. replace_in_file(to_spec, @from, @to)
  41. end
  42. if Dir.exist?("app/views/#{@from_resource_path}")
  43. `mv app/views/#{@from_resource_path} app/views/#{to_resource_path}`
  44. end
  45. to_helper_path = "app/helpers/#{to_resource_path}_helper.rb"
  46. if File.exist?("app/helpers/#{@from_resource_path}_helper.rb")
  47. `mv app/helpers/#{@from_resource_path}_helper.rb #{to_helper_path}`
  48. replace_in_file(to_helper_path, @from_resource_name, to_resource_name)
  49. end
  50. replace_in_file('config/routes.rb', @from_resource_path, to_resource_path)
  51. end
  52. def controller_action_rename
  53. setup_for_controller_rename
  54. controller_path = "app/controllers/#{@from_controller.underscore}.rb"
  55. replace_in_file(controller_path, @from_action, @to)
  56. views_for_action = "app/views/#{@from_resource_path}/#{@from_action}.*"
  57. Dir[views_for_action].each do |file|
  58. extension = file.split('.')[1..2].join('.')
  59. `mv #{file} app/views/#{@from_resource_path}/#{@to}.#{extension}`
  60. end
  61. end
  62. def setup_for_controller_rename
  63. @from_controller, @from_action = @from.split(".")
  64. @from_resource_name = @from_controller.gsub(/Controller$/, "")
  65. @from_resource_path = @from_resource_name.underscore
  66. end
  67. def replace_in_file(path, find, replace)
  68. contents = File.read(path)
  69. contents.gsub!(find, replace)
  70. File.open(path, "w+") { |f| f.write(contents) }
  71. end
  72. end
  73. if ARGV.length == 3
  74. command, from, to = ARGV
  75. renamer = Renamer.new(from, to)
  76. if command == "rename"
  77. if from.include? "Controller"
  78. if from.include? '.'
  79. renamer.controller_action_rename
  80. else
  81. renamer.controller_rename
  82. end
  83. else
  84. renamer.model_rename
  85. end
  86. end
  87. elsif ARGV[0] == "test"
  88. require 'test/unit'
  89. class RailsRefactorTest < Test::Unit::TestCase
  90. def setup
  91. raise "Run tests in 'dummy' rails project" if !Dir.pwd.end_with? "dummy"
  92. end
  93. def teardown
  94. `git checkout .`
  95. `git clean -f`
  96. `rm -rf app/views/hello_world`
  97. end
  98. def rename(from, to)
  99. `../lib/rails_refactor.rb rename #{from} #{to}`
  100. end
  101. def assert_file_changed(path, from, to)
  102. contents = File.read(path)
  103. assert contents.include?(to)
  104. assert !contents.include?(from)
  105. end
  106. def test_model_rename
  107. rename("DummyModel", "NewModel")
  108. assert File.exist?("app/models/new_model.rb")
  109. assert !File.exist?("app/models/dummy_model.rb")
  110. assert_file_changed("app/models/new_model.rb",
  111. "DummyModel", "NewModel")
  112. assert File.exist?("spec/models/new_model_spec.rb")
  113. assert !File.exist?("spec/models/dummy_model_spec.rb")
  114. assert_file_changed("spec/models/new_model_spec.rb",
  115. "DummyModel", "NewModel")
  116. assert File.exist?("db/migrate/20101230081247_create_new_models.rb")
  117. assert !File.exist?("db/migrate/20101230081247_create_dummy_models.rb")
  118. assert_file_changed("db/migrate/20101230081247_create_new_models.rb",
  119. "CreateDummyModels", "CreateNewModels")
  120. assert_file_changed("db/migrate/20101230081247_create_new_models.rb",
  121. ":dummy_models", ":new_models")
  122. end
  123. def test_controller_action_rename
  124. rename('DummiesController.index', 'new_action')
  125. assert_file_changed("app/controllers/dummies_controller.rb", "index", "new_action")
  126. assert File.exists?("app/views/dummies/new_action.html.erb")
  127. assert !File.exists?("app/views/dummies/index.html.erb")
  128. end
  129. def test_controller_rename
  130. rename("DummiesController", "HelloWorldController")
  131. assert File.exist?("app/controllers/hello_world_controller.rb")
  132. assert !File.exist?("app/controllers/dummies_controller.rb")
  133. assert File.exist?("app/views/hello_world/index.html.erb")
  134. assert !File.exist?("app/views/dummies/index.html.erb")
  135. assert_file_changed("app/controllers/hello_world_controller.rb",
  136. "DummiesController", "HelloWorldController")
  137. routes_contents = File.read("config/routes.rb")
  138. assert routes_contents.include?("hello_world")
  139. assert !routes_contents.include?("dummies")
  140. helper_contents = File.read("app/helpers/hello_world_helper.rb")
  141. assert helper_contents.include?("HelloWorldHelper")
  142. assert !helper_contents.include?("DummiesHelper")
  143. assert File.exist?("spec/controllers/hello_world_controller_spec.rb")
  144. assert !File.exist?("spec/controllers/dummies_controller_spec.rb")
  145. assert_file_changed("spec/controllers/hello_world_controller_spec.rb",
  146. "DummiesController", "HelloWorldController")
  147. end
  148. end
  149. else
  150. puts "Usage:"
  151. puts " rails_refactor rename DummyController NewController"
  152. puts " rails_refactor rename DummyController.my_action new_action"
  153. puts " rails_refactor rename DummyModel NewModel"
  154. end