composite_primary_keys 3.0.7 → 3.0.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,8 @@
1
+ == 3.0.8 2010-12-04
2
+ * Fix for translation issue in unique validation. Thanks to Daniel Berger for the patch.
3
+ * Fix to support child classes of composite primary key models
4
+
5
+
1
6
  == 3.0.7 2010-11-29
2
7
  * Fix has and belongs to many associations implementation.
3
8
 
@@ -23,11 +23,6 @@ module ActiveRecord
23
23
  include CompositeInstanceMethods
24
24
  include CompositePrimaryKeys::ActiveRecord::AssociationPreload
25
25
  EOV
26
-
27
- class << unscoped
28
- include CompositePrimaryKeys::ActiveRecord::FinderMethods::InstanceMethods
29
- include CompositePrimaryKeys::ActiveRecord::Relation::InstanceMethods
30
- end
31
26
  end
32
27
 
33
28
  def composite?
@@ -89,6 +84,20 @@ module ActiveRecord
89
84
  def ids_to_s(many_ids, id_sep = CompositePrimaryKeys::ID_SEP, list_sep = ',', left_bracket = '(', right_bracket = ')')
90
85
  many_ids.map {|ids| "#{left_bracket}#{CompositePrimaryKeys::CompositeKeys.new(ids)}#{right_bracket}"}.join(list_sep)
91
86
  end
87
+
88
+ def relation #:nodoc:
89
+ @relation ||= begin
90
+ result = Relation.new(self, arel_table)
91
+ # CPK
92
+ class << result
93
+ include CompositePrimaryKeys::ActiveRecord::FinderMethods::InstanceMethods
94
+ include CompositePrimaryKeys::ActiveRecord::Relation::InstanceMethods
95
+ end
96
+ result
97
+ end
98
+
99
+ finder_needs_type_condition? ? @relation.where(type_condition) : @relation
100
+ end
92
101
  end
93
102
 
94
103
  module CompositeInstanceMethods
@@ -6,6 +6,11 @@ module ActiveRecord
6
6
  table = finder_class.unscoped
7
7
 
8
8
  table_name = record.class.quoted_table_name
9
+
10
+ if value && record.class.serialized_attributes.key?(attribute.to_s)
11
+ value = YAML.dump value
12
+ end
13
+
9
14
  sql, params = mount_sql_and_params(finder_class, table_name, attribute, value)
10
15
 
11
16
  relation = table.where(sql, *params)
@@ -15,7 +20,7 @@ module ActiveRecord
15
20
  relation = relation.where(scope_item => scope_value)
16
21
  end
17
22
 
18
- unless record.new_record?
23
+ if record.persisted?
19
24
  # CPK
20
25
  if record.composite?
21
26
  predicate = nil
@@ -31,87 +36,9 @@ module ActiveRecord
31
36
  end
32
37
 
33
38
  if relation.exists?
34
- record.errors.add(attribute, :taken, :default => options[:message], :value => value)
39
+ record.errors.add(attribute, :taken, options.except(:case_sensitive, :scope).merge(:value => value))
35
40
  end
36
41
  end
37
42
  end
38
43
  end
39
- end
40
-
41
- #module CompositePrimaryKeys
42
- # module ActiveRecord
43
- # module Validations
44
- # module Uniqueness
45
- # module ClassMethods
46
- # def validates_uniqueness_of(*attr_names)
47
- # configuration = { :case_sensitive => true }
48
- # configuration.update(attr_names.extract_options!)
49
- #
50
- # validates_each(attr_names,configuration) do |record, attr_name, value|
51
- # # The check for an existing value should be run from a class that
52
- # # isn't abstract. This means working down from the current class
53
- # # (self), to the first non-abstract class. Since classes don't know
54
- # # their subclasses, we have to build the hierarchy between self and
55
- # # the record's class.
56
- # class_hierarchy = [record.class]
57
- # while class_hierarchy.first != self
58
- # class_hierarchy.insert(0, class_hierarchy.first.superclass)
59
- # end
60
- #
61
- # # Now we can work our way down the tree to the first non-abstract
62
- # # class (which has a database table to query from).
63
- # finder_class = class_hierarchy.detect { |klass| !klass.abstract_class? }
64
- #
65
- # column = finder_class.columns_hash[attr_name.to_s]
66
- #
67
- # if value.nil?
68
- # comparison_operator = "IS ?"
69
- # elsif column.text?
70
- # comparison_operator = "#{connection.case_sensitive_equality_operator} ?"
71
- # value = column.limit ? value.to_s.mb_chars[0, column.limit] : value.to_s
72
- # else
73
- # comparison_operator = "= ?"
74
- # end
75
- #
76
- # sql_attribute = "#{record.class.quoted_table_name}.#{connection.quote_column_name(attr_name)}"
77
- #
78
- # if value.nil? || (configuration[:case_sensitive] || !column.text?)
79
- # condition_sql = "#{sql_attribute} #{comparison_operator}"
80
- # condition_params = [value]
81
- # else
82
- # condition_sql = "LOWER(#{sql_attribute}) #{comparison_operator}"
83
- # condition_params = [value.mb_chars.downcase]
84
- # end
85
- #
86
- # if scope = configuration[:scope]
87
- # Array(scope).map do |scope_item|
88
- # scope_value = record.send(scope_item)
89
- # condition_sql << " AND " << attribute_condition("#{record.class.quoted_table_name}.#{scope_item}", scope_value)
90
- # condition_params << scope_value
91
- # end
92
- # end
93
- #
94
- # unless record.new_record?
95
- # if record.class.composite?
96
- # record.class.primary_keys.each do |key|
97
- # condition_sql << " AND #{record.class.quoted_table_name}.#{key} <> ?"
98
- # condition_params << record.send(key)
99
- # end
100
- # else
101
- # condition_sql << " AND #{record.class.quoted_table_name}.#{record.class.primary_key} <> ?"
102
- # condition_params << record.send(:id)
103
- # end
104
- # end
105
- #
106
- # finder_class.with_exclusive_scope do
107
- # if finder_class.exists?([condition_sql, *condition_params])
108
- # record.errors.add(attr_name, :taken, :default => configuration[:message], :value => value)
109
- # end
110
- # end
111
- # end
112
- # end
113
- # end
114
- # end
115
- # end
116
- # end
117
- #end
44
+ end
@@ -2,7 +2,7 @@ module CompositePrimaryKeys
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 3
4
4
  MINOR = 0
5
- TINY = 7
5
+ TINY = 8
6
6
  STRING = [MAJOR, MINOR, TINY].join('.')
7
7
  end
8
8
  end
@@ -558,4 +558,18 @@ PGError: ERROR: operator does not exist: character varying = integer
558
558
  LINE 1: ...ments".person_type = 'User') AND ("comments".person_id = 1))
559
559
  ^
560
560
  HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
561
+ : SELECT "hacks".* FROM "hacks" INNER JOIN "comments" ON ("hacks"."name" = "comments"."hack_id") WHERE (("comments".person_type = 'User') AND ("comments".person_id = 1))
562
+ SQL (35.0ms) SHOW client_min_messages
563
+ SQL (0.0ms) SET client_min_messages TO 'panic'
564
+ SQL (0.0ms) SET standard_conforming_strings = on
565
+ SQL (1.0ms) SET client_min_messages TO 'notice'
566
+ SQL (0.0ms) SHOW TIME ZONE
567
+ PGError: ERROR: column "tariff_idstart_date" does not exist
568
+ LINE 1: SELECT COUNT(DISTINCT tariff_idstart_date) FROM "tariffs" LE...
569
+ ^
570
+ : SELECT COUNT(DISTINCT tariff_idstart_date) FROM "tariffs" LEFT OUTER JOIN "product_tariffs" ON "product_tariffs"."tariff_id" = "tariffs"."tariff_id" AND "product_tariffs"."tariff_start_date" = "tariffs"."start_date"
571
+ PGError: ERROR: operator does not exist: character varying = integer
572
+ LINE 1: ...ments".person_type = 'User') AND ("comments".person_id = 1))
573
+ ^
574
+ HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
561
575
  : SELECT "hacks".* FROM "hacks" INNER JOIN "comments" ON ("hacks"."name" = "comments"."hack_id") WHERE (("comments".person_type = 'User') AND ("comments".person_id = 1))
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: composite_primary_keys
3
3
  version: !ruby/object:Gem::Version
4
- hash: 9
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
7
  - 3
8
8
  - 0
9
- - 7
10
- version: 3.0.7
9
+ - 8
10
+ version: 3.0.8
11
11
  platform: ruby
12
12
  authors:
13
13
  - Dr Nic Williams
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2010-11-29 00:00:00 -07:00
19
+ date: 2010-12-04 00:00:00 -07:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency