Browse files

Fixed bug that was crashing twin file generation

- also added better snippets for gen'd model, view, controller and
  helper classes
  • Loading branch information...
1 parent 8c91306 commit b93a56c1f6b56c71e744d117e35c71552fddd172 @dchelimsky committed Sep 30, 2009
Showing with 53 additions and 2 deletions.
  1. +28 −2 Support/lib/spec/mate/switch_command.rb
  2. +25 −0 Support/spec/spec/mate/switch_command_spec.rb
View
30 Support/lib/spec/mate/switch_command.rb
@@ -86,11 +86,37 @@ def content_for(file_type, relative_path)
case file_type
when /spec$/ then
spec(relative_path)
+ when "controller"
+ <<-CONTROLLER
+class #{class_from_path(relative_path)} < ApplicationController
+end
+CONTROLLER
+ when "model"
+ <<-MODEL
+class #{class_from_path(relative_path)} < ActiveRecord::Base
+end
+MODEL
+ when "helper"
+ <<-HELPER
+module #{class_from_path(relative_path)}
+end
+HELPER
+ when "view"
+ ""
else
klass(relative_path)
end
end
+ def class_from_path(path)
+ underscored = path.split('/').last.split('.rb').first
+ parts = underscored.split('_')
+ parts.inject("") do |word, part|
+ word << part.capitalize
+ word
+ end
+ end
+
# Extracts the snippet text
def snippet(snippet_name)
snippet_file = File.expand_path(File.dirname(__FILE__) + "/../../../../Snippets/#{snippet_name}")
@@ -106,9 +132,9 @@ def spec(path)
SPEC
end
- def klass(relative_path)
+ def klass(relative_path, content=nil)
parts = relative_path.split('/')
- lib_index = parts.index('lib')
+ lib_index = parts.index('lib') || 0
parts = parts[lib_index+1..-1]
lines = Array.new(parts.length*2)
parts.each_with_index do |part, n|
View
25 Support/spec/spec/mate/switch_command_spec.rb
@@ -196,6 +196,31 @@ def twin(expected)
SwitchCommand.new.content_for('controller spec', "spec/controllers/mooky_controller_spec.rb").split("\n")[0].should ==
"require 'spec_helper'"
end
+
+ it "creates a controller if twinned from a controller spec" do
+ SwitchCommand.new.content_for('controller', "spec/controllers/mooky_controller.rb").should == <<-EXPECTED
+class MookyController < ApplicationController
+end
+EXPECTED
+ end
+
+ it "creates a model if twinned from a model spec" do
+ SwitchCommand.new.content_for('model', "spec/models/mooky.rb").should == <<-EXPECTED
+class Mooky < ActiveRecord::Base
+end
+EXPECTED
+ end
+
+ it "creates a helper if twinned from a helper spec" do
+ SwitchCommand.new.content_for('helper', "spec/helpers/mooky_helper.rb").should == <<-EXPECTED
+module MookyHelper
+end
+EXPECTED
+ end
+
+ it "creates an empty view if twinned from a view spec" do
+ SwitchCommand.new.content_for('view', "spec/views/mookies/index.html.erb_spec.rb").should == ""
+ end
end
end
end

0 comments on commit b93a56c

Please sign in to comment.