composite_primary_keys 8.1.4 → 8.1.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8ca795714eef0beaa1a0e91a07b09b1735b12242
4
- data.tar.gz: 41e87e3f7ae758e975809e56d8c7a3396d604f4c
3
+ metadata.gz: abff4d9e394b49dc7b5202df306d247ac92cbcd4
4
+ data.tar.gz: f0faa4bff366aba7dd02ce375eb1ba69b0b33cff
5
5
  SHA512:
6
- metadata.gz: 1f142511ba73692d9599bafdf78b26e61405a76b34b4de455f313a248fec5b6a75240f56eb9f6ef87ec299edd9db3ac5fc2b7849e51ed84b1efb16e852fa62c5
7
- data.tar.gz: ebf3f4281ee1c7c372a8b035a2e7a2a8651f7972a673c38555de38449b05d781696971eb7f507f912322c4527a28664216d4f8c69cfaafb5d11f09a71c8aea20
6
+ metadata.gz: 25822213d2b20007dfa81137f7504e6df9b86b75e41592bf9adfb75ef903c234d3d5dcda2a4df8a82a0579701b1db8bea0a4320b9db62ce3aae2c96999236d48
7
+ data.tar.gz: 9734252590c4f158064e6c8f8ef1e695892f97084731639bd5875c9b0f67c18bc51070b3a7cc458b1477a53b85ebcdc915f561735b3f780d147fd9d77affd0e5
data/History.rdoc CHANGED
@@ -1,3 +1,7 @@
1
+ == 8.1.5 (2017-01-01)
2
+
3
+ * Don't nest PK twice when looking up id, fixes #319 (Kerey Roper)
4
+
1
5
  == 8.1.4 (2016-07-27)
2
6
 
3
7
  * Create OR predicates in a nicely balanced tree fixing #320 (Nathan Samson)
data/Rakefile CHANGED
@@ -1,34 +1,34 @@
1
- require 'rubygems'
2
- require 'rake'
3
- require 'rake/clean'
4
- require 'rake/testtask'
5
- require 'rubygems/package_task'
6
-
7
- # Set global variable so other tasks can access them
8
- ::PROJECT_ROOT = File.expand_path(".")
9
- ::GEM_NAME = 'composite_primary_keys'
10
-
11
- # Read the spec file
12
- spec = Gem::Specification.load("#{GEM_NAME}.gemspec")
13
-
14
- # Setup Rake tasks for managing the gem
15
- Gem::PackageTask.new(spec).define
16
-
17
- # Now load in other task files
18
- Dir.glob('tasks/**/*.rake').each do |rake_file|
19
- load File.join(File.dirname(__FILE__), rake_file)
20
- end
21
-
22
- # Set up test tasks for each supported connection adapter
23
- %w(mysql sqlite3 oracle oracle_enhanced postgresql ibm_db sqlserver).each do |adapter|
24
- namespace adapter do
25
- desc "Run tests using the #{adapter} adapter"
26
- task "test" do
27
- ENV["ADAPTER"] = adapter
28
- Rake::TestTask.new("subtest_#{adapter}") do |t|
29
- t.libs << "test"
30
- end
31
- Rake::Task["subtest_#{adapter}"].invoke
32
- end
33
- end
34
- end
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rake/clean'
4
+ require 'rake/testtask'
5
+ require 'rubygems/package_task'
6
+
7
+ # Set global variable so other tasks can access them
8
+ ::PROJECT_ROOT = File.expand_path(".")
9
+ ::GEM_NAME = 'composite_primary_keys'
10
+
11
+ # Read the spec file
12
+ spec = Gem::Specification.load("#{GEM_NAME}.gemspec")
13
+
14
+ # Setup Rake tasks for managing the gem
15
+ Gem::PackageTask.new(spec).define
16
+
17
+ # Now load in other task files
18
+ Dir.glob('tasks/**/*.rake').each do |rake_file|
19
+ load File.join(File.dirname(__FILE__), rake_file)
20
+ end
21
+
22
+ # Set up test tasks for each supported connection adapter
23
+ %w(mysql sqlite3 oracle oracle_enhanced postgresql ibm_db sqlserver).each do |adapter|
24
+ namespace adapter do
25
+ desc "Run tests using the #{adapter} adapter"
26
+ task "test" do
27
+ ENV["ADAPTER"] = adapter
28
+ Rake::TestTask.new("subtest_#{adapter}") do |t|
29
+ t.libs << "test"
30
+ end
31
+ Rake::Task["subtest_#{adapter}"].invoke
32
+ end
33
+ end
34
+ end
@@ -7,7 +7,8 @@ module ActiveRecord
7
7
  _read_attribute(attr_name, &block)
