GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Allow attribute aliases for timestamp magic columns · Ecco/rails@abae7fc

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • activerecord
    • lib/active_record
      • fixture_set
        • model_metadata.rb
      • integration.rb
      • relation.rb
      • timestamp.rb
    • test
      • cases
        • adapters/postgresql
          • timestamp_test.rb
        • base_test.rb
        • scoping
          • default_scoping_test.rb
        • timestamp_test.rb
      • models
        • developer.rb
      • schema
        • schema.rb
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎activerecord/lib/active_record/fixture_set/model_metadata.rb‎

Copy file name to clipboardExpand all lines: activerecord/lib/active_record/fixture_set/model_metadata.rb
+1-2Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ def has_primary_key_column?
2121
end
2222

2323
def timestamp_column_names
24-
@timestamp_column_names ||=
25-
%w(created_at created_on updated_at updated_on) & @model_class.column_names
24+
@model_class.all_timestamp_attributes_in_model
2625
end
2726

2827
def inheritance_column_name
Collapse file

‎activerecord/lib/active_record/integration.rb‎

Copy file name to clipboardExpand all lines: activerecord/lib/active_record/integration.rb
+5-5Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,17 +97,17 @@ def cache_key
9797
def cache_version
9898
return unless cache_versioning
9999

100-
if has_attribute?("updated_at")
100+
timestamp_column = self.class.attribute_aliases["updated_at"] || "updated_at"
101+
102+
if has_attribute?(timestamp_column)
101103
timestamp = updated_at_before_type_cast
102104
if can_use_fast_cache_version?(timestamp)
103105
raw_timestamp_to_cache_version(timestamp)
104106
elsif timestamp = updated_at
105107
timestamp.utc.to_s(cache_timestamp_format)
106108
end
107-
else
108-
if self.class.has_attribute?("updated_at")
109-
raise ActiveModel::MissingAttributeError, "missing attribute: updated_at"
110-
end
109+
elsif self.class.has_attribute?(timestamp_column)
110+
raise ActiveModel::MissingAttributeError, "missing attribute: updated_at"
111111
end
112112
end
113113

Collapse file

‎activerecord/lib/active_record/relation.rb‎

Copy file name to clipboardExpand all lines: activerecord/lib/active_record/relation.rb
+4-1Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,10 +343,13 @@ def cache_version(timestamp_column = :updated_at)
343343
end
344344

345345
def compute_cache_version(timestamp_column) # :nodoc:
346+
timestamp_column = timestamp_column.to_s
347+
timestamp_column = klass.attribute_aliases[timestamp_column] || timestamp_column
348+
346349
if loaded? || distinct_value
347350
size = records.size
348351
if size > 0
349-
timestamp = max_by(&timestamp_column)._read_attribute(timestamp_column)
352+
timestamp = records.map { |record| record._read_attribute(timestamp_column) }.max
350353
end
351354
else
352355
collection = eager_loading? ? apply_join_dependency : self
Collapse file

‎activerecord/lib/active_record/timestamp.rb‎

Copy file name to clipboardExpand all lines: activerecord/lib/active_record/timestamp.rb
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,11 @@ def current_time_from_proper_timezone
8080

8181
private
8282
def timestamp_attributes_for_create
83-
["created_at", "created_on"]
83+
["created_at", "created_on"].map! { |name| attribute_aliases[name] || name }
8484
end
8585

8686
def timestamp_attributes_for_update
87-
["updated_at", "updated_on"]
87+
["updated_at", "updated_on"].map! { |name| attribute_aliases[name] || name }
8888
end
8989

9090
def reload_schema_from_cache
Collapse file

‎activerecord/test/cases/adapters/postgresql/timestamp_test.rb‎

Copy file name to clipboardExpand all lines: activerecord/test/cases/adapters/postgresql/timestamp_test.rb
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ def test_group_by_date
5555
end
5656

5757
def test_load_infinity_and_beyond
58-
d = Developer.find_by_sql("select 'infinity'::timestamp as updated_at")
58+
d = Developer.find_by_sql("select 'infinity'::timestamp as legacy_updated_at")
5959
assert d.first.updated_at.infinite?, "timestamp should be infinite"
6060

61-
d = Developer.find_by_sql("select '-infinity'::timestamp as updated_at")
61+
d = Developer.find_by_sql("select '-infinity'::timestamp as legacy_updated_at")
6262
time = d.first.updated_at
6363
assert time.infinite?, "timestamp should be infinite"
6464
assert_operator time, :<, 0
Collapse file

‎activerecord/test/cases/base_test.rb‎

Copy file name to clipboardExpand all lines: activerecord/test/cases/base_test.rb
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,8 +1106,8 @@ def test_find_keeps_multiple_order_values
11061106
end
11071107

11081108
def test_find_keeps_multiple_group_values
1109-
combined = Developer.all.merge!(group: "developers.name, developers.salary, developers.id, developers.created_at, developers.updated_at, developers.created_on, developers.updated_on").to_a
1110-
assert_equal combined, Developer.all.merge!(group: ["developers.name", "developers.salary", "developers.id", "developers.created_at", "developers.updated_at", "developers.created_on", "developers.updated_on"]).to_a
1109+
combined = Developer.merge(group: "developers.name, developers.salary, developers.id, developers.legacy_created_at, developers.legacy_updated_at, developers.legacy_created_on, developers.legacy_updated_on").to_a
1110+
assert_equal combined, Developer.merge(group: ["developers.name", "developers.salary", "developers.id", "developers.created_at", "developers.updated_at", "developers.created_on", "developers.updated_on"]).to_a
11111111
end
11121112

11131113
def test_find_symbol_ordered_last
Collapse file

‎activerecord/test/cases/scoping/default_scoping_test.rb‎

Copy file name to clipboardExpand all lines: activerecord/test/cases/scoping/default_scoping_test.rb
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,10 @@ def test_unscope_multiple_where_clauses
166166
end
167167

168168
def test_unscope_string_where_clauses_involved
169-
dev_relation = Developer.order("salary DESC").where("created_at > ?", 1.year.ago)
169+
dev_relation = Developer.order("salary DESC").where("legacy_created_at > ?", 1.year.ago)
170170
expected = dev_relation.collect(&:name)
171171

172-
dev_ordered_relation = DeveloperOrderedBySalary.where(name: "Jamis").where("created_at > ?", 1.year.ago)
172+
dev_ordered_relation = DeveloperOrderedBySalary.where(name: "Jamis").where("legacy_created_at > ?", 1.year.ago)
173173
received = dev_ordered_relation.unscope(where: [:name]).collect(&:name)
174174

175175
assert_equal expected.sort, received.sort
Collapse file

‎activerecord/test/cases/timestamp_test.rb‎

Copy file name to clipboardExpand all lines: activerecord/test/cases/timestamp_test.rb
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def test_touching_a_record_updates_its_timestamp
4444
assert_predicate @developer, :changed?, "developer should be marked as changed"
4545
assert_equal ["salary"], @developer.changed
4646
assert_predicate @developer, :saved_changes?
47-
assert_equal ["updated_at", "updated_on"], @developer.saved_changes.keys.sort
47+
assert_equal ["legacy_updated_at", "legacy_updated_on"], @developer.saved_changes.keys.sort
4848

4949
@developer.reload
5050
assert_equal previous_salary, @developer.salary
@@ -57,7 +57,7 @@ def test_touching_a_record_with_default_scope_that_excludes_it_updates_its_times
5757
assert_not_equal @previously_updated_at, developer.updated_at
5858
assert_not_predicate developer, :changed?
5959
assert_predicate developer, :saved_changes?
60-
assert_equal ["updated_at", "updated_on"], developer.saved_changes.keys.sort
60+
assert_equal ["legacy_updated_at", "legacy_updated_on"], developer.saved_changes.keys.sort
6161

6262
developer.reload
6363
assert_not_equal @previously_updated_at, developer.updated_at
Collapse file

‎activerecord/test/models/developer.rb‎

Copy file name to clipboardExpand all lines: activerecord/test/models/developer.rb
+17Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,19 @@
33
require "ostruct"
44

55
class Developer < ActiveRecord::Base
6+
module TimestampAliases
7+
extend ActiveSupport::Concern
8+
9+
included do
10+
alias_attribute :created_at, :legacy_created_at
11+
alias_attribute :updated_at, :legacy_updated_at
12+
alias_attribute :created_on, :legacy_created_on
13+
alias_attribute :updated_on, :legacy_updated_on
14+
end
15+
end
16+
17+
include TimestampAliases
18+
619
module ProjectsAssociationExtension2
720
def find_least_recent
821
order("id ASC").first
@@ -193,6 +206,8 @@ class LazyBlockReferencingScopeDeveloperCalledDavid < ActiveRecord::Base
193206
end
194207

195208
class DeveloperCalledJamis < ActiveRecord::Base
209+
include Developer::TimestampAliases
210+
196211
self.table_name = "developers"
197212

198213
default_scope { where(name: "Jamis") }
@@ -280,6 +295,8 @@ def self.default_scope
280295
end
281296

282297
class CachedDeveloper < ActiveRecord::Base
298+
include Developer::TimestampAliases
299+
283300
self.table_name = "developers"
284301
self.cache_timestamp_format = :number
285302
end
Collapse file

‎activerecord/test/schema/schema.rb‎

Copy file name to clipboardExpand all lines: activerecord/test/schema/schema.rb
+8-8Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -295,15 +295,15 @@
295295
t.references :firm, index: false
296296
t.integer :mentor_id
297297
if supports_datetime_with_precision?
298-
t.datetime :created_at, precision: 6
299-
t.datetime :updated_at, precision: 6
300-
t.datetime :created_on, precision: 6
301-
t.datetime :updated_on, precision: 6
298+
t.datetime :legacy_created_at, precision: 6
299+
t.datetime :legacy_updated_at, precision: 6
300+
t.datetime :legacy_created_on, precision: 6
301+
t.datetime :legacy_updated_on, precision: 6
302302
else
303-
t.datetime :created_at
304-
t.datetime :updated_at
305-
t.datetime :created_on
306-
t.datetime :updated_on
303+
t.datetime :legacy_created_at
304+
t.datetime :legacy_updated_at
305+
t.datetime :legacy_created_on
306+
t.datetime :legacy_updated_on
307307
end
308308
end
309309

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.