matthuhiggins-foreigner 0.6.4 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/{README → README.rdoc} +16 -23
- data/lib/foreigner.rb +16 -24
- metadata +6 -9
data/{README → README.rdoc}
RENAMED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
Foreigner
|
|
2
|
-
=========
|
|
1
|
+
= Foreigner
|
|
3
2
|
|
|
4
3
|
Rails does not come with methods to add foreign keys. Foreigner introduces a few
|
|
5
4
|
methods to your migrations for adding and removing foreign key constraints.
|
|
@@ -7,8 +6,7 @@ methods to your migrations for adding and removing foreign key constraints.
|
|
|
7
6
|
Since each adapter implements the API, migrations using Foreigner will continue to
|
|
8
7
|
work on databases that do not support foreign keys, such as sqlite3.
|
|
9
8
|
|
|
10
|
-
Installation
|
|
11
|
-
------------
|
|
9
|
+
== Installation
|
|
12
10
|
|
|
13
11
|
Install as a plugin:
|
|
14
12
|
|
|
@@ -22,8 +20,7 @@ In Rails 3, install as a gem by adding the following to your Gemfile:
|
|
|
22
20
|
|
|
23
21
|
gem 'matthuhiggins-foreigner', :require => 'foreigner'
|
|
24
22
|
|
|
25
|
-
API
|
|
26
|
-
---
|
|
23
|
+
== API
|
|
27
24
|
|
|
28
25
|
An adapter implementing the Foreigner API implements three methods.
|
|
29
26
|
(Options are documented in connection_adapters/abstract/schema_definitions.rb):
|
|
@@ -32,8 +29,7 @@ An adapter implementing the Foreigner API implements three methods.
|
|
|
32
29
|
remove_foreign_key(from_table, options)
|
|
33
30
|
foreign_keys(table_name)
|
|
34
31
|
|
|
35
|
-
Example
|
|
36
|
-
-------
|
|
32
|
+
== Example
|
|
37
33
|
|
|
38
34
|
The most common use of foreign keys is to reference a table that a model belongs to.
|
|
39
35
|
For example, given the following model:
|
|
@@ -62,40 +58,37 @@ Lastly, a name can be specified for the foreign key constraint:
|
|
|
62
58
|
|
|
63
59
|
add_foreign_key(:comments, :posts, :name => 'comment_article_foreign_key')
|
|
64
60
|
|
|
65
|
-
Create/Change Table Shorthand
|
|
66
|
-
-----------------------------
|
|
61
|
+
== Create/Change Table Shorthand
|
|
67
62
|
|
|
68
63
|
Foreigner adds extra behavior to change_table, which lets you define foreign keys using shorthand.
|
|
69
64
|
|
|
70
65
|
Add a missing foreign key to comments:
|
|
71
66
|
|
|
72
|
-
change_table :comments do |t|
|
|
73
|
-
|
|
74
|
-
end
|
|
67
|
+
change_table :comments do |t|
|
|
68
|
+
t.foreign_key :posts, :dependent => :delete
|
|
69
|
+
end
|
|
75
70
|
|
|
76
71
|
t.foreign_key accepts the same options as add_foreign_key.
|
|
77
72
|
|
|
78
73
|
|
|
79
|
-
Additional t.references option
|
|
80
|
-
------------------------------
|
|
74
|
+
== Additional t.references option
|
|
81
75
|
|
|
82
76
|
Foreigner extends table.references with the :foreign_key option. Pass true, and the default
|
|
83
77
|
foreign key options are used:
|
|
84
78
|
|
|
85
|
-
change_table :comments do |t|
|
|
86
|
-
|
|
87
|
-
end
|
|
79
|
+
change_table :comments do |t|
|
|
80
|
+
t.references :post, :foreign_key => true
|
|
81
|
+
end
|
|
88
82
|
|
|
89
83
|
An options hash can also be passed. It accepts the same options as add_foreign_key:
|
|
90
84
|
|
|
91
|
-
change_table :comments do |t|
|
|
92
|
-
|
|
93
|
-
end
|
|
85
|
+
change_table :comments do |t|
|
|
86
|
+
t.references :author, :foreign_key => {:dependent => :destroy}
|
|
87
|
+
end
|
|
94
88
|
|
|
95
89
|
By default, t.references will not generate a foreign key.
|
|
96
90
|
|
|
97
|
-
schema.rb
|
|
98
|
-
---------
|
|
91
|
+
== schema.rb
|
|
99
92
|
|
|
100
93
|
Similar to indexes, the foreign keys in your database are automatically dumped to schema.rb.
|
|
101
94
|
This allows you to use foreign keys without switching to the :sql schema.
|
data/lib/foreigner.rb
CHANGED
|
@@ -13,6 +13,15 @@ module Foreigner
|
|
|
13
13
|
end
|
|
14
14
|
|
|
15
15
|
def load_adapter!
|
|
16
|
+
ActiveRecord::ConnectionAdapters.module_eval do
|
|
17
|
+
include Foreigner::ConnectionAdapters::SchemaStatements
|
|
18
|
+
include Foreigner::ConnectionAdapters::SchemaDefinitions
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
ActiveRecord::SchemaDumper.class_eval do
|
|
22
|
+
include Foreigner::SchemaDumper
|
|
23
|
+
end
|
|
24
|
+
|
|
16
25
|
if adapters.key?(configured_adapter)
|
|
17
26
|
require adapters[configured_adapter]
|
|
18
27
|
end
|
|
@@ -21,20 +30,6 @@ module Foreigner
|
|
|
21
30
|
def configured_adapter
|
|
22
31
|
ActiveRecord::Base.connection_pool.spec.config[:adapter]
|
|
23
32
|
end
|
|
24
|
-
|
|
25
|
-
def on_load(&block)
|
|
26
|
-
if defined?(Rails) && Rails.version >= '3.0'
|
|
27
|
-
ActiveSupport.on_load :active_record do
|
|
28
|
-
unless ActiveRecord::Base.connected?
|
|
29
|
-
ActiveRecord::Base.configurations = Rails.application.config.database_configuration
|
|
30
|
-
ActiveRecord::Base.establish_connection
|
|
31
|
-
end
|
|
32
|
-
block.call
|
|
33
|
-
end
|
|
34
|
-
else
|
|
35
|
-
yield
|
|
36
|
-
end
|
|
37
|
-
end
|
|
38
33
|
end
|
|
39
34
|
end
|
|
40
35
|
|
|
@@ -42,17 +37,14 @@ Foreigner.register 'mysql', 'foreigner/connection_adapters/mysql_adapter'
|
|
|
42
37
|
Foreigner.register 'mysql2', 'foreigner/connection_adapters/mysql_adapter'
|
|
43
38
|
Foreigner.register 'postgresql', 'foreigner/connection_adapters/postgresql_adapter'
|
|
44
39
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
SchemaDumper.class_eval do
|
|
53
|
-
include Foreigner::SchemaDumper
|
|
40
|
+
if defined?(Rails::Railtie)
|
|
41
|
+
class Railtie < Rails::Railtie
|
|
42
|
+
initializer 'foreigner.load_adapter' do
|
|
43
|
+
ActiveSupport.on_load :active_record do
|
|
44
|
+
Foreigner.load_adapter!
|
|
45
|
+
end
|
|
54
46
|
end
|
|
55
47
|
end
|
|
56
|
-
|
|
48
|
+
else
|
|
57
49
|
Foreigner.load_adapter!
|
|
58
50
|
end
|
metadata
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: matthuhiggins-foreigner
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
hash: 15
|
|
5
4
|
prerelease: false
|
|
6
5
|
segments:
|
|
7
6
|
- 0
|
|
8
|
-
-
|
|
9
|
-
-
|
|
10
|
-
version: 0.
|
|
7
|
+
- 7
|
|
8
|
+
- 0
|
|
9
|
+
version: 0.7.0
|
|
11
10
|
platform: ruby
|
|
12
11
|
authors:
|
|
13
12
|
- Matthew Higgins
|
|
@@ -15,7 +14,7 @@ autorequire:
|
|
|
15
14
|
bindir: bin
|
|
16
15
|
cert_chain: []
|
|
17
16
|
|
|
18
|
-
date: 2010-
|
|
17
|
+
date: 2010-09-06 00:00:00 -07:00
|
|
19
18
|
default_executable:
|
|
20
19
|
dependencies: []
|
|
21
20
|
|
|
@@ -26,11 +25,11 @@ executables: []
|
|
|
26
25
|
extensions: []
|
|
27
26
|
|
|
28
27
|
extra_rdoc_files:
|
|
29
|
-
- README
|
|
28
|
+
- README.rdoc
|
|
30
29
|
files:
|
|
31
30
|
- MIT-LICENSE
|
|
32
31
|
- Rakefile
|
|
33
|
-
- README
|
|
32
|
+
- README.rdoc
|
|
34
33
|
- lib/foreigner.rb
|
|
35
34
|
- lib/foreigner/schema_dumper.rb
|
|
36
35
|
- lib/foreigner/connection_adapters/sql_2003.rb
|
|
@@ -54,7 +53,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
54
53
|
requirements:
|
|
55
54
|
- - ">="
|
|
56
55
|
- !ruby/object:Gem::Version
|
|
57
|
-
hash: 59
|
|
58
56
|
segments:
|
|
59
57
|
- 1
|
|
60
58
|
- 8
|
|
@@ -65,7 +63,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
65
63
|
requirements:
|
|
66
64
|
- - ">="
|
|
67
65
|
- !ruby/object:Gem::Version
|
|
68
|
-
hash: 17
|
|
69
66
|
segments:
|
|
70
67
|
- 1
|
|
71
68
|
- 3
|