8
8
  else
9
9
  name = attr_name.to_s
10
- name = self.class.primary_key if name == 'id'.freeze
10
+ # CPK
11
+ name = self.class.primary_key if name == 'id'.freeze && !composite?
11
12
  _read_attribute(name, &block)
12
13
  end
13
14
  end
@@ -1,19 +1,19 @@
1
- module ActiveModel
2
- module Dirty
3
- def can_change_primary_key?
4
- true
5
- end
6
-
7
- def primary_key_changed?
8
- !!changed.detect { |key| ids_hash.keys.include?(key.to_s) }
9
- end
10
-
11
- def primary_key_was
12
- ids_hash.keys.inject(Hash.new) do |result, attribute_name|
13
- result[attribute_name] = attribute_was(attribute_name.to_s)
14
- result
15
- end
16
- end
17
- alias_method :ids_hash_was, :primary_key_was
18
- end
19
- end
1
+ module ActiveModel
2
+ module Dirty
3
+ def can_change_primary_key?
4
+ true
5
+ end
6
+
7
+ def primary_key_changed?
8
+ !!changed.detect { |key| ids_hash.keys.include?(key.to_s) }
9
+ end
10
+
11
+ def primary_key_was
12
+ ids_hash.keys.inject(Hash.new) do |result, attribute_name|
13
+ result[attribute_name] = attribute_was(attribute_name.to_s)
14
+ result
15
+ end
16
+ end
17
+ alias_method :ids_hash_was, :primary_key_was
18
+ end
19
+ end
@@ -2,7 +2,7 @@ module CompositePrimaryKeys
2
2
  module VERSION
3
3
  MAJOR = 8
4
4
  MINOR = 1
5
- TINY = 4
5
+ TINY = 5
6
6
  STRING = [MAJOR, MINOR, TINY].join('.')
7
7
  end
8
8
  end
