-
Notifications
You must be signed in to change notification settings - Fork 21.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Deal with global config better between AR::Base and AR::Model
- Loading branch information
1 parent
8c67e70
commit ba7ec73
Showing
12 changed files
with
147 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
require 'active_support/concern' | ||
|
||
module ActiveRecord | ||
# This module allows configuration options to be specified in a way such that | ||
# ActiveRecord::Base and ActiveRecord::Model will have access to the same value, | ||
# and will automatically get the appropriate readers and writers defined. | ||
# | ||
# In the future, we should probably move away from defining global config | ||
# directly on ActiveRecord::Base / ActiveRecord::Model. | ||
module Configuration #:nodoc: | ||
extend ActiveSupport::Concern | ||
|
||
module ClassMethods | ||
end | ||
|
||
def self.define(name, default = nil) | ||
singleton_class.send(:attr_accessor, name) | ||
|
||
[self, ClassMethods].each do |klass| | ||
klass.class_eval <<-CODE, __FILE__, __LINE__ | ||
def #{name} | ||
ActiveRecord::Configuration.#{name} | ||
end | ||
CODE | ||
end | ||
|
||
ClassMethods.class_eval <<-CODE, __FILE__, __LINE__ | ||
def #{name}=(val) | ||
ActiveRecord::Configuration.#{name} = val | ||
end | ||
CODE | ||
|
||
send("#{name}=", default) unless default.nil? | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
require 'cases/helper' | ||
|
||
class ConfigurationTest < ActiveRecord::TestCase | ||
def test_configuration | ||
@klass = Class.new do | ||
include ActiveRecord::Configuration | ||
end | ||
|
||
ActiveRecord::Configuration.define :omg | ||
|
||
ActiveRecord::Configuration.omg = "omg" | ||
|
||
assert_equal "omg", @klass.new.omg | ||
assert !@klass.new.respond_to?(:omg=) | ||
assert_equal "omg", @klass.omg | ||
|
||
@klass.omg = "wtf" | ||
|
||
assert_equal "wtf", @klass.omg | ||
assert_equal "wtf", @klass.new.omg | ||
ensure | ||
ActiveRecord::Configuration.send(:undef_method, :omg) | ||
ActiveRecord::Configuration::ClassMethods.send(:undef_method, :omg) | ||
ActiveRecord::Configuration::ClassMethods.send(:undef_method, :omg=) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters