Skip to content

Commit

Permalink
Revert "Get rid of the ActiveRecord::Model::DeprecationProxy thing."
Browse files Browse the repository at this point in the history
This reverts commit 8384683.
  • Loading branch information
jeremy committed Oct 20, 2012
1 parent c869ce0 commit 4ed1563
Show file tree
Hide file tree
Showing 15 changed files with 88 additions and 28 deletions.
9 changes: 1 addition & 8 deletions activerecord/CHANGELOG.md
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -945,14 +945,7 @@
* Plugins & libraries etc that add methods to `ActiveRecord::Base` * Plugins & libraries etc that add methods to `ActiveRecord::Base`
will not be compatible with `ActiveRecord::Model`. Those libraries will not be compatible with `ActiveRecord::Model`. Those libraries
should add to `ActiveRecord::Model` instead (which is included in should add to `ActiveRecord::Model` instead (which is included in
`Base`). This should be done using the `:active_record_model` `Base`), or better still, avoid monkey-patching AR and instead
load hook, which executes before `ActiveRecord::Base` loads:

ActiveSupport.on_load(:active_record_model) do
include MyPlugin
end

Or better still, avoid monkey-patching AR and instead
provide a module that users can include where they need it. provide a module that users can include where they need it.


* To minimise the risk of conflicts with other code, it is * To minimise the risk of conflicts with other code, it is
Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/attribute_methods/dirty.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
require 'active_support/deprecation' require 'active_support/deprecation'


module ActiveRecord module ActiveRecord
ActiveSupport.on_load(:active_record_model) do ActiveSupport.on_load(:active_record_config) do
mattr_accessor :partial_writes, instance_accessor: false mattr_accessor :partial_writes, instance_accessor: false
self.partial_writes = true self.partial_writes = true
end end
Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/attribute_methods/read.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,5 +1,5 @@
module ActiveRecord module ActiveRecord
ActiveSupport.on_load(:active_record_model) do ActiveSupport.on_load(:active_record_config) do
mattr_accessor :attribute_types_cached_by_default, instance_accessor: false mattr_accessor :attribute_types_cached_by_default, instance_accessor: false
end end


Expand Down
Original file line number Original file line Diff line number Diff line change
@@ -1,6 +1,6 @@


module ActiveRecord module ActiveRecord
ActiveSupport.on_load(:active_record_model) do ActiveSupport.on_load(:active_record_config) do
mattr_accessor :time_zone_aware_attributes, instance_accessor: false mattr_accessor :time_zone_aware_attributes, instance_accessor: false
self.time_zone_aware_attributes = false self.time_zone_aware_attributes = false


Expand Down
4 changes: 2 additions & 2 deletions activerecord/lib/active_record/base.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -323,6 +323,6 @@ module ActiveRecord #:nodoc:
class Base class Base
include ActiveRecord::Model include ActiveRecord::Model
end end

ActiveSupport.run_load_hooks(:active_record, Base)
end end

ActiveSupport.run_load_hooks(:active_record, ActiveRecord::Model::DeprecationProxy.new)
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/core.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require 'thread' require 'thread'


module ActiveRecord module ActiveRecord
ActiveSupport.on_load(:active_record_model) do ActiveSupport.on_load(:active_record_config) do
## ##
# :singleton-method: # :singleton-method:
# #
Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/explain.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'active_support/lazy_load_hooks' require 'active_support/lazy_load_hooks'


module ActiveRecord module ActiveRecord
ActiveSupport.on_load(:active_record_model) do ActiveSupport.on_load(:active_record_config) do
mattr_accessor :auto_explain_threshold_in_seconds, instance_accessor: false mattr_accessor :auto_explain_threshold_in_seconds, instance_accessor: false
end end


Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/inheritance.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,6 +1,6 @@


module ActiveRecord module ActiveRecord
ActiveSupport.on_load(:active_record_model) do ActiveSupport.on_load(:active_record_config) do
# Determine whether to store the full constant name including namespace when using STI # Determine whether to store the full constant name including namespace when using STI
mattr_accessor :store_full_sti_class, instance_accessor: false mattr_accessor :store_full_sti_class, instance_accessor: false
self.store_full_sti_class = true self.store_full_sti_class = true
Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/locking/optimistic.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,5 +1,5 @@
module ActiveRecord module ActiveRecord
ActiveSupport.on_load(:active_record_model) do ActiveSupport.on_load(:active_record_config) do
mattr_accessor :lock_optimistically, instance_accessor: false mattr_accessor :lock_optimistically, instance_accessor: false
self.lock_optimistically = true self.lock_optimistically = true
end end
Expand Down
45 changes: 38 additions & 7 deletions activerecord/lib/active_record/model.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -115,21 +115,52 @@ def inheritance_column
'type' 'type'
end end
end end

class DeprecationProxy < BasicObject #:nodoc:
def initialize(model = Model, base = Base)
@model = model
@base = base
end

def method_missing(name, *args, &block)
if @model.respond_to?(name, true)
@model.send(name, *args, &block)
else
::ActiveSupport::Deprecation.warn(
"The object passed to the active_record load hook was previously ActiveRecord::Base " \
"(a Class). Now it is ActiveRecord::Model (a Module). You have called `#{name}' which " \
"is only defined on ActiveRecord::Base. Please change your code so that it works with " \
"a module rather than a class. (Model is included in Base, so anything added to Model " \
"will be available on Base as well.)"
)
@base.send(name, *args, &block)
end
end

alias send method_missing

def extend(*mods)
::ActiveSupport::Deprecation.warn(
"The object passed to the active_record load hook was previously ActiveRecord::Base " \
"(a Class). Now it is ActiveRecord::Model (a Module). You have called `extend' which " \
"would add singleton methods to Model. This is presumably not what you want, since the " \
"methods would not be inherited down to Base. Rather than using extend, please use " \
"ActiveSupport::Concern + include, which will ensure that your class methods are " \
"inherited."
)
@base.extend(*mods)
end
end
end end


# This hook is where config accessors on Model should be defined. # This hook is where config accessors on Model get defined.
# #
# We don't want to just open the Model module and add stuff to it in other files, because # We don't want to just open the Model module and add stuff to it in other files, because
# that would cause Model to load, which causes all sorts of loading order issues. # that would cause Model to load, which causes all sorts of loading order issues.
# #
# We need this hook rather than just using the :active_record one, because users of the # We need this hook rather than just using the :active_record one, because users of the
# :active_record hook may need to use config options. # :active_record hook may need to use config options.
# ActiveSupport.run_load_hooks(:active_record_config, Model)
# Users who wish to include a module in Model that they want to also
# get inherited by Base should do so using this load hook. After Base
# has included Model, any modules subsequently included in Model won't
# be inherited by Base.
ActiveSupport.run_load_hooks(:active_record_model, Model)


# Load Base at this point, because the active_record load hook is run in that file. # Load Base at this point, because the active_record load hook is run in that file.
Base Base
Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/model_schema.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,6 +1,6 @@


module ActiveRecord module ActiveRecord
ActiveSupport.on_load(:active_record_model) do ActiveSupport.on_load(:active_record_config) do
mattr_accessor :primary_key_prefix_type, instance_accessor: false mattr_accessor :primary_key_prefix_type, instance_accessor: false


mattr_accessor :table_name_prefix, instance_accessor: false mattr_accessor :table_name_prefix, instance_accessor: false
Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/nested_attributes.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require 'active_support/core_ext/hash/indifferent_access' require 'active_support/core_ext/hash/indifferent_access'


module ActiveRecord module ActiveRecord
ActiveSupport.on_load(:active_record_model) do ActiveSupport.on_load(:active_record_config) do
mattr_accessor :nested_attributes_options, instance_accessor: false mattr_accessor :nested_attributes_options, instance_accessor: false
self.nested_attributes_options = {} self.nested_attributes_options = {}
end end
Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/serialization.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,5 +1,5 @@
module ActiveRecord #:nodoc: module ActiveRecord #:nodoc:
ActiveSupport.on_load(:active_record_model) do ActiveSupport.on_load(:active_record_config) do
mattr_accessor :include_root_in_json, instance_accessor: false mattr_accessor :include_root_in_json, instance_accessor: false
self.include_root_in_json = true self.include_root_in_json = true
end end
Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/timestamp.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,6 +1,6 @@


module ActiveRecord module ActiveRecord
ActiveSupport.on_load(:active_record_model) do ActiveSupport.on_load(:active_record_config) do
mattr_accessor :record_timestamps, instance_accessor: false mattr_accessor :record_timestamps, instance_accessor: false
self.record_timestamps = true self.record_timestamps = true
end end
Expand Down
36 changes: 36 additions & 0 deletions activerecord/test/cases/inclusion_test.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -82,6 +82,42 @@ def test_mirrored_configuration
def test_included_twice def test_included_twice
@klass.send :include, ActiveRecord::Model @klass.send :include, ActiveRecord::Model
end end

def test_deprecation_proxy
proxy = ActiveRecord::Model::DeprecationProxy.new

assert_equal ActiveRecord::Model.name, proxy.name
assert_equal ActiveRecord::Base.superclass, assert_deprecated { proxy.superclass }

sup, sup2 = nil, nil
ActiveSupport.on_load(:__test_active_record_model_deprecation) do
sup = superclass
sup2 = send(:superclass)
end
assert_deprecated do
ActiveSupport.run_load_hooks(:__test_active_record_model_deprecation, proxy)
end
assert_equal ActiveRecord::Base.superclass, sup
assert_equal ActiveRecord::Base.superclass, sup2
end

test "including in deprecation proxy" do
model, base = ActiveRecord::Model.dup, ActiveRecord::Base.dup
proxy = ActiveRecord::Model::DeprecationProxy.new(model, base)

mod = Module.new
proxy.include mod
assert model < mod
end

test "extending in deprecation proxy" do
model, base = ActiveRecord::Model.dup, ActiveRecord::Base.dup
proxy = ActiveRecord::Model::DeprecationProxy.new(model, base)

mod = Module.new
assert_deprecated { proxy.extend mod }
assert base.singleton_class < mod
end
end end


class InclusionFixturesTest < ActiveRecord::TestCase class InclusionFixturesTest < ActiveRecord::TestCase
Expand Down

0 comments on commit 4ed1563

Please sign in to comment.