@@ -1,42 +1,42 @@
1
- require File.join(PROJECT_ROOT, 'lib', 'composite_primary_keys')
2
- require File.join(PROJECT_ROOT, 'test', 'connections', 'connection_spec')
3
-
4
- namespace :mysql do
5
- desc 'Create the MySQL test database'
6
- task :create_database do
7
- ActiveRecord::Base.clear_all_connections!
8
- spec = CompositePrimaryKeys::ConnectionSpec['mysql'].dup
9
- database_name = spec.delete('database')
10
- connection = ActiveRecord::Base.establish_connection(spec)
11
- ActiveRecord::Base.connection.create_database(database_name)
12
- ActiveRecord::Base.clear_all_connections!
13
- end
14
-
15
- desc 'Build the MySQL test database'
16
- task :build_database => [:create_database] do
17
- path = File.join(PROJECT_ROOT, 'test', 'fixtures', 'db_definitions', 'mysql.sql')
18
- sql = File.open(path, 'rb') do |file|
19
- file.read
20
- end
21
-
22
- Rake::Task['mysql:load_connection'].reenable
23
- Rake::Task['mysql:load_connection'].invoke
24
- #puts %(ActiveRecord::Base.connection.instance_variable_get(:@config)=#{(ActiveRecord::Base.connection.instance_variable_get(:@config)).inspect})
25
- sql.split(";").each do |statement|
26
- ActiveRecord::Base.connection.execute(statement) unless statement.strip.length == 0
27
- end
28
- end
29
-
30
- desc 'Drop the MySQL test database'
31
- task :drop_database => :load_connection do
32
- ActiveRecord::Base.connection.drop_database(SPEC['database'])
33
- end
34
-
35
- desc 'Rebuild the MySQL test database'
36
- task :rebuild_database => [:drop_database, :build_database]
37
-
38
- task :load_connection do
39
- require File.join(PROJECT_ROOT, "test", "connections", "native_mysql", "connection")
40
- establish_connection
41
- end
42
- end
1
+ require File.join(PROJECT_ROOT, 'lib', 'composite_primary_keys')
2
+ require File.join(PROJECT_ROOT, 'test', 'connections', 'connection_spec')
3
+
4
+ namespace :mysql do
5
+ desc 'Create the MySQL test database'
6
+ task :create_database do
7
+ ActiveRecord::Base.clear_all_connections!
8
+ spec = CompositePrimaryKeys::ConnectionSpec['mysql'].dup
9
+ database_name = spec.delete('database')
10
+ connection = ActiveRecord::Base.establish_connection(spec)
11
+ ActiveRecord::Base.connection.create_database(database_name)
12
+ ActiveRecord::Base.clear_all_connections!
13
+ end
14
+
15
+ desc 'Build the MySQL test database'
16
+ task :build_database => [:create_database] do
17
+ path = File.join(PROJECT_ROOT, 'test', 'fixtures', 'db_definitions', 'mysql.sql')
18
+ sql = File.open(path, 'rb') do |file|
19
+ file.read
20
+ end
21
+
22
+ Rake::Task['mysql:load_connection'].reenable
23
+ Rake::Task['mysql:load_connection'].invoke
24
+ #puts %(ActiveRecord::Base.connection.instance_variable_get(:@config)=#{(ActiveRecord::Base.connection.instance_variable_get(:@config)).inspect})
25
+ sql.split(";").each do |statement|
26
+ ActiveRecord::Base.connection.execute(statement) unless statement.strip.length == 0
27
+ end
28
+ end
29
+
30
+ desc 'Drop the MySQL test database'
31
+ task :drop_database => :load_connection do
32
+ ActiveRecord::Base.connection.drop_database(SPEC['database'])
33
+ end
34
+
35
+ desc 'Rebuild the MySQL test database'
36
+ task :rebuild_database => [:drop_database, :build_database]
37
+
38
+ task :load_connection do
39
+ require File.join(PROJECT_ROOT, "test", "connections", "native_mysql", "connection")
40
+ establish_connection
41
+ end
42
+ end
@@ -1,48 +1,48 @@
1
- require File.join(PROJECT_ROOT, 'lib', 'composite_primary_keys')
2
- require File.join(PROJECT_ROOT, 'test', 'connections', 'connection_spec')
3
-
4
- namespace :postgresql do
5
- desc 'Create the PostgreSQL test database'
6
- task :create_database do
7
- ActiveRecord::Base.clear_all_connections!
8
- spec = CompositePrimaryKeys::ConnectionSpec['postgresql'].dup
9
- database_name = spec.delete('database')
10
- spec['database'] = 'postgres'
11
- connection = ActiveRecord::Base.establish_connection(spec)
12
- ActiveRecord::Base.connection.create_database(database_name)
13
- ActiveRecord::Base.clear_all_connections!
14
- end
15
-
16
- desc 'Build the PostgreSQL test database'
17
- task :build_database => [:create_database] do
18
- ActiveRecord::Base.clear_all_connections!
19
- spec = CompositePrimaryKeys::ConnectionSpec['postgresql'].dup
20
- connection = ActiveRecord::Base.establish_connection(spec)
21
-
22
- path = File.join(PROJECT_ROOT, 'test', 'fixtures', 'db_definitions', 'postgresql.sql')
23
- sql = File.open(path, 'rb') do |file|
24
- file.read
25
- end
26
-
27
- Rake::Task['postgresql:load_connection'].invoke
28
- ActiveRecord::Base.connection.execute(sql)
29
- end
30
-
31
- desc 'Drop the PostgreSQL test database'
32
- task :drop_database => :load_connection do
33
- ActiveRecord::Base.clear_all_connections!
34
- spec = CompositePrimaryKeys::ConnectionSpec['postgresql'].dup
35
- database_name = spec.delete('database')
36
- spec['database'] = 'postgres'
37
- connection = ActiveRecord::Base.establish_connection(spec)
38
- ActiveRecord::Base.connection.drop_database(SPEC['database'])
39
- ActiveRecord::Base.clear_all_connections!
40
- end
41
-
42
- desc 'Rebuild the PostgreSQL test database'
43
- task :rebuild_database => [:drop_database, :build_database]
44
-
45
- task :load_connection do
46
- require File.join(PROJECT_ROOT, "test", "connections", "native_postgresql", "connection")
47
- end
1
+ require File.join(PROJECT_ROOT, 'lib', 'composite_primary_keys')
2
+ require File.join(PROJECT_ROOT, 'test', 'connections', 'connection_spec')
3
+
4
+ namespace :postgresql do
5
+ desc 'Create the PostgreSQL test database'
6
+ task :create_database do
7
+ ActiveRecord::Base.clear_all_connections!
8
+ spec = CompositePrimaryKeys::ConnectionSpec['postgresql'].dup
9
+ database_name = spec.delete('database')
10
+ spec['database'] = 'postgres'
11
+ connection = ActiveRecord::Base.establish_connection(spec)
12
+ ActiveRecord::Base.connection.create_database(database_name)
13
+ ActiveRecord::Base.clear_all_connections!
14
+ end
15
+
16
+ desc 'Build the PostgreSQL test database'
17
+ task :build_database => [:create_database] do
18
+ ActiveRecord::Base.clear_all_connections!
19
+ spec = CompositePrimaryKeys::ConnectionSpec['postgresql'].dup
20
+ connection = ActiveRecord::Base.establish_connection(spec)
21
+
22
+ path = File.join(PROJECT_ROOT, 'test', 'fixtures', 'db_definitions', 'postgresql.sql')
23
+ sql = File.open(path, 'rb') do |file|
24
+ file.read
25
+ end
26
+
27
+ Rake::Task['postgresql:load_connection'].invoke
28
+ ActiveRecord::Base.connection.execute(sql)
29
+ end
30
+
31
+ desc 'Drop the PostgreSQL test database'
32
+ task :drop_database => :load_connection do
33
+ ActiveRecord::Base.clear_all_connections!
34
+ spec = CompositePrimaryKeys::ConnectionSpec['postgresql'].dup
35
+ database_name = spec.delete('database')
36
+ spec['database'] = 'postgres'
37
+ connection = ActiveRecord::Base.establish_connection(spec)
38
+ ActiveRecord::Base.connection.drop_database(SPEC['database'])
39
+ ActiveRecord::Base.clear_all_connections!
40
+ end
41
+
42
+ desc 'Rebuild the PostgreSQL test database'
43
+ task :rebuild_database => [:drop_database, :build_database]
44
+
45
+ task :load_connection do
46
+ require File.join(PROJECT_ROOT, "test", "connections", "native_postgresql", "connection")
47
+ end
48
48
  end
