1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Deal with global config better between AR::Base and AR::Model

This commit is contained in:
Jon Leighton 2011-12-23 21:37:52 +00:00
parent 8c67e709d0
commit ba7ec73f6a
12 changed files with 147 additions and 83 deletions

View file

@ -57,6 +57,7 @@ module ActiveRecord
autoload :Base
autoload :Callbacks
autoload :Configuration
autoload :Core
autoload :CounterCache
autoload :DynamicMatchers

View file

@ -5,10 +5,7 @@ module ActiveRecord
ATTRIBUTE_TYPES_CACHED_BY_DEFAULT = [:datetime, :timestamp, :time, :date]
included do
cattr_accessor :attribute_types_cached_by_default, :instance_writer => false
self.attribute_types_cached_by_default = ATTRIBUTE_TYPES_CACHED_BY_DEFAULT
end
Configuration.define :attribute_types_cached_by_default, ATTRIBUTE_TYPES_CACHED_BY_DEFAULT
module ClassMethods
# +cache_attributes+ allows you to declare which converted attribute values should

View file

@ -6,10 +6,9 @@ module ActiveRecord
module TimeZoneConversion
extend ActiveSupport::Concern
included do
cattr_accessor :time_zone_aware_attributes, :instance_writer => false
self.time_zone_aware_attributes = false
Configuration.define :time_zone_aware_attributes, false
included do
class_attribute :skip_time_zone_conversion_for_attributes, :instance_writer => false
self.skip_time_zone_conversion_for_attributes = []
end

View file

@ -328,7 +328,6 @@ module ActiveRecord #:nodoc:
# instances in the current object space.
class Base
include ActiveRecord::Model
self.connection_handler = ConnectionAdapters::ConnectionHandler.new
end
end

View file

@ -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

View file

@ -4,67 +4,64 @@ module ActiveRecord
module Core
extend ActiveSupport::Concern
##
# :singleton-method:
# Accepts a logger conforming to the interface of Log4r or the default Ruby 1.8+ Logger class,
# which is then passed on to any new database connections made and which can be retrieved on both
# a class and instance level by calling +logger+.
Configuration.define :logger
##
# :singleton-method:
# Contains the database configuration - as is typically stored in config/database.yml -
# as a Hash.
#
# For example, the following database.yml...
#
# development:
# adapter: sqlite3
# database: db/development.sqlite3
#
# production:
# adapter: sqlite3
# database: db/production.sqlite3
#
# ...would result in ActiveRecord::Base.configurations to look like this:
#
# {
# 'development' => {
# 'adapter' => 'sqlite3',
# 'database' => 'db/development.sqlite3'
# },
# 'production' => {
# 'adapter' => 'sqlite3',
# 'database' => 'db/production.sqlite3'
# }
# }
Configuration.define :configurations, {}
##
# :singleton-method:
# Determines whether to use Time.local (using :local) or Time.utc (using :utc) when pulling
# dates and times from the database. This is set to :local by default.
Configuration.define :default_timezone, :local
##
# :singleton-method:
# Specifies the format to use when dumping the database schema with Rails'
# Rakefile. If :sql, the schema is dumped as (potentially database-
# specific) SQL statements. If :ruby, the schema is dumped as an
# ActiveRecord::Schema file which can be loaded into any database that
# supports migrations. Use :ruby if you want to have different database
# adapters for, e.g., your development and test environments.
Configuration.define :schema_format, :ruby
##
# :singleton-method:
# Specify whether or not to use timestamps for migration versions
Configuration.define :timestamped_migrations, true
included do
##
# :singleton-method:
# Accepts a logger conforming to the interface of Log4r or the default Ruby 1.8+ Logger class,
# which is then passed on to any new database connections made and which can be retrieved on both
# a class and instance level by calling +logger+.
cattr_accessor :logger, :instance_writer => false
##
# :singleton-method:
# Contains the database configuration - as is typically stored in config/database.yml -
# as a Hash.
#
# For example, the following database.yml...
#
# development:
# adapter: sqlite3
# database: db/development.sqlite3
#
# production:
# adapter: sqlite3
# database: db/production.sqlite3
#
# ...would result in ActiveRecord::Base.configurations to look like this:
#
# {
# 'development' => {
# 'adapter' => 'sqlite3',
# 'database' => 'db/development.sqlite3'
# },
# 'production' => {
# 'adapter' => 'sqlite3',
# 'database' => 'db/production.sqlite3'
# }
# }
cattr_accessor :configurations, :instance_writer => false
self.configurations = {}
##
# :singleton-method:
# Determines whether to use Time.local (using :local) or Time.utc (using :utc) when pulling
# dates and times from the database. This is set to :local by default.
cattr_accessor :default_timezone, :instance_writer => false
self.default_timezone = :local
##
# :singleton-method:
# Specifies the format to use when dumping the database schema with Rails'
# Rakefile. If :sql, the schema is dumped as (potentially database-
# specific) SQL statements. If :ruby, the schema is dumped as an
# ActiveRecord::Schema file which can be loaded into any database that
# supports migrations. Use :ruby if you want to have different database
# adapters for, e.g., your development and test environments.
cattr_accessor :schema_format , :instance_writer => false
self.schema_format = :ruby
##
# :singleton-method:
# Specify whether or not to use timestamps for migration versions
cattr_accessor :timestamped_migrations , :instance_writer => false
self.timestamped_migrations = true
##
# :singleton-method:

View file

@ -48,10 +48,7 @@ module ActiveRecord
module Optimistic
extend ActiveSupport::Concern
included do
cattr_accessor :lock_optimistically, :instance_writer => false
self.lock_optimistically = true
end
Configuration.define :lock_optimistically, true
def locking_enabled? #:nodoc:
self.class.locking_enabled?

View file

@ -9,6 +9,8 @@ module ActiveRecord
base.class_eval do
include Tag
include Configuration
include ActiveRecord::Persistence
extend ActiveModel::Naming
extend QueryCache::ClassMethods
@ -43,7 +45,7 @@ module ActiveRecord
include Core
self.connection_handler = ActiveRecord::Base.connection_handler
self.connection_handler = Base.connection_handler
end
end
end

View file

@ -1,20 +1,20 @@
require 'active_support/concern'
require 'active_support/core_ext/class/attribute_accessors'
module ActiveRecord
module ModelSchema
extend ActiveSupport::Concern
included do
##
# :singleton-method:
# Accessor for the prefix type that will be prepended to every primary key column name.
# The options are :table_name and :table_name_with_underscore. If the first is specified,
# the Product class will look for "productid" instead of "id" as the primary column. If the
# latter is specified, the Product class will look for "product_id" instead of "id". Remember
# that this is a global setting for all Active Records.
cattr_accessor :primary_key_prefix_type, :instance_writer => false
self.primary_key_prefix_type = nil
##
# :singleton-method:
# Accessor for the prefix type that will be prepended to every primary key column name.
# The options are :table_name and :table_name_with_underscore. If the first is specified,
# the Product class will look for "productid" instead of "id" as the primary column. If the
# latter is specified, the Product class will look for "product_id" instead of "id". Remember
# that this is a global setting for all Active Records.
Configuration.define :primary_key_prefix_type
included do
##
# :singleton-method:
# Accessor for the name of the prefix string to prepend to every table name. So if set

View file

@ -18,6 +18,7 @@ module ActiveRecord
def self.active_record_super; Base; end
def self.base_class; self; end
include ActiveRecord::Configuration
include ActiveRecord::AttributeMethods
def self.column_names

View file

@ -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

View file

@ -41,4 +41,13 @@ class InclusionUnitTest < ActiveRecord::TestCase
def test_connection_handler
assert_equal ActiveRecord::Base.connection_handler, @klass.connection_handler
end
def test_mirrored_configuration
ActiveRecord::Base.time_zone_aware_attributes = true
assert @klass.time_zone_aware_attributes
ActiveRecord::Base.time_zone_aware_attributes = false
assert !@klass.time_zone_aware_attributes
ensure
ActiveRecord::Base.time_zone_aware_attributes = false
end
end