matthuhiggins-foreigner 0.4.1 → 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 +7 -3
 - data/lib/foreigner/connection_adapters/sql_2003.rb +6 -11
 - data/lib/foreigner.rb +48 -12
 - metadata +25 -16
 
    
        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 
     | 
    
         
            -
             
     | 
| 
      
 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" 
     | 
| 
      
 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 
     | 
    
         
             
            ---
         
     | 
| 
         @@ -94,6 +98,6 @@ schema.rb 
     | 
|
| 
       94 
98 
     | 
    
         
             
            ---------
         
     | 
| 
       95 
99 
     | 
    
         | 
| 
       96 
100 
     | 
    
         
             
            Similar to indexes, the foreign keys in your database are automatically dumped to schema.rb.
         
     | 
| 
       97 
     | 
    
         
            -
            This allows you to use foreign keys without  
     | 
| 
      
 101 
     | 
    
         
            +
            This allows you to use foreign keys without switching to the :sql schema.
         
     | 
| 
       98 
102 
     | 
    
         | 
| 
       99 
103 
     | 
    
         
             
            Copyright (c) 2009 Matthew Higgins, released under the MIT license
         
     | 
| 
         @@ -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 
     | 
    
         
            -
                       
     | 
| 
       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 
     | 
    
         
            -
             
     | 
| 
      
 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  
     | 
| 
       7 
     | 
    
         
            -
               
     | 
| 
       8 
     | 
    
         
            -
             
     | 
| 
       9 
     | 
    
         
            -
             
     | 
| 
       10 
     | 
    
         
            -
               
     | 
| 
       11 
     | 
    
         
            -
             
     | 
| 
       12 
     | 
    
         
            -
             
     | 
| 
       13 
     | 
    
         
            -
                 
     | 
| 
       14 
     | 
    
         
            -
              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
         
     | 
| 
       15 
14 
     | 
    
         | 
| 
       16 
     | 
    
         
            -
             
     | 
| 
       17 
     | 
    
         
            -
             
     | 
| 
       18 
     | 
    
         
            -
             
     | 
| 
      
 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
         
     | 
| 
       19 
37 
     | 
    
         
             
                end
         
     | 
| 
       20 
38 
     | 
    
         
             
              end
         
     | 
| 
       21 
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
         
     | 
| 
      
 53 
     | 
    
         
            +
                end
         
     | 
| 
      
 54 
     | 
    
         
            +
              end
         
     | 
| 
      
 55 
     | 
    
         
            +
              
         
     | 
| 
      
 56 
     | 
    
         
            +
              Foreigner.load_adapter!  
         
     | 
| 
      
 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 
     | 
    
         
            -
               
     | 
| 
      
 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:  
     | 
| 
      
 17 
     | 
    
         
            +
            date: 2010-04-02 00:00:00 -07:00
         
     | 
| 
       13 
18 
     | 
    
         
             
            default_executable: 
         
     | 
| 
       14 
19 
     | 
    
         
             
            dependencies: []
         
     | 
| 
       15 
20 
     | 
    
         | 
| 
       16 
     | 
    
         
            -
            description:  
     | 
| 
      
 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 
     | 
| 
      
 43 
     | 
    
         
            +
            homepage: http://github.com/matthuhiggins/foreigner
         
     | 
| 
       39 
44 
     | 
    
         
             
            licenses: []
         
     | 
| 
       40 
45 
     | 
    
         | 
| 
       41 
46 
     | 
    
         
             
            post_install_message: 
         
     | 
| 
       42 
     | 
    
         
            -
            rdoc_options: 
         
     | 
| 
       43 
     | 
    
         
            -
             
     | 
| 
       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 
     | 
    
         
            -
                   
     | 
| 
       53 
     | 
    
         
            -
             
     | 
| 
      
 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 
     | 
    
         
            -
                   
     | 
| 
       59 
     | 
    
         
            -
             
     | 
| 
      
 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. 
     | 
| 
      
 71 
     | 
    
         
            +
            rubyforge_project: matthuhiggins-foreigner
         
     | 
| 
      
 72 
     | 
    
         
            +
            rubygems_version: 1.3.6
         
     | 
| 
       64 
73 
     | 
    
         
             
            signing_key: 
         
     | 
| 
       65 
     | 
    
         
            -
            specification_version:  
     | 
| 
       66 
     | 
    
         
            -
            summary: Foreign keys for Rails 
     | 
| 
      
 74 
     | 
    
         
            +
            specification_version: 3
         
     | 
| 
      
 75 
     | 
    
         
            +
            summary: Foreign keys for Rails
         
     | 
| 
       67 
76 
     | 
    
         
             
            test_files: []
         
     | 
| 
       68 
77 
     | 
    
         |