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
......
...@@ -5,12 +5,30 @@ require 'activesupport' ...@@ -5,12 +5,30 @@ 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") }
...@@ -20,46 +38,81 @@ describe AnnotateModels do ...@@ -20,46 +38,81 @@ describe AnnotateModels do
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") }
it "should get schema info" do describe "schema info" do
before(:each) do
@schema_info = <<-EOS
# Schema Info
#
# Table name: users
#
# id :integer not null, primary key
# name :string(50) not null
#
AnnotateModels.get_schema_info(mock_klass( EOS
AnnotateModels.model_dir = @dir
@user_file = create('user.rb', <<-EOS)
class User < ActiveRecord::Base
end
EOS
@mock = mock_klass(
:connection => mock("Conn", :indexes => []), :connection => mock("Conn", :indexes => []),
:table_name => "users", :table_name => "users",
:primary_key => "id", :primary_key => "id",
:column_names => ["id","login"], :column_names => ["id","name"],
:columns => [ :columns => [
mock_column(:type => "integer", :default => nil, :null => false, :name => "id", :limit => nil), mock_column(:type => "integer", :default => nil, :null => false, :name => "id", :limit => nil),
mock_column(:type => "string", :default => nil, :null => false, :name => "name", :limit => 50) mock_column(:type => "string", :default => nil, :null => false, :name => "name", :limit => 50)
]), "Schema Info").should eql(<<-EOS) ])
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 # 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 class User < ActiveRecord::Base
end
EOF
end end
describe "#get_model_class" do it "should write the schema after" do
module ::ActiveRecord AnnotateModels.stub!(:get_schema_info).and_return @schema_info
class Base AnnotateModels.do_annotations(:position => :after)
end File.read(@user_file).should eql(<<-EOF)
end class User < ActiveRecord::Base
end
# Schema Info
#
# Table name: users
#
# id :integer not null, primary key
# name :string(50) not null
#
EOF
def create(file, body="hi")
File.open(@dir + '/' + file, "w") do |f|
f.puts(body)
end end
end end
describe "#get_model_class" do
before :all do before :all do
require "tmpdir"
@dir = Dir.tmpdir + "/#{Time.now.to_i}" + "/annotate_models"
FileUtils.mkdir_p(@dir)
AnnotateModels.model_dir = @dir
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