Commit 7cf64571 by Alexander Belozerov

Annotate on_delete/on_update constraints

parent 91da93dd
...@@ -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: 507 Max: 513
# Offense count: 7 # Offense count: 7
Metrics/PerceivedComplexity: Metrics/PerceivedComplexity:
......
...@@ -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
......
...@@ -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),
......
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