composite_primary_keys 11.3.1 → 12.0.0.rc1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. checksums.yaml +4 -4
  2. data/History.rdoc +17 -7
  3. data/README.rdoc +1 -0
  4. data/lib/composite_primary_keys.rb +1 -6
  5. data/lib/composite_primary_keys/arel/to_sql.rb +13 -13
  6. data/lib/composite_primary_keys/associations/association_scope.rb +4 -4
  7. data/lib/composite_primary_keys/associations/has_many_association.rb +2 -1
  8. data/lib/composite_primary_keys/associations/has_many_through_association.rb +0 -2
  9. data/lib/composite_primary_keys/associations/join_dependency.rb +9 -8
  10. data/lib/composite_primary_keys/associations/preloader/association.rb +28 -36
  11. data/lib/composite_primary_keys/attribute_methods.rb +2 -2
  12. data/lib/composite_primary_keys/attribute_methods/read.rb +11 -14
  13. data/lib/composite_primary_keys/attribute_methods/write.rb +8 -11
  14. data/lib/composite_primary_keys/base.rb +2 -2
  15. data/lib/composite_primary_keys/composite_arrays.rb +7 -50
  16. data/lib/composite_primary_keys/connection_adapters/abstract_adapter.rb +1 -1
  17. data/lib/composite_primary_keys/connection_adapters/postgresql/database_statements.rb +1 -1
  18. data/lib/composite_primary_keys/core.rb +10 -14
  19. data/lib/composite_primary_keys/counter_cache.rb +4 -22
  20. data/lib/composite_primary_keys/fixtures.rb +5 -10
  21. data/lib/composite_primary_keys/nested_attributes.rb +6 -6
  22. data/lib/composite_primary_keys/persistence.rb +8 -8
  23. data/lib/composite_primary_keys/reflection.rb +3 -3
  24. data/lib/composite_primary_keys/relation.rb +39 -27
  25. data/lib/composite_primary_keys/relation/calculations.rb +15 -18
  26. data/lib/composite_primary_keys/relation/finder_methods.rb +56 -58
  27. data/lib/composite_primary_keys/relation/query_methods.rb +4 -3
  28. data/lib/composite_primary_keys/relation/where_clause.rb +2 -2
  29. data/lib/composite_primary_keys/sanitization.rb +37 -37
  30. data/lib/composite_primary_keys/validations/uniqueness.rb +2 -2
  31. data/lib/composite_primary_keys/version.rb +4 -4
  32. data/tasks/databases/mysql.rake +1 -1
  33. data/test/abstract_unit.rb +1 -5
  34. data/test/connections/databases.ci.yml +3 -0
  35. data/test/db_test.rb +53 -0
  36. data/test/fixtures/db_definitions/mysql.sql +1 -7
  37. data/test/fixtures/db_definitions/oracle.drop.sql +1 -3
  38. data/test/fixtures/db_definitions/oracle.sql +0 -8
  39. data/test/fixtures/db_definitions/postgresql.sql +0 -6
  40. data/test/fixtures/db_definitions/sqlite.sql +0 -6
  41. data/test/fixtures/db_definitions/sqlserver.sql +1 -8
  42. data/test/test_associations.rb +1 -1
  43. data/test/test_composite_arrays.rb +0 -14
  44. data/test/test_create.rb +12 -26
  45. data/test/test_find.rb +0 -8
  46. data/test/test_ids.rb +0 -3
  47. data/test/test_nested_attributes.rb +10 -10
  48. data/test/test_update.rb +1 -1
  49. metadata +9 -11
  50. data/lib/composite_primary_keys/connection_adapters/abstract_mysql_adapter.rb +0 -20
  51. data/lib/composite_primary_keys/connection_adapters/sqlite3_adapter.rb +0 -23
  52. data/test/fixtures/cpk_with_default_value.rb +0 -3
  53. data/test/fixtures/cpk_with_default_values.yml +0 -7
@@ -4,7 +4,7 @@ module ActiveRecord
4
4
  def quote_column_names(name)
5
5
  Array(name).map do |col|
6
6
  quote_column_name(col.to_s)
