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
7cf64571
Commit
7cf64571
authored
Mar 23, 2016
by
Alexander Belozerov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Annotate on_delete/on_update constraints
parent
91da93dd
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
41 additions
and
5 deletions
+41
-5
.rubocop_todo.yml
.rubocop_todo.yml
+1
-1
annotate_models.rb
lib/annotate/annotate_models.rb
+6
-2
annotate_models_spec.rb
spec/annotate/annotate_models_spec.rb
+34
-2
No files found.
.rubocop_todo.yml
View file @
7cf64571
...
@@ -105,7 +105,7 @@ Metrics/MethodLength:
...
@@ -105,7 +105,7 @@ Metrics/MethodLength:
# Offense count: 2
# Offense count: 2
# Configuration parameters: CountComments.
# Configuration parameters: CountComments.
Metrics/ModuleLength
:
Metrics/ModuleLength
:
Max
:
5
07
Max
:
5
13
# Offense count: 7
# Offense count: 7
Metrics/PerceivedComplexity
:
Metrics/PerceivedComplexity
:
...
...
lib/annotate/annotate_models.rb
View file @
7cf64571
...
@@ -330,10 +330,14 @@ module AnnotateModels
...
@@ -330,10 +330,14 @@ module AnnotateModels
max_size
=
foreign_keys
.
collect
{
|
fk
|
fk
.
name
.
size
}.
max
+
1
max_size
=
foreign_keys
.
collect
{
|
fk
|
fk
.
name
.
size
}.
max
+
1
foreign_keys
.
sort_by
(
&
:name
).
each
do
|
fk
|
foreign_keys
.
sort_by
(
&
:name
).
each
do
|
fk
|
ref_info
=
"
#{
fk
.
column
}
=>
#{
fk
.
to_table
}
.
#{
fk
.
primary_key
}
"
ref_info
=
"
#{
fk
.
column
}
=>
#{
fk
.
to_table
}
.
#{
fk
.
primary_key
}
"
constraints_info
=
''
constraints_info
+=
"ON DELETE =>
#{
fk
.
on_delete
}
"
if
fk
.
on_delete
constraints_info
+=
"ON UPDATE =>
#{
fk
.
on_update
}
"
if
fk
.
on_update
constraints_info
.
strip!
if
options
[
:format_markdown
]
if
options
[
:format_markdown
]
fk_info
<<
sprintf
(
"# * `%s`
:
\n
# * **`%s`**
\n
"
,
fk
.
name
,
ref_info
)
fk_info
<<
sprintf
(
"# * `%s`
%s:
\n
# * **`%s`**
\n
"
,
fk
.
name
,
constraints_info
.
blank?
?
''
:
" (_
#{
constraints_info
}
_)"
,
ref_info
)
else
else
fk_info
<<
sprintf
(
"# %-
#{
max_size
}
.
#{
max_size
}
s %s
"
,
fk
.
name
,
"(
#{
ref_info
}
)"
).
rstrip
+
"
\n
"
fk_info
<<
sprintf
(
"# %-
#{
max_size
}
.
#{
max_size
}
s %s
%s"
,
fk
.
name
,
"(
#{
ref_info
}
)"
,
constraints_info
).
rstrip
+
"
\n
"
end
end
end
end
...
...
spec/annotate/annotate_models_spec.rb
View file @
7cf64571
...
@@ -5,12 +5,14 @@ require 'annotate/active_record_patch'
...
@@ -5,12 +5,14 @@ require 'annotate/active_record_patch'
require
'active_support/core_ext/string'
require
'active_support/core_ext/string'
describe
AnnotateModels
do
describe
AnnotateModels
do
def
mock_foreign_key
(
name
,
from_column
,
to_table
,
to_column
=
'id'
)
def
mock_foreign_key
(
name
,
from_column
,
to_table
,
to_column
=
'id'
,
constraints
=
{}
)
double
(
"ForeignKeyDefinition"
,
double
(
"ForeignKeyDefinition"
,
:name
=>
name
,
:name
=>
name
,
:column
=>
from_column
,
:column
=>
from_column
,
:to_table
=>
to_table
,
:to_table
=>
to_table
,
:primary_key
=>
to_column
,
:primary_key
=>
to_column
,
:on_delete
=>
constraints
[
:on_delete
],
:on_update
=>
constraints
[
:on_update
]
)
)
end
end
...
@@ -197,6 +199,36 @@ EOS
...
@@ -197,6 +199,36 @@ EOS
EOS
EOS
end
end
it
"should get foreign key info if on_delete/on_update options present"
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'
,
'id'
,
on_delete:
'on_delete_value'
,
on_update:
'on_update_value'
)
])
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) ON DELETE => on_delete_value ON UPDATE => on_update_value
#
EOS
end
it
"should get schema info as RDoc"
do
it
"should get schema info as RDoc"
do
klass
=
mock_class
(
:users
,
:id
,
[
klass
=
mock_class
(
:users
,
:id
,
[
mock_column
(
:id
,
:integer
),
mock_column
(
:id
,
:integer
),
...
@@ -261,7 +293,7 @@ EOS
...
@@ -261,7 +293,7 @@ EOS
# notes :text(55) not null
# notes :text(55) not null
#
#
EOS
EOS
when_called_with
hide_limit_column_types:
'integer,boolean,string,text'
,
returns:
when_called_with
hide_limit_column_types:
'integer,boolean,string,text'
,
returns:
<<-
EOS
.
strip_heredoc
<<-
EOS
.
strip_heredoc
# Schema Info
# Schema Info
...
...
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