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
5803fbaa
Commit
5803fbaa
authored
Jul 02, 2017
by
Alexander Belozerov
Committed by
Cuong Tran
Jul 01, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Annotate ordered indexes (#479)
parent
8e75c911
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
71 additions
and
9 deletions
+71
-9
annotate_models.rb
lib/annotate/annotate_models.rb
+20
-2
annotate_models_spec.rb
spec/annotate/annotate_models_spec.rb
+51
-7
No files found.
lib/annotate/annotate_models.rb
View file @
5803fbaa
...
...
@@ -337,15 +337,33 @@ module AnnotateModels
max_size
=
indexes
.
collect
{
|
index
|
index
.
name
.
size
}.
max
+
1
indexes
.
sort_by
(
&
:name
).
each
do
|
index
|
index_info
<<
if
options
[
:format_markdown
]
sprintf
(
"# * `%s`%s:
\n
# * **`%s`**
\n
"
,
index
.
name
,
index
.
unique
?
" (_unique_)"
:
""
,
Array
(
index
.
columns
).
join
(
"`**
\n
# * **`"
)
)
final_index_string_in_markdown
(
index
)
else
sprintf
(
"# %-
#{
max_size
}
.
#{
max_size
}
s %s %s"
,
index
.
name
,
"(
#{
Array
(
index
.
columns
).
join
(
","
)
}
)"
,
index
.
unique
?
"UNIQUE"
:
""
).
rstrip
+
"
\n
"
final_index_string
(
index
,
max_size
)
end
end
index_info
end
def
index_columns_info
(
index
)
Array
(
index
.
columns
).
map
do
|
col
|
if
index
.
try
(
:orders
)
&&
index
.
orders
[
col
.
to_s
]
"
#{
col
}
#{
index
.
orders
[
col
.
to_s
].
upcase
}
"
else
col
.
to_s
end
end
end
def
final_index_string_in_markdown
(
index
)
sprintf
(
"# * `%s`%s:
\n
# * **`%s`**
\n
"
,
index
.
name
,
index
.
unique
?
" (_unique_)"
:
""
,
index_columns_info
(
index
).
join
(
"`**
\n
# * **`"
))
end
def
final_index_string
(
index
,
max_size
)
sprintf
(
"# %-
#{
max_size
}
.
#{
max_size
}
s %s %s"
,
index
.
name
,
"(
#{
index_columns_info
(
index
).
join
(
','
)
}
)"
,
index
.
unique
?
"UNIQUE"
:
""
).
rstrip
+
"
\n
"
end
def
hide_limit?
(
col_type
,
options
)
excludes
=
if
options
[
:hide_limit_column_types
].
blank?
...
...
spec/annotate/annotate_models_spec.rb
View file @
5803fbaa
...
...
@@ -5,11 +5,12 @@ require 'annotate/active_record_patch'
require
'active_support/core_ext/string'
describe
AnnotateModels
do
def
mock_index
(
name
,
columns
=
[],
unique
=
false
)
def
mock_index
(
name
,
columns
=
[],
orders
=
{},
unique
=
false
)
double
(
'IndexKeyDefinition'
,
name:
name
,
columns:
columns
,
unique:
unique
)
unique:
unique
,
orders:
orders
)
end
def
mock_foreign_key
(
name
,
from_column
,
to_table
,
to_column
=
'id'
,
constraints
=
{})
...
...
@@ -320,14 +321,52 @@ EOS
EOS
end
it
'should get ordered indexes keys'
do
klass
=
mock_class
(
:users
,
:id
,
[
mock_column
(
"id"
,
:integer
),
mock_column
(
"firstname"
,
:string
),
mock_column
(
"surname"
,
:string
),
mock_column
(
"value"
,
:string
)
],
[
mock_index
(
'index_rails_02e851e3b7'
,
[
'id'
]),
mock_index
(
'index_rails_02e851e3b8'
,
%w(firstname surname value)
,
'surname'
=>
:asc
,
'value'
=>
:desc
)
])
expect
(
AnnotateModels
.
get_schema_info
(
klass
,
'Schema Info'
,
show_indexes:
true
)).
to
eql
(
<<-
EOS
)
# Schema Info
#
# Table name: users
#
# id :integer not null, primary key
# firstname :string not null
# surname :string not null
# value :string not null
#
# Indexes
#
# index_rails_02e851e3b7 (id)
# index_rails_02e851e3b8 (firstname,surname ASC,value DESC)
#
EOS
end
it
'should get simple indexes keys'
do
klass
=
mock_class
(
:users
,
:id
,
[
mock_column
(
:id
,
:integer
),
mock_column
(
:foreign_thing_id
,
:integer
)
],
[
mock_index
(
'index_rails_02e851e3b7'
,
[
'id'
]),
mock_index
(
'index_rails_02e851e3b8'
,
[
'foreign_thing_id'
])])
],
[
mock_index
(
'index_rails_02e851e3b7'
,
[
'id'
]),
mock_index
(
'index_rails_02e851e3b8'
,
[
'foreign_thing_id'
],
'foreign_thing_id'
=>
:desc
)
])
expect
(
AnnotateModels
.
get_schema_info
(
klass
,
'Schema Info'
,
simple_indexes:
true
)).
to
eql
(
<<-
EOS
)
# Schema Info
#
...
...
@@ -460,8 +499,13 @@ EOS
[
mock_column
(
:id
,
:integer
),
mock_column
(
:name
,
:string
,
limit:
50
)
],
[
mock_index
(
'index_rails_02e851e3b7'
,
[
'id'
]),
mock_index
(
'index_rails_02e851e3b8'
,
[
'foreign_thing_id'
])])
],
[
mock_index
(
'index_rails_02e851e3b7'
,
[
'id'
]),
mock_index
(
'index_rails_02e851e3b8'
,
[
'foreign_thing_id'
],
'foreign_thing_id'
=>
:desc
)
])
expect
(
AnnotateModels
.
get_schema_info
(
klass
,
AnnotateModels
::
PREFIX
,
format_markdown:
true
,
show_indexes:
true
)).
to
eql
(
<<-
EOS
)
#
#{
AnnotateModels
::
PREFIX
}
#
...
...
@@ -479,7 +523,7 @@ EOS
# * `index_rails_02e851e3b7`:
# * **`id`**
# * `index_rails_02e851e3b8`:
# * **`foreign_thing_id`**
# * **`foreign_thing_id
DESC
`**
#
EOS
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