Skip to content

Commit 9e4c41c

Browse files
committed
Remove ActiveRecord::Model
In the end I think the pain of implementing this seamlessly was not worth the gain provided. The intention was that it would allow plain ruby objects that might not live in your main application to be subclassed and have persistence mixed in. But I've decided that the benefit of doing that is not worth the amount of complexity that the implementation introduced.
1 parent a27b517 commit 9e4c41c

40 files changed

+232
-642
lines changed

activerecord/CHANGELOG.md

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -926,34 +926,6 @@
926926

927927
*Aaron Patterson*
928928

929-
* Added the `ActiveRecord::Model` module which can be included in a
930-
class as an alternative to inheriting from `ActiveRecord::Base`:
931-
932-
class Post
933-
include ActiveRecord::Model
934-
end
935-
936-
Please note:
937-
938-
* Up until now it has been safe to assume that all AR models are
939-
descendants of `ActiveRecord::Base`. This is no longer a safe
940-
assumption, but it may transpire that there are areas of the
941-
code which still make this assumption. So there may be
942-
'teething difficulties' with this feature. (But please do try it
943-
and report bugs.)
944-
945-
* Plugins & libraries etc that add methods to `ActiveRecord::Base`
946-
will not be compatible with `ActiveRecord::Model`. Those libraries
947-
should add to `ActiveRecord::Model` instead (which is included in
948-
`Base`), or better still, avoid monkey-patching AR and instead
949-
provide a module that users can include where they need it.
950-
951-
* To minimise the risk of conflicts with other code, it is
952-
advisable to include `ActiveRecord::Model` early in your class
953-
definition.
954-
955-
*Jon Leighton*
956-
957929
* PostgreSQL hstore records can be created.
958930

959931
*Aaron Patterson*

activerecord/lib/active_record.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ module ActiveRecord
4343
autoload :Integration
4444
autoload :Migration
4545
autoload :Migrator, 'active_record/migration'
46-
autoload :Model
4746
autoload :ModelSchema
4847
autoload :NestedAttributes
4948
autoload :Observer

activerecord/lib/active_record/associations/alias_tracker.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class AliasTracker # :nodoc:
88
attr_reader :aliases, :table_joins, :connection
99

1010
# table_joins is an array of arel joins which might conflict with the aliases we assign here
11-
def initialize(connection = ActiveRecord::Model.connection, table_joins = [])
11+
def initialize(connection = Base.connection, table_joins = [])
1212
@aliases = Hash.new { |h,k| h[k] = initial_count_for(k) }
1313
@table_joins = table_joins
1414
@connection = connection

activerecord/lib/active_record/attribute_methods.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def instance_method_already_implemented?(method_name)
5959
raise DangerousAttributeError, "#{method_name} is defined by ActiveRecord"
6060
end
6161

62-
if [Base, Model].include?(active_record_super)
62+
if superclass == Base
6363
super
6464
else
6565
# If B < A and A defines its own attribute method, then we don't want to overwrite that.

activerecord/lib/active_record/attribute_methods/dirty.rb

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@
22
require 'active_support/deprecation'
33

44
module ActiveRecord
5-
ActiveSupport.on_load(:active_record_config) do
6-
mattr_accessor :partial_writes, instance_accessor: false
7-
self.partial_writes = true
8-
end
9-
105
module AttributeMethods
116
module Dirty # :nodoc:
127
extend ActiveSupport::Concern
@@ -18,7 +13,8 @@ module Dirty # :nodoc:
1813
raise "You cannot include Dirty after Timestamp"
1914
end
2015

21-
config_attribute :partial_writes
16+
class_attribute :partial_writes, instance_writer: false
17+
self.partial_writes = true
2218

2319
def self.partial_updates=(v); self.partial_writes = v; end
2420
def self.partial_updates?; partial_writes?; end

activerecord/lib/active_record/attribute_methods/read.rb

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
module ActiveRecord
2-
ActiveSupport.on_load(:active_record_config) do
3-
mattr_accessor :attribute_types_cached_by_default, instance_accessor: false
4-
end
5-
62
module AttributeMethods
73
module Read
84
extend ActiveSupport::Concern
95

106
ATTRIBUTE_TYPES_CACHED_BY_DEFAULT = [:datetime, :timestamp, :time, :date]
117

128
included do
13-
config_attribute :attribute_types_cached_by_default
9+
class_attribute :attribute_types_cached_by_default, instance_writer: false
10+
self.attribute_types_cached_by_default = ATTRIBUTE_TYPES_CACHED_BY_DEFAULT
1411
end
1512

1613
module ClassMethods
@@ -79,8 +76,6 @@ def cacheable_column?(column)
7976
end
8077
end
8178

82-
ActiveRecord::Model.attribute_types_cached_by_default = ATTRIBUTE_TYPES_CACHED_BY_DEFAULT
83-
8479
# Returns the value of the attribute identified by <tt>attr_name</tt> after
8580
# it has been typecast (for example, "2004-12-12" in a data column is cast
8681
# to a date object, like Date.new(2004, 12, 12)).

activerecord/lib/active_record/attribute_methods/time_zone_conversion.rb

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,4 @@
1-
21
module ActiveRecord
3-
ActiveSupport.on_load(:active_record_config) do
4-
mattr_accessor :time_zone_aware_attributes, instance_accessor: false
5-
self.time_zone_aware_attributes = false
6-
7-
mattr_accessor :skip_time_zone_conversion_for_attributes, instance_accessor: false
8-
self.skip_time_zone_conversion_for_attributes = []
9-
end
10-
112
module AttributeMethods
123
module TimeZoneConversion
134
class Type # :nodoc:
@@ -28,8 +19,11 @@ def type
2819
extend ActiveSupport::Concern
2920

3021
included do
31-
config_attribute :time_zone_aware_attributes, global: true
32-
config_attribute :skip_time_zone_conversion_for_attributes
22+
mattr_accessor :time_zone_aware_attributes, instance_writer: false
23+
self.time_zone_aware_attributes = false
24+
25+
class_attribute :skip_time_zone_conversion_for_attributes, instance_writer: false
26+
self.skip_time_zone_conversion_for_attributes = []
3327
end
3428

3529
module ClassMethods

activerecord/lib/active_record/base.rb

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,8 +321,48 @@ module ActiveRecord #:nodoc:
321321
# So it's possible to assign a logger to the class through <tt>Base.logger=</tt> which will then be used by all
322322
# instances in the current object space.
323323
class Base
324-
include ActiveRecord::Model
324+
extend ActiveModel::Observing::ClassMethods
325+
extend ActiveModel::Naming
326+
327+
extend ActiveSupport::Benchmarkable
328+
extend ActiveSupport::DescendantsTracker
329+
330+
extend ConnectionHandling
331+
extend QueryCache::ClassMethods
332+
extend Querying
333+
extend Translation
334+
extend DynamicMatchers
335+
extend Explain
336+
extend ConnectionHandling
337+
338+
include Persistence
339+
include ReadonlyAttributes
340+
include ModelSchema
341+
include Inheritance
342+
include Scoping
343+
include Sanitization
344+
include AttributeAssignment
345+
include ActiveModel::Conversion
346+
include Integration
347+
include Validations
348+
include CounterCache
349+
include Locking::Optimistic
350+
include Locking::Pessimistic
351+
include AttributeMethods
352+
include Callbacks
353+
include ActiveModel::Observing
354+
include Timestamp
355+
include Associations
356+
include ActiveModel::SecurePassword
357+
include AutosaveAssociation
358+
include NestedAttributes
359+
include Aggregations
360+
include Transactions
361+
include Reflection
362+
include Serialization
363+
include Store
364+
include Core
325365
end
326-
end
327366

328-
ActiveSupport.run_load_hooks(:active_record, ActiveRecord::Model::DeprecationProxy.new)
367+
ActiveSupport.run_load_hooks(:active_record, Base)
368+
end

activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -441,11 +441,11 @@ def release(conn)
441441
end
442442

443443
def new_connection
444-
ActiveRecord::Model.send(spec.adapter_method, spec.config)
444+
Base.send(spec.adapter_method, spec.config)
445445
end
446446

447447
def current_connection_id #:nodoc:
448-
ActiveRecord::Model.connection_id ||= Thread.current.object_id
448+
Base.connection_id ||= Thread.current.object_id
449449
end
450450

451451
def checkout_new_connection
@@ -567,10 +567,10 @@ def retrieve_connection_pool(klass)
567567
class_to_pool[klass] ||= begin
568568
until pool = pool_for(klass)
569569
klass = klass.superclass
570-
break unless klass < ActiveRecord::Tag
570+
break unless klass <= Base
571571
end
572572

573-
class_to_pool[klass] = pool || pool_for(ActiveRecord::Model)
573+
class_to_pool[klass] = pool
574574
end
575575
end
576576

activerecord/lib/active_record/connection_handling.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
module ActiveRecord
32
module ConnectionHandling
43
# Establishes the connection to the database. Accepts a hash as input where

0 commit comments

Comments
 (0)