Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
annotate
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
open-source
annotate
Commits
a90dd234
Commit
a90dd234
authored
Jun 20, 2010
by
fistfvck
Browse files
Options
Browse Files
Download
Plain Diff
Merge remote branch 'stereosupersonic/master' into restart
parents
cbd3ed0e
5e4f9292
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
93 additions
and
39 deletions
+93
-39
annotate_models.rb
lib/annotate/annotate_models.rb
+3
-5
annotate_models.rake
lib/tasks/annotate_models.rake
+2
-2
annotate_models_spec.rb
spec/annotate/annotate_models_spec.rb
+88
-32
No files found.
lib/annotate/annotate_models.rb
View file @
a90dd234
...
...
@@ -134,7 +134,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
...
...
@@ -189,15 +189,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
...
...
@@ -307,7 +305,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
...
...
lib/tasks/annotate_models.rake
View file @
a90dd234
desc
"Add schema information (as comments) to model and fixture files"
task
:annotate_models
=>
:environment
do
require
'annotate/annotate_models'
require
File
.
join
(
File
.
dirname
(
__FILE__
),
'..'
,
'annotate'
,
'annotate_models'
)
options
=
{}
options
[
:position_in_class
]
=
ENV
[
'position_in_class'
]
||
ENV
[
'position'
]
||
:before
options
[
:position_in_fixture
]
=
ENV
[
'position_in_fixture'
]
||
ENV
[
'position'
]
||
:before
...
...
@@ -14,7 +14,7 @@ end
desc
"Remove schema information from model and fixture files"
task
:remove_annotation
=>
:environment
do
require
'annotate/annotate_models'
require
File
.
join
(
File
.
dirname
(
__FILE__
),
'..'
,
'annotate'
,
'annotate_models'
)
options
=
{}
options
[
:model_dir
]
=
ENV
[
'model_dir'
]
AnnotateModels
.
remove_annotations
(
options
)
...
...
spec/annotate/annotate_models_spec.rb
View file @
a90dd234
...
...
@@ -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
(
1
e
-
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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment