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
You need to sign in or sign up before continuing.
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
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
112 additions
and
101 deletions
+112
-101
annotate_models.rb
lib/annotate_models.rb
+112
-101
No files found.
lib/annotate_models.rb
View file @
4012cbe8
module
AnnotateModels
module
AnnotateModels
MODEL_DIR
=
"app/models"
class
<<
self
FIXTURE_DIR
=
"test/fixtures"
MODEL_DIR
=
"app/models"
PREFIX
=
"== Schema Information"
FIXTURE_DIR
=
"test/fixtures"
PREFIX
=
"== Schema Information"
# Simple quoting for the default column value
def
self
.
quote
(
value
)
# Simple quoting for the default column value
case
value
def
quote
(
value
)
when
NilClass
then
"NULL"
case
value
when
TrueClass
then
"TRUE"
when
NilClass
then
"NULL"
when
FalseClass
then
"FALSE"
when
TrueClass
then
"TRUE"
when
Float
,
Fixnum
,
Bignum
then
value
.
to_s
when
FalseClass
then
"FALSE"
# BigDecimals need to be output in a non-normalized form and quoted.
when
Float
,
Fixnum
,
Bignum
then
value
.
to_s
when
BigDecimal
then
value
.
to_s
(
'F'
)
# BigDecimals need to be output in a non-normalized form and quoted.
else
when
BigDecimal
then
value
.
to_s
(
'F'
)
value
.
inspect
else
value
.
inspect
end
end
end
end
# Use the column information in an ActiveRecord class
# Use the column information in an ActiveRecord class
# 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
"
max_size
=
klass
.
column_names
.
collect
{
|
name
|
name
.
size
}.
max
+
1
max_size
=
klass
.
column_names
.
collect
{
|
name
|
name
.
size
}.
max
+
1
klass
.
columns
.
each
do
|
col
|
klass
.
columns
.
each
do
|
col
|
attrs
=
[]
attrs
=
[]
attrs
<<
"default(
#{
quote
(
col
.
default
)
}
)"
if
col
.
default
attrs
<<
"default(
#{
quote
(
col
.
default
)
}
)"
if
col
.
default
attrs
<<
"not null"
unless
col
.
null
attrs
<<
"not null"
unless
col
.
null
attrs
<<
"primary key"
if
col
.
name
==
klass
.
primary_key
attrs
<<
"primary key"
if
col
.
name
==
klass
.
primary_key
col_type
=
col
.
type
.
to_s
col_type
=
col
.
type
.
to_s
if
col_type
==
"decimal"
if
col_type
==
"decimal"
col_type
<<
"(
#{
col
.
precision
}
,
#{
col
.
scale
}
)"
col_type
<<
"(
#{
col
.
precision
}
,
#{
col
.
scale
}
)"
else
else
col_type
<<
"(
#{
col
.
limit
}
)"
if
col
.
limit
col_type
<<
"(
#{
col
.
limit
}
)"
if
col
.
limit
end
end
info
<<
sprintf
(
"# %-
#{
max_size
}
.
#{
max_size
}
s:%-15.15s %s
\n
"
,
col
.
name
,
col_type
,
attrs
.
join
(
", "
))
info
<<
sprintf
(
"# %-
#{
max_size
}
.
#{
max_size
}
s:%-15.15s %s
\n
"
,
col
.
name
,
col_type
,
attrs
.
join
(
", "
))
end
end
info
<<
"#
\n\n
"
info
<<
"#
\n\n
"
end
end
# Add a schema block to a file. If the file already contains
# Add a schema block to a file. If the file already contains
# 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
)
# Remove old schema info
# Remove old schema info
content
.
sub!
(
/^#
#{
PREFIX
}
.*?\n(#.*\n)*\n/
,
''
)
content
.
sub!
(
/^#
#{
PREFIX
}
.*?\n(#.*\n)*\n/
,
''
)
# Write it back
# Write it back
File
.
open
(
file_name
,
"w"
)
{
|
f
|
f
.
puts
info_block
+
content
}
File
.
open
(
file_name
,
"w"
)
{
|
f
|
f
.
puts
info_block
+
content
}
end
end
end
end
# Given the name of an ActiveRecord class, create a schema
# Given the name of an ActiveRecord class, create a schema
# info block (basically a comment containing information
# info block (basically a comment containing information
# 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"
)
annotate_one_file
(
fixture_file_name
,
info
)
annotate_one_file
(
fixture_file_name
,
info
)
end
end
# Return a list of the model files to annotate. If we have
# Return a list of the model files to annotate. If we have
# command line arguments, they're assumed to be either
# command line arguments, they're assumed to be either
# 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_names
def
get_model_files
models
=
ARGV
.
dup
models
=
ARGV
.
dup
models
.
shift
models
.
shift
if
models
.
empty?
if
models
.
empty?
Dir
.
chdir
(
MODEL_DIR
)
do
Dir
.
chdir
(
MODEL_DIR
)
do
models
=
Dir
[
"**/*.rb"
]
models
=
Dir
[
"**/*.rb"
]
end
end
end
models
end
end
models
end
# Retrieve the classes belonging to the model names we're asked to process
# Check for namespaced models in subdirectories as well as models
# We're passed a name of things that might be
# in subdirectories without namespacing.
# ActiveRecord models. If we can find the class, and
def
get_model_class
(
file
)
# if its a subclass of ActiveRecord::Base,
model
=
file
.
gsub
(
/\.rb$/
,
''
).
camelize
# then pas it to the associated block
parts
=
model
.
split
(
'::'
)
begin
def
self
.
do_annotations
parts
.
inject
(
Object
)
{
|
klass
,
part
|
klass
.
const_get
(
part
)
}
header
=
PREFIX
.
dup
rescue
LoadError
version
=
ActiveRecord
::
Migrator
.
current_version
rescue
0
Object
.
const_get
(
parts
.
last
)
if
version
>
0
end
header
<<
"
\n
# Schema version:
#{
version
}
"
end
end
annotated
=
[]
# We're passed a name of things that might be
self
.
get_model_names
.
each
do
|
m
|
# ActiveRecord models. If we can find the class, and
class_name
=
m
.
sub
(
/\.rb$/
,
''
).
camelize
# if its a subclass of ActiveRecord::Base,
begin
# then pas it to the associated block
klass
=
class_name
.
split
(
'::'
).
inject
(
Object
){
|
klass
,
part
|
klass
.
const_get
(
part
)
}
def
do_annotations
if
klass
<
ActiveRecord
::
Base
&&
!
klass
.
abstract_class?
header
=
PREFIX
.
dup
annotated
<<
class_name
version
=
ActiveRecord
::
Migrator
.
current_version
rescue
0
self
.
annotate
(
klass
,
header
)
if
version
>
0
end
header
<<
"
\n
# Schema version:
#{
version
}
"
rescue
Exception
=>
e
puts
"Unable to annotate
#{
class_name
}
:
#{
e
.
message
}
"
end
end
annotated
=
[]
get_model_files
.
each
do
|
file
|
begin
klass
=
get_model_class
(
file
)
if
klass
<
ActiveRecord
::
Base
&&
!
klass
.
abstract_class?
annotated
<<
klass
annotate
(
klass
,
file
,
header
)
end
rescue
Exception
=>
e
puts
"Unable to annotate
#{
file
}
:
#{
e
.
message
}
"
end
end
puts
"Annotated
#{
annotated
.
join
(
', '
)
}
"
end
end
puts
"Annotated
#{
annotated
.
join
(
', '
)
}
"
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