composite_primary_keys 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,7 @@
1
+ * 0.1.4 *
2
+ - it was important that #{primary_key} for composites --> 'key1,key2' and not 'key1key2'
3
+ so created PrimaryKeys class
4
+
1
5
  * 0.0.1 * Initial version
2
6
  - set_primary_keys(*keys) is the activation class method to transform an ActiveRecord into
3
7
  a composite primary key AR
@@ -37,5 +37,5 @@ end
37
37
  require 'composite_primary_keys/base'
38
38
 
39
39
  ActiveRecord::Base.class_eval do
40
- include CompositePrimayKeys::ActiveRecord::Base
40
+ include CompositePrimaryKeys::ActiveRecord::Base
41
41
  end
@@ -1,29 +1,38 @@
1
- module CompositePrimayKeys
1
+ module CompositePrimaryKeys
2
2
  module ActiveRecord #:nodoc:
3
3
  module Base #:nodoc:
4
4
 
5
5
  INVALID_FOR_COMPOSITE_KEYS = 'Not appropriate for composite primary keys'
6
- ID_SEP = ','
7
6
 
8
7
  def self.append_features(base)
9
8
  super
9
+ base.send(:include, InstanceMethods)
10
10
  base.extend(ClassMethods)
11
11
  end
12
12
 
13
13
  module ClassMethods
14
14
  def set_primary_keys(*keys)
15
- @@primary_keys = []
16
15
  cattr_accessor :primary_keys
17
- self.primary_keys = keys
16
+ self.primary_keys = CompositePrimaryKeys::PrimaryKeys.new(keys)
18
17
 
19
18
  class_eval <<-EOV
20
- include CompositePrimayKeys::ActiveRecord::Base::InstanceMethods
21
- extend CompositePrimayKeys::ActiveRecord::Base::CompositeClassMethods
19
+ include CompositePrimaryKeys::ActiveRecord::Base::CompositeInstanceMethods
20
+ extend CompositePrimaryKeys::ActiveRecord::Base::CompositeClassMethods
22
21
  EOV
22
+
23
+ #puts "#{self.class}-#{self}.composite = #{self.composite?}"
24
+ end
25
+
26
+ def composite?
27
+ false
23
28
  end
24
29
  end
30
+
31
+ module InstanceMethods
32
+ def composite?; self.class.composite?; end
33
+ end
25
34
 
26
- module InstanceMethods
35
+ module CompositeInstanceMethods
27
36
 
28
37
  # A model instance's primary keys is always available as model.ids
29
38
  # whether you name it the default 'id' or set it to something else.
@@ -35,7 +44,7 @@ module CompositePrimayKeys
35
44
 
36
45
  #id_to_s([1,2]) -> "1,2"
37
46
  #id_to_s([1,2], '-') -> "1-2"
38
- def id_to_s(ids, id_sep = CompositePrimayKeys::ActiveRecord::Base::ID_SEP)
47
+ def id_to_s(ids, id_sep = CompositePrimaryKeys::ID_SEP)
39
48
  ids.map{|id| self.class.sanitize(id)}.join("#{id_sep}")
40
49
  end
41
50
 
@@ -104,14 +113,16 @@ module CompositePrimayKeys
104
113
  end
105
114
 
106
115
  module CompositeClassMethods
116
+ def primary_key; primary_keys; end
117
+ def primary_key=(keys); primary_keys = keys; end
107
118
 
108
- def primary_keys_to_s(sep = CompositePrimayKeys::ActiveRecord::Base::ID_SEP)
109
- primary_keys.map(&:to_s).join(sep)
119
+ def composite?
120
+ true
110
121
  end
111
122
 
112
123
  #ids_to_s([[1,2],[7,3]]) -> "(1,2),(7,3)"
113
124
  #ids_to_s([[1,2],[7,3]], ',', ';', '', '') -> "1,2;7,3"
114
- def ids_to_s(ids, id_sep = CompositePrimayKeys::ActiveRecord::Base::ID_SEP, list_sep = ',', left_bracket = '(', right_bracket = ')')
125
+ def ids_to_s(ids, id_sep = CompositePrimaryKeys::ID_SEP, list_sep = ',', left_bracket = '(', right_bracket = ')')
115
126
  "#{left_bracket}#{ids.map{|id| sanitize(id)}.join('#{id_sep}')}#{right_bracket}"
116
127
  end
117
128
 
@@ -127,7 +138,7 @@ module CompositePrimayKeys
127
138
  # If an array of ids is provided (e.g. delete([1,2], [3,4]), all of them
128
139
  # are deleted.
129
140
  def delete(*ids)
130
- delete_all([ "(#{primary_keys_to_s}) IN (#{ids_to_s(ids)})" ])
141
+ delete_all([ "(#{primary_keys}) IN (#{ids_to_s(ids)})" ])
131
142
  end
132
143
 
133
144
  # Destroys the record with the given +ids+ by instantiating the object and calling #destroy (all the callbacks are the triggered).
@@ -136,13 +147,6 @@ module CompositePrimayKeys
136
147
  ids.first.is_a?(Array) ? ids.each { |id_set| destroy(id_set) } : find(ids).destroy
137
148
  end
138
149
 
139
- # Alias for the composite primary_keys accessor method
140
- def primary_key
141
- raise CompositePrimayKeys::ActiveRecord::Base::INVALID_FOR_COMPOSITE_KEYS
142
- # primary_keys
143
- # Initially invalidate the method to find places where its used
144
- end
145
-
146
150
  # Returns an array of column objects for the table associated with this class.
147
151
  # Each column that matches to one of the primary keys has its
148
152
  # primary attribute set to true
@@ -159,24 +163,24 @@ module CompositePrimayKeys
159
163
  # Lazy-set the sequence name to the connection's default. This method
160
164
  # is only ever called once since set_sequence_name overrides it.
161
165
  def sequence_name #:nodoc:
162
- raise CompositePrimayKeys::ActiveRecord::Base::INVALID_FOR_COMPOSITE_KEYS
166
+ raise CompositePrimaryKeys::ActiveRecord::Base::INVALID_FOR_COMPOSITE_KEYS
163
167
  end
164
168
 
165
169
  def reset_sequence_name #:nodoc:
166
- raise CompositePrimayKeys::ActiveRecord::Base::INVALID_FOR_COMPOSITE_KEYS
170
+ raise CompositePrimaryKeys::ActiveRecord::Base::INVALID_FOR_COMPOSITE_KEYS
167
171
  end
168
172
 
169
173
  def set_primary_key(value = nil, &block)
170
- raise CompositePrimayKeys::ActiveRecord::Base::INVALID_FOR_COMPOSITE_KEYS
174
+ raise CompositePrimaryKeys::ActiveRecord::Base::INVALID_FOR_COMPOSITE_KEYS
171
175
  end
172
176
 
173
177
  private
174
178
  def find_one(id, options)
175
- raise CompositePrimayKeys::ActiveRecord::Base::INVALID_FOR_COMPOSITE_KEYS
179
+ raise CompositePrimaryKeys::ActiveRecord::Base::INVALID_FOR_COMPOSITE_KEYS
176
180
  end
177
181
 
178
182
  def find_some(ids, options)
179
- raise CompositePrimayKeys::ActiveRecord::Base::INVALID_FOR_COMPOSITE_KEYS
183
+ raise CompositePrimaryKeys::ActiveRecord::Base::INVALID_FOR_COMPOSITE_KEYS
180
184
  end
181
185
 
182
186
  def find_from_ids(ids, options)
@@ -0,0 +1,10 @@
1
+ module CompositePrimaryKeys
2
+ ID_SEP = ','
3
+
4
+ class PrimaryKeys < Array
5
+
6
+ def to_s
7
+ join(ID_SEP)
8
+ end
9
+ end
10
+ end
@@ -2,7 +2,7 @@ module CompositePrimayKeys
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 0
4
4
  MINOR = 1
5
- TINY = 3
5
+ TINY = 4
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
@@ -49,6 +49,29 @@ class Test::Unit::TestCase #:nodoc:
49
49
  def assert_no_queries(&block)
50
50
  assert_queries(0, &block)
51
51
  end
52
+
53
+ cattr_accessor :classes
54
+ protected
55
+
56
+ def testing_with(&block)
57
+ classes.keys.each do |@key_test|
58
+ @klass, @primary_keys = classes[@key_test][:class], classes[@key_test][:primary_keys]
59
+ @first = @klass.find_first
60
+ yield
61
+ end
62
+ end
63
+
64
+ def first_id
65
+ (1..@primary_keys.length).map {|num| 1}
66
+ end
67
+
68
+ def first_id_str
69
+ first_id.join(CompositePrimaryKeys::ID_SEP)
70
+ end
71
+
72
+ def composite?
73
+ @key_test != :single
74
+ end
52
75
  end
53
76
 
54
77
  def current_adapter?(type)
@@ -1,4 +1,6 @@
1
1
  require 'abstract_unit'
2
+ require 'fixtures/reference_type'
3
+ require 'fixtures/reference_code'
2
4
 
3
5
  class DummyTest < Test::Unit::TestCase
4
6
 
@@ -6,7 +6,7 @@ require 'fixtures/reference_code'
6
6
  class FindTest < Test::Unit::TestCase
7
7
  fixtures :reference_types, :reference_codes
8
8
 
9
- CLASSES = {
9
+ @@classes = {
10
10
  :single => {
11
11
  :class => ReferenceType,
12
12
  :primary_keys => [:reference_type_id],
@@ -17,16 +17,6 @@ class FindTest < Test::Unit::TestCase
17
17
  }
18
18
  }
19
19
 
20
- def test_primary_keys
21
- testing_with do
22
- if composite?
23
- assert_equal @primary_keys, @klass.primary_keys
24
- else
25
- assert_equal @primary_keys, [@klass.primary_key.to_sym]
26
- end
27
- end
28
- end
29
-
30
20
  def test_find_first
31
21
  testing_with do
32
22
  obj = @klass.find_first
@@ -64,25 +54,4 @@ class FindTest < Test::Unit::TestCase
64
54
  end
65
55
  end
66
56
 
67
- protected
68
-
69
- def testing_with(&block)
70
- CLASSES.keys.each do |@key_test|
71
- @klass, @primary_keys = CLASSES[@key_test][:class], CLASSES[@key_test][:primary_keys]
72
- @first = @klass.find_first
73
- yield
74
- end
75
- end
76
-
77
- def first_id
78
- (1..@primary_keys.length).map {|num| 1}
79
- end
80
-
81
- def first_id_str
82
- first_id.join(',')
83
- end
84
-
85
- def composite?
86
- @key_test != :single
87
- end
88
57
  end
@@ -0,0 +1,33 @@
1
+ require 'abstract_unit'
2
+ require 'fixtures/reference_type'
3
+ require 'fixtures/reference_code'
4
+
5
+ class MiscellaneousTest < Test::Unit::TestCase
6
+
7
+ def test_composite_class
8
+ testing_with do
9
+ assert_equal composite?, @klass.composite?
10
+ end
11
+ end
12
+
13
+ def test_composite_instance
14
+ testing_with do
15
+ assert_equal composite?, @first.composite?
16
+ end
17
+ end
18
+
19
+ def test_primary_keys
20
+ testing_with do
21
+ if composite?
22
+ assert_not_nil @klass.primary_keys
23
+ assert_equal @primary_keys, @klass.primary_keys
24
+ assert_equal @klass.primary_keys, @klass.primary_key
25
+ else
26
+ assert_not_nil @klass.primary_key
27
+ assert_equal @primary_keys, [@klass.primary_key.to_sym]
28
+ end
29
+ assert_equal @primary_keys.join(','), @klass.primary_key.to_s
30
+ # Need a :primary_keys should be Array with to_s overridden
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,31 @@
1
+ require 'abstract_unit'
2
+ require 'fixtures/reference_type'
3
+ require 'fixtures/reference_code'
4
+ require 'action_controller/pagination'
5
+
6
+ class PaginationTest < Test::Unit::TestCase
7
+ include ActionController::Pagination
8
+ DEFAULT_PAGE_SIZE = 2
9
+
10
+ @@classes = {
11
+ :single => {
12
+ :class => ReferenceType,
13
+ :primary_keys => [:reference_type_id],
14
+ },
15
+ :dual => {
16
+ :class => ReferenceCode,
17
+ :primary_keys => [:reference_type_id, :reference_code],
18
+ }
19
+ }
20
+
21
+ def setup
22
+ @params = {}
23
+ end
24
+
25
+ def test_paginate_all
26
+ testing_with do
27
+ @object_pages, @objects = paginate :reference_codes, :per_page => DEFAULT_PAGE_SIZE
28
+ assert_equal 2, @objects.length, "Each page should have #{DEFAULT_PAGE_SIZE} items"
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,20 @@
1
+ require 'abstract_unit'
2
+ require 'fixtures/reference_type'
3
+ require 'fixtures/reference_code'
4
+
5
+ class PrimaryKeyTest < Test::Unit::TestCase
6
+
7
+ def test_new
8
+ keys = CompositePrimaryKeys::PrimaryKeys.new
9
+ assert_not_nil keys
10
+ assert_equal '', keys.to_s
11
+ assert_equal '', "#{keys}"
12
+ end
13
+
14
+ def test_initialize
15
+ keys = CompositePrimaryKeys::PrimaryKeys.new([1,2,3])
16
+ assert_not_nil keys
17
+ assert_equal '1,2,3', keys.to_s
18
+ assert_equal '1,2,3', "#{keys}"
19
+ end
20
+ end
metadata CHANGED
@@ -3,7 +3,7 @@ rubygems_version: 0.8.11
3
3
  specification_version: 1
4
4
  name: composite_primary_keys
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.1.3
6
+ version: 0.1.4
7
7
  date: 2006-07-22 00:00:00 +02:00
8
8
  summary: Support for composite primary keys in ActiveRecords
9
9
  require_paths:
@@ -37,11 +37,15 @@ files:
37
37
  - lib/composite_primary_keys/version.rb
38
38
  - lib/composite_primary_keys/base.rb
39
39
  - lib/composite_primary_keys/fixtures.rb
40
+ - lib/composite_primary_keys/primary_keys.rb
40
41
  - test/connections
41
42
  - test/abstract_unit.rb
42
43
  - test/dummy_test.rb
43
44
  - test/fixtures
44
45
  - test/find_test.rb
46
+ - test/pagination_test.rb
47
+ - test/miscellaneous_test.rb
48
+ - test/primary_keys_test.rb
45
49
  - test/connections/native_mysql
46
50
  - test/connections/native_mysql/connection.rb
47
51
  - test/fixtures/reference_type.rb