matthuhiggins-foreigner 0.3.1 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README +2 -9
- data/lib/foreigner/connection_adapters/abstract/schema_definitions.rb +11 -54
- metadata +2 -2
data/README
CHANGED
@@ -61,14 +61,7 @@ Lastly, a name can be specified for the foreign key constraint:
|
|
61
61
|
Create/Change Table Shorthand
|
62
62
|
-----------------------------
|
63
63
|
|
64
|
-
Foreigner adds extra behavior to
|
65
|
-
|
66
|
-
Create the comments table with a foreign key to posts:
|
67
|
-
|
68
|
-
create_table :comments do |t|
|
69
|
-
t.integer :post_id
|
70
|
-
t.foreign_key :posts
|
71
|
-
end
|
64
|
+
Foreigner adds extra behavior to change_table, which lets you define foreign keys using shorthand.
|
72
65
|
|
73
66
|
Add a missing foreign key to comments:
|
74
67
|
|
@@ -85,7 +78,7 @@ Additional t.references option
|
|
85
78
|
Foreigner extends table.references with the :foreign_key option. Pass true, and the default
|
86
79
|
foreign key options are used:
|
87
80
|
|
88
|
-
|
81
|
+
change_table :comments do |t|
|
89
82
|
t.references :post, :foreign_key => true
|
90
83
|
end
|
91
84
|
|
@@ -2,7 +2,7 @@ module Foreigner
|
|
2
2
|
module ConnectionAdapters
|
3
3
|
class ForeignKeyDefinition < Struct.new(:from_table, :to_table, :options) #:nodoc:
|
4
4
|
end
|
5
|
-
|
5
|
+
|
6
6
|
module SchemaDefinitions
|
7
7
|
def self.included(base)
|
8
8
|
base::TableDefinition.class_eval do
|
@@ -16,75 +16,32 @@ module Foreigner
|
|
16
16
|
end
|
17
17
|
|
18
18
|
module TableDefinition
|
19
|
-
class ForeignKey < Struct.new(:base, :to_table, :options)
|
20
|
-
def to_sql
|
21
|
-
base.foreign_key_definition(to_table, options)
|
22
|
-
end
|
23
|
-
alias to_s :to_sql
|
24
|
-
end
|
25
|
-
|
26
19
|
def self.included(base)
|
27
20
|
base.class_eval do
|
28
21
|
include InstanceMethods
|
29
22
|
alias_method_chain :references, :foreign_keys
|
30
|
-
alias_method_chain :to_sql, :foreign_keys
|
31
23
|
end
|
32
24
|
end
|
33
25
|
|
34
26
|
module InstanceMethods
|
35
|
-
# Adds a :foreign_key option to TableDefinition.references.
|
36
|
-
# If :foreign_key is true, a foreign key constraint is added to the table.
|
37
|
-
# You can also specify a hash, which is passed as foreign key options.
|
38
|
-
#
|
39
|
-
# ===== Examples
|
40
|
-
# ====== Add goat_id column and a foreign key to the goats table.
|
41
|
-
# t.references(:goat, :foreign_key => true)
|
42
|
-
# ====== Add goat_id column and a cascading foreign key to the goats table.
|
43
|
-
# t.references(:goat, :foreign_key => {:dependent => :delete})
|
44
|
-
#
|
45
|
-
# Note: No foreign key is created if :polymorphic => true is used.
|
46
|
-
# Note: If no name is specified, the database driver creates one for you!
|
47
27
|
def references_with_foreign_keys(*args)
|
48
28
|
options = args.extract_options!
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
29
|
+
if options[:foreign_key].present?
|
30
|
+
ActiveSupport::Deprecation.warn(
|
31
|
+
':foreign_key option is deprecated inside create_table. ' +
|
32
|
+
'to add a foreign key, use add_foreign_key', caller[0,10]
|
33
|
+
)
|
54
34
|
end
|
55
35
|
|
56
36
|
references_without_foreign_keys(*(args << options))
|
57
37
|
end
|
58
38
|
|
59
|
-
# Defines a foreign key for the table. +to_table+ can be a single Symbol, or
|
60
|
-
# an Array of Symbols. See SchemaStatements#add_foreign_key
|
61
|
-
#
|
62
|
-
# ===== Examples
|
63
|
-
# ====== Creating a simple foreign key
|
64
|
-
# t.foreign_key(:people)
|
65
|
-
# ====== Defining the column
|
66
|
-
# t.foreign_key(:people, :column => :sender_id)
|
67
|
-
# ====== Creating a named foreign key
|
68
|
-
# t.foreign_key(:people, :column => :sender_id, :name => 'sender_foreign_key')
|
69
|
-
# ====== Defining the column of the +to_table+.
|
70
|
-
# t.foreign_key(:people, :column => :sender_id, :primary_key => :person_id)
|
71
39
|
def foreign_key(to_table, options = {})
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
40
|
+
ActiveSupport::Deprecation.warn(
|
41
|
+
'adding a foreign key inside create_table is deprecated. ' +
|
42
|
+
'to add a foreign key, use add_foreign_key', caller[0,10]
|
43
|
+
)
|
76
44
|
end
|
77
|
-
|
78
|
-
def to_sql_with_foreign_keys
|
79
|
-
sql = to_sql_without_foreign_keys
|
80
|
-
sql << ', ' << (foreign_keys * ', ') if foreign_keys.present?
|
81
|
-
sql
|
82
|
-
end
|
83
|
-
|
84
|
-
private
|
85
|
-
def foreign_keys
|
86
|
-
@foreign_keys ||= []
|
87
|
-
end
|
88
45
|
end
|
89
46
|
end
|
90
47
|
|
@@ -142,7 +99,7 @@ module Foreigner
|
|
142
99
|
polymorphic = options[:polymorphic]
|
143
100
|
fk_options = options.delete(:foreign_key)
|
144
101
|
|
145
|
-
references_without_foreign_keys(*(args << options))
|
102
|
+
references_without_foreign_keys(*(args.dup << options))
|
146
103
|
|
147
104
|
if fk_options && !polymorphic
|
148
105
|
fk_options = {} if fk_options == true
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: matthuhiggins-foreigner
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matthew Higgins
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-12-25 00:00:00 -08:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|