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.
- checksums.yaml +4 -4
- data/History.rdoc +17 -7
- data/README.rdoc +1 -0
- data/lib/composite_primary_keys.rb +1 -6
- data/lib/composite_primary_keys/arel/to_sql.rb +13 -13
- data/lib/composite_primary_keys/associations/association_scope.rb +4 -4
- data/lib/composite_primary_keys/associations/has_many_association.rb +2 -1
- data/lib/composite_primary_keys/associations/has_many_through_association.rb +0 -2
- data/lib/composite_primary_keys/associations/join_dependency.rb +9 -8
- data/lib/composite_primary_keys/associations/preloader/association.rb +28 -36
- data/lib/composite_primary_keys/attribute_methods.rb +2 -2
- data/lib/composite_primary_keys/attribute_methods/read.rb +11 -14
- data/lib/composite_primary_keys/attribute_methods/write.rb +8 -11
- data/lib/composite_primary_keys/base.rb +2 -2
- data/lib/composite_primary_keys/composite_arrays.rb +7 -50
- data/lib/composite_primary_keys/connection_adapters/abstract_adapter.rb +1 -1
- data/lib/composite_primary_keys/connection_adapters/postgresql/database_statements.rb +1 -1
- data/lib/composite_primary_keys/core.rb +10 -14
- data/lib/composite_primary_keys/counter_cache.rb +4 -22
- data/lib/composite_primary_keys/fixtures.rb +5 -10
- data/lib/composite_primary_keys/nested_attributes.rb +6 -6
- data/lib/composite_primary_keys/persistence.rb +8 -8
- data/lib/composite_primary_keys/reflection.rb +3 -3
- data/lib/composite_primary_keys/relation.rb +39 -27
- data/lib/composite_primary_keys/relation/calculations.rb +15 -18
- data/lib/composite_primary_keys/relation/finder_methods.rb +56 -58
- data/lib/composite_primary_keys/relation/query_methods.rb +4 -3
- data/lib/composite_primary_keys/relation/where_clause.rb +2 -2
- data/lib/composite_primary_keys/sanitization.rb +37 -37
- data/lib/composite_primary_keys/validations/uniqueness.rb +2 -2
- data/lib/composite_primary_keys/version.rb +4 -4
- data/tasks/databases/mysql.rake +1 -1
- data/test/abstract_unit.rb +1 -5
- data/test/connections/databases.ci.yml +3 -0
- data/test/db_test.rb +53 -0
- data/test/fixtures/db_definitions/mysql.sql +1 -7
- data/test/fixtures/db_definitions/oracle.drop.sql +1 -3
- data/test/fixtures/db_definitions/oracle.sql +0 -8
- data/test/fixtures/db_definitions/postgresql.sql +0 -6
- data/test/fixtures/db_definitions/sqlite.sql +0 -6
- data/test/fixtures/db_definitions/sqlserver.sql +1 -8
- data/test/test_associations.rb +1 -1
- data/test/test_composite_arrays.rb +0 -14
- data/test/test_create.rb +12 -26
- data/test/test_find.rb +0 -8
- data/test/test_ids.rb +0 -3
- data/test/test_nested_attributes.rb +10 -10
- data/test/test_update.rb +1 -1
- metadata +9 -11
- data/lib/composite_primary_keys/connection_adapters/abstract_mysql_adapter.rb +0 -20
- data/lib/composite_primary_keys/connection_adapters/sqlite3_adapter.rb +0 -23
- data/test/fixtures/cpk_with_default_value.rb +0 -3
- data/test/fixtures/cpk_with_default_values.yml +0 -7
@@ -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,
|
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
|
-
|
9
|
+
_run_initialize_callbacks
|
10
10
|
|
11
|
-
@
|
12
|
-
@
|
13
|
-
|
14
|
-
@
|
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
|
-
|
26
|
-
|
27
|
-
|
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)
|
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
|
-
|
23
|
-
predicate =
|
24
|
-
|
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
|
-
|
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
|
-
|
4
|
+
raise FixtureClassNotFound, "No class attached to find." unless model_class
|
5
|
+
model_class.unscoped do
|
5
6
|
# CPK
|
6
|
-
#
|
7
|
-
|
8
|
-
|
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
|
-
|
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 =
|
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 =
|
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[
|
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
|
-
|
16
|
+
delete_by(cpk_id_predicate(self.arel_table, self.primary_key, id))
|
17
17
|
end
|
18
18
|
else
|
19
|
-
|
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
|
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(
|
63
|
+
new_id = self.class._insert_record(
|
64
|
+
attributes_with_values(attribute_names)
|
65
|
+
)
|
65
66
|
|
66
67
|
# CPK
|
67
|
-
if self.composite?
|
68
|
-
|
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
|
-
#
|
15
|
-
|
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
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
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.
|
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
|
-
|
56
|
-
|
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
|
-
|
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
|
-
|
76
|
-
|
77
|
-
|
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.
|
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
|
-
|
8
|
+
@klass.arel_attribute(column_name)
|
9
9
|
end
|
10
|
-
elsif @klass.
|
11
|
-
|
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(
|
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
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
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:
|
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.
|
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
|
-
|
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
|
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
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
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
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
rescue ::ActiveRecord::IrreversibleOrderError
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
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
|
-
|
139
|
-
|
140
|
-
|
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
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
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
|