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
bc2662b4
Commit
bc2662b4
authored
Mar 27, 2015
by
Cuong Tran
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #241 from subakva/features/foreign_key_annotations
Adds support for foreign key annotations
parents
b38328d9
5322fd96
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
82 additions
and
3 deletions
+82
-3
README.rdoc
README.rdoc
+1
-0
annotate
bin/annotate
+5
-0
annotate.rb
lib/annotate.rb
+1
-1
annotate_models.rb
lib/annotate/annotate_models.rb
+26
-0
auto_annotate_models.rake
lib/generators/annotate/templates/auto_annotate_models.rake
+1
-0
annotate_models_spec.rb
spec/annotate/annotate_models_spec.rb
+46
-2
auto_annotate_models.rake
...tegration/rails_4.1.1/lib/tasks/auto_annotate_models.rake
+1
-0
auto_annotate_models.rake
...tegration/rails_4.2.0/lib/tasks/auto_annotate_models.rake
+1
-0
No files found.
README.rdoc
View file @
bc2662b4
...
...
@@ -175,6 +175,7 @@ you can do so with a simple environment variable, instead of editing the
-v, --version Show the current version of this gem
-m, --show-migration Include the migration version number in the annotation
-i, --show-indexes List the table's database indexes in the annotation
-k, --show-foreign-keys List the table's foreign key constraints in the annotation
-s, --simple-indexes Concat the column's related indexes in the annotation
--model-dir dir Annotate model files stored in dir rather than app/models, separate multiple dirs with comas
--ignore-model-subdirects Ignore subdirectories of the models directory
...
...
bin/annotate
View file @
bc2662b4
...
...
@@ -109,6 +109,11 @@ OptionParser.new do |opts|
ENV
[
'include_version'
]
=
"yes"
end
opts
.
on
(
'-k'
,
'--show-foreign-keys'
,
"List the table's foreign key constraints in the annotation"
)
do
ENV
[
'show_foreign_keys'
]
=
"yes"
end
opts
.
on
(
'-i'
,
'--show-indexes'
,
"List the table's database indexes in the annotation"
)
do
ENV
[
'show_indexes'
]
=
"yes"
...
...
lib/annotate.rb
View file @
bc2662b4
...
...
@@ -26,7 +26,7 @@ module Annotate
:show_indexes
,
:simple_indexes
,
:include_version
,
:exclude_tests
,
:exclude_fixtures
,
:exclude_factories
,
:ignore_model_sub_dir
,
:format_bare
,
:format_rdoc
,
:format_markdown
,
:sort
,
:force
,
:trace
,
:timestamp
,
:exclude_serializers
,
:classified_sort
:timestamp
,
:exclude_serializers
,
:classified_sort
,
:show_foreign_keys
,
]
OTHER_OPTIONS
=
[
:ignore_columns
...
...
lib/annotate/annotate_models.rb
View file @
bc2662b4
...
...
@@ -193,6 +193,10 @@ module AnnotateModels
info
<<
get_index_info
(
klass
,
options
)
end
if
options
[
:show_foreign_keys
]
&&
klass
.
table_exists?
info
<<
get_foreign_key_info
(
klass
,
options
)
end
if
options
[
:format_rdoc
]
info
<<
"#--
\n
"
info
<<
"#
#{
END_MARK
}
\n
"
...
...
@@ -223,6 +227,28 @@ module AnnotateModels
return
index_info
end
def
get_foreign_key_info
(
klass
,
options
=
{})
if
(
options
[
:format_markdown
])
fk_info
=
"#
\n
# ### Foreign Keys
\n
#
\n
"
else
fk_info
=
"#
\n
# Foreign Keys
\n
#
\n
"
end
foreign_keys
=
klass
.
connection
.
respond_to?
(
:foreign_keys
)
?
klass
.
connection
.
foreign_keys
(
klass
.
table_name
)
:
[]
return
""
if
foreign_keys
.
empty?
max_size
=
foreign_keys
.
collect
{
|
fk
|
fk
.
name
.
size
}.
max
+
1
foreign_keys
.
sort_by
{
|
fk
|
fk
.
name
}.
each
do
|
fk
|
ref_info
=
"
#{
fk
.
column
}
=>
#{
fk
.
to_table
}
.
#{
fk
.
primary_key
}
"
if
(
options
[
:format_markdown
])
fk_info
<<
sprintf
(
"# * `%s`:
\n
# * **`%s`**
\n
"
,
fk
.
name
,
ref_info
)
else
fk_info
<<
sprintf
(
"# %-
#{
max_size
}
.
#{
max_size
}
s %s"
,
fk
.
name
,
"(
#{
ref_info
}
)"
).
rstrip
+
"
\n
"
end
end
return
fk_info
end
# Add a schema block to a file. If the file already contains
# a schema info block (a comment starting with "== Schema Information"), check if it
# matches the block that is already there. If so, leave it be. If not, remove the old
...
...
lib/generators/annotate/templates/auto_annotate_models.rake
View file @
bc2662b4
...
...
@@ -11,6 +11,7 @@ if Rails.env.development?
'position_in_test'
=>
"before"
,
'position_in_fixture'
=>
"before"
,
'position_in_factory'
=>
"before"
,
'show_foreign_keys'
=>
"true"
,
'show_indexes'
=>
"true"
,
'simple_indexes'
=>
"false"
,
'model_dir'
=>
"app/models"
,
...
...
spec/annotate/annotate_models_spec.rb
View file @
bc2662b4
...
...
@@ -4,9 +4,26 @@ require 'annotate/annotate_models'
require
'annotate/active_record_patch'
describe
AnnotateModels
do
def
mock_class
(
table_name
,
primary_key
,
columns
)
def
mock_foreign_key
(
name
,
from_column
,
to_table
,
to_column
=
'id'
)
double
(
"ForeignKeyDefinition"
,
:name
=>
name
,
:column
=>
from_column
,
:to_table
=>
to_table
,
:primary_key
=>
to_column
,
)
end
def
mock_connection
(
indexes
=
[],
foreign_keys
=
[])
double
(
"Conn"
,
:indexes
=>
indexes
,
:foreign_keys
=>
foreign_keys
,
)
end
def
mock_class
(
table_name
,
primary_key
,
columns
,
foreign_keys
=
[])
options
=
{
:connection
=>
double
(
"Conn"
,
:indexes
=>
[]),
:connection
=>
mock_connection
([],
foreign_keys
),
:table_exists?
=>
true
,
:table_name
=>
table_name
,
:primary_key
=>
primary_key
,
:column_names
=>
columns
.
map
{
|
col
|
col
.
name
.
to_s
},
...
...
@@ -127,6 +144,33 @@ EOS
EOS
end
it
"should get foreign key info"
do
klass
=
mock_class
(
:users
,
:id
,
[
mock_column
(
:id
,
:integer
),
mock_column
(
:foreign_thing_id
,
:integer
),
],
[
mock_foreign_key
(
'fk_rails_02e851e3b7'
,
'foreign_thing_id'
,
'foreign_things'
)
])
expect
(
AnnotateModels
.
get_schema_info
(
klass
,
"Schema Info"
,
:show_foreign_keys
=>
true
)).
to
eql
(
<<-
EOS
)
# Schema Info
#
# Table name: users
#
# id :integer not null, primary key
# foreign_thing_id :integer not null
#
# Foreign Keys
#
# fk_rails_02e851e3b7 (foreign_thing_id => foreign_things.id)
#
EOS
end
it
"should get schema info as RDoc"
do
klass
=
mock_class
(
:users
,
:id
,
[
mock_column
(
:id
,
:integer
),
...
...
spec/integration/rails_4.1.1/lib/tasks/auto_annotate_models.rake
View file @
bc2662b4
...
...
@@ -11,6 +11,7 @@ if Rails.env.development?
'position_in_test'
=>
"before"
,
'position_in_fixture'
=>
"before"
,
'position_in_factory'
=>
"before"
,
'show_foreign_keys'
=>
"true"
,
'show_indexes'
=>
"true"
,
'simple_indexes'
=>
"false"
,
'model_dir'
=>
"app/models"
,
...
...
spec/integration/rails_4.2.0/lib/tasks/auto_annotate_models.rake
View file @
bc2662b4
...
...
@@ -11,6 +11,7 @@ if Rails.env.development?
'position_in_test'
=>
"before"
,
'position_in_fixture'
=>
"before"
,
'position_in_factory'
=>
"before"
,
'show_foreign_keys'
=>
"true"
,
'show_indexes'
=>
"true"
,
'simple_indexes'
=>
"false"
,
'model_dir'
=>
"app/models"
,
...
...
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