Commit cf9ea036 by Michael Deimel Committed by Ryan Wilcox

fixt default position

parent 9c68c8c9
...@@ -130,7 +130,7 @@ module AnnotateModels ...@@ -130,7 +130,7 @@ module AnnotateModels
old_content.sub!(/^# #{COMPAT_PREFIX}.*?\n(#.*\n)*\n/, '') old_content.sub!(/^# #{COMPAT_PREFIX}.*?\n(#.*\n)*\n/, '')
# Write it back # 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 } File.open(file_name, "wb") { |f| f.puts new_content }
true true
...@@ -182,15 +182,13 @@ module AnnotateModels ...@@ -182,15 +182,13 @@ module AnnotateModels
].each do |file| ].each do |file|
annotate_one_file(file, info, options_with_position(options, :position_in_fixture)) annotate_one_file(file, info, options_with_position(options, :position_in_fixture))
end end
FIXTURE_DIRS.each do |dir| 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) if File.exist?(fixture_file_name)
annotate_one_file(fixture_file_name, info, options_with_position(options, :position_in_fixture)) annotate_one_file(fixture_file_name, info, options_with_position(options, :position_in_fixture))
end end
end end
end end
annotated annotated
end end
...@@ -300,7 +298,7 @@ module AnnotateModels ...@@ -300,7 +298,7 @@ module AnnotateModels
remove_annotation_of_file(model_file_name) remove_annotation_of_file(model_file_name)
FIXTURE_DIRS.each do |dir| 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) remove_annotation_of_file(fixture_file_name) if File.exist?(fixture_file_name)
end end
......
...@@ -4,13 +4,31 @@ require 'rubygems' ...@@ -4,13 +4,31 @@ require 'rubygems'
require 'activesupport' require 'activesupport'
describe AnnotateModels do 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={}) def mock_klass(stubs={})
@mock_file ||= mock("Klass", stubs) mock("Klass", stubs)
end end
def mock_column(stubs={}) def mock_column(stubs={})
@mock_column ||= mock("Column", stubs) mock("Column", stubs)
end end
it { AnnotateModels.quote(nil).should eql("NULL") } it { AnnotateModels.quote(nil).should eql("NULL") }
...@@ -19,47 +37,82 @@ describe AnnotateModels do ...@@ -19,47 +37,82 @@ describe AnnotateModels do
it { AnnotateModels.quote(25).should eql("25") } it { AnnotateModels.quote(25).should eql("25") }
it { AnnotateModels.quote(25.6).should eql("25.6") } it { AnnotateModels.quote(25.6).should eql("25.6") }
it { AnnotateModels.quote(1e-20).should eql("1.0e-20") } it { AnnotateModels.quote(1e-20).should eql("1.0e-20") }
describe "schema info" do
it "should get schema info" do before(:each) do
@schema_info = <<-EOS
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)
# Schema Info # Schema Info
# #
# Table name: users # 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 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 # Schema Info
module ::ActiveRecord #
class Base # Table name: users
end #
end # id :integer not null, primary key
# name :string(50) not null
#
def create(file, body="hi") EOF
File.open(@dir + '/' + file, "w") do |f|
f.puts(body)
end
end
before :all do end
require "tmpdir" end
@dir = Dir.tmpdir + "/#{Time.now.to_i}" + "/annotate_models"
FileUtils.mkdir_p(@dir) describe "#get_model_class" do
AnnotateModels.model_dir = @dir
before :all do
create('foo.rb', <<-EOS) create('foo.rb', <<-EOS)
class Foo < ActiveRecord::Base class Foo < ActiveRecord::Base
end end
...@@ -70,14 +123,17 @@ EOS ...@@ -70,14 +123,17 @@ EOS
end end
EOS EOS
end end
it "should work" do it "should work" do
klass = AnnotateModels.get_model_class("foo.rb") klass = AnnotateModels.get_model_class("foo.rb")
klass.name.should == "Foo" klass.name.should == "Foo"
end end
it "should not care about unknown macros" do it "should not care about unknown macros" do
klass = AnnotateModels.get_model_class("foo_with_macro.rb") klass = AnnotateModels.get_model_class("foo_with_macro.rb")
klass.name.should == "FooWithMacro" klass.name.should == "FooWithMacro"
end end
end 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