composite_primary_keys 10.0.3 → 10.0.4

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.
Files changed (69) hide show
  1. checksums.yaml +4 -4
  2. data/History.rdoc +6 -0
  3. data/lib/composite_primary_keys.rb +0 -2
  4. data/lib/composite_primary_keys/associations/preloader/association.rb +2 -0
  5. data/lib/composite_primary_keys/attribute_methods/primary_key.rb +1 -1
  6. data/lib/composite_primary_keys/connection_adapters/abstract_adapter.rb +10 -10
  7. data/lib/composite_primary_keys/locking/optimistic.rb +21 -31
  8. data/lib/composite_primary_keys/relation.rb +14 -3
  9. data/lib/composite_primary_keys/version.rb +1 -1
  10. data/scripts/console.rb +48 -48
  11. data/scripts/txt2html +76 -76
  12. data/scripts/txt2js +65 -65
  13. data/tasks/website.rake +18 -18
  14. data/test/README_tests.rdoc +56 -56
  15. data/test/connections/databases.yml +40 -30
  16. data/test/db_test.rb +52 -52
  17. data/test/fixtures/articles.yml +6 -6
  18. data/test/fixtures/capitol.rb +3 -3
  19. data/test/fixtures/capitols.yml +16 -16
  20. data/test/fixtures/comments.yml +15 -15
  21. data/test/fixtures/department.rb +5 -5
  22. data/test/fixtures/departments.yml +15 -15
  23. data/test/fixtures/dorms.yml +4 -4
  24. data/test/fixtures/group.rb +2 -2
  25. data/test/fixtures/groups.yml +6 -6
  26. data/test/fixtures/hack.rb +4 -4
  27. data/test/fixtures/hacks.yml +2 -2
  28. data/test/fixtures/membership_status.rb +2 -2
  29. data/test/fixtures/product.rb +9 -9
  30. data/test/fixtures/product_tariff.rb +5 -5
  31. data/test/fixtures/products.yml +11 -11
  32. data/test/fixtures/reading.rb +4 -4
  33. data/test/fixtures/readings.yml +10 -10
  34. data/test/fixtures/reference_code_using_composite_key_alias.rb +8 -8
  35. data/test/fixtures/reference_code_using_simple_key_alias.rb +8 -8
  36. data/test/fixtures/reference_codes.yml +28 -28
  37. data/test/fixtures/reference_types.yml +9 -9
  38. data/test/fixtures/restaurant.rb +9 -9
  39. data/test/fixtures/restaurants.yml +14 -14
  40. data/test/fixtures/restaurants_suburbs.yml +10 -10
  41. data/test/fixtures/room.rb +11 -11
  42. data/test/fixtures/room_assignment.rb +13 -13
  43. data/test/fixtures/room_assignments.yml +24 -24
  44. data/test/fixtures/room_attribute.rb +2 -2
  45. data/test/fixtures/room_attribute_assignment.rb +4 -4
  46. data/test/fixtures/room_attribute_assignments.yml +4 -4
  47. data/test/fixtures/room_attributes.yml +2 -2
  48. data/test/fixtures/rooms.yml +12 -12
  49. data/test/fixtures/seat.rb +5 -5
  50. data/test/fixtures/seats.yml +8 -8
  51. data/test/fixtures/street.rb +2 -2
  52. data/test/fixtures/streets.yml +16 -16
  53. data/test/fixtures/student.rb +3 -3
  54. data/test/fixtures/students.yml +15 -15
  55. data/test/fixtures/suburbs.yml +14 -14
  56. data/test/plugins/pagination.rb +405 -405
  57. data/test/plugins/pagination_helper.rb +135 -135
  58. data/test/setup.rb +50 -50
  59. data/test/test_aliases.rb +18 -18
  60. data/test/test_composite_arrays.rb +24 -24
  61. data/test/test_counter_cache.rb +30 -30
  62. data/test/test_dup.rb +37 -37
  63. data/test/test_exists.rb +39 -39
  64. data/test/test_miscellaneous.rb +32 -32
  65. data/test/test_pagination.rb +35 -35
  66. data/test/test_preload.rb +7 -0
  67. data/test/test_validations.rb +13 -13
  68. metadata +5 -6
  69. data/lib/composite_primary_keys/attribute_set/builder.rb +0 -22
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 29b9cbec2b60475dd15921781f0cd0d13bcc926a7e30d5044b6eec3785c58d33
4
- data.tar.gz: 571a02475e5baaa6a09893c98de1d370040579ce0c57dd3d9a1df098cd77f436
3
+ metadata.gz: 89c1bd9183cbf26dfed31a862493f5760bd3e1892f3c8b39bed62b2605d4b241
4
+ data.tar.gz: 0c2e627e3e9b7b9fc27410967f400496c2fc5cc90c81c20528374ee4024033ea
5
5
  SHA512:
6
- metadata.gz: 114b0f20ab6ec000a62c4186927b72f5600afad9e6ff79a9d7e7d60d3bf07b38a20bca74fd4d2b81e892e252692f5c892ee860affc134cfda76bae87614073da
7
- data.tar.gz: 26e7a765dbbff53d865073b83dfc1a876219aa6067b33216134d7f8d90dd0451a0c5534783e89f443555abb4c76bc4596c26de3e20db537de6d6fc91c06901fa
6
+ metadata.gz: d1c56f5c4ce9c03c604d3d8b3df31ea4c8d652cd310d15af11ed3cd6d72c9b44c9fcb12065ba0ba225ddd918fa9655d75bb1e5fabf5b4923de2a0c8535ecca3e
7
+ data.tar.gz: ad9c9d9a3b5df644fa5accc5b2cb9db0465b9254a26912f4cc66ba76a0e44666df1c47cc00a87b891fbdfce1f8bbc516ba05043f3d59fb1e51f82a37e527ef21
data/History.rdoc CHANGED
@@ -1,3 +1,9 @@
1
+ == 10.0.4 (2018-06-17)
2
+
3
+ * Lock mysql2 gem to 0.4.x (Le Trung Kien)
4
+ * Set inversion when using preload (Le Trung Kien)
5
+ * Fixes for AR 5.1.6 (Tim Morgan)
6
+
1
7
  == 10.0.3 (2018-02-25)
2
8
 
3
9
  * Require a minimum of activerecord version 5.1.5 (Tom Hughes)
@@ -51,7 +51,6 @@ require 'active_record/associations/preloader/belongs_to'
51
51
  require 'active_record/associations/singular_association'
52
52
  require 'active_record/associations/collection_association'
53
53
 
54
- require 'active_record/attribute_set/builder'
55
54
  require 'active_record/attribute_methods/primary_key'
56
55
  require 'active_record/attribute_methods/read'
57
56
  require 'active_record/attribute_methods/write'
@@ -77,7 +76,6 @@ require 'composite_primary_keys/composite_predicates'
77
76
  require 'composite_primary_keys/fixtures'
78
77
  require 'composite_primary_keys/relation'
79
78
  require 'composite_primary_keys/sanitization'
80
- require 'composite_primary_keys/attribute_set/builder'
81
79
  require 'composite_primary_keys/attribute_methods'
82
80
  require 'composite_primary_keys/autosave_association'
83
81
  require 'composite_primary_keys/version'
@@ -43,6 +43,8 @@ module ActiveRecord
43
43
  records.each do |record, owner_key|
44
44
  owners_map[owner_key].each do |owner|
45
45
  records_by_owner[owner] << record
46
+ association = owner.association(reflection.name)
47
+ association.set_inverse_instance(record)
46
48
  end
47
49
  end
48
50
  end
@@ -9,7 +9,7 @@ module ActiveRecord
9
9
  # attribute_was(self.class.primary_key)
10
10
  if self.composite?
11
11
  self.class.primary_keys.map do |key_attr|
12
- attribute_changed?(key_attr) ? changed_attributes[key_attr] : self.ids_hash[key_attr]
12
+ attribute_was(key_attr)
13
13
  end
14
14
  else
15
15
  attribute_was(self.class.primary_key)
@@ -1,11 +1,11 @@
1
- module ActiveRecord
2
- module ConnectionAdapters
3
- class AbstractAdapter
4
- def quote_column_names(name)
5
- Array(name).map do |col|
6
- quote_column_name(col.to_s)
7
- end.join(CompositePrimaryKeys::ID_SEP)
8
- end
9
- end
10
- end
1
+ module ActiveRecord
2
+ module ConnectionAdapters
3
+ class AbstractAdapter
4
+ def quote_column_names(name)
5
+ Array(name).map do |col|
6
+ quote_column_name(col.to_s)
7
+ end.join(CompositePrimaryKeys::ID_SEP)
8
+ end
9
+ end
10
+ end
11
11
  end
@@ -5,50 +5,40 @@ module ActiveRecord
5
5
  private
6
6
 
7
7
  silence_warnings do
8
- def _update_record(attribute_names = @attributes.keys) #:nodoc:
8
+ def _update_row(attribute_names, attempted_action = "update")
9
9
  return super unless locking_enabled?
10
- return 0 if attribute_names.empty?
11
-
12
- lock_col = self.class.locking_column
13
- previous_lock_value = send(lock_col).to_i
14
- increment_lock
15
-
16
- attribute_names += [lock_col]
17
- attribute_names.uniq!
18
10
 
19
11
  begin
20
- relation = self.class.unscoped
21
-
22
- if self.composite?
23
- affected_rows = relation.where(
24
- relation.cpk_id_predicate(relation.table, self.class.primary_key, id_was)
25
- ).where(
26
- lock_col => previous_lock_value
27
- ).update_all(
28
- Hash[attributes_for_update(attribute_names).map do |name|
29
- [name, _read_attribute(name)]
30
- end]
12
+ locking_column = self.class.locking_column
13
+ previous_lock_value = read_attribute_before_type_cast(locking_column)
14
+ attribute_names << locking_column
15
+
16
+ self[locking_column] += 1
17
+
18
+ if composite?
19
+ affected_rows = self.class.unscoped._update_record(
20
+ arel_attributes_with_values(attribute_names),
21
+ Hash[self.class.primary_key.zip(id_in_database)].merge(
22
+ locking_column => previous_lock_value
23
+ )
31
24
  )
32
25
  else
33
- affected_rows = relation.where(
34
- self.class.primary_key => id,
35
- lock_col => previous_lock_value,
36
- ).update_all(
37
- Hash[attributes_for_update(attribute_names).map do |name|
38
- [name, _read_attribute(name)]
39
- end]
26
+ affected_rows = self.class.unscoped._update_record(
27
+ arel_attributes_with_values(attribute_names),
28
+ self.class.primary_key => id_in_database,
29
+ locking_column => previous_lock_value
40
30
  )
41
31
  end
42
32
 
43
- unless affected_rows == 1
44
- raise ActiveRecord::StaleObjectError.new(self, "update")
33
+ if affected_rows != 1
34
+ raise ActiveRecord::StaleObjectError.new(self, attempted_action)
45
35
  end
46
36
 
47
37
  affected_rows
48
38
 
49
- # If something went wrong, revert the version.
39
+ # If something went wrong, revert the locking_column value.
50
40
  rescue Exception
51
- send(lock_col + '=', previous_lock_value)
41
+ self[locking_column] = previous_lock_value.to_i
52
42
  raise
53
43
  end
54
44
  end
@@ -17,7 +17,7 @@ module ActiveRecord
17
17
  end
18
18
 
19
19
  silence_warnings do
20
- def _update_record(values, id, id_was) # :nodoc:
20
+ def _update_record(values, constraints) # :nodoc:
21
21
  substitutes, binds = substitute_values values
22
22
 
23
23
  scope = @klass.unscoped
@@ -27,10 +27,21 @@ module ActiveRecord
27
27
  end
28
28
 
29
29
  # CPK
30
+ # relation = scope.where(constraints)
30
31
  if self.composite?
31
- relation = @klass.unscoped.where(cpk_id_predicate(@klass.arel_table, @klass.primary_key, id_was || id))
32
+ if constraints[@klass.primary_key]
33
+ cpks = @klass.primary_key.zip(constraints[@klass.primary_key]).to_h
34
+ others = constraints.except(@klass.primary_key)
35
+ else
36
+ cpks = Hash.new
37
+ others = Hash.new
38
+ constraints.each do |field, value|
39
+ (@klass.primary_key.include?(field) ? cpks : others)[field] = value
40
+ end
41
+ end
42
+ relation = @klass.unscoped.where(cpk_id_predicate(@klass.arel_table, cpks.keys, cpks.values)).where(others)
32
43
  else
33
- relation = scope.where(@klass.primary_key => (id_was || id))
44
+ relation = scope.where(constraints)
34
45
  end
35
46
 
36
47
 
@@ -2,7 +2,7 @@ module CompositePrimaryKeys
2
2
  module VERSION
3
3
  MAJOR = 10
4
4
  MINOR = 0
5
- TINY = 3
5
+ TINY = 4
6
6
  STRING = [MAJOR, MINOR, TINY].join('.')
7
7
  end
8
8
  end
data/scripts/console.rb CHANGED
@@ -1,48 +1,48 @@
1
- #!/usr/bin/env ruby
2
-
3
- #
4
- # if run as script, load the file as library while starting irb
5
- #
6
- if __FILE__ == $0
7
- irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
8
- ENV['ADAPTER'] = ARGV[0]
9
- exec "#{irb} -f -r #{$0} --simple-prompt"
10
- end
11
-
12
- #
13
- # check if the given adapter is supported (default: mysql)
14
- #
15
- adapters = %w[mysql sqlite oracle oracle_enhanced postgresql ibm_db]
16
- adapter = ENV['ADAPTER'] || 'mysql'
17
- unless adapters.include? adapter
18
- puts "Usage: #{__FILE__} <adapter>"
19
- puts ''
20
- puts 'Adapters: '
21
- puts adapters.map{ |adapter| " #{adapter}" }.join("\n")
22
- exit 1
23
- end
24
-
25
- #
26
- # load all necessary libraries
27
- #
28
- require 'rubygems'
29
- require 'local/database_connections'
30
-
31
- $LOAD_PATH.unshift 'lib'
32
-
33
- begin
34
- require 'local/paths'
35
- $LOAD_PATH.unshift "#{ENV['EDGE_RAILS_DIR']}/active_record/lib" if ENV['EDGE_RAILS_DIR']
36
- $LOAD_PATH.unshift "#{ENV['EDGE_RAILS_DIR']}/activesupport/lib" if ENV['EDGE_RAILS_DIR']
37
- rescue
38
- end
39
-
40
- require 'active_support'
41
- require 'active_record'
42
-
43
- require "test/connections/native_#{adapter}/connection"
44
- require 'composite_primary_keys'
45
-
46
- PROJECT_ROOT = File.join(File.dirname(__FILE__), '..')
47
- Dir[File.join(PROJECT_ROOT,'test/fixtures/*.rb')].each { |model| require model }
48
-
1
+ #!/usr/bin/env ruby
2
+
3
+ #
4
+ # if run as script, load the file as library while starting irb
5
+ #
6
+ if __FILE__ == $0
7
+ irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
8
+ ENV['ADAPTER'] = ARGV[0]
9
+ exec "#{irb} -f -r #{$0} --simple-prompt"
10
+ end
11
+
12
+ #
13
+ # check if the given adapter is supported (default: mysql)
14
+ #
15
+ adapters = %w[mysql sqlite oracle oracle_enhanced postgresql ibm_db]
16
+ adapter = ENV['ADAPTER'] || 'mysql'
17
+ unless adapters.include? adapter
18
+ puts "Usage: #{__FILE__} <adapter>"
19
+ puts ''
20
+ puts 'Adapters: '
21
+ puts adapters.map{ |adapter| " #{adapter}" }.join("\n")
22
+ exit 1
23
+ end
24
+
25
+ #
26
+ # load all necessary libraries
27
+ #
28
+ require 'rubygems'
29
+ require 'local/database_connections'
30
+
31
+ $LOAD_PATH.unshift 'lib'
32
+
33
+ begin
34
+ require 'local/paths'
35
+ $LOAD_PATH.unshift "#{ENV['EDGE_RAILS_DIR']}/active_record/lib" if ENV['EDGE_RAILS_DIR']
36
+ $LOAD_PATH.unshift "#{ENV['EDGE_RAILS_DIR']}/activesupport/lib" if ENV['EDGE_RAILS_DIR']
37
+ rescue
38
+ end
39
+
40
+ require 'active_support'
41
+ require 'active_record'
42
+
43
+ require "test/connections/native_#{adapter}/connection"
44
+ require 'composite_primary_keys'
45
+
46
+ PROJECT_ROOT = File.join(File.dirname(__FILE__), '..')
47
+ Dir[File.join(PROJECT_ROOT,'test/fixtures/*.rb')].each { |model| require model }
48
+
data/scripts/txt2html CHANGED
@@ -1,76 +1,76 @@
1
- #!/usr/bin/env ruby
2
-
3
- require 'rubygems'
4
- gem 'RedCloth'
5
- gem 'syntax'
6
-
7
- require 'redcloth'
8
- require 'syntax/convertors/html'
9
- require 'erb'
10
-
11
- version_path = File.join(File.dirname(__FILE__), '..', 'lib', 'composite_primary_keys', 'version.rb')
12
- require File.expand_path(version_path)
13
-
14
- version = CompositePrimaryKeys::VERSION::STRING
15
- download = 'http://rubygems.org/gems/composite_primary_keys'
16
-
17
- class Fixnum
18
- def ordinal
19
- # teens
20
- return 'th' if (10..19).include?(self % 100)
21
- # others
22
- case self % 10
23
- when 1
24
- return 'st'
25
- when 2
26
- return 'nd'
27
- when 3
28
- return 'rd'
29
- else
30
- return 'th'
31
- end
32
- end
33
- end
34
-
35
- class Time
36
- def pretty
37
- return "#{mday}#{mday.ordinal} #{strftime('%B')} #{year}"
38
- end
39
- end
40
-
41
- def convert_syntax(syntax, source)
42
- return Syntax::Convertors::HTML.for_syntax(syntax).convert(source).gsub(%r!^<pre>|</pre>$!,'')
43
- end
44
-
45
- if ARGV.length >= 1
46
- src, template = ARGV
47
- template ||= File.dirname(__FILE__) + '/../website/template.rhtml'
48
-
49
- else
50
- puts("Usage: #{File.split($0).last} source.txt [template.rhtml] > output.html")
51
- exit!
52
- end
53
-
54
- template = ERB.new(File.open(template).read)
55
-
56
- title = nil
57
- body = nil
58
- File.open(src) do |fsrc|
59
- title_text = fsrc.readline
60
- body_text = fsrc.read
61
- syntax_items = []
62
- body_text.gsub!(%r!<(pre|code)[^>]*?syntax=['"]([^'"]+)[^>]*>(.*?)</\1>!m){
63
- ident = syntax_items.length
64
- element, syntax, source = $1, $2, $3
65
- syntax_items << "<#{element} class=\"syntax\">#{convert_syntax(syntax, source)}</#{element}>"
66
- "syntax-temp-#{ident}"
67
- }
68
- title = RedCloth.new(title_text).to_html.gsub(%r!<.*?>!,'').strip
69
- body = RedCloth.new(body_text).to_html
70
- body.gsub!(%r!(?:<pre><code>)?syntax-temp-(\d+)(?:</code></pre>)?!){ syntax_items[$1.to_i] }
71
- end
72
- stat = File.stat(src)
73
- created = stat.ctime
74
- modified = stat.mtime
75
-
76
- $stdout << template.result(binding)
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ gem 'RedCloth'
5
+ gem 'syntax'
6
+
7
+ require 'redcloth'
8
+ require 'syntax/convertors/html'
9
+ require 'erb'
10
+
11
+ version_path = File.join(File.dirname(__FILE__), '..', 'lib', 'composite_primary_keys', 'version.rb')
12
+ require File.expand_path(version_path)
13
+
14
+ version = CompositePrimaryKeys::VERSION::STRING
15
+ download = 'http://rubygems.org/gems/composite_primary_keys'
16
+
17
+ class Fixnum
18
+ def ordinal
19
+ # teens
20
+ return 'th' if (10..19).include?(self % 100)
21
+ # others
22
+ case self % 10
23
+ when 1
24
+ return 'st'
25
+ when 2
26
+ return 'nd'
27
+ when 3
28
+ return 'rd'
29
+ else
30
+ return 'th'
31
+ end
32
+ end
33
+ end
34
+
35
+ class Time
36
+ def pretty
37
+ return "#{mday}#{mday.ordinal} #{strftime('%B')} #{year}"
38
+ end
39
+ end
40
+
41
+ def convert_syntax(syntax, source)
42
+ return Syntax::Convertors::HTML.for_syntax(syntax).convert(source).gsub(%r!^<pre>|</pre>$!,'')
43
+ end
44
+
45
+ if ARGV.length >= 1
46
+ src, template = ARGV
47
+ template ||= File.dirname(__FILE__) + '/../website/template.rhtml'
48
+
49
+ else
50
+ puts("Usage: #{File.split($0).last} source.txt [template.rhtml] > output.html")
51
+ exit!
52
+ end
53
+
54
+ template = ERB.new(File.open(template).read)
55
+
56
+ title = nil
57
+ body = nil
58
+ File.open(src) do |fsrc|
59
+ title_text = fsrc.readline
60
+ body_text = fsrc.read
61
+ syntax_items = []
62
+ body_text.gsub!(%r!<(pre|code)[^>]*?syntax=['"]([^'"]+)[^>]*>(.*?)</\1>!m){
63
+ ident = syntax_items.length
64
+ element, syntax, source = $1, $2, $3
65
+ syntax_items << "<#{element} class=\"syntax\">#{convert_syntax(syntax, source)}</#{element}>"
66
+ "syntax-temp-#{ident}"
67
+ }
68
+ title = RedCloth.new(title_text).to_html.gsub(%r!<.*?>!,'').strip
69
+ body = RedCloth.new(body_text).to_html
70
+ body.gsub!(%r!(?:<pre><code>)?syntax-temp-(\d+)(?:</code></pre>)?!){ syntax_items[$1.to_i] }
71
+ end
72
+ stat = File.stat(src)
73
+ created = stat.ctime
74
+ modified = stat.mtime
75
+
76
+ $stdout << template.result(binding)