matthuhiggins-foreigner 0.3.0 → 0.5.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 CHANGED
@@ -14,9 +14,13 @@ Install as a plugin:
14
14
 
15
15
  ruby script/plugin install git://github.com/matthuhiggins/foreigner.git
16
16
 
17
- Install as a gem by adding the following to environment.rb:
17
+ In Rails 2, install as a gem by adding the following to config/environment.rb:
18
18
 
19
- config.gem "matthuhiggins-foreigner", :lib => "foreigner", :source => http://gemcutter.org
19
+ config.gem "matthuhiggins-foreigner", :lib => "foreigner"
20
+
21
+ In Rails 3, install as a gem by adding the following to your Gemfile:
22
+
23
+ gem 'matthuhiggins-foreigner', :require => 'foreigner'
20
24
 
21
25
  API
22
26
  ---
@@ -61,14 +65,7 @@ Lastly, a name can be specified for the foreign key constraint:
61
65
  Create/Change Table Shorthand
62
66
  -----------------------------
63
67
 
64
- Foreigner adds extra behavior to create_table and change_table, which lets you define foreign keys using shorthand.
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
68
+ Foreigner adds extra behavior to change_table, which lets you define foreign keys using shorthand.
72
69
 
73
70
  Add a missing foreign key to comments:
74
71
 
@@ -85,7 +82,7 @@ Additional t.references option
85
82
  Foreigner extends table.references with the :foreign_key option. Pass true, and the default
86
83
  foreign key options are used:
87
84
 
88
- create_table :comments do |t|
85
+ change_table :comments do |t|
89
86
  t.references :post, :foreign_key => true
90
87
  end
91
88
 
@@ -101,6 +98,6 @@ schema.rb
101
98
  ---------
102
99
 
103
100
  Similar to indexes, the foreign keys in your database are automatically dumped to schema.rb.
104
- This allows you to use foreign keys without fighting Rails!
101
+ This allows you to use foreign keys without switching to the :sql schema.
105
102
 
106
103
  Copyright (c) 2009 Matthew Higgins, released under the MIT license
@@ -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
- fk_options = options.delete(:foreign_key)
50
-
51
- if fk_options && !options[:polymorphic]
52
- fk_options = {} if fk_options == true
53
- args.each { |to_table| foreign_key(to_table, fk_options) }
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
- if @base.supports_foreign_keys?
73
- to_table = to_table.to_s.pluralize if ActiveRecord::Base.pluralize_table_names
74
- foreign_keys << ForeignKey.new(@base, to_table, options)
75
- end
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
@@ -8,23 +8,18 @@ module Foreigner
8
8
  def add_foreign_key(from_table, to_table, options = {})
9
9
  column = options[:column] || "#{to_table.to_s.singularize}_id"
10
10
  foreign_key_name = foreign_key_name(from_table, column, options)
11
+ primary_key = options[:primary_key] || "id"
12
+ dependency = dependency_sql(options[:dependent])
11
13
 
12
14
  sql =
13
15
  "ALTER TABLE #{quote_table_name(from_table)} " +
14
16
  "ADD CONSTRAINT #{quote_column_name(foreign_key_name)} " +
15
- foreign_key_definition(to_table, options)
16
-
17
- execute(sql)
18
- end
19
-
20
- def foreign_key_definition(to_table, options = {})
21
- column = options[:column] || "#{to_table.to_s.singularize}_id"
22
- primary_key = options[:primary_key] || "id"
23
- dependency = dependency_sql(options[:dependent])
17
+ "FOREIGN KEY (#{quote_column_name(column)}) " +
18
+ "REFERENCES #{quote_table_name(ActiveRecord::Migrator.proper_table_name(to_table))}(#{primary_key})"
24
19
 
25
- sql = "FOREIGN KEY (#{quote_column_name(column)}) REFERENCES #{quote_table_name(to_table)}(#{primary_key})"
26
20
  sql << " #{dependency}" unless dependency.blank?
27
- sql
21
+
22
+ execute(sql)
28
23
  end
29
24
 
30
25
  private
data/lib/foreigner.rb CHANGED
@@ -3,19 +3,55 @@ require 'foreigner/connection_adapters/abstract/schema_definitions'
3
3
  require 'foreigner/connection_adapters/sql_2003'
4
4
  require 'foreigner/schema_dumper'
5
5
 
6
- module ActiveRecord
7
- module ConnectionAdapters
8
- include Foreigner::ConnectionAdapters::SchemaStatements
9
- include Foreigner::ConnectionAdapters::SchemaDefinitions
10
- end
6
+ module Foreigner
7
+ mattr_accessor :adapters
8
+ self.adapters = {}
9
+
10
+ class << self
11
+ def register(adapter_name, file_name)
12
+ adapters[adapter_name] = file_name
13
+ end
11
14
 
12
- SchemaDumper.class_eval do
13
- include Foreigner::SchemaDumper
15
+ def load_adapter!
16
+ if adapters.key?(configured_adapter)
17
+ require adapters[configured_adapter]
18
+ end
19
+ end
20
+
21
+ def configured_adapter
22
+ ActiveRecord::Base.connection.adapter_name.downcase
23
+ end
24
+
25
+ def on_load(&block)
26
+ if 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
14
38
  end
15
-
16
- Base.class_eval do
17
- if %w(MySQL PostgreSQL).include? connection.adapter_name
18
- require "foreigner/connection_adapters/#{connection.adapter_name.downcase}_adapter"
39
+ end
40
+
41
+ Foreigner.register 'mysql', 'foreigner/connection_adapters/mysql_adapter'
42
+ Foreigner.register 'postgresql', 'foreigner/connection_adapters/postgresql_adapter'
43
+
44
+ Foreigner.on_load do
45
+ module ActiveRecord
46
+ module ConnectionAdapters
47
+ include Foreigner::ConnectionAdapters::SchemaStatements
48
+ include Foreigner::ConnectionAdapters::SchemaDefinitions
49
+ end
50
+
51
+ SchemaDumper.class_eval do
52
+ include Foreigner::SchemaDumper
19
53
  end
20
54
  end
55
+
56
+ Foreigner.load_adapter!
21
57
  end
metadata CHANGED
@@ -1,7 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: matthuhiggins-foreigner
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 5
8
+ - 0
9
+ version: 0.5.0
5
10
  platform: ruby
6
11
  authors:
7
12
  - Matthew Higgins
@@ -9,11 +14,11 @@ autorequire:
9
14
  bindir: bin
10
15
  cert_chain: []
11
16
 
12
- date: 2009-09-07 00:00:00 -07:00
17
+ date: 2010-04-02 00:00:00 -07:00
13
18
  default_executable:
14
19
  dependencies: []
15
20
 
16
- description: Foreign keys for Rails migrations
21
+ description: Adds helpers to migrations and correctly dumps foreign keys to schema.rb
17
22
  email: developer@matthewhiggins.com
18
23
  executables: []
19
24
 
@@ -35,34 +40,38 @@ files:
35
40
  - test/helper.rb
36
41
  - test/mysql_adapter_test.rb
37
42
  has_rdoc: true
38
- homepage: http://github.com/matthuhiggins/foreigner/tree/master
43
+ homepage: http://github.com/matthuhiggins/foreigner
39
44
  licenses: []
40
45
 
41
46
  post_install_message:
42
- rdoc_options:
43
- - --line-numbers
44
- - --main
45
- - README
47
+ rdoc_options: []
48
+
46
49
  require_paths:
47
50
  - lib
48
51
  required_ruby_version: !ruby/object:Gem::Requirement
49
52
  requirements:
50
53
  - - ">="
51
54
  - !ruby/object:Gem::Version
52
- version: "0"
53
- version:
55
+ segments:
56
+ - 1
57
+ - 8
58
+ - 6
59
+ version: 1.8.6
54
60
  required_rubygems_version: !ruby/object:Gem::Requirement
55
61
  requirements:
56
62
  - - ">="
57
63
  - !ruby/object:Gem::Version
58
- version: "0"
59
- version:
64
+ segments:
65
+ - 1
66
+ - 3
67
+ - 6
68
+ version: 1.3.6
60
69
  requirements: []
61
70
 
62
- rubyforge_project:
63
- rubygems_version: 1.3.5
71
+ rubyforge_project: matthuhiggins-foreigner
72
+ rubygems_version: 1.3.6
64
73
  signing_key:
65
- specification_version: 1
66
- summary: Foreign keys for Rails migrations
74
+ specification_version: 3
75
+ summary: Foreign keys for Rails
67
76
  test_files: []
68
77