Commit 95638467 by Michael Deimel

fixt default position

parent 9c68c8c9
......@@ -130,7 +130,7 @@ module AnnotateModels
old_content.sub!(/^# #{COMPAT_PREFIX}.*?\n(#.*\n)*\n/, '')
# Write it back
new_content = options[:position] == 'before' ? (info_block + old_content) : (old_content + "\n" + info_block)
new_content = options[:position].to_s == 'after' ? (old_content + "\n" + info_block) : (info_block + old_content)
File.open(file_name, "wb") { |f| f.puts new_content }
true
......@@ -182,15 +182,13 @@ module AnnotateModels
].each do |file|
annotate_one_file(file, info, options_with_position(options, :position_in_fixture))
end
FIXTURE_DIRS.each do |dir|
fixture_file_name = File.join(dir,klass.table_name + ".yml")
fixture_file_name = File.join(dir,(klass.table_name || "") + ".yml")
if File.exist?(fixture_file_name)
annotate_one_file(fixture_file_name, info, options_with_position(options, :position_in_fixture))
end
end
end
annotated
end
......@@ -300,7 +298,7 @@ module AnnotateModels
remove_annotation_of_file(model_file_name)
FIXTURE_DIRS.each do |dir|
fixture_file_name = File.join(dir,klass.table_name + ".yml")
fixture_file_name = File.join(dir,(klass.table_name || "") + ".yml")
remove_annotation_of_file(fixture_file_name) if File.exist?(fixture_file_name)
end
......
......@@ -4,13 +4,31 @@ require 'rubygems'
require 'activesupport'
describe AnnotateModels do
before(:all) do
require "tmpdir"
@dir = Dir.tmpdir + "/#{Time.now.to_i}" + "/annotate_models"
FileUtils.mkdir_p(@dir)
end
module ::ActiveRecord
class Base
end
end
def create(file, body="hi")
File.open(@dir + '/' + file, "w") do |f|
f.puts(body)
end
@dir + '/' + file
end
def mock_klass(stubs={})
@mock_file ||= mock("Klass", stubs)
mock("Klass", stubs)
end
def mock_column(stubs={})
@mock_column ||= mock("Column", stubs)
mock("Column", stubs)
end
it { AnnotateModels.quote(nil).should eql("NULL") }
......@@ -19,47 +37,82 @@ describe AnnotateModels do
it { AnnotateModels.quote(25).should eql("25") }
it { AnnotateModels.quote(25.6).should eql("25.6") }
it { AnnotateModels.quote(1e-20).should eql("1.0e-20") }
describe "schema info" do
it "should get schema info" do
AnnotateModels.get_schema_info(mock_klass(
:connection => mock("Conn", :indexes => []),
:table_name => "users",
:primary_key => "id",
:column_names => ["id","login"],
:columns => [
mock_column(:type => "integer", :default => nil, :null => false, :name => "id", :limit => nil),
mock_column(:type => "string", :default => nil, :null => false, :name => "name", :limit => 50)
]), "Schema Info").should eql(<<-EOS)
before(:each) do
@schema_info = <<-EOS
# Schema Info
#
# Table name: users
#
# id :integer not null, primary key
# id :integer not null, primary key
# id :integer not null, primary key
# name :string(50) not null
#
EOS
AnnotateModels.model_dir = @dir
@user_file = create('user.rb', <<-EOS)
class User < ActiveRecord::Base
end
EOS
@mock = mock_klass(
:connection => mock("Conn", :indexes => []),
:table_name => "users",
:primary_key => "id",
:column_names => ["id","name"],
:columns => [
mock_column(:type => "integer", :default => nil, :null => false, :name => "id", :limit => nil),
mock_column(:type => "string", :default => nil, :null => false, :name => "name", :limit => 50)
])
end
it "should get schema info" do
AnnotateModels.get_schema_info(@mock , "Schema Info").should eql(@schema_info)
end
it "should write the schema before (default)" do
AnnotateModels.stub!(:get_schema_info).and_return @schema_info
AnnotateModels.do_annotations
File.read(@user_file).should eql(<<-EOF)
# Schema Info
#
# Table name: users
#
# id :integer not null, primary key
# name :string(50) not null
#
end
class User < ActiveRecord::Base
end
EOF
end
it "should write the schema after" do
AnnotateModels.stub!(:get_schema_info).and_return @schema_info
AnnotateModels.do_annotations(:position => :after)
File.read(@user_file).should eql(<<-EOF)
class User < ActiveRecord::Base
end
describe "#get_model_class" do
module ::ActiveRecord
class Base
end
end
# Schema Info
#
# Table name: users
#
# id :integer not null, primary key
# name :string(50) not null
#
def create(file, body="hi")
File.open(@dir + '/' + file, "w") do |f|
f.puts(body)
end
end
EOF
before :all do
require "tmpdir"
@dir = Dir.tmpdir + "/#{Time.now.to_i}" + "/annotate_models"
FileUtils.mkdir_p(@dir)
AnnotateModels.model_dir = @dir
end
end
describe "#get_model_class" do
before :all do
create('foo.rb', <<-EOS)
class Foo < ActiveRecord::Base
end
......@@ -70,14 +123,17 @@ EOS
end
EOS
end
it "should work" do
klass = AnnotateModels.get_model_class("foo.rb")
klass.name.should == "Foo"
end
it "should not care about unknown macros" do
klass = AnnotateModels.get_model_class("foo_with_macro.rb")
klass.name.should == "FooWithMacro"
end
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment