Skip to content

Commit 6b9a96a

Browse files
committed
Update for Rails 6.0.0.rc1
1 parent d2de6f7 commit 6b9a96a

File tree

7 files changed

+112
-28
lines changed

7 files changed

+112
-28
lines changed

Gemfile.lock

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,31 @@ PATH
22
remote: .
33
specs:
44
active_record-returning_attributes (0.1.0)
5-
activerecord
5+
activerecord (= 6.0.0.rc1)
66
pg
77

88
GEM
99
remote: https://rubygems.org/
1010
specs:
11-
activemodel (5.2.0)
12-
activesupport (= 5.2.0)
13-
activerecord (5.2.0)
14-
activemodel (= 5.2.0)
15-
activesupport (= 5.2.0)
16-
arel (>= 9.0)
17-
activesupport (5.2.0)
11+
activemodel (6.0.0.rc1)
12+
activesupport (= 6.0.0.rc1)
13+
activerecord (6.0.0.rc1)
14+
activemodel (= 6.0.0.rc1)
15+
activesupport (= 6.0.0.rc1)
16+
activesupport (6.0.0.rc1)
1817
concurrent-ruby (~> 1.0, >= 1.0.2)
1918
i18n (>= 0.7, < 2)
2019
minitest (~> 5.1)
2120
tzinfo (~> 1.1)
22-
arel (9.0.0)
21+
zeitwerk (~> 2.1, >= 2.1.4)
2322
coderay (1.1.2)
24-
concurrent-ruby (1.0.5)
23+
concurrent-ruby (1.1.5)
2524
diff-lcs (1.3)
26-
i18n (1.0.1)
25+
i18n (1.6.0)
2726
concurrent-ruby (~> 1.0)
2827
method_source (0.9.0)
2928
minitest (5.11.3)
30-
pg (1.0.0)
29+
pg (1.1.4)
3130
pry (0.11.3)
3231
coderay (~> 1.1.0)
3332
method_source (~> 0.9.0)
@@ -52,6 +51,7 @@ GEM
5251
tzinfo (1.2.5)
5352
thread_safe (~> 0.1)
5453
yard (0.9.12)
54+
zeitwerk (2.1.9)
5555

5656
PLATFORMS
5757
ruby
@@ -65,4 +65,4 @@ DEPENDENCIES
6565
rspec (~> 3.0)
6666

6767
BUNDLED WITH
68-
1.16.2
68+
1.17.2

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Internally this library changes the `RETURNING` clause for inserts and adds it f
1111
Caveats:
1212

1313
- PostgreSQL only
14-
- Only tested in Rails 5.2.0
14+
- Only tested in Rails 6.0.0.rc1
1515
- Overrides ActiveRecord core code
1616

1717
## Installation

activerecord-returning_attributes.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
1818
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
1919
spec.require_paths = ['lib']
2020

21-
spec.add_runtime_dependency 'activerecord'
21+
spec.add_runtime_dependency 'activerecord', '6.0.0.rc1'
2222
spec.add_runtime_dependency 'pg'
2323

2424
spec.add_development_dependency 'bundler', '~> 1.16'

lib/active_record/returning_attributes.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def self.postgresql?
2222
end
2323
end
2424

25-
def self.active_record_5_2_tested_only
25+
def self.active_record_6_0_0_rc1_tested_only
2626
unless ActiveRecord.version == Gem::Version.new('5.2.0')
2727
warn 'ActiveRecord::ReturningAttributes was only tested on ActiveRecord 5.2.0 and might not work with your version.'
2828
end
@@ -36,7 +36,7 @@ def self.active_record_5_2_tested_only
3636
ReturningAttributes::Patching.patch_postgresql_database_statements
3737
ReturningAttributes::Patching.patch_postgresql_adapter
3838

39-
ReturningAttributes.active_record_5_2_tested_only
39+
ReturningAttributes.active_record_6_0_0_rc1_tested_only
4040
end
4141
end
4242
end

lib/active_record/returning_attributes/accessor.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,16 @@ module ReturningAttributes
55
module Accessor
66
extend ActiveSupport::Concern
77

8-
included do
9-
class_attribute :returning_attributes, default: []
8+
def returning_attributes
9+
self.class.returning_attributes
10+
end
11+
12+
module ClassMethods
13+
attr_reader :returning_attributes
14+
15+
def returning_attributes=(attributes)
16+
@returning_attributes = attributes.map(&:to_s)
17+
end
1018
end
1119
end
1220
end

lib/active_record/returning_attributes/patching.rb

Lines changed: 78 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,32 @@ def self.patch_persistence
1212
Persistence.module_eval do
1313
private
1414

15+
# def _create_record(attribute_names = self.attribute_names)
16+
# attribute_names = attributes_for_create(attribute_names)
17+
18+
# new_id = self.class._insert_record(
19+
# attributes_with_values(attribute_names)
20+
# )
21+
22+
# self.id ||= new_id if @primary_key
23+
24+
# @new_record = false
25+
26+
# yield(self) if block_given?
27+
28+
# id
29+
# end
1530
def _create_record(attribute_names = self.attribute_names)
16-
attribute_names &= self.class.column_names
17-
attributes_values = attributes_with_values_for_create(attribute_names)
31+
attribute_names = attributes_for_create(attribute_names)
1832

1933
new_id, returned_attributes = self.class.connection.with_returning_attributes(returning_attributes) do
20-
self.class._insert_record(attributes_values)
34+
self.class._insert_record(attributes_with_values(attribute_names))
2135
end
2236

23-
self.id ||= new_id if self.class.primary_key
37+
self.id ||= new_id if @primary_key
2438

2539
returning_attributes.each do |attribute|
26-
write_attribute(attribute, returned_attributes[attribute.to_s]) if returned_attributes.key?(attribute.to_s)
40+
write_attribute(attribute, returned_attributes[attribute]) if returned_attributes.key?(attribute)
2741
end
2842

2943
@new_record = false
@@ -33,8 +47,22 @@ def _create_record(attribute_names = self.attribute_names)
3347
id
3448
end
3549

50+
# def _update_record(attribute_names = self.attribute_names)
51+
# attribute_names = attributes_for_update(attribute_names)
52+
53+
# if attribute_names.empty?
54+
# affected_rows = 0
55+
# @_trigger_update_callback = true
56+
# else
57+
# affected_rows = _update_row(attribute_names)
58+
# @_trigger_update_callback = affected_rows == 1
59+
# end
60+
61+
# yield(self) if block_given?
62+
63+
# affected_rows
64+
# end
3665
def _update_record(attribute_names = self.attribute_names)
37-
attribute_names &= self.class.column_names
3866
attribute_names = attributes_for_update(attribute_names)
3967

4068
if attribute_names.empty?
@@ -46,7 +74,7 @@ def _update_record(attribute_names = self.attribute_names)
4674
end
4775

4876
returning_attributes.each do |attribute|
49-
write_attribute(attribute, returned_attributes[attribute.to_s]) if returned_attributes.key?(attribute.to_s)
77+
write_attribute(attribute, returned_attributes[attribute]) if returned_attributes.key?(attribute)
5078
end
5179

5280
@_trigger_update_callback = affected_rows == 1
@@ -61,8 +89,12 @@ def _update_record(attribute_names = self.attribute_names)
6189

6290
def self.patch_database_statements
6391
ConnectionAdapters::DatabaseStatements.module_eval do
92+
# def exec_insert(sql, name = nil, binds = [], pk = nil, sequence_name = nil)
93+
# sql, binds = sql_for_insert(sql, pk, binds)
94+
# exec_query(sql, name, binds)
95+
# end
6496
def exec_insert(sql, name = nil, binds = [], pk = nil, sequence_name = nil)
65-
sql, binds = sql_for_insert(sql, pk, nil, sequence_name, binds)
97+
sql, binds = sql_for_insert(sql, pk, binds)
6698

6799
exec_query(sql, name, binds).tap do |result|
68100
if @_returning_attributes.present?
@@ -71,6 +103,10 @@ def exec_insert(sql, name = nil, binds = [], pk = nil, sequence_name = nil)
71103
end
72104
end
73105

106+
# def update(arel, name = nil, binds = [])
107+
# sql, binds = to_sql_and_binds(arel, binds)
108+
# exec_update(sql, name, binds)
109+
# end
74110
def update(arel, name = nil, binds = [])
75111
sql, binds = to_sql_and_binds(arel, binds)
76112

@@ -89,7 +125,20 @@ def self.patch_postgresql_database_statements
89125
ConnectionAdapters::PostgreSQL::DatabaseStatements.module_eval do
90126
private
91127

92-
def sql_for_insert(sql, pk, id_value, sequence_name, binds)
128+
# def sql_for_insert(sql, pk, binds) # :nodoc:
129+
# if pk.nil?
130+
# # Extract the table from the insert sql. Yuck.
131+
# table_ref = extract_table_ref_from_insert_sql(sql)
132+
# pk = primary_key(table_ref) if table_ref
133+
# end
134+
135+
# if pk = suppress_composite_primary_key(pk)
136+
# sql = "#{sql} RETURNING #{quote_column_name(pk)}"
137+
# end
138+
139+
# super
140+
# end
141+
def sql_for_insert(sql, pk, binds)
93142
if pk.nil?
94143
# Extract the table from the insert sql. Yuck.
95144
table_ref = extract_table_ref_from_insert_sql(sql)
@@ -117,7 +166,27 @@ def self.patch_postgresql_adapter
117166

118167
private
119168

169+
# def execute_and_clear(sql, name, binds, prepare: false)
170+
# if preventing_writes? && write_query?(sql)
171+
# raise ActiveRecord::ReadOnlyError, "Write query attempted while in readonly mode: #{sql}"
172+
# end
173+
174+
# if without_prepared_statement?(binds)
175+
# result = exec_no_cache(sql, name, [])
176+
# elsif !prepare
177+
# result = exec_no_cache(sql, name, binds)
178+
# else
179+
# result = exec_cache(sql, name, binds)
180+
# end
181+
# ret = yield result
182+
# result.clear
183+
# ret
184+
# end
120185
def execute_and_clear(sql, name, binds, prepare: false)
186+
if preventing_writes? && write_query?(sql)
187+
raise ActiveRecord::ReadOnlyError, "Write query attempted while in readonly mode: #{sql}"
188+
end
189+
121190
if without_prepared_statement?(binds)
122191
result = exec_no_cache(sql, name, [])
123192
elsif !prepare

spec/support/schema_and_models.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
ActiveRecord::Base.establish_connection(adapter: 'postgresql')
2+
3+
begin
4+
ActiveRecord::Base.connection.create_database('returning_attributes')
5+
rescue ActiveRecord::StatementInvalid
6+
end
7+
18
ActiveRecord::Base.establish_connection(adapter: 'postgresql', database: 'returning_attributes')
29
ActiveRecord::Base.logger = Logger.new(STDOUT)
310

0 commit comments

Comments
 (0)