mingusbabcock-composite_primary_keys 2.2.2 → 2.2.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (122) hide show
  1. data/History.txt +156 -0
  2. data/Manifest.txt +122 -0
  3. data/README.txt +41 -0
  4. data/README_DB2.txt +33 -0
  5. data/Rakefile +65 -0
  6. data/init.rb +2 -0
  7. data/install.rb +30 -0
  8. data/lib/adapter_helper/base.rb +63 -0
  9. data/lib/adapter_helper/mysql.rb +13 -0
  10. data/lib/adapter_helper/oracle.rb +12 -0
  11. data/lib/adapter_helper/postgresql.rb +13 -0
  12. data/lib/adapter_helper/sqlite3.rb +13 -0
  13. data/lib/composite_primary_keys.rb +56 -0
  14. data/lib/composite_primary_keys/association_preload.rb +253 -0
  15. data/lib/composite_primary_keys/associations.rb +428 -0
  16. data/lib/composite_primary_keys/attribute_methods.rb +84 -0
  17. data/lib/composite_primary_keys/base.rb +341 -0
  18. data/lib/composite_primary_keys/calculations.rb +69 -0
  19. data/lib/composite_primary_keys/composite_arrays.rb +30 -0
  20. data/lib/composite_primary_keys/connection_adapters/ibm_db_adapter.rb +21 -0
  21. data/lib/composite_primary_keys/connection_adapters/oracle_adapter.rb +15 -0
  22. data/lib/composite_primary_keys/connection_adapters/postgresql_adapter.rb +53 -0
  23. data/lib/composite_primary_keys/connection_adapters/sqlite3_adapter.rb +15 -0
  24. data/lib/composite_primary_keys/fixtures.rb +8 -0
  25. data/lib/composite_primary_keys/migration.rb +20 -0
  26. data/lib/composite_primary_keys/reflection.rb +19 -0
  27. data/lib/composite_primary_keys/version.rb +8 -0
  28. data/loader.rb +24 -0
  29. data/local/database_connections.rb.sample +10 -0
  30. data/local/paths.rb.sample +2 -0
  31. data/local/tasks.rb.sample +2 -0
  32. data/scripts/console.rb +48 -0
  33. data/scripts/txt2html +67 -0
  34. data/scripts/txt2js +59 -0
  35. data/tasks/activerecord_selection.rake +43 -0
  36. data/tasks/databases.rake +12 -0
  37. data/tasks/databases/mysql.rake +30 -0
  38. data/tasks/databases/oracle.rake +25 -0
  39. data/tasks/databases/postgresql.rake +26 -0
  40. data/tasks/databases/sqlite3.rake +28 -0
  41. data/tasks/deployment.rake +22 -0
  42. data/tasks/local_setup.rake +13 -0
  43. data/tasks/website.rake +18 -0
  44. data/test/README_tests.txt +67 -0
  45. data/test/abstract_unit.rb +94 -0
  46. data/test/connections/native_ibm_db/connection.rb +23 -0
  47. data/test/connections/native_mysql/connection.rb +13 -0
  48. data/test/connections/native_oracle/connection.rb +14 -0
  49. data/test/connections/native_postgresql/connection.rb +9 -0
  50. data/test/connections/native_sqlite/connection.rb +9 -0
  51. data/test/fixtures/article.rb +5 -0
  52. data/test/fixtures/articles.yml +6 -0
  53. data/test/fixtures/comment.rb +6 -0
  54. data/test/fixtures/comments.yml +16 -0
  55. data/test/fixtures/db_definitions/db2-create-tables.sql +113 -0
  56. data/test/fixtures/db_definitions/db2-drop-tables.sql +16 -0
  57. data/test/fixtures/db_definitions/mysql.sql +174 -0
  58. data/test/fixtures/db_definitions/oracle.drop.sql +39 -0
  59. data/test/fixtures/db_definitions/oracle.sql +188 -0
  60. data/test/fixtures/db_definitions/postgresql.sql +199 -0
  61. data/test/fixtures/db_definitions/sqlite.sql +160 -0
  62. data/test/fixtures/department.rb +5 -0
  63. data/test/fixtures/departments.yml +3 -0
  64. data/test/fixtures/employee.rb +4 -0
  65. data/test/fixtures/employees.yml +9 -0
  66. data/test/fixtures/group.rb +3 -0
  67. data/test/fixtures/groups.yml +3 -0
  68. data/test/fixtures/hack.rb +6 -0
  69. data/test/fixtures/hacks.yml +2 -0
  70. data/test/fixtures/membership.rb +7 -0
  71. data/test/fixtures/membership_status.rb +3 -0
  72. data/test/fixtures/membership_statuses.yml +10 -0
  73. data/test/fixtures/memberships.yml +6 -0
  74. data/test/fixtures/product.rb +7 -0
  75. data/test/fixtures/product_tariff.rb +5 -0
  76. data/test/fixtures/product_tariffs.yml +12 -0
  77. data/test/fixtures/products.yml +6 -0
  78. data/test/fixtures/reading.rb +4 -0
  79. data/test/fixtures/readings.yml +10 -0
  80. data/test/fixtures/reference_code.rb +7 -0
  81. data/test/fixtures/reference_codes.yml +28 -0
  82. data/test/fixtures/reference_type.rb +7 -0
  83. data/test/fixtures/reference_types.yml +9 -0
  84. data/test/fixtures/street.rb +3 -0
  85. data/test/fixtures/streets.yml +15 -0
  86. data/test/fixtures/suburb.rb +6 -0
  87. data/test/fixtures/suburbs.yml +9 -0
  88. data/test/fixtures/tariff.rb +6 -0
  89. data/test/fixtures/tariffs.yml +13 -0
  90. data/test/fixtures/user.rb +10 -0
  91. data/test/fixtures/users.yml +6 -0
  92. data/test/hash_tricks.rb +34 -0
  93. data/test/plugins/pagination.rb +405 -0
  94. data/test/plugins/pagination_helper.rb +135 -0
  95. data/test/test_associations.rb +160 -0
  96. data/test/test_attribute_methods.rb +22 -0
  97. data/test/test_attributes.rb +84 -0
  98. data/test/test_clone.rb +34 -0
  99. data/test/test_composite_arrays.rb +51 -0
  100. data/test/test_create.rb +68 -0
  101. data/test/test_delete.rb +96 -0
  102. data/test/test_dummy.rb +28 -0
  103. data/test/test_exists.rb +29 -0
  104. data/test/test_find.rb +73 -0
  105. data/test/test_ids.rb +97 -0
  106. data/test/test_miscellaneous.rb +39 -0
  107. data/test/test_pagination.rb +38 -0
  108. data/test/test_polymorphic.rb +31 -0
  109. data/test/test_santiago.rb +27 -0
  110. data/test/test_tutorial_examle.rb +26 -0
  111. data/test/test_update.rb +40 -0
  112. data/website/index.html +199 -0
  113. data/website/index.txt +159 -0
  114. data/website/javascripts/rounded_corners_lite.inc.js +285 -0
  115. data/website/stylesheets/screen.css +126 -0
  116. data/website/template.js +3 -0
  117. data/website/template.rhtml +53 -0
  118. data/website/version-raw.js +3 -0
  119. data/website/version-raw.txt +2 -0
  120. data/website/version.js +4 -0
  121. data/website/version.txt +3 -0
  122. metadata +180 -18
@@ -0,0 +1,30 @@
1
+ module CompositePrimaryKeys
2
+ ID_SEP = ','
3
+ ID_SET_SEP = ';'
4
+
5
+ module ArrayExtension
6
+ def to_composite_keys
7
+ CompositeKeys.new(self)
8
+ end
9
+
10
+ def to_composite_ids
11
+ CompositeIds.new(self)
12
+ end
13
+ end
14
+
15
+ class CompositeArray < Array
16
+ def to_s
17
+ join(ID_SEP)
18
+ end
19
+ end
20
+
21
+ class CompositeKeys < CompositeArray
22
+
23
+ end
24
+
25
+ class CompositeIds < CompositeArray
26
+
27
+ end
28
+ end
29
+
30
+ Array.send(:include, CompositePrimaryKeys::ArrayExtension)
@@ -0,0 +1,21 @@
1
+ module ActiveRecord
2
+ module ConnectionAdapters
3
+ class IBM_DBAdapter < AbstractAdapter
4
+
5
+ # This mightn't be in Core, but count(distinct x,y) doesn't work for me
6
+ def supports_count_distinct? #:nodoc:
7
+ false
8
+ end
9
+
10
+ alias_method :quote_original, :quote
11
+ def quote(value, column = nil)
12
+ if value.kind_of?(String) && column && [:integer, :float].include?(column.type)
13
+ value = column.type == :integer ? value.to_i : value.to_f
14
+ value.to_s
15
+ else
16
+ quote_original(value, column)
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,15 @@
1
+ module ActiveRecord
2
+ module ConnectionAdapters
3
+ class OracleAdapter < AbstractAdapter
4
+
5
+ # This mightn't be in Core, but count(distinct x,y) doesn't work for me
6
+ def supports_count_distinct? #:nodoc:
7
+ false
8
+ end
9
+
10
+ def concat(*columns)
11
+ "(#{columns.join('||')})"
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,53 @@
1
+ module ActiveRecord
2
+ module ConnectionAdapters
3
+ class PostgreSQLAdapter < AbstractAdapter
4
+
5
+ # This mightn't be in Core, but count(distinct x,y) doesn't work for me
6
+ def supports_count_distinct? #:nodoc:
7
+ false
8
+ end
9
+
10
+ def concat(*columns)
11
+ columns = columns.map { |c| "CAST(#{c} AS varchar)" }
12
+ "(#{columns.join('||')})"
13
+ end
14
+
15
+ # Executes an INSERT query and returns the new record's ID
16
+ def insert(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil)
17
+ # Extract the table from the insert sql. Yuck.
18
+ table = sql.split(" ", 4)[2].gsub('"', '')
19
+
20
+ # Try an insert with 'returning id' if available (PG >= 8.2)
21
+ if supports_insert_with_returning?
22
+ pk, sequence_name = *pk_and_sequence_for(table) unless pk
23
+ if pk
24
+ quoted_pk = if pk.is_a?(Array)
25
+ pk.map { |col| quote_column_name(col) }.join(ID_SEP)
26
+ else
27
+ quote_column_name(pk)
28
+ end
29
+ id = select_value("#{sql} RETURNING #{quoted_pk}")
30
+ clear_query_cache
31
+ return id
32
+ end
33
+ end
34
+
35
+ # Otherwise, insert then grab last_insert_id.
36
+ if insert_id = super
37
+ insert_id
38
+ else
39
+ # If neither pk nor sequence name is given, look them up.
40
+ unless pk || sequence_name
41
+ pk, sequence_name = *pk_and_sequence_for(table)
42
+ end
43
+
44
+ # If a pk is given, fallback to default sequence name.
45
+ # Don't fetch last insert id for a table without a pk.
46
+ if pk && sequence_name ||= default_sequence_name(table, pk)
47
+ last_insert_id(table, sequence_name)
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,15 @@
1
+ require 'active_record/connection_adapters/sqlite_adapter'
2
+
3
+ module ActiveRecord
4
+ module ConnectionAdapters #:nodoc:
5
+ class SQLite3Adapter < SQLiteAdapter # :nodoc:
6
+ def supports_count_distinct? #:nodoc:
7
+ false
8
+ end
9
+
10
+ def concat(*columns)
11
+ "(#{columns.join('||')})"
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,8 @@
1
+ class Fixture #:nodoc:
2
+ def [](key)
3
+ if key.is_a? Array
4
+ return key.map { |a_key| self[a_key.to_s] }.to_composite_ids.to_s
5
+ end
6
+ @fixture[key]
7
+ end
8
+ end
@@ -0,0 +1,20 @@
1
+ ActiveRecord::ConnectionAdapters::ColumnDefinition.send(:alias_method, :to_s_without_composite_keys, :to_s)
2
+
3
+ ActiveRecord::ConnectionAdapters::ColumnDefinition.class_eval <<-'EOF'
4
+ def to_s
5
+ if name.is_a? Array
6
+ "PRIMARY KEY (#{name.join(',')})"
7
+ else
8
+ to_s_without_composite_keys
9
+ end
10
+ end
11
+ EOF
12
+
13
+ ActiveRecord::ConnectionAdapters::TableDefinition.class_eval <<-'EOF'
14
+ def [](name)
15
+ @columns.find { |column|
16
+ !column.name.is_a?(Array) && column.name.to_s == name.to_s
17
+ }
18
+ end
19
+ EOF
20
+
@@ -0,0 +1,19 @@
1
+ module ActiveRecord
2
+ module Reflection
3
+ class AssociationReflection
4
+ def primary_key_name
5
+ return @primary_key_name if @primary_key_name
6
+ case
7
+ when macro == :belongs_to
8
+ @primary_key_name = options[:foreign_key] || class_name.foreign_key
9
+ when options[:as]
10
+ @primary_key_name = options[:foreign_key] || "#{options[:as]}_id"
11
+ else
12
+ @primary_key_name = options[:foreign_key] || active_record.name.foreign_key
13
+ end
14
+ @primary_key_name = @primary_key_name.to_composite_keys.to_s if @primary_key_name.is_a? Array
15
+ @primary_key_name
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,8 @@
1
+ module CompositePrimaryKeys
2
+ module VERSION #:nodoc:
3
+ MAJOR = 2
4
+ MINOR = 2
5
+ TINY = 2
6
+ STRING = [MAJOR, MINOR, TINY].join('.')
7
+ end
8
+ end
data/loader.rb ADDED
@@ -0,0 +1,24 @@
1
+ # Load local config files in /local
2
+ begin
3
+ local_file_supported = Dir[File.join(PROJECT_ROOT, 'local/*.sample')].map { |path| File.basename(path).sub(".sample","") }
4
+ local_file_supported.each do |file|
5
+ require "local/#{file}"
6
+ end
7
+ rescue LoadError
8
+ puts <<-EOS
9
+ This Gem supports local developer extensions in local/ folder.
10
+ Supported files:
11
+ #{local_file_supported.map { |f| "local/#{f}"}.join(', ')}
12
+
13
+ Setup default sample files:
14
+ rake local:setup
15
+
16
+ Current warning: #{$!}
17
+
18
+ EOS
19
+ end
20
+
21
+
22
+ # Now load Rake tasks from /tasks
23
+ rakefiles = Dir[File.join(File.dirname(__FILE__), "tasks/**/*.rake")]
24
+ rakefiles.each { |rakefile| load File.expand_path(rakefile) }
@@ -0,0 +1,10 @@
1
+ require 'yaml'
2
+
3
+ ENV['cpk_adapters'] = {
4
+ "mysql" => {
5
+ :adapter => "mysql",
6
+ :username => "root",
7
+ :password => "root",
8
+ # ...
9
+ }
10
+ }.to_yaml
@@ -0,0 +1,2 @@
1
+ # location of folder containing activerecord, railties, etc folders for each Rails gem
2
+ ENV['EDGE_RAILS_DIR'] ||= "/path/to/copy/of/edge/rails"
@@ -0,0 +1,2 @@
1
+ # This file loaded into Rakefile
2
+ # Place any extra development tasks you want here
@@ -0,0 +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']}/activerecord/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 ADDED
@@ -0,0 +1,67 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'redcloth'
5
+ require 'syntax/convertors/html'
6
+ require 'erb'
7
+ require File.dirname(__FILE__) + '/../lib/composite_primary_keys/version.rb'
8
+
9
+ version = CompositePrimaryKeys::VERSION::STRING
10
+ download = 'http://rubyforge.org/projects/compositekeys'
11
+
12
+ class Fixnum
13
+ def ordinal
14
+ # teens
15
+ return 'th' if (10..19).include?(self % 100)
16
+ # others
17
+ case self % 10
18
+ when 1: return 'st'
19
+ when 2: return 'nd'
20
+ when 3: return 'rd'
21
+ else return 'th'
22
+ end
23
+ end
24
+ end
25
+
26
+ class Time
27
+ def pretty
28
+ return "#{mday}#{mday.ordinal} #{strftime('%B')} #{year}"
29
+ end
30
+ end
31
+
32
+ def convert_syntax(syntax, source)
33
+ return Syntax::Convertors::HTML.for_syntax(syntax).convert(source).gsub(%r!^<pre>|</pre>$!,'')
34
+ end
35
+
36
+ if ARGV.length >= 1
37
+ src, template = ARGV
38
+ template ||= File.dirname(__FILE__) + '/../website/template.rhtml'
39
+
40
+ else
41
+ puts("Usage: #{File.split($0).last} source.txt [template.rhtml] > output.html")
42
+ exit!
43
+ end
44
+
45
+ template = ERB.new(File.open(template).read)
46
+
47
+ title = nil
48
+ body = nil
49
+ File.open(src) do |fsrc|
50
+ title_text = fsrc.readline
51
+ body_text = fsrc.read
52
+ syntax_items = []
53
+ body_text.gsub!(%r!<(pre|code)[^>]*?syntax=['"]([^'"]+)[^>]*>(.*?)</\1>!m){
54
+ ident = syntax_items.length
55
+ element, syntax, source = $1, $2, $3
56
+ syntax_items << "<#{element} class=\"syntax\">#{convert_syntax(syntax, source)}</#{element}>"
57
+ "syntax-temp-#{ident}"
58
+ }
59
+ title = RedCloth.new(title_text).to_html.gsub(%r!<.*?>!,'').strip
60
+ body = RedCloth.new(body_text).to_html
61
+ body.gsub!(%r!(?:<pre><code>)?syntax-temp-(\d+)(?:</code></pre>)?!){ syntax_items[$1.to_i] }
62
+ end
63
+ stat = File.stat(src)
64
+ created = stat.ctime
65
+ modified = stat.mtime
66
+
67
+ $stdout << template.result(binding)
data/scripts/txt2js ADDED
@@ -0,0 +1,59 @@
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
+ require File.dirname(__FILE__) + '/../lib/composite_primary_keys/version.rb'
9
+
10
+ version = CompositePrimaryKeys::VERSION::STRING
11
+ download = 'http://rubyforge.org/projects/compositekeys'
12
+
13
+ class Fixnum
14
+ def ordinal
15
+ # teens
16
+ return 'th' if (10..19).include?(self % 100)
17
+ # others
18
+ case self % 10
19
+ when 1: return 'st'
20
+ when 2: return 'nd'
21
+ when 3: return 'rd'
22
+ else return 'th'
23
+ end
24
+ end
25
+ end
26
+
27
+ class Time
28
+ def pretty
29
+ return "#{mday}#{mday.ordinal} #{strftime('%B')} #{year}"
30
+ end
31
+ end
32
+
33
+ def convert_syntax(syntax, source)
34
+ return Syntax::Convertors::HTML.for_syntax(syntax).convert(source).gsub(%r!^<pre>|</pre>$!,'')
35
+ end
36
+
37
+ if ARGV.length >= 1
38
+ src, template = ARGV
39
+ template ||= File.dirname(__FILE__) + '/../website/template.js'
40
+ else
41
+ puts("Usage: #{File.split($0).last} source.txt [template.js] > output.html")
42
+ exit!
43
+ end
44
+
45
+ template = ERB.new(File.open(template).read)
46
+
47
+ title = nil
48
+ body = nil
49
+ File.open(src) do |fsrc|
50
+ title_text = fsrc.readline
51
+ body_text = fsrc.read
52
+ title = RedCloth.new(title_text).to_html.gsub(%r!<.*?>!,'').strip
53
+ body = RedCloth.new(body_text)
54
+ end
55
+ stat = File.stat(src)
56
+ created = stat.ctime
57
+ modified = stat.mtime
58
+
59
+ $stdout << template.result(binding)
@@ -0,0 +1,43 @@
1
+ namespace :ar do
2
+ desc 'Pre-load edge rails ActiveRecord'
3
+ task :edge do
4
+ unless path = ENV['EDGE_RAILS_DIR'] || ENV['EDGE_RAILS']
5
+ puts <<-EOS
6
+
7
+ Need to define env var EDGE_RAILS_DIR or EDGE_RAILS- root of edge rails on your machine.
8
+ i) Get copy of Edge Rails - http://dev.rubyonrails.org
9
+ ii) Set EDGE_RAILS_DIR to this folder in local/paths.rb - see local/paths.rb.sample for example
10
+ or
11
+ a) Set folder from environment or command line (rake ar:edge EDGE_RAILS_DIR=/path/to/rails)
12
+
13
+ EOS
14
+ exit
15
+ end
16
+
17
+ ENV['AR_LOAD_PATH'] = File.join(path, "activerecord/lib")
18
+ end
19
+
20
+ desc 'Pre-load ActiveRecord using VERSION=X.Y.Z, instead of latest'
21
+ task :set do
22
+ unless version = ENV['VERSION']
23
+ puts <<-EOS
24
+ Usage: rake ar:get_version VERSION=1.15.3
25
+ Specify the version number with VERSION=X.Y.Z; and make sure you have that activerecord gem version installed.
26
+
27
+ EOS
28
+ end
29
+ version = nil if version == "" || version == []
30
+ begin
31
+ version ? gem('activerecord', version) : gem('activerecord')
32
+ require 'active_record'
33
+ ENV['AR_LOAD_PATH'] = $:.reverse.find { |path| /activerecord/ =~ path }
34
+ rescue LoadError
35
+ puts <<-EOS
36
+ Missing: Cannot find activerecord #{version} installed.
37
+ Install: gem install activerecord -v #{version}
38
+
39
+ EOS
40
+ exit
41
+ end
42
+ end
43
+ end