@@ -1,27 +1,27 @@
1
- require File.join(PROJECT_ROOT, 'lib', 'composite_primary_keys')
2
- require File.join(PROJECT_ROOT, 'test', 'connections', 'connection_spec')
3
-
4
- namespace :sqlite3 do
5
- desc 'Build the sqlite test database'
6
- task :build_database => :load_connection do
7
- schema = File.join(PROJECT_ROOT, 'test', 'fixtures', 'db_definitions', 'sqlite.sql')
8
- dbfile = File.join(PROJECT_ROOT, connection_string)
9
- FileUtils.mkdir_p(File.dirname(dbfile))
10
- cmd = "sqlite3 #{dbfile} < #{schema}"
11
- puts cmd
12
- sh %{ #{cmd} }
13
- end
14
-
15
- desc 'Drop the sqlite test database'
16
- task :drop_database => :load_connection do
17
- dbfile = connection_string
18
- sh %{ rm -f #{dbfile} }
19
- end
20
-
21
- desc 'Rebuild the sqlite test database'
22
- task :rebuild_database => [:drop_database, :build_database]
23
-
24
- task :load_connection do
25
- require File.join(PROJECT_ROOT, "test", "connections", "native_sqlite3", "connection")
26
- end
27
- end
1
+ require File.join(PROJECT_ROOT, 'lib', 'composite_primary_keys')
2
+ require File.join(PROJECT_ROOT, 'test', 'connections', 'connection_spec')
3
+
4
+ namespace :sqlite3 do
5
+ desc 'Build the sqlite test database'
6
+ task :build_database => :load_connection do
7
+ schema = File.join(PROJECT_ROOT, 'test', 'fixtures', 'db_definitions', 'sqlite.sql')
8
+ dbfile = File.join(PROJECT_ROOT, connection_string)
9
+ FileUtils.mkdir_p(File.dirname(dbfile))
10
+ cmd = "sqlite3 #{dbfile} < #{schema}"
11
+ puts cmd
12
+ sh %{ #{cmd} }
13
+ end
14
+
15
+ desc 'Drop the sqlite test database'
16
+ task :drop_database => :load_connection do
17
+ dbfile = connection_string
18
+ sh %{ rm -f #{dbfile} }
19
+ end
20
+
21
+ desc 'Rebuild the sqlite test database'
22
+ task :rebuild_database => [:drop_database, :build_database]
23
+
24
+ task :load_connection do
25
+ require File.join(PROJECT_ROOT, "test", "connections", "native_sqlite3", "connection")
26
+ end
27
+ end
@@ -1,18 +1,18 @@
1
- require 'yaml'
2
-
3
- module CompositePrimaryKeys
4
- class ConnectionSpec
5
- def self.[](adapter)
6
- config[adapter.to_s]
7
- end
8
-
9
- private
10
-
11
- def self.config
12
- @config ||= begin
13
- path = File.join(PROJECT_ROOT, 'test', 'connections', 'databases.yml')
14
- YAML.load_file(path)
15
- end
16
- end
17
- end
18
- end
1
+ require 'yaml'
2
+
3
+ module CompositePrimaryKeys
4
+ class ConnectionSpec
5
+ def self.[](adapter)
6
+ config[adapter.to_s]
7
+ end
8
+
9
+ private
10
+
11
+ def self.config
12
+ @config ||= begin
13
+ path = File.join(PROJECT_ROOT, 'test', 'connections', 'databases.yml')
14
+ YAML.load_file(path)
15
+ end
16
+ end
17
+ end
18
+ end
@@ -1,9 +1,30 @@
1
- # To run tests:
2
- # 1. Copy this file to test/connections/databases.yml.
3
- # 2. Update to match the databases you want to test against.
4
- # 3. Build the database using rake <databasename>:build_database
1
+ mysql:
2
+ adapter: mysql2
3
+ username: root
4
+ password: mysql
5
+ database: composite_primary_keys_unittest
6
+
7
+ oracle:
8
+ adapter: oracle_enhanced
9
+ database: xe
10
+ username: SYSTEM
11
+ password: oracle
12
+ host: localhost
13
+
5
14
  postgresql:
6
15
  adapter: postgresql
7
16
  database: composite_primary_keys_unittest
8
17
  username: postgres
9
18
  host: localhost
19
+
20
+ sqlite:
21
+ adapter: sqlite3
22
+ database: <%= File.join(project_root, 'db', 'composite_primary_keys_unittest.sqlite') %>
23
+
24
+ sqlserver:
25
+ adapter: sqlserver
26
+ username: cpk
27
+ password: cpk
28
+ database: composite_primary_keys_unittest
29
+ host: localhost
30
+ port: 1433
@@ -1,19 +1,19 @@
1
- print "Using IBM2 \n"
2
-
3
- gem 'ibm_db'
4
- require 'IBM_DB'
5
-
6
- RAILS_CONNECTION_ADAPTERS = %w( mysql postgresql sqlite firebird sqlserver db2 oracle sybase openbase frontbase ibm_db )
7
-
8
- db1 = 'composite_primary_keys_unittest'
9
-
10
- connection_options = {
11
- :adapter => "ibm_db",
12
- :database => "ocdpdev",
13
- :username => "db2inst1",
14
- :password => "password",
15
- :host => '192.168.2.21'
16
- }
17
-
18
- ActiveRecord::Base.configurations = { db1 => connection_options }
1
+ print "Using IBM2 \n"
2
+
3
+ gem 'ibm_db'
4
+ require 'IBM_DB'
5
+
6
+ RAILS_CONNECTION_ADAPTERS = %w( mysql postgresql sqlite firebird sqlserver db2 oracle sybase openbase frontbase ibm_db )
7
+
8
+ db1 = 'composite_primary_keys_unittest'
9
+
10
+ connection_options = {
11
+ :adapter => "ibm_db",
12
+ :database => "ocdpdev",
13
+ :username => "db2inst1",
14
+ :password => "password",
15
+ :host => '192.168.2.21'
16
+ }
17
+
18
+ ActiveRecord::Base.configurations = { db1 => connection_options }
19
19
  ActiveRecord::Base.establish_connection(connection_options)
@@ -1,17 +1,17 @@
1
- print "Using native MySQL\n"
2
-
3
- def connection_string
4
- options = {}
5
- options['u'] = SPEC['username'] if SPEC['username']
6
- options['p'] = SPEC['password'] if SPEC['password']
7
- options['S'] = SPEC['sock'] if SPEC['sock']
8
- options.map { |key, value| "-#{key}#{value}" }.join(" ")
9
- end
10
-
11
- # Adapter config setup in test/connections/databases.yml
12
- SPEC = CompositePrimaryKeys::ConnectionSpec['mysql']
13
-
14
- def establish_connection
15
- ActiveRecord::Base.establish_connection(SPEC)
16
- end
17
- establish_connection
1
+ print "Using native MySQL\n"
2
+
3
+ def connection_string
4
+ options = {}
5
+ options['u'] = SPEC['username'] if SPEC['username']
6
+ options['p'] = SPEC['password'] if SPEC['password']
7
+ options['S'] = SPEC['sock'] if SPEC['sock']
8
+ options.map { |key, value| "-#{key}#{value}" }.join(" ")
9
+ end
10
+
11
+ # Adapter config setup in test/connections/databases.yml
12
+ SPEC = CompositePrimaryKeys::ConnectionSpec['mysql']
13
+
14
+ def establish_connection
15
+ ActiveRecord::Base.establish_connection(SPEC)
16
+ end
17
+ establish_connection
@@ -1,13 +1,13 @@
1
- print "Using native Postgresql\n"
2
-
3
- def connection_string
4
- options = Hash.new
5
- options['U'] = SPEC['username'] if SPEC['username']
6
- options['h'] = SPEC['host'] if SPEC['host']
7
- options['p'] = SPEC['port'] if SPEC['port']
8
- options.map { |key, value| "-#{key} #{value}" }.join(" ")
9
- end
10
-
11
- # Adapter config setup in text/connections/databases.yml
12
- SPEC = CompositePrimaryKeys::ConnectionSpec['postgresql']
1
+ print "Using native Postgresql\n"
2
+
3
+ def connection_string
4
+ options = Hash.new
5
+ options['U'] = SPEC['username'] if SPEC['username']
6
+ options['h'] = SPEC['host'] if SPEC['host']
7
+ options['p'] = SPEC['port'] if SPEC['port']
8
+ options.map { |key, value| "-#{key} #{value}" }.join(" ")
9
+ end
10
+
11
+ # Adapter config setup in text/connections/databases.yml
12
+ SPEC = CompositePrimaryKeys::ConnectionSpec['postgresql']
13
13
  ActiveRecord::Base.establish_connection(SPEC)
@@ -1,9 +1,9 @@
1
- print "Using native Sqlite3\n"
2
-
3
- def connection_string
4
- SPEC['database']
5
- end
6
-
7
- # Adapter config setup in text/connections/databases.yml
8
- SPEC = CompositePrimaryKeys::ConnectionSpec['sqlite3']
9
- ActiveRecord::Base.establish_connection(SPEC)
1
+ print "Using native Sqlite3\n"
2
+
3
+ def connection_string
4
+ SPEC['database']
5
+ end
6
+
7
+ # Adapter config setup in text/connections/databases.yml
8
+ SPEC = CompositePrimaryKeys::ConnectionSpec['sqlite3']
9
+ ActiveRecord::Base.establish_connection(SPEC)
@@ -1,5 +1,5 @@
1
- class Article < ActiveRecord::Base
2
- has_many :readings, :dependent => :delete_all
3
- has_many :users, :through => :readings
4
- end
5
-
1
+ class Article < ActiveRecord::Base
2
+ has_many :readings, :dependent => :delete_all
3
+ has_many :users, :through => :readings
4
+ end
5
+
@@ -218,3 +218,12 @@ create table employees_groups (
218
218
  employee_id int not null,
219
219
  group_id int not null
220
220
  );
221
+
222
+ create table pk_called_ids (
223
+ id serial not null,
224
+ reference_code int not null,
225
+ code_label varchar(50) default null,
226
+ abbreviation varchar(50) default null,
227
+ description varchar(50) default null,
228
+ primary key (id, reference_code)
229
+ );
@@ -0,0 +1,5 @@
1
+ class PkCalledId < ActiveRecord::Base
2
+ self.primary_keys = :id, :reference_code
3
+
4
+ validates_presence_of :reference_code, :code_label, :abbreviation
5
+ end
@@ -0,0 +1,11 @@
1
+ name_prefix_mr:
2
+ id: 1
3
+ reference_code: 1
4
+ code_label: MR
5
+ abbreviation: Mr
6
+
7
+ name_prefix_mrs:
8
+ id: 2
9
+ reference_code: 1
10
+ code_label: MRS
11
+ abbreviation: Mrs
data/test/test_ids.rb CHANGED
@@ -1,109 +1,113 @@
1
- require File.expand_path('../abstract_unit', __FILE__)
2
-
3
- class ChildCpkTest < ReferenceCode
4
- end
5
-
6
- class TestIds < ActiveSupport::TestCase
7
- fixtures :reference_types, :reference_codes
8
-
9
- CLASSES = {
10
- :single => {
11
- :class => ReferenceType,
12
- :primary_keys => [:reference_type_id],
13
- },
14
- :dual => {
15
- :class => ReferenceCode,
16
- :primary_keys => [:reference_type_id, :reference_code],
17
- },
18
- :dual_strs => {
19
- :class => ReferenceCode,
20
- :primary_keys => ['reference_type_id', 'reference_code'],
21
- },
22
- }
23
-
24
- def setup
25
- self.class.classes = CLASSES
26
- end
27
-
28
- def test_id
29
- testing_with do
30
- assert_equal @first.id, @first.ids if composite?
31
- assert_kind_of(CompositePrimaryKeys::CompositeKeys, @first.id) if composite?
32
- end
33
- end
34
-
35
- def test_to_param
36
- testing_with do
37
- assert_equal '1,1', @first.to_param if composite?
38
- end
39
- end
40
-
41
- def test_ids_to_s
42
- testing_with do
43
- order = @klass.primary_key.is_a?(String) ? @klass.primary_key : @klass.primary_key.join(',')
44
- to_test = @klass.order(order)[0..1].map(&:id)
45
- assert_equal '(1,1),(1,2)', @klass.ids_to_s(to_test) if @key_test == :dual
46
- assert_equal '1,1;1,2', @klass.ids_to_s(to_test, ',', ';', '', '') if @key_test == :dual
47
- end
48
- end
49
-
50
- def test_set_ids_string
51
- testing_with do
52
- array = @primary_keys.collect {|key| 5}
53
- expected = composite? ? array.to_composite_keys : array.first
54
- @first.id = expected.to_s
55
- assert_equal expected, @first.id
56
- end
57
- end
58
-
59
- def test_set_ids_array
60
- testing_with do
61
- array = @primary_keys.collect {|key| 5}
62
- expected = composite? ? array.to_composite_keys : array.first
63
- @first.id = expected
64
- assert_equal expected, @first.id
65
- end
66
- end
67
-
68
- def test_set_ids_comp
69
- testing_with do
70
- array = @primary_keys.collect {|key| 5}
71
- expected = composite? ? array.to_composite_keys : array.first
72
- @first.id = expected
73
- assert_equal expected, @first.id
74
- end
75
- end
76
-
77
- def test_primary_keys
78
- testing_with do
79
- if composite?
80
- assert_not_nil @klass.primary_keys
81
- assert_equal @primary_keys.map {|key| key.to_s}, @klass.primary_keys
82
- assert_equal @klass.primary_keys, @klass.primary_key
83
- assert_kind_of(CompositePrimaryKeys::CompositeKeys, @klass.primary_keys)
84
- assert_equal @primary_keys.map {|key| key.to_sym}.join(','), @klass.primary_key.to_s
85
- else
86
- assert_not_nil @klass.primary_key
87
- assert_equal @primary_keys.first, @klass.primary_key.to_sym
88
- assert_equal @primary_keys.first.to_s, @klass.primary_key.to_s
89
- end
90
- end
91
- end
92
-
93
- def test_inherited_primary_keys
94
- assert_equal(["reference_type_id", "reference_code"], ChildCpkTest.primary_keys)
95
- end
96
-
97
- def test_inherited_ids
98
- cpk_test = ChildCpkTest.new
99
- assert_equal([nil, nil], cpk_test.id)
100
- end
101
-
102
- def test_assign_ids
103
- ref_code = ReferenceCode.new
104
- assert_equal([nil, nil], ref_code.id)
105
-
106
- ref_code.id = [2,1]
107
- assert_equal([2,1], ref_code.id)
108
- end
109
- end
1
+ require File.expand_path('../abstract_unit', __FILE__)
2
+
3
+ class ChildCpkTest < ReferenceCode
4
+ end
5
+
6
+ class TestIds < ActiveSupport::TestCase
7
+ fixtures :reference_types, :reference_codes, :pk_called_ids
8
+
9
+ CLASSES = {
10
+ :single => {
11
+ :class => ReferenceType,
12
+ :primary_keys => [:reference_type_id],
13
+ },
14
+ :dual => {
15
+ :class => ReferenceCode,
16
+ :primary_keys => [:reference_type_id, :reference_code],
17
+ },
18
+ :dual_strs => {
19
+ :class => ReferenceCode,
20
+ :primary_keys => ['reference_type_id', 'reference_code'],
21
+ },
22
+ :pk_called_id => {
23
+ :class => PkCalledId,
24
+ :primary_keys => ['id', 'reference_code'],
25
+ },
26
+ }
27
+
28
+ def setup
29
+ self.class.classes = CLASSES
30
+ end
31
+
32
+ def test_id
33
+ testing_with do
34
+ assert_equal @first.id, @first.ids if composite?
35
+ assert_kind_of(CompositePrimaryKeys::CompositeKeys, @first.id) if composite?
36
+ end
37
+ end
38
+
39
+ def test_to_param
40
+ testing_with do
41
+ assert_equal '1,1', @first.to_param if composite?
42
+ end
43
+ end
44
+
45
+ def test_ids_to_s
46
+ testing_with do
47
+ order = @klass.primary_key.is_a?(String) ? @klass.primary_key : @klass.primary_key.join(',')
48
+ to_test = @klass.order(order)[0..1].map(&:id)
49
+ assert_equal '(1,1),(1,2)', @klass.ids_to_s(to_test) if @key_test == :dual
50
+ assert_equal '1,1;1,2', @klass.ids_to_s(to_test, ',', ';', '', '') if @key_test == :dual
51
+ end
52
+ end
53
+
54
+ def test_set_ids_string
55
+ testing_with do
56
+ array = @primary_keys.collect {|key| 5}
57
+ expected = composite? ? array.to_composite_keys : array.first
58
+ @first.id = expected.to_s
59
+ assert_equal expected, @first.id
60
+ end
61
+ end
62
+
63
+ def test_set_ids_array
64
+ testing_with do
65
+ array = @primary_keys.collect {|key| 5}
66
+ expected = composite? ? array.to_composite_keys : array.first
67
+ @first.id = expected
68
+ assert_equal expected, @first.id
69
+ end
70
+ end
71
+
72
+ def test_set_ids_comp
73
+ testing_with do
74
+ array = @primary_keys.collect {|key| 5}
75
+ expected = composite? ? array.to_composite_keys : array.first
76
+ @first.id = expected
77
+ assert_equal expected, @first.id
78
+ end
79
+ end
80
+
81
+ def test_primary_keys
82
+ testing_with do
83
+ if composite?
84
+ assert_not_nil @klass.primary_keys
85
+ assert_equal @primary_keys.map {|key| key.to_s}, @klass.primary_keys
86
+ assert_equal @klass.primary_keys, @klass.primary_key
87
+ assert_kind_of(CompositePrimaryKeys::CompositeKeys, @klass.primary_keys)
88
+ assert_equal @primary_keys.map {|key| key.to_sym}.join(','), @klass.primary_key.to_s
89
+ else
90
+ assert_not_nil @klass.primary_key
91
+ assert_equal @primary_keys.first, @klass.primary_key.to_sym
92
+ assert_equal @primary_keys.first.to_s, @klass.primary_key.to_s
93
+ end
94
+ end
95
+ end
96
+
97
+ def test_inherited_primary_keys
98
+ assert_equal(["reference_type_id", "reference_code"], ChildCpkTest.primary_keys)
99
+ end
100
+
101
+ def test_inherited_ids
102
+ cpk_test = ChildCpkTest.new
103
+ assert_equal([nil, nil], cpk_test.id)
104
+ end
105
+
106
+ def test_assign_ids
107
+ ref_code = ReferenceCode.new
108
+ assert_equal([nil, nil], ref_code.id)
109
+
110
+ ref_code.id = [2,1]
111
+ assert_equal([2,1], ref_code.id)
112
+ end
113
+ end
data/test/test_update.rb CHANGED
@@ -62,10 +62,12 @@ class TestUpdate < ActiveSupport::TestCase
62
62
 
63
63
  def test_update_all
64
64
  assert_nothing_raised do
65
- reference_code = ReferenceCode.create
65
+ reference_code = ReferenceCode.first
66
66
  primary_key = reference_code.class.primary_key
67
- ReferenceCode.where(primary_key => reference_code[primary_key]).
68
- update_all(description: 'random value')
67
+ ReferenceCode.where(primary_key => reference_code[primary_key]).update_all(description: 'random value')
68
+
69
+ reference_code.reload
70
+ assert_equal('random value', reference_code.description)
69
71
  end
70
72
  end
71
73
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: composite_primary_keys
3
3
  version: !ruby/object:Gem::Version
4
- version: 8.1.4
4
+ version: 8.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Charlie Savage
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-07-28 00:00:00.000000000 Z
11
+ date: 2017-01-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -172,6 +172,8 @@ files:
172
172
  - test/fixtures/membership_status.rb
173
173
  - test/fixtures/membership_statuses.yml
174
174
  - test/fixtures/memberships.yml
175
+ - test/fixtures/pk_called_id.rb
176
+ - test/fixtures/pk_called_ids.yml
175
177
  - test/fixtures/product.rb
176
178
  - test/fixtures/product_tariff.rb
177
179
  - test/fixtures/product_tariffs.yml
@@ -269,7 +271,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
269
271
  version: '0'
270
272
  requirements: []
271
273
  rubyforge_project:
272
- rubygems_version: 2.6.6
274
+ rubygems_version: 2.6.8
273
275
  signing_key:
274
276
  specification_version: 4
275
277
  summary: Composite key support for ActiveRecord