Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
6af7192a
R
rails
项目概览
张重言
/
rails
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rails
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
6af7192a
编写于
12月 18, 2011
作者:
J
Jon Leighton
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
I herd you like modules.
上级
b2c9ce34
变更
15
隐藏空白更改
内联
并排
Showing
15 changed file
with
153 addition
and
71 deletion
+153
-71
activerecord/lib/active_record/base.rb
activerecord/lib/active_record/base.rb
+2
-0
activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb
...ve_record/connection_adapters/abstract/connection_pool.rb
+6
-1
activerecord/lib/active_record/connection_adapters/abstract/connection_specification.rb
.../connection_adapters/abstract/connection_specification.rb
+48
-52
activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb
...d/lib/active_record/connection_adapters/mysql2_adapter.rb
+2
-2
activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
...rd/lib/active_record/connection_adapters/mysql_adapter.rb
+2
-2
activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
...b/active_record/connection_adapters/postgresql_adapter.rb
+3
-3
activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb
.../lib/active_record/connection_adapters/sqlite3_adapter.rb
+2
-2
activerecord/lib/active_record/core.rb
activerecord/lib/active_record/core.rb
+12
-1
activerecord/lib/active_record/inheritance.rb
activerecord/lib/active_record/inheritance.rb
+10
-4
activerecord/lib/active_record/model.rb
activerecord/lib/active_record/model.rb
+10
-1
activerecord/lib/active_record/model_schema.rb
activerecord/lib/active_record/model_schema.rb
+2
-2
activerecord/test/cases/connection_specification/resolver_test.rb
...cord/test/cases/connection_specification/resolver_test.rb
+1
-1
activerecord/test/cases/inclusion_test.rb
activerecord/test/cases/inclusion_test.rb
+36
-0
activerecord/test/models/teapot.rb
activerecord/test/models/teapot.rb
+13
-0
activerecord/test/schema/schema.rb
activerecord/test/schema/schema.rb
+4
-0
未找到文件。
activerecord/lib/active_record/base.rb
浏览文件 @
6af7192a
...
...
@@ -328,5 +328,7 @@ module ActiveRecord #:nodoc:
# instances in the current object space.
class
Base
include
ActiveRecord
::
Model
self
.
connection_handler
=
ConnectionAdapters
::
ConnectionHandler
.
new
end
end
activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb
浏览文件 @
6af7192a
...
...
@@ -371,7 +371,12 @@ def retrieve_connection_pool(klass)
pool
=
@class_to_pool
[
klass
.
name
]
return
pool
if
pool
return
nil
if
ActiveRecord
::
Base
==
klass
retrieve_connection_pool
klass
.
superclass
if
klass
.
superclass
&&
klass
.
superclass
<
Model
retrieve_connection_pool
klass
.
superclass
else
retrieve_connection_pool
ActiveRecord
::
Base
end
end
end
...
...
activerecord/lib/active_record/connection_adapters/abstract/connection_specification.rb
浏览文件 @
6af7192a
require
'active_support/core_ext/module/delegation'
module
ActiveRecord
class
Bas
e
module
Cor
e
class
ConnectionSpecification
#:nodoc:
attr_reader
:config
,
:adapter_method
def
initialize
(
config
,
adapter_method
)
...
...
@@ -75,12 +77,6 @@ def connection_url_to_hash(url) # :nodoc:
end
end
##
# :singleton-method:
# The connection handler
class_attribute
:connection_handler
,
:instance_writer
=>
false
self
.
connection_handler
=
ConnectionAdapters
::
ConnectionHandler
.
new
# Returns the connection currently associated with the class. This can
# also be used to "borrow" the connection to do database work that isn't
# easily done without going straight to SQL.
...
...
@@ -88,53 +84,53 @@ def connection
self
.
class
.
connection
end
# Establishes the connection to the database. Accepts a hash as input where
# the <tt>:adapter</tt> key must be specified with the name of a database adapter (in lower-case)
# example for regular databases (MySQL, Postgresql, etc):
#
# ActiveRecord::Base.establish_connection(
# :adapter => "mysql",
# :host => "localhost",
# :username => "myuser",
# :password => "mypass",
# :database => "somedatabase"
# )
#
# Example for SQLite database:
#
# ActiveRecord::Base.establish_connection(
# :adapter => "sqlite",
# :database => "path/to/dbfile"
# )
#
# Also accepts keys as strings (for parsing from YAML for example):
#
# ActiveRecord::Base.establish_connection(
# "adapter" => "sqlite",
# "database" => "path/to/dbfile"
# )
#
# Or a URL:
#
# ActiveRecord::Base.establish_connection(
# "postgres://myuser:mypass@localhost/somedatabase"
# )
#
# The exceptions AdapterNotSpecified, AdapterNotFound and ArgumentError
# may be returned on an error.
def
self
.
establish_connection
(
spec
=
ENV
[
"DATABASE_URL"
])
resolver
=
ConnectionSpecification
::
Resolver
.
new
spec
,
configurations
spec
=
resolver
.
spec
unless
respond_to?
(
spec
.
adapter_method
)
raise
AdapterNotFound
,
"database configuration specifies nonexistent
#{
spec
.
config
[
:adapter
]
}
adapter"
end
module
ClassMethods
# Establishes the connection to the database. Accepts a hash as input where
# the <tt>:adapter</tt> key must be specified with the name of a database adapter (in lower-case)
# example for regular databases (MySQL, Postgresql, etc):
#
# ActiveRecord::Base.establish_connection(
# :adapter => "mysql",
# :host => "localhost",
# :username => "myuser",
# :password => "mypass",
# :database => "somedatabase"
# )
#
# Example for SQLite database:
#
# ActiveRecord::Base.establish_connection(
# :adapter => "sqlite",
# :database => "path/to/dbfile"
# )
#
# Also accepts keys as strings (for parsing from YAML for example):
#
# ActiveRecord::Base.establish_connection(
# "adapter" => "sqlite",
# "database" => "path/to/dbfile"
# )
#
# Or a URL:
#
# ActiveRecord::Base.establish_connection(
# "postgres://myuser:mypass@localhost/somedatabase"
# )
#
# The exceptions AdapterNotSpecified, AdapterNotFound and ArgumentError
# may be returned on an error.
def
establish_connection
(
spec
=
ENV
[
"DATABASE_URL"
])
resolver
=
ConnectionSpecification
::
Resolver
.
new
spec
,
configurations
spec
=
resolver
.
spec
unless
respond_to?
(
spec
.
adapter_method
)
raise
AdapterNotFound
,
"database configuration specifies nonexistent
#{
spec
.
config
[
:adapter
]
}
adapter"
end
remove_connection
connection_handler
.
establish_connection
name
,
spec
end
remove_connection
connection_handler
.
establish_connection
name
,
spec
end
class
<<
self
# Returns the connection currently associated with the class. This can
# also be used to "borrow" the connection to do database work unrelated
# to any of the specific Active Records.
...
...
activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb
浏览文件 @
6af7192a
...
...
@@ -4,9 +4,9 @@
require
'mysql2'
module
ActiveRecord
class
Base
module
Core::ClassMethods
# Establishes a connection to the database that's used by all Active Record objects.
def
self
.
mysql2_connection
(
config
)
def
mysql2_connection
(
config
)
config
[
:username
]
=
'root'
if
config
[
:username
].
nil?
if
Mysql2
::
Client
.
const_defined?
:FOUND_ROWS
...
...
activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
浏览文件 @
6af7192a
...
...
@@ -18,9 +18,9 @@ class Result; include Enumerable end
end
module
ActiveRecord
class
Base
module
Core::ClassMethods
# Establishes a connection to the database that's used by all Active Record objects.
def
self
.
mysql_connection
(
config
)
# :nodoc:
def
mysql_connection
(
config
)
# :nodoc:
config
=
config
.
symbolize_keys
host
=
config
[
:host
]
port
=
config
[
:port
]
...
...
activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
浏览文件 @
6af7192a
...
...
@@ -7,9 +7,9 @@
require
'pg'
module
ActiveRecord
class
Base
module
Core::ClassMethods
# Establishes a connection to the database that's used by all Active Record objects
def
self
.
postgresql_connection
(
config
)
# :nodoc:
def
postgresql_connection
(
config
)
# :nodoc:
config
=
config
.
symbolize_keys
host
=
config
[
:host
]
port
=
config
[
:port
]
||
5432
...
...
@@ -876,7 +876,7 @@ def indexes(table_name, name = nil)
# add info on sort order for columns (only desc order is explicitly specified, asc is the default)
desc_order_columns
=
inddef
.
scan
(
/(\w+) DESC/
).
flatten
orders
=
desc_order_columns
.
any?
?
Hash
[
desc_order_columns
.
map
{
|
order_column
|
[
order_column
,
:desc
]}]
:
{}
column_names
.
empty?
?
nil
:
IndexDefinition
.
new
(
table_name
,
index_name
,
unique
,
column_names
,
[],
orders
)
end
.
compact
end
...
...
activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb
浏览文件 @
6af7192a
...
...
@@ -4,9 +4,9 @@
require
'sqlite3'
module
ActiveRecord
class
Base
module
Core::ClassMethods
# sqlite3 adapter reuses sqlite_connection.
def
s
elf
.
s
qlite3_connection
(
config
)
# :nodoc:
def
sqlite3_connection
(
config
)
# :nodoc:
# Require database.
unless
config
[
:database
]
raise
ArgumentError
,
"No database file specified. Missing argument: database"
...
...
activerecord/lib/active_record/core.rb
浏览文件 @
6af7192a
...
...
@@ -65,6 +65,11 @@ module Core
# Specify whether or not to use timestamps for migration versions
cattr_accessor
:timestamped_migrations
,
:instance_writer
=>
false
self
.
timestamped_migrations
=
true
##
# :singleton-method:
# The connection handler
class_attribute
:connection_handler
,
:instance_writer
=>
false
end
module
ClassMethods
...
...
@@ -111,7 +116,13 @@ def arel_engine
if
self
==
ActiveRecord
::
Base
ActiveRecord
::
Base
else
connection_handler
.
connection_pools
[
name
]
?
self
:
superclass
.
arel_engine
if
connection_handler
.
connection_pools
[
name
]
self
elsif
superclass
<
ActiveRecord
::
Model
superclass
.
arel_engine
else
ActiveRecord
::
Base
end
end
end
end
...
...
activerecord/lib/active_record/inheritance.rb
浏览文件 @
6af7192a
...
...
@@ -13,7 +13,9 @@ module Inheritance
module
ClassMethods
# True if this isn't a concrete subclass needing a STI type condition.
def
descends_from_active_record?
if
superclass
.
abstract_class?
if
!
(
superclass
<
Model
)
true
elsif
superclass
.
abstract_class?
superclass
.
descends_from_active_record?
else
superclass
==
Base
||
!
columns_hash
.
include?
(
inheritance_column
)
...
...
@@ -84,10 +86,14 @@ def instantiate(record)
# Returns the class descending directly from ActiveRecord::Base or an
# abstract class, if any, in the inheritance hierarchy.
def
class_of_active_record_descendant
(
klass
)
if
klass
==
Base
||
klass
.
superclass
==
Base
||
klass
.
superclass
.
abstract_class?
klass
elsif
klass
.
superclass
.
nil?
unless
klass
<
Model
::
Tag
raise
ActiveRecordError
,
"
#{
name
}
doesn't belong in a hierarchy descending from ActiveRecord"
end
if
klass
==
Base
||
klass
.
superclass
==
Base
||
klass
.
superclass
<
Model
::
Tag
&&
klass
.
superclass
.
abstract_class?
||
!
(
klass
.
superclass
<
Model
::
Tag
)
klass
else
class_of_active_record_descendant
(
klass
.
superclass
)
end
...
...
activerecord/lib/active_record/model.rb
浏览文件 @
6af7192a
module
ActiveRecord
module
Model
# So we can recognise an AR class even while self.included is being
# executed. (At that time, klass < Model == false.)
module
Tag
#:nodoc:
end
def
self
.
included
(
base
)
base
.
class_eval
do
include
Tag
include
ActiveRecord
::
Persistence
extend
ActiveModel
::
Naming
extend
QueryCache
::
ClassMethods
...
...
@@ -35,10 +42,12 @@ def self.included(base)
include
Aggregations
,
Transactions
,
Reflection
,
Serialization
,
Store
include
Core
self
.
connection_handler
=
ActiveRecord
::
Base
.
connection_handler
end
end
end
end
require
'active_record/connection_adapters/abstract/connection_specification'
ActiveSupport
.
run_load_hooks
(
:active_record
,
ActiveRecord
::
Base
)
ActiveSupport
.
run_load_hooks
(
:active_record
,
ActiveRecord
::
Model
)
activerecord/lib/active_record/model_schema.rb
浏览文件 @
6af7192a
...
...
@@ -128,7 +128,7 @@ def quoted_table_name
# Computes the table name, (re)sets it internally, and returns it.
def
reset_table_name
#:nodoc:
if
superclass
.
abstract_class?
if
(
superclass
<
ActiveRecord
::
Model
)
&&
superclass
.
abstract_class?
self
.
table_name
=
superclass
.
table_name
||
compute_table_name
elsif
abstract_class?
self
.
table_name
=
superclass
==
Base
?
nil
:
superclass
.
table_name
...
...
@@ -143,7 +143,7 @@ def full_table_name_prefix #:nodoc:
# The name of the column containing the object's class when Single Table Inheritance is used
def
inheritance_column
if
self
==
Base
if
self
==
Base
||
!
(
superclass
<
Model
)
'type'
else
(
@inheritance_column
||=
nil
)
||
superclass
.
inheritance_column
...
...
activerecord/test/cases/connection_specification/resolver_test.rb
浏览文件 @
6af7192a
require
"cases/helper"
module
ActiveRecord
class
Bas
e
module
Cor
e
class
ConnectionSpecification
class
ResolverTest
<
ActiveRecord
::
TestCase
def
resolve
(
spec
)
...
...
activerecord/test/cases/inclusion_test.rb
0 → 100644
浏览文件 @
6af7192a
require
'cases/helper'
require
'models/teapot'
class
BasicInclusionModelTest
<
ActiveRecord
::
TestCase
def
test_basic_model
Teapot
.
create!
(
:name
=>
"Ronnie Kemper"
)
assert_equal
"Ronnie Kemper"
,
Teapot
.
find
(
1
).
name
end
end
class
InclusionUnitTest
<
ActiveRecord
::
TestCase
def
setup
@klass
=
Class
.
new
{
include
ActiveRecord
::
Model
}
end
def
test_non_abstract_class
assert
!
@klass
.
abstract_class?
end
def
test_abstract_class
@klass
.
abstract_class
=
true
assert
@klass
.
abstract_class?
end
def
test_establish_connection
assert
@klass
.
respond_to?
(
:establish_connection
)
end
def
test_adapter_connection
assert
@klass
.
respond_to?
(
"
#{
ActiveRecord
::
Base
.
connection_config
[
:adapter
]
}
_connection"
)
end
def
test_connection_handler
assert_equal
ActiveRecord
::
Base
.
connection_handler
,
@klass
.
connection_handler
end
end
activerecord/test/models/teapot.rb
0 → 100644
浏览文件 @
6af7192a
class
Teapot
# I'm a little teapot,
# Short and stout,
# Here is my handle
# Here is my spout
# When I get all steamed up,
# Hear me shout,
# Tip me over and pour me out!
#
# HELL YEAH TEAPOT SONG
include
ActiveRecord
::
Model
end
activerecord/test/schema/schema.rb
浏览文件 @
6af7192a
...
...
@@ -596,6 +596,10 @@ def create_table(*args, &block)
t
.
datetime
:ending
end
create_table
:teapots
,
:force
=>
true
do
|
t
|
t
.
string
:name
end
create_table
:topics
,
:force
=>
true
do
|
t
|
t
.
string
:title
t
.
string
:author_name
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录