composite_primary_keys 8.1.6 → 9.0.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (153) hide show
  1. checksums.yaml +5 -5
  2. data/History.rdoc +59 -7
  3. data/README.rdoc +62 -1
  4. data/Rakefile +4 -1
  5. data/lib/composite_primary_keys/arel/in.rb +6 -0
  6. data/lib/composite_primary_keys/arel/sqlserver.rb +36 -0
  7. data/lib/composite_primary_keys/arel/to_sql.rb +26 -0
  8. data/lib/composite_primary_keys/associations/association.rb +14 -12
  9. data/lib/composite_primary_keys/associations/association_scope.rb +22 -27
  10. data/lib/composite_primary_keys/associations/collection_association.rb +39 -8
  11. data/lib/composite_primary_keys/associations/has_many_association.rb +17 -40
  12. data/lib/composite_primary_keys/associations/has_many_through_association.rb +58 -58
  13. data/lib/composite_primary_keys/associations/join_dependency/join_association.rb +13 -11
  14. data/lib/composite_primary_keys/associations/join_dependency.rb +73 -56
  15. data/lib/composite_primary_keys/associations/preloader/association.rb +75 -73
  16. data/lib/composite_primary_keys/attribute_methods/primary_key.rb +13 -11
  17. data/lib/composite_primary_keys/attribute_methods/read.rb +18 -15
  18. data/lib/composite_primary_keys/attribute_methods/write.rb +21 -19
  19. data/lib/composite_primary_keys/attribute_methods.rb +6 -4
  20. data/lib/composite_primary_keys/autosave_association.rb +19 -54
  21. data/lib/composite_primary_keys/base.rb +18 -82
  22. data/lib/composite_primary_keys/composite_arrays.rb +9 -1
  23. data/lib/composite_primary_keys/composite_predicates.rb +1 -0
  24. data/lib/composite_primary_keys/connection_adapters/abstract_adapter.rb +10 -10
  25. data/lib/composite_primary_keys/connection_adapters/abstract_mysql_adapter.rb +5 -6
  26. data/lib/composite_primary_keys/connection_adapters/sqlite3_adapter.rb +23 -0
  27. data/lib/composite_primary_keys/core.rb +46 -45
  28. data/lib/composite_primary_keys/fixtures.rb +19 -17
  29. data/lib/composite_primary_keys/locking/optimistic.rb +53 -49
  30. data/lib/composite_primary_keys/nested_attributes.rb +60 -53
  31. data/lib/composite_primary_keys/persistence.rb +49 -41
  32. data/lib/composite_primary_keys/relation/batches.rb +35 -32
  33. data/lib/composite_primary_keys/relation/calculations.rb +3 -7
  34. data/lib/composite_primary_keys/relation/finder_methods.rb +122 -55
  35. data/lib/composite_primary_keys/relation/predicate_builder.rb +18 -29
  36. data/lib/composite_primary_keys/relation/query_methods.rb +25 -36
  37. data/lib/composite_primary_keys/relation/where_clause.rb +33 -0
  38. data/lib/composite_primary_keys/relation.rb +96 -43
  39. data/lib/composite_primary_keys/sanitization.rb +6 -15
  40. data/lib/composite_primary_keys/version.rb +3 -3
  41. data/lib/composite_primary_keys.rb +10 -19
  42. data/scripts/console.rb +48 -48
  43. data/scripts/txt2html +76 -76
  44. data/scripts/txt2js +65 -65
  45. data/tasks/databases/mysql.rake +17 -19
  46. data/tasks/databases/oracle.rake +29 -15
  47. data/tasks/databases/postgresql.rake +20 -29
  48. data/tasks/databases/sqlite.rake +25 -0
  49. data/tasks/databases/sqlserver.rake +32 -16
  50. data/tasks/website.rake +18 -18
  51. data/test/README_tests.rdoc +56 -56
  52. data/test/abstract_unit.rb +24 -18
  53. data/test/connections/connection_spec.rb +11 -2
  54. data/test/connections/databases.ci.yml +5 -4
  55. data/test/connections/databases.example.yml +19 -4
  56. data/test/connections/databases.yml +40 -30
  57. data/test/db_test.rb +52 -52
  58. data/test/fixtures/article.rb +1 -0
  59. data/test/fixtures/articles.yml +6 -6
  60. data/test/fixtures/capitol.rb +3 -3
  61. data/test/fixtures/capitols.yml +16 -16
  62. data/test/fixtures/comments.yml +15 -15
  63. data/test/fixtures/db_definitions/mysql.sql +16 -17
  64. data/test/fixtures/db_definitions/oracle.drop.sql +2 -0
  65. data/test/fixtures/db_definitions/oracle.sql +26 -17
  66. data/test/fixtures/db_definitions/postgresql.sql +2 -3
  67. data/test/fixtures/db_definitions/sqlite.sql +0 -1
  68. data/test/fixtures/db_definitions/sqlserver.sql +20 -35
  69. data/test/fixtures/department.rb +5 -5
  70. data/test/fixtures/departments.yml +15 -15
  71. data/test/fixtures/dorms.yml +4 -4
  72. data/test/fixtures/employee.rb +5 -7
  73. data/test/fixtures/employees.yml +1 -5
  74. data/test/fixtures/group.rb +2 -2
  75. data/test/fixtures/groups.yml +6 -6
  76. data/test/fixtures/hack.rb +4 -4
  77. data/test/fixtures/hacks.yml +2 -2
  78. data/test/fixtures/membership_status.rb +2 -2
  79. data/test/fixtures/product.rb +9 -9
  80. data/test/fixtures/product_tariff.rb +5 -5
  81. data/test/fixtures/products.yml +11 -11
  82. data/test/fixtures/reading.rb +4 -4
  83. data/test/fixtures/readings.yml +10 -10
  84. data/test/fixtures/reference_code_using_composite_key_alias.rb +8 -8
  85. data/test/fixtures/reference_code_using_simple_key_alias.rb +8 -8
  86. data/test/fixtures/reference_codes.yml +28 -28
  87. data/test/fixtures/reference_type.rb +1 -1
  88. data/test/fixtures/reference_types.yml +9 -9
  89. data/test/fixtures/restaurant.rb +9 -9
  90. data/test/fixtures/restaurants.yml +14 -14
  91. data/test/fixtures/restaurants_suburbs.yml +10 -10
  92. data/test/fixtures/room.rb +11 -11
  93. data/test/fixtures/room_assignment.rb +13 -13
  94. data/test/fixtures/room_assignments.yml +24 -24
  95. data/test/fixtures/room_attribute.rb +2 -2
  96. data/test/fixtures/room_attribute_assignment.rb +4 -4
  97. data/test/fixtures/room_attribute_assignments.yml +4 -4
  98. data/test/fixtures/room_attributes.yml +2 -2
  99. data/test/fixtures/rooms.yml +12 -12
  100. data/test/fixtures/seat.rb +5 -5
  101. data/test/fixtures/seats.yml +8 -8
  102. data/test/fixtures/street.rb +2 -2
  103. data/test/fixtures/streets.yml +16 -16
  104. data/test/fixtures/student.rb +3 -3
  105. data/test/fixtures/students.yml +15 -15
  106. data/test/fixtures/suburbs.yml +14 -14
  107. data/test/fixtures/tariff.rb +1 -1
  108. data/test/fixtures/tariffs.yml +14 -14
  109. data/test/fixtures/user.rb +0 -1
  110. data/test/plugins/pagination.rb +405 -405
  111. data/test/plugins/pagination_helper.rb +135 -135
  112. data/test/setup.rb +50 -50
  113. data/test/test_aliases.rb +18 -18
  114. data/test/test_associations.rb +18 -17
  115. data/test/test_composite_arrays.rb +24 -24
  116. data/test/test_counter_cache.rb +30 -30
  117. data/test/test_create.rb +14 -6
  118. data/test/test_delete.rb +36 -34
  119. data/test/test_dup.rb +37 -37
  120. data/test/test_exists.rb +39 -39
  121. data/test/test_find.rb +16 -4
  122. data/test/test_habtm.rb +27 -3
  123. data/test/test_miscellaneous.rb +32 -32
  124. data/test/test_nested_attributes.rb +6 -6
  125. data/test/test_pagination.rb +35 -35
  126. data/test/test_polymorphic.rb +0 -7
  127. data/test/test_predicates.rb +20 -20
  128. data/test/test_preload.rb +94 -0
  129. data/test/test_suite.rb +1 -1
  130. data/test/test_update.rb +10 -7
  131. data/test/test_validations.rb +13 -13
  132. metadata +30 -39
  133. data/README_DB2.rdoc +0 -33
  134. data/lib/composite_primary_keys/arel/visitors/to_sql.rb +0 -36
  135. data/lib/composite_primary_keys/associations/singular_association.rb +0 -18
  136. data/lib/composite_primary_keys/attribute_methods/dirty.rb +0 -29
  137. data/lib/composite_primary_keys/attribute_set/builder.rb +0 -20
  138. data/lib/composite_primary_keys/connection_adapters/abstract/connection_specification_changes.rb +0 -70
  139. data/lib/composite_primary_keys/connection_adapters/postgresql_adapter.rb +0 -19
  140. data/lib/composite_primary_keys/dirty.rb +0 -19
  141. data/lib/composite_primary_keys/validations/uniqueness.rb +0 -41
  142. data/tasks/databases/oracle_enhanced.rake +0 -27
  143. data/tasks/databases/sqlite3.rake +0 -27
  144. data/test/connections/native_ibm_db/connection.rb +0 -19
  145. data/test/connections/native_mysql/connection.rb +0 -17
  146. data/test/connections/native_oracle/connection.rb +0 -11
  147. data/test/connections/native_oracle_enhanced/connection.rb +0 -16
  148. data/test/connections/native_postgresql/connection.rb +0 -13
  149. data/test/connections/native_sqlite3/connection.rb +0 -9
  150. data/test/connections/native_sqlserver/connection.rb +0 -11
  151. data/test/fixtures/db_definitions/sqlserver.drop.sql +0 -92
  152. data/test/test_delete_all.rb +0 -35
  153. data/test/test_find_in_batches.rb +0 -30
@@ -1,8 +1,8 @@
1
1
  module ActiveRecord
2
2
  class Relation
3
3
  alias :initialize_without_cpk :initialize
4
- def initialize(klass, table, values = {})
5
- initialize_without_cpk(klass, table, values)
4
+ def initialize(klass, table, predicate_builder, values = {})
5
+ initialize_without_cpk(klass, table, predicate_builder, values)
6
6
  add_cpk_support if klass && klass.composite?
7
7
  end
8
8
 
@@ -16,60 +16,113 @@ module ActiveRecord
16
16
  extend CompositePrimaryKeys::CompositeRelation
17
17
  end
18
18
 
19
- alias :where_values_hash_without_cpk :where_values_hash
20
- def where_values_hash(relation_table_name = table_name)
21
- # CPK
22
- nodes_from_and = where_values.grep(Arel::Nodes::And).map { |and_node|
23
- and_node.children.grep(Arel::Nodes::Equality)
24
- }.flatten
25
-
26
- # CPK
27
- # equalities = where_values.grep(Arel::Nodes::Equality).find_all { |node|
28
- # node.left.relation.name == relation_table_name
29
- # }
30
- equalities = (nodes_from_and + where_values.grep(Arel::Nodes::Equality)).find_all { |node|
31
- node.left.relation.name == relation_table_name
32
- }
19
+ silence_warnings do
20
+ def _update_record(values, id, id_was) # :nodoc:
21
+ substitutes, binds = substitute_values values
22
+
23
+ scope = @klass.unscoped
33
24
 
34
- binds = Hash[bind_values.find_all(&:first).map { |column, v| [column.name, v] }]
25
+ if @klass.finder_needs_type_condition?
26
+ scope.unscope!(where: @klass.inheritance_column)
27
+ end
35
28
 
36
- Hash[equalities.map { |where|
37
- name = where.left.name
38
- [name, binds.fetch(name.to_s) {
39
- case where.right
40
- when Array then where.right.map(&:val)
41
- else
42
- where.right.val
29
+ # CPK
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]
43
34
  end
44
- }]
45
- }]
35
+
36
+ relation = @klass.unscoped.where(cpk_id_predicate(@klass.arel_table, @klass.primary_key, id_update))
37
+ else
38
+ relation = scope.where(@klass.primary_key => (id_was || id))
39
+ end
40
+
41
+
42
+ bvs = binds + relation.bound_attributes
43
+ um = relation
44
+ .arel
45
+ .compile_update(substitutes, @klass.primary_key)
46
+
47
+ @klass.connection.update(
48
+ um,
49
+ 'SQL',
50
+ bvs,
51
+ )
52
+ end
46
53
  end
47
54
 
48
- def _update_record(values, id, id_was)
49
- substitutes, binds = substitute_values values
55
+ def update_all(updates)
56
+ raise ArgumentError, "Empty list of attributes to change" if updates.blank?
50
57
 
51
- # CPK
52
- um = if self.composite?
53
- relation = @klass.unscoped.where(cpk_id_predicate(@klass.arel_table, @klass.primary_key, id_was || id))
58
+ stmt = Arel::UpdateManager.new
54
59
 
55
- relation.arel.compile_update(substitutes, @klass.primary_key)
56
- else
57
- scope = @klass.unscoped
60
+ stmt.set Arel.sql(@klass.send(:sanitize_sql_for_assignment, updates))
61
+ stmt.table(table)
58
62
 
59
- if @klass.finder_needs_type_condition?
60
- scope.unscope!(where: @klass.inheritance_column)
63
+ if joins_values.any?
64
+ # CPK
65
+ #@klass.connection.join_to_update(stmt, arel, arel_attribute(primary_key))
66
+ if primary_key.kind_of?(Array)
67
+ attributes = primary_key.map do |key|
68
+ arel_attribute(key)
69
+ end
70
+ @klass.connection.join_to_update(stmt, arel, attributes.to_composite_keys)
71
+ else
72
+ @klass.connection.join_to_update(stmt, arel, arel_attribute(primary_key))
61
73
  end
74
+ else
75
+ stmt.key = arel_attribute(primary_key)
76
+ stmt.take(arel.limit)
77
+ stmt.order(*arel.orders)
78
+ stmt.wheres = arel.constraints
79
+ end
80
+
81
+ @klass.connection.update stmt, 'SQL', bound_attributes
82
+ end
62
83
 
63
- relation = scope.where(@klass.primary_key => (id_was || id))
64
- binds += relation.bind_values
65
84
 
66
- relation.arel.compile_update(substitutes, @klass.primary_key)
85
+ def delete_all(conditions = nil)
86
+ invalid_methods = INVALID_METHODS_FOR_DELETE_ALL.select { |method|
87
+ if MULTI_VALUE_METHODS.include?(method)
88
+ send("#{method}_values").any?
89
+ elsif SINGLE_VALUE_METHODS.include?(method)
90
+ send("#{method}_value")
91
+ elsif CLAUSE_METHODS.include?(method)
92
+ send("#{method}_clause").any?
93
+ end
94
+ }
95
+ if invalid_methods.any?
96
+ raise ActiveRecordError.new("delete_all doesn't support #{invalid_methods.join(', ')}")
67
97
  end
68
98
 
69
- @klass.connection.update(
70
- um,
71
- 'SQL',
72
- binds)
99
+ if conditions
100
+ ActiveSupport::Deprecation.warn(<<-MESSAGE.squish)
101
+ Passing conditions to delete_all is deprecated and will be removed in Rails 5.1.
102
+ To achieve the same use where(conditions).delete_all.
103
+ MESSAGE
104
+ where(conditions).delete_all
105
+ else
106
+ stmt = Arel::DeleteManager.new
107
+ stmt.from(table)
108
+
109
+ # CPK
110
+ if joins_values.any? && @klass.composite?
111
+ arel_attributes = Array(primary_key).map do |key|
112
+ arel_attribute(key)
113
+ end.to_composite_keys
114
+ @klass.connection.join_to_delete(stmt, arel, arel_attributes)
115
+ elsif joins_values.any?
116
+ @klass.connection.join_to_delete(stmt, arel, arel_attribute(primary_key))
117
+ else
118
+ stmt.wheres = arel.constraints
119
+ end
120
+
121
+ affected = @klass.connection.delete(stmt, 'SQL', bound_attributes)
122
+
123
+ reset
124
+ affected
125
+ end
73
126
  end
74
127
  end
75
128
  end
@@ -2,23 +2,14 @@ module ActiveRecord
2
2
  module Sanitization
3
3
  module ClassMethods
4
4
  protected
5
- # Accepts a hash of SQL conditions and replaces those attributes
6
- # that correspond to a +composed_of+ relationship with their expanded
7
- # aggregate attribute values.
8
- # Given:
9
- # class Person < ActiveRecord::Base
10
- # composed_of :address, class_name: "Address",
11
- # mapping: [%w(address_street street), %w(address_city city)]
12
- # end
13
- # Then:
14
- # { address: Address.new("813 abc st.", "chicago") }
15
- # # => { address_street: "813 abc st.", address_city: "chicago" }
5
+
16
6
  def expand_hash_conditions_for_aggregates(attrs)
17
7
  expanded_attrs = {}
18
8
  attrs.each do |attr, value|
19
9
  # CPK
20
10
  # if aggregation = reflect_on_aggregation(attr.to_sym)
21
11
  if attr.is_a?(CompositePrimaryKeys::CompositeKeys)
12
+ value = value.split('/') if value.is_a?(String)
22
13
  attr.each_with_index do |key,i|
23
14
  expanded_attrs[key] = value.respond_to?(:flatten) ? value.flatten[i] : value
24
15
  end
@@ -40,15 +31,15 @@ module ActiveRecord
40
31
 
41
32
  def quoted_id
42
33
  # CPK
43
- # self.class.quote_value(id, column_for_attribute(self.class.primary_key))
34
+ # self.class.quote_value(@attributes[self.class.primary_key].value_for_database)
44
35
  if self.composite?
45
36
  [self.class.primary_keys, ids].transpose.map { |attr_name,id|
46
- self.class.quote_value(id, column_for_attribute(attr_name))
37
+ self.class.quote_value(@attributes[attr_name].value_for_database)
47
38
  }
48
39
  else
49
- self.class.quote_value(id, column_for_attribute(self.class.primary_key))
40
+ self.class.quote_value(@attributes[self.class.primary_key].value_for_database)
50
41
  end
51
42
  end
52
43
  end
53
44
  end
54
- end
45
+ end
@@ -1,8 +1,8 @@
1
1
  module CompositePrimaryKeys
2
2
  module VERSION
3
- MAJOR = 8
4
- MINOR = 1
5
- TINY = 6
3
+ MAJOR = 9
4
+ MINOR = 0
5
+ TINY = 10
6
6
  STRING = [MAJOR, MINOR, TINY].join('.')
7
7
  end
8
8
  end
@@ -1,5 +1,5 @@
1
1
  #--
2
- # Copyright (c) 2006-2012 Nic Williams and Charlie Savage
2
+ # Copyright (c) 2006-2016 Nic Williams and Charlie Savage
3
3
  #
4
4
  # Permission is hereby granted, free of charge, to any person obtaining
5
5
  # a copy of this software and associated documentation files (the
@@ -26,13 +26,10 @@ $:.unshift(File.dirname(__FILE__)) unless
26
26
 
27
27
  unless defined?(ActiveRecord)
28
28
  require 'rubygems'
29
- gem 'activerecord', '~>4.2.0'
29
+ gem 'activerecord', '~>5.0.0.beta3'
30
30
  require 'active_record'
31
31
  end
32
32
 
33
- # Arel files we override
34
- require 'arel/visitors/to_sql'
35
-
36
33
  # AR files we override
37
34
  require 'active_record/counter_cache'
38
35
  require 'active_record/fixtures'
@@ -41,6 +38,7 @@ require 'active_record/persistence'
41
38
  require 'active_record/relation'
42
39
  require 'active_record/sanitization'
43
40
  require 'active_record/attribute_methods'
41
+ require 'active_record/autosave_association'
44
42
 
45
43
  require 'active_record/associations/association'
46
44
  require 'active_record/associations/association_scope'
@@ -53,7 +51,6 @@ require 'active_record/associations/preloader/belongs_to'
53
51
  require 'active_record/associations/singular_association'
54
52
  require 'active_record/associations/collection_association'
55
53
 
56
- require 'active_record/attribute_set/builder'
57
54
  require 'active_record/attribute_methods/primary_key'
58
55
  require 'active_record/attribute_methods/read'
59
56
  require 'active_record/attribute_methods/write'
@@ -64,16 +61,13 @@ require 'active_record/connection_adapters/abstract_adapter'
64
61
  require 'active_record/connection_adapters/abstract_mysql_adapter'
65
62
 
66
63
  require 'active_record/relation/batches'
64
+ require 'active_record/relation/where_clause'
67
65
  require 'active_record/relation/calculations'
68
66
  require 'active_record/relation/finder_methods'
69
67
  require 'active_record/relation/predicate_builder'
70
68
  require 'active_record/relation/query_methods'
71
69
 
72
- require 'active_record/validations/uniqueness'
73
-
74
70
  # CPK files
75
- require 'composite_primary_keys/arel/visitors/to_sql'
76
-
77
71
  require 'composite_primary_keys/persistence'
78
72
  require 'composite_primary_keys/base'
79
73
  require 'composite_primary_keys/core'
@@ -82,8 +76,8 @@ require 'composite_primary_keys/composite_predicates'
82
76
  require 'composite_primary_keys/fixtures'
83
77
  require 'composite_primary_keys/relation'
84
78
  require 'composite_primary_keys/sanitization'
85
- require 'composite_primary_keys/attribute_set/builder'
86
79
  require 'composite_primary_keys/attribute_methods'
80
+ require 'composite_primary_keys/autosave_association'
87
81
  require 'composite_primary_keys/version'
88
82
 
89
83
  require 'composite_primary_keys/associations/association'
@@ -94,14 +88,9 @@ require 'composite_primary_keys/associations/join_dependency'
94
88
  require 'composite_primary_keys/associations/join_dependency/join_association'
95
89
  require 'composite_primary_keys/associations/preloader/association'
96
90
  require 'composite_primary_keys/associations/preloader/belongs_to'
97
- require 'composite_primary_keys/associations/singular_association'
98
91
  require 'composite_primary_keys/associations/collection_association'
99
92
 
100
- require 'composite_primary_keys/dirty'
101
- require 'composite_primary_keys/autosave_association'
102
-
103
93
  require 'composite_primary_keys/attribute_methods/primary_key'
104
- require 'composite_primary_keys/attribute_methods/dirty'
105
94
  require 'composite_primary_keys/attribute_methods/read'
106
95
  require 'composite_primary_keys/attribute_methods/write'
107
96
  require 'composite_primary_keys/locking/optimistic'
@@ -109,14 +98,16 @@ require 'composite_primary_keys/nested_attributes'
109
98
 
110
99
  require 'composite_primary_keys/connection_adapters/abstract_adapter'
111
100
  require 'composite_primary_keys/connection_adapters/abstract_mysql_adapter'
112
- require 'composite_primary_keys/connection_adapters/abstract/connection_specification_changes'
113
101
 
114
102
  require 'composite_primary_keys/relation/batches'
103
+ require 'composite_primary_keys/relation/where_clause'
115
104
  require 'composite_primary_keys/relation/calculations'
116
105
  require 'composite_primary_keys/relation/finder_methods'
117
106
  require 'composite_primary_keys/relation/predicate_builder'
118
107
  require 'composite_primary_keys/relation/query_methods'
119
108
 
120
- require 'composite_primary_keys/validations/uniqueness'
121
-
122
109
  require 'composite_primary_keys/composite_relation'
110
+
111
+ require 'composite_primary_keys/arel/in'
112
+ require 'composite_primary_keys/arel/to_sql'
113
+ require 'composite_primary_keys/arel/sqlserver'
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)