Skip to content
This repository has been archived by the owner on Aug 16, 2023. It is now read-only.

Commit

Permalink
Fixed bug that was crashing twin file generation
Browse files Browse the repository at this point in the history
- also added better snippets for gen'd model, view, controller and
  helper classes
  • Loading branch information
dchelimsky committed Sep 30, 2009
1 parent 8c91306 commit b93a56c
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
30 changes: 28 additions & 2 deletions Support/lib/spec/mate/switch_command.rb
Expand Up @@ -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}")
Expand All @@ -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|
Expand Down
25 changes: 25 additions & 0 deletions Support/spec/spec/mate/switch_command_spec.rb
Expand Up @@ -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
Expand Down

0 comments on commit b93a56c

Please sign in to comment.