foreigner 1.2.0 → 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -8,11 +8,11 @@ module Foreigner
8
8
 
9
9
  module ClassMethods
10
10
  def dump_foreign_key(foreign_key)
11
- statement_parts = [ ('add_foreign_key ' + foreign_key.from_table.inspect) ]
12
- statement_parts << foreign_key.to_table.inspect
11
+ statement_parts = [ ('add_foreign_key ' + remove_prefix_and_suffix(foreign_key.from_table).inspect) ]
12
+ statement_parts << remove_prefix_and_suffix(foreign_key.to_table).inspect
13
13
  statement_parts << (':name => ' + foreign_key.options[:name].inspect)
14
14
 
15
- if foreign_key.options[:column] != "#{foreign_key.to_table.singularize}_id"
15
+ if foreign_key.options[:column] != "#{remove_prefix_and_suffix(foreign_key.to_table).singularize}_id"
16
16
  statement_parts << (':column => ' + foreign_key.options[:column].inspect)
17
17
  end
18
18
  if foreign_key.options[:primary_key] != 'id'
@@ -24,6 +24,10 @@ module Foreigner
24
24
 
25
25
  statement_parts.join(', ')
26
26
  end
27
+
28
+ def remove_prefix_and_suffix(table)
29
+ table.gsub(/^(#{ActiveRecord::Base.table_name_prefix})(.+)(#{ActiveRecord::Base.table_name_suffix})$/, "\\2")
30
+ end
27
31
  end
28
32
 
29
33
  def tables_with_foreign_keys(stream)
@@ -48,6 +48,16 @@ class Foreigner::SchemaDumperTest < Foreigner::UnitTest
48
48
  assert_equal ['bar'].to_set, dumper.processed_tables.to_set
49
49
  end
50
50
 
51
+ test 'removes table name suffix and prefix' do
52
+ begin
53
+ ActiveRecord::Base.table_name_prefix = 'pre_'
54
+ ActiveRecord::Base.table_name_suffix = '_suf'
55
+ assert_dump "add_foreign_key \"foos\", \"bars\", :name => \"lulz\"", Foreigner::ConnectionAdapters::ForeignKeyDefinition.new('pre_foos_suf', 'pre_bars_suf', column: 'bar_id', primary_key: 'id', name: 'lulz')
56
+ ensure
57
+ ActiveRecord::Base.table_name_suffix = ActiveRecord::Base.table_name_prefix = ''
58
+ end
59
+ end
60
+
51
61
  private
52
62
  def assert_dump(expected, definition)
53
63
  assert_equal expected, MockSchemaDumper.dump_foreign_key(definition)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: foreigner
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.2.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-18 00:00:00.000000000 Z
12
+ date: 2012-08-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord