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
4012cbe8
Commit
4012cbe8
authored
Mar 10, 2008
by
Cuong Tran
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'master' of
git://github.com/JackDanger/annotate_models
into JackDanger/master
parents
da0a6841
6713e923
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
28 additions
and
15 deletions
+28
-15
annotate_models.rb
lib/annotate_models.rb
+28
-15
No files found.
lib/annotate_models.rb
View file @
4012cbe8
module
AnnotateModels
module
AnnotateModels
class
<<
self
MODEL_DIR
=
"app/models"
MODEL_DIR
=
"app/models"
FIXTURE_DIR
=
"test/fixtures"
FIXTURE_DIR
=
"test/fixtures"
PREFIX
=
"== Schema Information"
PREFIX
=
"== Schema Information"
# Simple quoting for the default column value
# Simple quoting for the default column value
def
self
.
quote
(
value
)
def
quote
(
value
)
case
value
case
value
when
NilClass
then
"NULL"
when
NilClass
then
"NULL"
when
TrueClass
then
"TRUE"
when
TrueClass
then
"TRUE"
...
@@ -21,7 +22,7 @@ module AnnotateModels
...
@@ -21,7 +22,7 @@ module AnnotateModels
# to create a comment block containing a line for
# to create a comment block containing a line for
# each column. The line contains the column name,
# each column. The line contains the column name,
# the type (and length), and any optional attributes
# the type (and length), and any optional attributes
def
self
.
get_schema_info
(
klass
,
header
)
def
get_schema_info
(
klass
,
header
)
info
=
"#
#{
header
}
\n
#
\n
"
info
=
"#
#{
header
}
\n
#
\n
"
info
<<
"# Table name:
#{
klass
.
table_name
}
\n
#
\n
"
info
<<
"# Table name:
#{
klass
.
table_name
}
\n
#
\n
"
...
@@ -48,7 +49,7 @@ module AnnotateModels
...
@@ -48,7 +49,7 @@ module AnnotateModels
# a schema info block (a comment starting
# a schema info block (a comment starting
# with "Schema as of ..."), remove it first.
# with "Schema as of ..."), remove it first.
def
self
.
annotate_one_file
(
file_name
,
info_block
)
def
annotate_one_file
(
file_name
,
info_block
)
if
File
.
exist?
(
file_name
)
if
File
.
exist?
(
file_name
)
content
=
File
.
read
(
file_name
)
content
=
File
.
read
(
file_name
)
...
@@ -65,10 +66,10 @@ module AnnotateModels
...
@@ -65,10 +66,10 @@ module AnnotateModels
# on the columns and their types) and put it at the front
# on the columns and their types) and put it at the front
# of the model and fixture source files.
# of the model and fixture source files.
def
self
.
annotate
(
klass
,
header
)
def
annotate
(
klass
,
file
,
header
)
info
=
get_schema_info
(
klass
,
header
)
info
=
get_schema_info
(
klass
,
header
)
model_file_name
=
File
.
join
(
MODEL_DIR
,
klass
.
name
.
underscore
+
".rb"
)
model_file_name
=
File
.
join
(
MODEL_DIR
,
file
)
annotate_one_file
(
model_file_name
,
info
)
annotate_one_file
(
model_file_name
,
info
)
fixture_file_name
=
File
.
join
(
FIXTURE_DIR
,
klass
.
table_name
+
".yml"
)
fixture_file_name
=
File
.
join
(
FIXTURE_DIR
,
klass
.
table_name
+
".yml"
)
...
@@ -80,7 +81,7 @@ module AnnotateModels
...
@@ -80,7 +81,7 @@ module AnnotateModels
# the underscore or CamelCase versions of model names.
# the underscore or CamelCase versions of model names.
# Otherwise we take all the model files in the
# Otherwise we take all the model files in the
# app/models directory.
# app/models directory.
def
self
.
get_model_nam
es
def
get_model_fil
es
models
=
ARGV
.
dup
models
=
ARGV
.
dup
models
.
shift
models
.
shift
...
@@ -92,12 +93,24 @@ module AnnotateModels
...
@@ -92,12 +93,24 @@ module AnnotateModels
models
models
end
end
# Retrieve the classes belonging to the model names we're asked to process
# Check for namespaced models in subdirectories as well as models
# in subdirectories without namespacing.
def
get_model_class
(
file
)
model
=
file
.
gsub
(
/\.rb$/
,
''
).
camelize
parts
=
model
.
split
(
'::'
)
begin
parts
.
inject
(
Object
)
{
|
klass
,
part
|
klass
.
const_get
(
part
)
}
rescue
LoadError
Object
.
const_get
(
parts
.
last
)
end
end
# We're passed a name of things that might be
# We're passed a name of things that might be
# ActiveRecord models. If we can find the class, and
# ActiveRecord models. If we can find the class, and
# if its a subclass of ActiveRecord::Base,
# if its a subclass of ActiveRecord::Base,
# then pas it to the associated block
# then pas it to the associated block
def
do_annotations
def
self
.
do_annotations
header
=
PREFIX
.
dup
header
=
PREFIX
.
dup
version
=
ActiveRecord
::
Migrator
.
current_version
rescue
0
version
=
ActiveRecord
::
Migrator
.
current_version
rescue
0
if
version
>
0
if
version
>
0
...
@@ -105,19 +118,18 @@ module AnnotateModels
...
@@ -105,19 +118,18 @@ module AnnotateModels
end
end
annotated
=
[]
annotated
=
[]
self
.
get_model_names
.
each
do
|
m
|
get_model_files
.
each
do
|
file
|
class_name
=
m
.
sub
(
/\.rb$/
,
''
).
camelize
begin
begin
klass
=
class_name
.
split
(
'::'
).
inject
(
Object
){
|
klass
,
part
|
klass
.
const_get
(
part
)
}
klass
=
get_model_class
(
file
)
if
klass
<
ActiveRecord
::
Base
&&
!
klass
.
abstract_class?
if
klass
<
ActiveRecord
::
Base
&&
!
klass
.
abstract_class?
annotated
<<
class_name
annotated
<<
klass
self
.
annotate
(
klass
,
header
)
annotate
(
klass
,
file
,
header
)
end
end
rescue
Exception
=>
e
rescue
Exception
=>
e
puts
"Unable to annotate
#{
class_nam
e
}
:
#{
e
.
message
}
"
puts
"Unable to annotate
#{
fil
e
}
:
#{
e
.
message
}
"
end
end
end
end
puts
"Annotated
#{
annotated
.
join
(
', '
)
}
"
puts
"Annotated
#{
annotated
.
join
(
', '
)
}
"
end
end
end
end
end
\ No newline at end of file
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