7
- end.to_composite_keys.to_s
7
+ end.join(CompositePrimaryKeys::ID_SEP)
8
8
  end
9
9
  end
10
10
  end
@@ -2,7 +2,7 @@ module ActiveRecord
2
2
  module ConnectionAdapters
3
3
  module PostgreSQL
4
4
  module DatabaseStatements
5
- def sql_for_insert(sql, pk, id_value, sequence_name, binds) # :nodoc:
5
+ def sql_for_insert(sql, pk, binds) # :nodoc:
6
6
  if pk.nil?
7
7
  # Extract the table from the insert sql. Yuck.
8
8
  table_ref = extract_table_ref_from_insert_sql(sql)
@@ -6,13 +6,12 @@ module ActiveRecord
6
6
  # @attributes.reset(self.class.primary_key)
7
7
  Array(self.class.primary_key).each {|key| @attributes.reset(key)}
8
8
 
9
- run_callbacks(:initialize) unless _initialize_callbacks.empty?
9
+ _run_initialize_callbacks
10
10
 
11
- @aggregation_cache = {}
12
- @association_cache = {}
13
-
14
- @new_record = true
15
- @destroyed = false
11
+ @new_record = true
12
+ @destroyed = false
13
+ @_start_transaction_state = {}
14
+ @transaction_state = nil
16
15
 
17
16
  super
18
17
  end
@@ -22,9 +21,9 @@ module ActiveRecord
22
21
  # We don't have cache keys for this stuff yet
23
22
  return super unless ids.length == 1
24
23
  return super if block_given? ||
25
- primary_key.nil? ||
26
- scope_attributes? ||
27
- columns_hash.include?(inheritance_column)
24
+ primary_key.nil? ||
25
+ scope_attributes? ||
26
+ columns_hash.include?(inheritance_column)
28
27
 
29
28
  # CPK
30
29
  return super if self.composite?
@@ -39,16 +38,13 @@ module ActiveRecord
39
38
  where(key => params.bind).limit(1)
40
39
  }
41
40
 
42
- record = statement.execute([id], connection).first
41
+ record = statement.execute([id], connection)&.first
43
42
  unless record
44
43
  raise RecordNotFound.new("Couldn't find #{name} with '#{primary_key}'=#{id}",
45
44
  name, primary_key, id)
46
45
  end
47
46
  record
48
- rescue ::RangeError
49
- raise RecordNotFound.new("Couldn't find #{name} with an out of range value for '#{primary_key}'",
50
- name, primary_key)
51
47
  end
52
48
  end
53
49
  end
54
- end
50
+ end
@@ -2,31 +2,13 @@ module ActiveRecord
2
2
  module CounterCache
3
3
  module ClassMethods
4
4
  def update_counters(id, counters)
5
- touch = counters.delete(:touch)
6
-
7
- updates = counters.map do |counter_name, value|
8
- operator = value < 0 ? "-" : "+"
9
- quoted_column = connection.quote_column_name(counter_name)
10
- "#{quoted_column} = COALESCE(#{quoted_column}, 0) #{operator} #{value.abs}"
11
- end
12
-
13
- if touch
14
- names = touch if touch != true
15
- touch_updates = touch_attributes_with_time(*names)
16
- updates << sanitize_sql_for_assignment(touch_updates) unless touch_updates.empty?
17
- end
18
-
19
- if id.is_a?(Relation) && self == id.klass
20
- relation = id
21
5
  # CPK
22
- elsif primary_key.is_a?(Array)
23
- predicate = self.cpk_id_predicate(self.arel_table, self.primary_key, id)
24
- relation = unscoped.where!(predicate)
6
+ if self.composite?
7
+ predicate = cpk_id_predicate(self.arel_table, primary_key, id)
8
+ unscoped.where!(predicate).update_counters(counters)
25
9
  else
26
- relation = unscoped.where!(primary_key => id)
10
+ unscoped.where!(primary_key => id).update_counters(counters)
27
11
  end
28
-
29
- relation.update_all updates.join(", ")
30
12
  end
31
13
  end
32
14
  end
@@ -1,17 +1,12 @@
1
1
  module ActiveRecord
2
2
  class Fixture
3
3
  def find
4
- if model_class
4
+ raise FixtureClassNotFound, "No class attached to find." unless model_class
5
+ model_class.unscoped do
5
6
  # CPK
6
- # model_class.unscoped do
7
- # model_class.find(fixture[model_class.primary_key])
8
- # end
9
- model_class.unscoped do
10
- ids = self.ids(model_class.primary_key)
11
- model_class.find(ids)
12
- end
13
- else
14
- raise FixtureClassNotFound, "No class attached to find."
7
+ #model_class.find(fixture[model_class.primary_key])
8
+ ids = self.ids(model_class.primary_key)
9
+ model_class.find(ids)
15
10
  end
16
11
  end
17
12
 
@@ -56,17 +56,17 @@ module ActiveRecord
56
56
  attributes = attributes.to_h
57
57
  end
58
58
  attributes = attributes.with_indifferent_access
59
- if attributes['id'].blank?
59
+
60
+ if attributes["id"].blank?
60
61
  unless reject_new_record?(association_name, attributes)
61
- association.build(attributes.except(*UNASSIGNABLE_KEYS))
62
+ association.reader.build(attributes.except(*UNASSIGNABLE_KEYS))
62
63
  end
63
- elsif existing_record = cpk_detect_record(attributes['id'], existing_records)
64
+ elsif existing_record = existing_records.detect { |record| record.id.to_s == attributes["id"].to_s }
64
65
  unless call_reject_if(association_name, attributes)
65
66
  # Make sure we are operating on the actual object which is in the association's
66
67
  # proxy_target array (either by finding it, or adding it if not found)
67
68
  # Take into account that the proxy_target may have changed due to callbacks
68
- target_record = cpk_detect_record(attributes['id'], association.target)
69
-
69
+ target_record = association.target.detect { |record| record.id.to_s == attributes["id"].to_s }
70
70
  if target_record
71
71
  existing_record = target_record
72
72
  else
@@ -76,7 +76,7 @@ module ActiveRecord
76
76
  assign_to_or_mark_for_destruction(existing_record, attributes, options[:allow_destroy])
77
77
  end
78
78
  else
79
- raise_nested_attributes_record_not_found!(association_name, attributes['id'])
79
+ raise_nested_attributes_record_not_found!(association_name, attributes["id"])
80
80
  end
81
81
  end
82
82
  end
@@ -13,10 +13,10 @@ module ActiveRecord
13
13
  id_or_array.each do |id|
14
14
  # Is the passed in id actually a record?
15
15
  id = id.kind_of?(::ActiveRecord::Base) ? id.id : id
16
- where(cpk_id_predicate(self.arel_table, self.primary_key, id)).delete_all
16
+ delete_by(cpk_id_predicate(self.arel_table, self.primary_key, id))
17
17
  end
18
18
  else
19
- where(primary_key => id_or_array).delete_all
19
+ delete_by(primary_key => id_or_array)
20
20
  end
21
21
  end
22
22
 
@@ -58,15 +58,15 @@ module ActiveRecord
58
58
  end
59
59
 
60
60
  def _create_record(attribute_names = self.attribute_names)
61
- attribute_names &= self.class.column_names
62
- attributes_values = attributes_with_values_for_create(attribute_names)
61
+ attribute_names = attributes_for_create(attribute_names)
63
62
 
64
- new_id = self.class._insert_record(attributes_values)
63
+ new_id = self.class._insert_record(
64
+ attributes_with_values(attribute_names)
65
+ )
65
66
 
66
67
  # CPK
67
- if self.composite?
68
- # Merge together the specified id with the new id (specified id gets precedence)
69
- self.id = self.id.zip(Array(new_id)).map {|id1, id2| (id1 || id2)}
68
+ if self.composite? && self.id.compact.empty?
69
+ self.id = new_id
70
70
  else
71
71
  self.id ||= new_id if self.class.primary_key
72
72
  end
@@ -1,7 +1,6 @@
1
1
  module ActiveRecord
2
2
  module Reflection
3
3
  class AbstractReflection
4
- # Overriding for activerecord v5.2.4
5
4
  def join_scope(table, foreign_table, foreign_klass)
6
5
  predicate_builder = predicate_builder(table)
7
6
  scope_chain_items = join_scopes(table, predicate_builder)
@@ -11,8 +10,9 @@ module ActiveRecord
11
10
  foreign_key = join_keys.foreign_key
12
11
 
13
12
  # CPK
14
- # klass_scope.where!(table[key].eq(foreign_table[foreign_key]))
15
- klass_scope.where!(cpk_join_predicate(table, key, foreign_table, foreign_key))
13
+ #klass_scope.where!(table[key].eq(foreign_table[foreign_key]))
14
+ constraint = cpk_join_predicate(table, key, foreign_table, foreign_key)
15
+ klass_scope.where!(constraint)
16
16
 
17
17
  if type
18
18
  klass_scope.where!(type => foreign_klass.polymorphic_name)
@@ -25,26 +25,33 @@ module ActiveRecord
25
25
  end
26
26
 
27
27
  stmt = Arel::UpdateManager.new
28
+ if @klass.composite?
29
+ arel_attributes = primary_key.map do |key|
30
+ arel_attribute(key)
31
+ end.to_composite_keys
32
+ subselect = arel.clone
33
+ subselect.projections = [arel_attributes]
34
+ stmt.table(table)
35
+ stmt.key = arel_attributes.in(subselect)
36
+ else
37
+ stmt.table(arel.join_sources.empty? ? table : arel.source)
38
+ stmt.key = arel_attribute(primary_key)
39
+ end
40
+ stmt.take(arel.limit)
41
+ stmt.offset(arel.offset)
42
+ stmt.order(*arel.orders)
43
+ stmt.wheres = arel.constraints
28
44
 
29
- stmt.set Arel.sql(@klass.send(:sanitize_sql_for_assignment, updates))
30
- stmt.table(table)
31
-
32
- if has_join_values?
33
- # CPK
34
- #@klass.connection.join_to_update(stmt, arel, arel_attribute(primary_key))
35
- if primary_key.kind_of?(Array)
36
- attributes = primary_key.map do |key|
37
- arel_attribute(key)
38
- end
39
- @klass.connection.join_to_update(stmt, arel, attributes.to_composite_keys)
40
- else
41
- @klass.connection.join_to_update(stmt, arel, arel_attribute(primary_key))
45
+ if updates.is_a?(Hash)
46
+ if klass.locking_enabled? &&
47
+ !updates.key?(klass.locking_column) &&
48
+ !updates.key?(klass.locking_column.to_sym)
49
+ attr = arel_attribute(klass.locking_column)
50
+ updates[attr.name] = _increment_attribute(attr)
42
51
  end
52
+ stmt.set _substitute_values(updates)
43
53
  else
44
- stmt.key = arel_attribute(primary_key)
45
- stmt.take(arel.limit)
46
- stmt.order(*arel.orders)
47
- stmt.wheres = arel.constraints
54
+ stmt.set Arel.sql(klass.sanitize_sql_for_assignment(updates, table.name))
48
55
  end
49
56
 
50
57
  @klass.connection.update stmt, "#{@klass} Update All"
@@ -52,8 +59,10 @@ module ActiveRecord
52
59
 
53
60
  def delete_all
54
61
  invalid_methods = INVALID_METHODS_FOR_DELETE_ALL.select do |method|
55
- value = get_value(method)
56
- SINGLE_VALUE_METHODS.include?(method) ? value : value.any?
62
+ if respond_to?(:get_value)
63
+ value = get_value(method)
64
+ SINGLE_VALUE_METHODS.include?(method) ? value : value.any?
65
+ end
57
66
  end
58
67
  if invalid_methods.any?
59
68
  raise ActiveRecordError.new("delete_all doesn't support #{invalid_methods.join(', ')}")
@@ -65,19 +74,22 @@ module ActiveRecord
65
74
  end
66
75
 
67
76
  stmt = Arel::DeleteManager.new
68
- stmt.from(table)
69
-
70
- # CPK
71
- if has_join_values? && @klass.composite?
77
+ if @klass.composite?
72
78
  arel_attributes = primary_key.map do |key|
73
79
  arel_attribute(key)
74
80
  end.to_composite_keys
75
- @klass.connection.join_to_delete(stmt, arel, arel_attributes)
76
- elsif has_join_values? || has_limit_or_offset?
77
- @klass.connection.join_to_delete(stmt, arel, arel_attribute(primary_key))
81
+ subselect = arel.clone
82
+ subselect.projections = [arel_attributes]
83
+ stmt.from(table)
84
+ stmt.key = arel_attributes.in(subselect)
78
85
  else
79
- stmt.wheres = arel.constraints
86
+ stmt.from(arel.join_sources.empty? ? table : arel.source)
87
+ stmt.key = arel_attribute(primary_key)
80
88
  end
89
+ stmt.take(arel.limit)
90
+ stmt.offset(arel.offset)
91
+ stmt.order(*arel.orders)
92
+ stmt.wheres = arel.constraints
81
93
 
82
94
  affected = @klass.connection.delete(stmt, "#{@klass} Destroy")
83
95
 
@@ -5,10 +5,10 @@ module CompositePrimaryKeys
5
5
  # CPK
6
6
  if column_name.kind_of?(Array)
7
7
  column_name.map do |column|
8
- Arel::Attribute.new(@klass.unscoped.table, column)
8
+ @klass.arel_attribute(column_name)
9
9
  end
10
- elsif @klass.column_names.include?(column_name.to_s)
11
- Arel::Attribute.new(@klass.unscoped.table, column_name)
10
+ elsif @klass.has_attribute?(column_name) || @klass.attribute_alias?(column_name)
11
+ @klass.arel_attribute(column_name)
12
12
  else
13
13
  Arel.sql(column_name == :all ? "*" : column_name.to_s)
14
14
  end
@@ -18,14 +18,14 @@ module CompositePrimaryKeys
18
18
  column_alias = column_name
19
19
 
20
20
  # CPK
21
- # if operation == "count" && has_limit_or_offset?
21
+ # if operation == "count" && (column_name == :all && distinct || has_limit_or_offset?)
22
22
  # # Shortcut when limit is zero.
23
23
  # return 0 if limit_value == 0
24
24
  #
25
25
  # query_builder = build_count_subquery(spawn, column_name, distinct)
26
26
  if operation == "count"
27
27
  relation = unscope(:order)
28
- query_builder = build_count_subquery(relation, column_name, distinct)
28
+ query_builder = build_count_subquery(spawn, column_name, distinct)
29
29
  else
30
30
  # PostgreSQL doesn't like ORDER BY when there are no GROUP BY
31
31
  relation = unscope(:order).distinct!(false)
@@ -55,19 +55,16 @@ module CompositePrimaryKeys
55
55
  end
56
56
 
57
57
  def build_count_subquery(relation, column_name, distinct)
58
- relation.select_values = [
59
- if column_name == :all
60
- distinct ? table[Arel.star] : Arel.sql(::ActiveRecord::FinderMethods::ONE_AS_ONE)
61
- # CPK
62
- elsif column_name.is_a?(Array)
63
- relation.select_values = column_name.map do |column|
64
- Arel::Attribute.new(@klass.unscoped.table, column)
65
- end
66
- else
67
- column_alias = Arel.sql("count_column")
68
- aggregate_column(column_name).as(column_alias)
69
- end
70
- ]
58
+ if column_name == :all
59
+ relation.select_values = [ Arel.sql(::ActiveRecord::FinderMethods::ONE_AS_ONE) ] unless distinct
60
+ elsif column_name.is_a?(Array)
61
+ relation.select_values = column_name.map do |column|
62
+ Arel::Attribute.new(@klass.unscoped.table, column)
63
+ end
64
+ else
65
+ column_alias = Arel.sql("count_column")
66
+ relation.select_values = [ aggregate_column(column_name).as(column_alias) ]
67
+ end
71
68
 
72
69
  subquery = relation.arel.as(Arel.sql("subquery_for_count"))
73
70
  select_value = operation_over_aggregate_column(column_alias || Arel.star, "count", false)
@@ -1,8 +1,8 @@
1
1
  module CompositePrimaryKeys
2
2
  module ActiveRecord
3
3
  module FinderMethods
4
- def apply_join_dependency(eager_loading: true)
5
- join_dependency = construct_join_dependency
4
+ def apply_join_dependency(eager_loading: group_values.empty?)
5
+ join_dependency = construct_join_dependency(eager_load_values + includes_values)
6
6
  relation = except(:includes, :eager_load, :preload).joins!(join_dependency)
7
7
 
8
8
  if eager_loading && !using_limitable_reflections?(join_dependency.reflections)
@@ -12,13 +12,11 @@ module CompositePrimaryKeys
12
12
  # CPK
13
13
  # limited_ids.empty? ? relation.none! : relation.where!(primary_key => limited_ids)
14
14
  limited_ids.empty? ? relation.none! : relation.where!(cpk_in_predicate(table, self.primary_keys, limited_ids))
15
-
16
15
  end
17
16
  relation.limit_value = relation.offset_value = nil
18
17
  end
19
18
 
20
19
  if block_given?
21
- join_dependency.apply_column_aliases(relation)
22
20
  yield relation, join_dependency
23
21
  else
24
22
  relation
@@ -33,7 +31,7 @@ module CompositePrimaryKeys
33
31
  # )
34
32
 
35
33
  columns = @klass.primary_keys.map do |key|
36
- connection.column_name_from_arel_node(arel_attribute(key))
34
+ connection.visitor.compile(arel_attribute(key))
37
35
  end
38
36
  values = @klass.connection.columns_for_distinct(columns, relation.order_values)
39
37
 
@@ -50,7 +48,13 @@ module CompositePrimaryKeys
50
48
  end
51
49
 
52
50
  def construct_relation_for_exists(conditions)
53
- relation = except(:select, :distinct, :order)._select!(::ActiveRecord::FinderMethods::ONE_AS_ONE).limit!(1)
51
+ conditions = sanitize_forbidden_attributes(conditions)
52
+
53
+ if distinct_value && offset_value
54
+ relation = limit(1)
55
+ else
56
+ relation = except(:select, :distinct, :order)._select!(::ActiveRecord::FinderMethods::ONE_AS_ONE).limit!(1)
57
+ end
54
58
 
55
59
  case conditions
56
60
  # CPK
@@ -66,7 +70,7 @@ module CompositePrimaryKeys
66
70
  relation = relation.where(conditions)
67
71
  end
68
72
  when Array, Hash
69
- relation.where!(conditions)
73
+ relation.where!(conditions) unless conditions.empty?
70
74
  else
71
75
  relation.where!(primary_key => conditions) unless conditions == :none
72
76
  end
@@ -79,7 +83,7 @@ module CompositePrimaryKeys
79
83
 
80
84
  # CPK
81
85
  # expects_array = ids.first.kind_of?(Array)
82
- ids = CompositePrimaryKeys.normalize(ids, @klass.primary_keys.length)
86
+ ids = CompositePrimaryKeys.normalize(ids)
83
87
  expects_array = ids.flatten != ids.flatten(1)
84
88
  return ids.first if expects_array && ids.first.empty?
85
89
 
@@ -90,55 +94,49 @@ module CompositePrimaryKeys
90
94
  model_name = @klass.name
91
95
 
92
96
  case ids.size
93
- when 0
94
- error_message = "Couldn't find #{model_name} without an ID"
95
- raise RecordNotFound.new(error_message, model_name, primary_key)
96
- when 1
97
- result = find_one(ids.first)
98
- expects_array ? [ result ] : result
99
- else
100
- find_some(ids)
97
+ when 0
98
+ error_message = "Couldn't find #{model_name} without an ID"
99
+ raise RecordNotFound.new(error_message, model_name, primary_key)
100
+ when 1
101
+ result = find_one(ids.first)
102
+ expects_array ? [ result ] : result
103
+ else
104
+ find_some(ids)
101
105
  end
102
- rescue ::RangeError
103
- error_message = "Couldn't find #{model_name} with an out of range ID"
104
- raise RecordNotFound.new(error_message, model_name, primary_key, ids)
105
106
  end
106
107
 
107
- def last(limit = nil)
108
- return find_last(limit) if loaded? || limit_value
109
-
110
- result = limit(limit || 1)
111
- # CPK
112
- # result.order!(arel_attribute(primary_key)) if order_values.empty? && primary_key
113
- if order_values.empty? && primary_key
114
- if composite?
115
- result.order!(primary_keys.map { |pk| arel_attribute(pk).asc })
116
- elsif
117
- result.order!(arel_attribute(primary_key))
118
- end
119
- end
120
-
121
- result = result.reverse_order!
122
-
123
- limit ? result.reverse : result.first
124
- rescue ::ActiveRecord::IrreversibleOrderError
125
- ActiveSupport::Deprecation.warn(<<-WARNING.squish)
126
- Finding a last element by loading the relation when SQL ORDER
127
- can not be reversed is deprecated.
128
- Rails 5.1 will raise ActiveRecord::IrreversibleOrderError in this case.
129
- Please call `to_a.last` if you still want to load the relation.
130
- WARNING
131
- find_last(limit)
132
- end
108
+ # def last(limit = nil)
109
+ # return find_last(limit) if loaded? || has_limit_or_offset?
110
+ #
111
+ # result = limit(limit || 1)
112
+ # # CPK
113
+ # # result.order!(arel_attribute(primary_key)) if order_values.empty? && primary_key
114
+ # if order_values.empty? && primary_key
115
+ # if composite?
116
+ # result.order!(primary_keys.map { |pk| arel_attribute(pk).asc })
117
+ # elsif
118
+ # result.order!(arel_attribute(primary_key))
119
+ # end
120
+ # end
121
+ #
122
+ # result = result.reverse_order!
123
+ #
124
+ # limit ? result.reverse : result.first
125
+ # rescue ::ActiveRecord::IrreversibleOrderError
126
+ # ActiveSupport::Deprecation.warn(<<-WARNING.squish)
127
+ # Finding a last element by loading the relation when SQL ORDER
128
+ # can not be reversed is deprecated.
129
+ # Rails 5.1 will raise ActiveRecord::IrreversibleOrderError in this case.
130
+ # Please call `to_a.last` if you still want to load the relation.
131
+ # WARNING
132
+ # find_last(limit)
133
+ # end
133
134
 
134
135
  def find_one(id)
135
- # CPK
136
- # if ActiveRecord::Base === id
137
136
  if ::ActiveRecord::Base === id
138
- id = id.id
139
- ActiveSupport::Deprecation.warn(<<-MSG.squish)
140
- You are passing an instance of ActiveRecord::Base to `find`.
141
- Please pass the id of the object by calling `.id`
137
+ raise ArgumentError, <<-MSG.squish
138
+ You are passing an instance of ActiveRecord::Base to `find`.
139
+ Please pass the id of the object by calling `.id`.
142
140
  MSG
143
141
  end
144
142
 
@@ -156,10 +154,10 @@ module CompositePrimaryKeys
156
154
  # CPK
157
155
  if composite?
158
156
  ids = if ids.length == 1
159
- CompositePrimaryKeys::CompositeKeys.parse(ids.first)
160
- else
161
- ids.to_composite_keys
162
- end
157
+ ids.first.split(CompositePrimaryKeys::ID_SEP).to_composite_keys
158
+ else
159
+ ids.to_composite_keys
160
+ end
163
161
  end
164
162
 
165
163
  return find_some_ordered(ids) unless order_values.present?
@@ -224,10 +222,10 @@ module CompositePrimaryKeys
224
222
  end
225
223
 
226
224
  def ordered_relation
227
- if order_values.empty? && primary_key
225
+ if order_values.empty? && (implicit_order_column || primary_key)
228
226
  # CPK
229
- #order(arel_attribute(primary_key).asc)
230
- order(Array(primary_key).map {|key| arel_attribute(key).asc})
227
+ # order(arel_attribute(implicit_order_column || primary_key).asc)
228
+ order(Array(implicit_order_column || primary_key).map {|key| arel_attribute(key).asc})
231
229
  else
232
230
  self
233
231
  end