Commit 47e1b1ec by Cuong Tran

Merge pull request #358 from sashabelozerov/on_delete_on_update

Annotate on_delete/on_update foreign key constraints
parents 91da93dd 7cf64571
......@@ -105,7 +105,7 @@ Metrics/MethodLength:
# Offense count: 2
# Configuration parameters: CountComments.
Metrics/ModuleLength:
Max: 507
Max: 513
# Offense count: 7
Metrics/PerceivedComplexity:
......
......@@ -330,10 +330,14 @@ module AnnotateModels
max_size = foreign_keys.collect{|fk| fk.name.size}.max + 1
foreign_keys.sort_by(&:name).each do |fk|
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]
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
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
......
......@@ -5,12 +5,14 @@ require 'annotate/active_record_patch'
require 'active_support/core_ext/string'
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",
:name => name,
:column => from_column,
:to_table => to_table,
:primary_key => to_column,
:on_delete => constraints[:on_delete],
:on_update => constraints[:on_update]
)
end
......@@ -197,6 +199,36 @@ EOS
EOS
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
klass = mock_class(:users, :id, [
mock_column(:id, :integer),
......@@ -261,7 +293,7 @@ EOS
# notes :text(55) not null
#
EOS
when_called_with hide_limit_column_types: 'integer,boolean,string,text', returns:
<<-EOS.strip_heredoc
# Schema Info
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment