composite_primary_keys 9.0.10 → 10.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (85) hide show
  1. checksums.yaml +5 -5
  2. data/History.rdoc +4 -9
  3. data/README.rdoc +10 -9
  4. data/lib/composite_primary_keys.rb +6 -5
  5. data/lib/composite_primary_keys/arel/sqlserver.rb +2 -1
  6. data/lib/composite_primary_keys/associations/association_scope.rb +4 -4
  7. data/lib/composite_primary_keys/associations/collection_association.rb +1 -1
  8. data/lib/composite_primary_keys/associations/preloader/association.rb +2 -2
  9. data/lib/composite_primary_keys/associations/preloader/belongs_to.rb +2 -2
  10. data/lib/composite_primary_keys/attribute_methods/primary_key.rb +14 -1
  11. data/lib/composite_primary_keys/attribute_methods/write.rb +14 -0
  12. data/lib/composite_primary_keys/attribute_set/builder.rb +22 -0
  13. data/lib/composite_primary_keys/base.rb +6 -5
  14. data/lib/composite_primary_keys/composite_predicates.rb +4 -0
  15. data/lib/composite_primary_keys/connection_adapters/abstract_adapter.rb +10 -10
  16. data/lib/composite_primary_keys/connection_adapters/postgresql/database_statements.rb +26 -0
  17. data/lib/composite_primary_keys/core.rb +9 -12
  18. data/lib/composite_primary_keys/locking/optimistic.rb +7 -7
  19. data/lib/composite_primary_keys/nested_attributes.rb +1 -1
  20. data/lib/composite_primary_keys/relation.rb +1 -6
  21. data/lib/composite_primary_keys/relation/predicate_builder/association_query_handler.rb +33 -0
  22. data/lib/composite_primary_keys/version.rb +2 -2
  23. data/scripts/console.rb +48 -48
  24. data/scripts/txt2html +76 -76
  25. data/scripts/txt2js +65 -65
  26. data/tasks/website.rake +18 -18
  27. data/test/README_tests.rdoc +56 -56
  28. data/test/connections/databases.yml +30 -40
  29. data/test/db_test.rb +52 -52
  30. data/test/fixtures/articles.yml +6 -6
  31. data/test/fixtures/capitol.rb +3 -3
  32. data/test/fixtures/capitols.yml +16 -16
  33. data/test/fixtures/comments.yml +15 -15
  34. data/test/fixtures/department.rb +5 -5
  35. data/test/fixtures/departments.yml +15 -15
  36. data/test/fixtures/dorms.yml +4 -4
  37. data/test/fixtures/group.rb +2 -2
  38. data/test/fixtures/groups.yml +6 -6
  39. data/test/fixtures/hack.rb +4 -4
  40. data/test/fixtures/hacks.yml +2 -2
  41. data/test/fixtures/membership_status.rb +2 -2
  42. data/test/fixtures/product.rb +9 -9
  43. data/test/fixtures/product_tariff.rb +5 -5
  44. data/test/fixtures/products.yml +11 -11
  45. data/test/fixtures/reading.rb +4 -4
  46. data/test/fixtures/readings.yml +10 -10
  47. data/test/fixtures/reference_code_using_composite_key_alias.rb +8 -8
  48. data/test/fixtures/reference_code_using_simple_key_alias.rb +8 -8
  49. data/test/fixtures/reference_codes.yml +28 -28
  50. data/test/fixtures/reference_types.yml +9 -9
  51. data/test/fixtures/restaurant.rb +9 -9
  52. data/test/fixtures/restaurants.yml +14 -14
  53. data/test/fixtures/restaurants_suburbs.yml +10 -10
  54. data/test/fixtures/room.rb +11 -11
  55. data/test/fixtures/room_assignment.rb +13 -13
  56. data/test/fixtures/room_assignments.yml +24 -24
  57. data/test/fixtures/room_attribute.rb +2 -2
  58. data/test/fixtures/room_attribute_assignment.rb +4 -4
  59. data/test/fixtures/room_attribute_assignments.yml +4 -4
  60. data/test/fixtures/room_attributes.yml +2 -2
  61. data/test/fixtures/rooms.yml +12 -12
  62. data/test/fixtures/seat.rb +5 -5
  63. data/test/fixtures/seats.yml +8 -8
  64. data/test/fixtures/street.rb +2 -2
  65. data/test/fixtures/streets.yml +16 -16
  66. data/test/fixtures/student.rb +3 -3
  67. data/test/fixtures/students.yml +15 -15
  68. data/test/fixtures/suburbs.yml +14 -14
  69. data/test/fixtures/tariffs.yml +14 -14
  70. data/test/plugins/pagination.rb +405 -405
  71. data/test/plugins/pagination_helper.rb +135 -135
  72. data/test/setup.rb +50 -50
  73. data/test/test_aliases.rb +18 -18
  74. data/test/test_associations.rb +0 -10
  75. data/test/test_composite_arrays.rb +24 -24
  76. data/test/test_counter_cache.rb +30 -30
  77. data/test/test_dup.rb +37 -37
  78. data/test/test_exists.rb +39 -39
  79. data/test/test_find.rb +9 -3
  80. data/test/test_miscellaneous.rb +32 -32
  81. data/test/test_pagination.rb +35 -35
  82. data/test/test_validations.rb +13 -13
  83. metadata +8 -13
  84. data/lib/composite_primary_keys/autosave_association.rb +0 -32
  85. data/lib/composite_primary_keys/relation/predicate_builder.rb +0 -26
@@ -41,7 +41,7 @@ module ActiveRecord
41
41
  attribute_ids = attribute_collection['id'] || attribute_collection[:id]
42
42
  if attribute_ids
43
43
  ids = CompositePrimaryKeys::CompositeKeys.parse(attribute_ids)
44
- eq_predicates = Class.new.extend(CompositePrimaryKeys::Predicates).cpk_id_predicate(association.klass.arel_table, association.klass.primary_key, ids)
44
+ eq_predicates = CompositePrimaryKeys::Predicates.cpk_id_predicate(association.klass.arel_table, association.klass.primary_key, ids)
45
45
  association.scope.where(eq_predicates).to_a
46
46
  else
47
47
  []
@@ -28,12 +28,7 @@ module ActiveRecord
28
28
 
29
29
  # CPK
30
30
  if self.composite?
31
- # Not sure if this is a good idea, but replace nil id_was with the new id
32
- id_update = id_was.each_with_index.map do |value, i|
33
- value || id[i]
34
- end
35
-
36
- relation = @klass.unscoped.where(cpk_id_predicate(@klass.arel_table, @klass.primary_key, id_update))
31
+ relation = @klass.unscoped.where(cpk_id_predicate(@klass.arel_table, @klass.primary_key, id_was || id))
37
32
  else
38
33
  relation = scope.where(@klass.primary_key => (id_was || id))
39
34
  end
@@ -0,0 +1,33 @@
1
+ module ActiveRecord
2
+ class PredicateBuilder
3
+ class AssociationQueryHandler
4
+ def call(attribute, value)
5
+ queries = {}
6
+
7
+ table = value.associated_table
8
+ if value.base_class
9
+ queries[table.association_foreign_type.to_s] = value.base_class.name
10
+ end
11
+
12
+ # CPK
13
+ # queries[table.association_foreign_key.to_s] = value.ids
14
+ # predicate_builder.build_from_hash(queries)
15
+ if table.association_foreign_key.is_a?(Array)
16
+ values = case value.value
17
+ when Relation
18
+ value.ids.map {|record| record.ids}
19
+ when Array
20
+ value.ids
21
+ else
22
+ [value.ids]
23
+ end
24
+
25
+ CompositePrimaryKeys::Predicates.cpk_in_predicate(attribute.relation, table.association_foreign_key, values)
26
+ else
27
+ queries[table.association_foreign_key.to_s] = value.ids
28
+ predicate_builder.build_from_hash(queries)
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -1,8 +1,8 @@
1
1
  module CompositePrimaryKeys
2
2
  module VERSION
3
- MAJOR = 9
3
+ MAJOR = 10
4
4
  MINOR = 0
5
- TINY = 10
5
+ TINY = 0
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)
data/scripts/txt2js CHANGED
@@ -1,65 +1,65 @@
1
- #!/usr/bin/env ruby
2
-
3
- require 'rubygems'
4
- require 'redcloth'
5
- require 'syntax/convertors/html'
6
- require 'erb'
7
- require 'active_support'
8
-
9
- version_path = File.join(File.dirname(__FILE__), '..', 'lib', 'composite_primary_keys', 'version.rb')
10
- require File.expand_path(version_path)
11
-
12
- version = CompositePrimaryKeys::VERSION::STRING
13
- download = 'http://rubygems.org/gems/composite_primary_keys'
14
-
15
- class Fixnum
16
- def ordinal
17
- # teens
18
- return 'th' if (10..19).include?(self % 100)
19
- # others
20
- case self % 10
21
- when 1
22
- return 'st'
23
- when 2
24
- return 'nd'
25
- when 3
26
- return 'rd'
27
- else
28
- return 'th'
29
- end
30
- end
31
- end
32
-
33
- class Time
34
- def pretty
35
- return "#{mday}#{mday.ordinal} #{strftime('%B')} #{year}"
36
- end
37
- end
38
-
39
- def convert_syntax(syntax, source)
40
- return Syntax::Convertors::HTML.for_syntax(syntax).convert(source).gsub(%r!^<pre>|</pre>$!,'')
41
- end
42
-
43
- if ARGV.length >= 1
44
- src, template = ARGV
45
- template ||= File.dirname(__FILE__) + '/../website/template.js'
46
- else
47
- puts("Usage: #{File.split($0).last} source.txt [template.js] > output.html")
48
- exit!
49
- end
50
-
51
- template = ERB.new(File.open(template).read)
52
-
53
- title = nil
54
- body = nil
55
- File.open(src) do |fsrc|
56
- title_text = fsrc.readline
57
- body_text = fsrc.read
58
- title = RedCloth.new(title_text).to_html.gsub(%r!<.*?>!,'').strip
59
- body = RedCloth.new(body_text)
60
- end
61
- stat = File.stat(src)
62
- created = stat.ctime
63
- modified = stat.mtime
64
-
65
- $stdout << template.result(binding)
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'redcloth'
5
+ require 'syntax/convertors/html'
6
+ require 'erb'
7
+ require 'active_support'
8
+
9
+ version_path = File.join(File.dirname(__FILE__), '..', 'lib', 'composite_primary_keys', 'version.rb')
10
+ require File.expand_path(version_path)
11
+
12
+ version = CompositePrimaryKeys::VERSION::STRING
13
+ download = 'http://rubygems.org/gems/composite_primary_keys'
14
+
15
+ class Fixnum
16
+ def ordinal
17
+ # teens
18
+ return 'th' if (10..19).include?(self % 100)
19
+ # others
20
+ case self % 10
21
+ when 1
22
+ return 'st'
23
+ when 2
24
+ return 'nd'
25
+ when 3
26
+ return 'rd'
27
+ else
28
+ return 'th'
29
+ end
30
+ end
31
+ end
32
+
33
+ class Time
34
+ def pretty
35
+ return "#{mday}#{mday.ordinal} #{strftime('%B')} #{year}"
36
+ end
37
+ end
38
+
39
+ def convert_syntax(syntax, source)
40
+ return Syntax::Convertors::HTML.for_syntax(syntax).convert(source).gsub(%r!^<pre>|</pre>$!,'')
41
+ end
42
+
43
+ if ARGV.length >= 1
44
+ src, template = ARGV
45
+ template ||= File.dirname(__FILE__) + '/../website/template.js'
46
+ else
47
+ puts("Usage: #{File.split($0).last} source.txt [template.js] > output.html")
48
+ exit!
49
+ end
50
+
51
+ template = ERB.new(File.open(template).read)
52
+
53
+ title = nil
54
+ body = nil
55
+ File.open(src) do |fsrc|
56
+ title_text = fsrc.readline
57
+ body_text = fsrc.read
58
+ title = RedCloth.new(title_text).to_html.gsub(%r!<.*?>!,'').strip
59
+ body = RedCloth.new(body_text)
60
+ end
61
+ stat = File.stat(src)
62
+ created = stat.ctime
63
+ modified = stat.mtime
64
+
65
+ $stdout << template.result(binding)