activerecord 3.1.11 → 3.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG.md +6294 -97
- data/README.rdoc +2 -2
- data/examples/performance.rb +55 -31
- data/lib/active_record/aggregations.rb +2 -2
- data/lib/active_record/associations/association.rb +2 -42
- data/lib/active_record/associations/association_scope.rb +3 -30
- data/lib/active_record/associations/builder/association.rb +6 -4
- data/lib/active_record/associations/builder/belongs_to.rb +3 -3
- data/lib/active_record/associations/builder/collection_association.rb +2 -2
- data/lib/active_record/associations/builder/has_many.rb +4 -4
- data/lib/active_record/associations/builder/has_one.rb +5 -6
- data/lib/active_record/associations/builder/singular_association.rb +3 -16
- data/lib/active_record/associations/collection_association.rb +55 -28
- data/lib/active_record/associations/collection_proxy.rb +1 -35
- data/lib/active_record/associations/has_many_association.rb +5 -1
- data/lib/active_record/associations/has_many_through_association.rb +11 -8
- data/lib/active_record/associations/join_dependency.rb +1 -1
- data/lib/active_record/associations/preloader/association.rb +3 -1
- data/lib/active_record/associations.rb +82 -69
- data/lib/active_record/attribute_assignment.rb +221 -0
- data/lib/active_record/attribute_methods/deprecated_underscore_read.rb +32 -0
- data/lib/active_record/attribute_methods/dirty.rb +3 -3
- data/lib/active_record/attribute_methods/primary_key.rb +62 -25
- data/lib/active_record/attribute_methods/read.rb +72 -83
- data/lib/active_record/attribute_methods/serialization.rb +93 -0
- data/lib/active_record/attribute_methods/time_zone_conversion.rb +9 -14
- data/lib/active_record/attribute_methods/write.rb +27 -5
- data/lib/active_record/attribute_methods.rb +209 -30
- data/lib/active_record/autosave_association.rb +23 -8
- data/lib/active_record/base.rb +217 -1709
- data/lib/active_record/connection_adapters/abstract/connection_pool.rb +98 -132
- data/lib/active_record/connection_adapters/abstract/connection_specification.rb +82 -29
- data/lib/active_record/connection_adapters/abstract/database_statements.rb +13 -42
- data/lib/active_record/connection_adapters/abstract/query_cache.rb +1 -1
- data/lib/active_record/connection_adapters/abstract/quoting.rb +9 -12
- data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +36 -25
- data/lib/active_record/connection_adapters/abstract/schema_statements.rb +43 -22
- data/lib/active_record/connection_adapters/abstract_adapter.rb +78 -43
- data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +653 -0
- data/lib/active_record/connection_adapters/column.rb +2 -2
- data/lib/active_record/connection_adapters/mysql2_adapter.rb +138 -578
- data/lib/active_record/connection_adapters/mysql_adapter.rb +86 -658
- data/lib/active_record/connection_adapters/postgresql_adapter.rb +144 -94
- data/lib/active_record/connection_adapters/schema_cache.rb +50 -0
- data/lib/active_record/connection_adapters/sqlite3_adapter.rb +2 -6
- data/lib/active_record/connection_adapters/sqlite_adapter.rb +43 -22
- data/lib/active_record/counter_cache.rb +4 -3
- data/lib/active_record/dynamic_matchers.rb +79 -0
- data/lib/active_record/errors.rb +11 -1
- data/lib/active_record/explain.rb +83 -0
- data/lib/active_record/explain_subscriber.rb +21 -0
- data/lib/active_record/fixtures/file.rb +65 -0
- data/lib/active_record/fixtures.rb +31 -76
- data/lib/active_record/identity_map.rb +4 -11
- data/lib/active_record/inheritance.rb +167 -0
- data/lib/active_record/integration.rb +49 -0
- data/lib/active_record/locking/optimistic.rb +30 -25
- data/lib/active_record/locking/pessimistic.rb +23 -1
- data/lib/active_record/log_subscriber.rb +3 -3
- data/lib/active_record/migration/command_recorder.rb +8 -8
- data/lib/active_record/migration.rb +47 -30
- data/lib/active_record/model_schema.rb +366 -0
- data/lib/active_record/nested_attributes.rb +3 -2
- data/lib/active_record/persistence.rb +51 -9
- data/lib/active_record/querying.rb +58 -0
- data/lib/active_record/railtie.rb +24 -28
- data/lib/active_record/railties/controller_runtime.rb +3 -1
- data/lib/active_record/railties/databases.rake +134 -77
- data/lib/active_record/railties/jdbcmysql_error.rb +1 -1
- data/lib/active_record/readonly_attributes.rb +26 -0
- data/lib/active_record/reflection.rb +7 -15
- data/lib/active_record/relation/batches.rb +5 -2
- data/lib/active_record/relation/calculations.rb +27 -6
- data/lib/active_record/relation/delegation.rb +49 -0
- data/lib/active_record/relation/finder_methods.rb +6 -5
- data/lib/active_record/relation/predicate_builder.rb +12 -19
- data/lib/active_record/relation/query_methods.rb +76 -10
- data/lib/active_record/relation/spawn_methods.rb +11 -2
- data/lib/active_record/relation.rb +77 -34
- data/lib/active_record/result.rb +1 -1
- data/lib/active_record/sanitization.rb +194 -0
- data/lib/active_record/schema_dumper.rb +5 -2
- data/lib/active_record/scoping/default.rb +142 -0
- data/lib/active_record/scoping/named.rb +202 -0
- data/lib/active_record/scoping.rb +152 -0
- data/lib/active_record/serialization.rb +1 -43
- data/lib/active_record/serializers/xml_serializer.rb +2 -44
- data/lib/active_record/session_store.rb +15 -15
- data/lib/active_record/store.rb +50 -0
- data/lib/active_record/test_case.rb +11 -7
- data/lib/active_record/timestamp.rb +16 -3
- data/lib/active_record/transactions.rb +5 -5
- data/lib/active_record/translation.rb +22 -0
- data/lib/active_record/validations/associated.rb +5 -4
- data/lib/active_record/validations/uniqueness.rb +4 -4
- data/lib/active_record/validations.rb +1 -1
- data/lib/active_record/version.rb +2 -2
- data/lib/active_record.rb +28 -2
- data/lib/rails/generators/active_record/migration/migration_generator.rb +1 -1
- data/lib/rails/generators/active_record/migration/templates/migration.rb +9 -3
- data/lib/rails/generators/active_record/model/model_generator.rb +5 -1
- data/lib/rails/generators/active_record/model/templates/migration.rb +3 -5
- data/lib/rails/generators/active_record/session_migration/templates/migration.rb +1 -5
- metadata +50 -40
- checksums.yaml +0 -7
- data/lib/active_record/named_scope.rb +0 -200
data/lib/active_record.rb
CHANGED
|
@@ -34,10 +34,12 @@ module ActiveRecord
|
|
|
34
34
|
eager_autoload do
|
|
35
35
|
autoload :ActiveRecordError, 'active_record/errors'
|
|
36
36
|
autoload :ConnectionNotEstablished, 'active_record/errors'
|
|
37
|
+
autoload :ConnectionAdapters, 'active_record/connection_adapters/abstract_adapter'
|
|
37
38
|
|
|
38
39
|
autoload :Aggregations
|
|
39
40
|
autoload :Associations
|
|
40
41
|
autoload :AttributeMethods
|
|
42
|
+
autoload :AttributeAssignment
|
|
41
43
|
autoload :AutosaveAssociation
|
|
42
44
|
|
|
43
45
|
autoload :Relation
|
|
@@ -49,29 +51,42 @@ module ActiveRecord
|
|
|
49
51
|
autoload :PredicateBuilder
|
|
50
52
|
autoload :SpawnMethods
|
|
51
53
|
autoload :Batches
|
|
54
|
+
autoload :Explain
|
|
55
|
+
autoload :Delegation
|
|
52
56
|
end
|
|
53
57
|
|
|
54
58
|
autoload :Base
|
|
55
59
|
autoload :Callbacks
|
|
56
60
|
autoload :CounterCache
|
|
61
|
+
autoload :DynamicMatchers
|
|
57
62
|
autoload :DynamicFinderMatch
|
|
58
63
|
autoload :DynamicScopeMatch
|
|
64
|
+
autoload :Explain
|
|
65
|
+
autoload :IdentityMap
|
|
66
|
+
autoload :Inheritance
|
|
67
|
+
autoload :Integration
|
|
59
68
|
autoload :Migration
|
|
60
69
|
autoload :Migrator, 'active_record/migration'
|
|
61
|
-
autoload :
|
|
70
|
+
autoload :ModelSchema
|
|
62
71
|
autoload :NestedAttributes
|
|
63
72
|
autoload :Observer
|
|
64
73
|
autoload :Persistence
|
|
65
74
|
autoload :QueryCache
|
|
75
|
+
autoload :Querying
|
|
76
|
+
autoload :ReadonlyAttributes
|
|
66
77
|
autoload :Reflection
|
|
78
|
+
autoload :Result
|
|
79
|
+
autoload :Sanitization
|
|
67
80
|
autoload :Schema
|
|
68
81
|
autoload :SchemaDumper
|
|
82
|
+
autoload :Scoping
|
|
69
83
|
autoload :Serialization
|
|
70
84
|
autoload :SessionStore
|
|
85
|
+
autoload :Store
|
|
71
86
|
autoload :Timestamp
|
|
72
87
|
autoload :Transactions
|
|
88
|
+
autoload :Translation
|
|
73
89
|
autoload :Validations
|
|
74
|
-
autoload :IdentityMap
|
|
75
90
|
end
|
|
76
91
|
|
|
77
92
|
module Coders
|
|
@@ -89,6 +104,8 @@ module ActiveRecord
|
|
|
89
104
|
autoload :Read
|
|
90
105
|
autoload :TimeZoneConversion
|
|
91
106
|
autoload :Write
|
|
107
|
+
autoload :Serialization
|
|
108
|
+
autoload :DeprecatedUnderscoreRead
|
|
92
109
|
end
|
|
93
110
|
end
|
|
94
111
|
|
|
@@ -110,6 +127,15 @@ module ActiveRecord
|
|
|
110
127
|
end
|
|
111
128
|
end
|
|
112
129
|
|
|
130
|
+
module Scoping
|
|
131
|
+
extend ActiveSupport::Autoload
|
|
132
|
+
|
|
133
|
+
eager_autoload do
|
|
134
|
+
autoload :Named
|
|
135
|
+
autoload :Default
|
|
136
|
+
end
|
|
137
|
+
end
|
|
138
|
+
|
|
113
139
|
autoload :TestCase
|
|
114
140
|
autoload :TestFixtures, 'active_record/fixtures'
|
|
115
141
|
end
|
|
@@ -3,7 +3,7 @@ require 'rails/generators/active_record'
|
|
|
3
3
|
module ActiveRecord
|
|
4
4
|
module Generators
|
|
5
5
|
class MigrationGenerator < Base
|
|
6
|
-
argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
|
|
6
|
+
argument :attributes, :type => :array, :default => [], :banner => "field[:type][:index] field[:type][:index]"
|
|
7
7
|
|
|
8
8
|
def create_migration_file
|
|
9
9
|
set_local_assigns!
|
|
@@ -2,14 +2,20 @@ class <%= migration_class_name %> < ActiveRecord::Migration
|
|
|
2
2
|
<%- if migration_action == 'add' -%>
|
|
3
3
|
def change
|
|
4
4
|
<% attributes.each do |attribute| -%>
|
|
5
|
-
add_column :<%= table_name %>, :<%= attribute.name %>, :<%= attribute.type %>
|
|
5
|
+
add_column :<%= table_name %>, :<%= attribute.name %>, :<%= attribute.type %><%= attribute.inject_options %>
|
|
6
|
+
<%- if attribute.has_index? -%>
|
|
7
|
+
add_index :<%= table_name %>, :<%= attribute.index_name %><%= attribute.inject_index_options %>
|
|
8
|
+
<%- end %>
|
|
6
9
|
<%- end -%>
|
|
7
10
|
end
|
|
8
11
|
<%- else -%>
|
|
9
12
|
def up
|
|
10
13
|
<% attributes.each do |attribute| -%>
|
|
11
14
|
<%- if migration_action -%>
|
|
12
|
-
<%= migration_action %>_column :<%= table_name %>, :<%= attribute.name %><% if migration_action == 'add' %>, :<%= attribute.type %><% end %>
|
|
15
|
+
<%= migration_action %>_column :<%= table_name %>, :<%= attribute.name %><% if migration_action == 'add' %>, :<%= attribute.type %><%= attribute.inject_options %><% end %>
|
|
16
|
+
<% if attribute.has_index? && migration_action == 'add' %>
|
|
17
|
+
add_index :<%= table_name %>, :<%= attribute.index_name %><%= attribute.inject_index_options %>
|
|
18
|
+
<% end -%>
|
|
13
19
|
<%- end -%>
|
|
14
20
|
<%- end -%>
|
|
15
21
|
end
|
|
@@ -17,7 +23,7 @@ class <%= migration_class_name %> < ActiveRecord::Migration
|
|
|
17
23
|
def down
|
|
18
24
|
<% attributes.reverse.each do |attribute| -%>
|
|
19
25
|
<%- if migration_action -%>
|
|
20
|
-
<%= migration_action == 'add' ? 'remove' : 'add' %>_column :<%= table_name %>, :<%= attribute.name %><% if migration_action == 'remove' %>, :<%= attribute.type %><% end %>
|
|
26
|
+
<%= migration_action == 'add' ? 'remove' : 'add' %>_column :<%= table_name %>, :<%= attribute.name %><% if migration_action == 'remove' %>, :<%= attribute.type %><%= attribute.inject_options %><% end %>
|
|
21
27
|
<%- end -%>
|
|
22
28
|
<%- end -%>
|
|
23
29
|
end
|
|
@@ -3,7 +3,7 @@ require 'rails/generators/active_record'
|
|
|
3
3
|
module ActiveRecord
|
|
4
4
|
module Generators
|
|
5
5
|
class ModelGenerator < Base
|
|
6
|
-
argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
|
|
6
|
+
argument :attributes, :type => :array, :default => [], :banner => "field[:type][:index] field[:type][:index]"
|
|
7
7
|
|
|
8
8
|
check_class_collision
|
|
9
9
|
|
|
@@ -26,6 +26,10 @@ module ActiveRecord
|
|
|
26
26
|
template 'module.rb', File.join('app/models', "#{class_path.join('/')}.rb") if behavior == :invoke
|
|
27
27
|
end
|
|
28
28
|
|
|
29
|
+
def attributes_with_index
|
|
30
|
+
attributes.select { |a| a.has_index? || (a.reference? && options[:indexes]) }
|
|
31
|
+
end
|
|
32
|
+
|
|
29
33
|
hook_for :test_framework
|
|
30
34
|
|
|
31
35
|
protected
|
|
@@ -2,16 +2,14 @@ class <%= migration_class_name %> < ActiveRecord::Migration
|
|
|
2
2
|
def change
|
|
3
3
|
create_table :<%= table_name %> do |t|
|
|
4
4
|
<% attributes.each do |attribute| -%>
|
|
5
|
-
t.<%= attribute.type %> :<%= attribute.name %>
|
|
5
|
+
t.<%= attribute.type %> :<%= attribute.name %><%= attribute.inject_options %>
|
|
6
6
|
<% end -%>
|
|
7
7
|
<% if options[:timestamps] %>
|
|
8
8
|
t.timestamps
|
|
9
9
|
<% end -%>
|
|
10
10
|
end
|
|
11
|
-
<%
|
|
12
|
-
|
|
13
|
-
add_index :<%= table_name %>, :<%= attribute.name %>_id
|
|
14
|
-
<% end -%>
|
|
11
|
+
<% attributes_with_index.each do |attribute| -%>
|
|
12
|
+
add_index :<%= table_name %>, :<%= attribute.index_name %><%= attribute.inject_index_options %>
|
|
15
13
|
<% end -%>
|
|
16
14
|
end
|
|
17
15
|
end
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
class <%= migration_class_name %> < ActiveRecord::Migration
|
|
2
|
-
def
|
|
2
|
+
def change
|
|
3
3
|
create_table :<%= session_table_name %> do |t|
|
|
4
4
|
t.string :session_id, :null => false
|
|
5
5
|
t.text :data
|
|
@@ -9,8 +9,4 @@ class <%= migration_class_name %> < ActiveRecord::Migration
|
|
|
9
9
|
add_index :<%= session_table_name %>, :session_id
|
|
10
10
|
add_index :<%= session_table_name %>, :updated_at
|
|
11
11
|
end
|
|
12
|
-
|
|
13
|
-
def down
|
|
14
|
-
drop_table :<%= session_table_name %>
|
|
15
|
-
end
|
|
16
12
|
end
|
metadata
CHANGED
|
@@ -1,71 +1,60 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: activerecord
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 3.
|
|
4
|
+
version: 3.2.0
|
|
5
|
+
prerelease:
|
|
5
6
|
platform: ruby
|
|
6
7
|
authors:
|
|
7
8
|
- David Heinemeier Hansson
|
|
8
9
|
autorequire:
|
|
9
10
|
bindir: bin
|
|
10
11
|
cert_chain: []
|
|
11
|
-
date:
|
|
12
|
+
date: 2012-01-20 00:00:00.000000000 Z
|
|
12
13
|
dependencies:
|
|
13
14
|
- !ruby/object:Gem::Dependency
|
|
14
15
|
name: activesupport
|
|
15
|
-
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirement: &70230647834700 !ruby/object:Gem::Requirement
|
|
17
|
+
none: false
|
|
16
18
|
requirements:
|
|
17
|
-
- -
|
|
19
|
+
- - =
|
|
18
20
|
- !ruby/object:Gem::Version
|
|
19
|
-
version: 3.
|
|
21
|
+
version: 3.2.0
|
|
20
22
|
type: :runtime
|
|
21
23
|
prerelease: false
|
|
22
|
-
version_requirements:
|
|
23
|
-
requirements:
|
|
24
|
-
- - '='
|
|
25
|
-
- !ruby/object:Gem::Version
|
|
26
|
-
version: 3.1.11
|
|
24
|
+
version_requirements: *70230647834700
|
|
27
25
|
- !ruby/object:Gem::Dependency
|
|
28
26
|
name: activemodel
|
|
29
|
-
requirement: !ruby/object:Gem::Requirement
|
|
27
|
+
requirement: &70230647834260 !ruby/object:Gem::Requirement
|
|
28
|
+
none: false
|
|
30
29
|
requirements:
|
|
31
|
-
- -
|
|
30
|
+
- - =
|
|
32
31
|
- !ruby/object:Gem::Version
|
|
33
|
-
version: 3.
|
|
32
|
+
version: 3.2.0
|
|
34
33
|
type: :runtime
|
|
35
34
|
prerelease: false
|
|
36
|
-
version_requirements:
|
|
37
|
-
requirements:
|
|
38
|
-
- - '='
|
|
39
|
-
- !ruby/object:Gem::Version
|
|
40
|
-
version: 3.1.11
|
|
35
|
+
version_requirements: *70230647834260
|
|
41
36
|
- !ruby/object:Gem::Dependency
|
|
42
37
|
name: arel
|
|
43
|
-
requirement: !ruby/object:Gem::Requirement
|
|
38
|
+
requirement: &70230647833760 !ruby/object:Gem::Requirement
|
|
39
|
+
none: false
|
|
44
40
|
requirements:
|
|
45
|
-
- -
|
|
41
|
+
- - ~>
|
|
46
42
|
- !ruby/object:Gem::Version
|
|
47
|
-
version:
|
|
43
|
+
version: 3.0.0
|
|
48
44
|
type: :runtime
|
|
49
45
|
prerelease: false
|
|
50
|
-
version_requirements:
|
|
51
|
-
requirements:
|
|
52
|
-
- - "~>"
|
|
53
|
-
- !ruby/object:Gem::Version
|
|
54
|
-
version: 2.2.3
|
|
46
|
+
version_requirements: *70230647833760
|
|
55
47
|
- !ruby/object:Gem::Dependency
|
|
56
48
|
name: tzinfo
|
|
57
|
-
requirement: !ruby/object:Gem::Requirement
|
|
49
|
+
requirement: &70230647833280 !ruby/object:Gem::Requirement
|
|
50
|
+
none: false
|
|
58
51
|
requirements:
|
|
59
|
-
- -
|
|
52
|
+
- - ~>
|
|
60
53
|
- !ruby/object:Gem::Version
|
|
61
54
|
version: 0.3.29
|
|
62
55
|
type: :runtime
|
|
63
56
|
prerelease: false
|
|
64
|
-
version_requirements:
|
|
65
|
-
requirements:
|
|
66
|
-
- - "~>"
|
|
67
|
-
- !ruby/object:Gem::Version
|
|
68
|
-
version: 0.3.29
|
|
57
|
+
version_requirements: *70230647833280
|
|
69
58
|
description: Databases on Rails. Build a persistent domain model by mapping database
|
|
70
59
|
tables to Ruby classes. Strong conventions for associations, validations, aggregations,
|
|
71
60
|
migrations, and testing come baked-in.
|
|
@@ -120,11 +109,14 @@ files:
|
|
|
120
109
|
- lib/active_record/associations/singular_association.rb
|
|
121
110
|
- lib/active_record/associations/through_association.rb
|
|
122
111
|
- lib/active_record/associations.rb
|
|
112
|
+
- lib/active_record/attribute_assignment.rb
|
|
123
113
|
- lib/active_record/attribute_methods/before_type_cast.rb
|
|
114
|
+
- lib/active_record/attribute_methods/deprecated_underscore_read.rb
|
|
124
115
|
- lib/active_record/attribute_methods/dirty.rb
|
|
125
116
|
- lib/active_record/attribute_methods/primary_key.rb
|
|
126
117
|
- lib/active_record/attribute_methods/query.rb
|
|
127
118
|
- lib/active_record/attribute_methods/read.rb
|
|
119
|
+
- lib/active_record/attribute_methods/serialization.rb
|
|
128
120
|
- lib/active_record/attribute_methods/time_zone_conversion.rb
|
|
129
121
|
- lib/active_record/attribute_methods/write.rb
|
|
130
122
|
- lib/active_record/attribute_methods.rb
|
|
@@ -141,52 +133,69 @@ files:
|
|
|
141
133
|
- lib/active_record/connection_adapters/abstract/schema_definitions.rb
|
|
142
134
|
- lib/active_record/connection_adapters/abstract/schema_statements.rb
|
|
143
135
|
- lib/active_record/connection_adapters/abstract_adapter.rb
|
|
136
|
+
- lib/active_record/connection_adapters/abstract_mysql_adapter.rb
|
|
144
137
|
- lib/active_record/connection_adapters/column.rb
|
|
145
138
|
- lib/active_record/connection_adapters/mysql2_adapter.rb
|
|
146
139
|
- lib/active_record/connection_adapters/mysql_adapter.rb
|
|
147
140
|
- lib/active_record/connection_adapters/postgresql_adapter.rb
|
|
141
|
+
- lib/active_record/connection_adapters/schema_cache.rb
|
|
148
142
|
- lib/active_record/connection_adapters/sqlite3_adapter.rb
|
|
149
143
|
- lib/active_record/connection_adapters/sqlite_adapter.rb
|
|
150
144
|
- lib/active_record/connection_adapters/statement_pool.rb
|
|
151
145
|
- lib/active_record/counter_cache.rb
|
|
152
146
|
- lib/active_record/dynamic_finder_match.rb
|
|
147
|
+
- lib/active_record/dynamic_matchers.rb
|
|
153
148
|
- lib/active_record/dynamic_scope_match.rb
|
|
154
149
|
- lib/active_record/errors.rb
|
|
150
|
+
- lib/active_record/explain.rb
|
|
151
|
+
- lib/active_record/explain_subscriber.rb
|
|
152
|
+
- lib/active_record/fixtures/file.rb
|
|
155
153
|
- lib/active_record/fixtures.rb
|
|
156
154
|
- lib/active_record/identity_map.rb
|
|
155
|
+
- lib/active_record/inheritance.rb
|
|
156
|
+
- lib/active_record/integration.rb
|
|
157
157
|
- lib/active_record/locale/en.yml
|
|
158
158
|
- lib/active_record/locking/optimistic.rb
|
|
159
159
|
- lib/active_record/locking/pessimistic.rb
|
|
160
160
|
- lib/active_record/log_subscriber.rb
|
|
161
161
|
- lib/active_record/migration/command_recorder.rb
|
|
162
162
|
- lib/active_record/migration.rb
|
|
163
|
-
- lib/active_record/
|
|
163
|
+
- lib/active_record/model_schema.rb
|
|
164
164
|
- lib/active_record/nested_attributes.rb
|
|
165
165
|
- lib/active_record/observer.rb
|
|
166
166
|
- lib/active_record/persistence.rb
|
|
167
167
|
- lib/active_record/query_cache.rb
|
|
168
|
+
- lib/active_record/querying.rb
|
|
168
169
|
- lib/active_record/railtie.rb
|
|
169
170
|
- lib/active_record/railties/console_sandbox.rb
|
|
170
171
|
- lib/active_record/railties/controller_runtime.rb
|
|
171
172
|
- lib/active_record/railties/databases.rake
|
|
172
173
|
- lib/active_record/railties/jdbcmysql_error.rb
|
|
174
|
+
- lib/active_record/readonly_attributes.rb
|
|
173
175
|
- lib/active_record/reflection.rb
|
|
174
176
|
- lib/active_record/relation/batches.rb
|
|
175
177
|
- lib/active_record/relation/calculations.rb
|
|
178
|
+
- lib/active_record/relation/delegation.rb
|
|
176
179
|
- lib/active_record/relation/finder_methods.rb
|
|
177
180
|
- lib/active_record/relation/predicate_builder.rb
|
|
178
181
|
- lib/active_record/relation/query_methods.rb
|
|
179
182
|
- lib/active_record/relation/spawn_methods.rb
|
|
180
183
|
- lib/active_record/relation.rb
|
|
181
184
|
- lib/active_record/result.rb
|
|
185
|
+
- lib/active_record/sanitization.rb
|
|
182
186
|
- lib/active_record/schema.rb
|
|
183
187
|
- lib/active_record/schema_dumper.rb
|
|
188
|
+
- lib/active_record/scoping/default.rb
|
|
189
|
+
- lib/active_record/scoping/named.rb
|
|
190
|
+
- lib/active_record/scoping.rb
|
|
184
191
|
- lib/active_record/serialization.rb
|
|
185
192
|
- lib/active_record/serializers/xml_serializer.rb
|
|
186
193
|
- lib/active_record/session_store.rb
|
|
194
|
+
- lib/active_record/store.rb
|
|
187
195
|
- lib/active_record/test_case.rb
|
|
188
196
|
- lib/active_record/timestamp.rb
|
|
189
197
|
- lib/active_record/transactions.rb
|
|
198
|
+
- lib/active_record/translation.rb
|
|
190
199
|
- lib/active_record/validations/associated.rb
|
|
191
200
|
- lib/active_record/validations/uniqueness.rb
|
|
192
201
|
- lib/active_record/validations.rb
|
|
@@ -206,27 +215,28 @@ files:
|
|
|
206
215
|
- lib/rails/generators/active_record.rb
|
|
207
216
|
homepage: http://www.rubyonrails.org
|
|
208
217
|
licenses: []
|
|
209
|
-
metadata: {}
|
|
210
218
|
post_install_message:
|
|
211
219
|
rdoc_options:
|
|
212
|
-
-
|
|
220
|
+
- --main
|
|
213
221
|
- README.rdoc
|
|
214
222
|
require_paths:
|
|
215
223
|
- lib
|
|
216
224
|
required_ruby_version: !ruby/object:Gem::Requirement
|
|
225
|
+
none: false
|
|
217
226
|
requirements:
|
|
218
|
-
- -
|
|
227
|
+
- - ! '>='
|
|
219
228
|
- !ruby/object:Gem::Version
|
|
220
229
|
version: 1.8.7
|
|
221
230
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
231
|
+
none: false
|
|
222
232
|
requirements:
|
|
223
|
-
- -
|
|
233
|
+
- - ! '>='
|
|
224
234
|
- !ruby/object:Gem::Version
|
|
225
235
|
version: '0'
|
|
226
236
|
requirements: []
|
|
227
237
|
rubyforge_project:
|
|
228
|
-
rubygems_version:
|
|
238
|
+
rubygems_version: 1.8.10
|
|
229
239
|
signing_key:
|
|
230
|
-
specification_version:
|
|
240
|
+
specification_version: 3
|
|
231
241
|
summary: Object-relational mapper framework (part of Rails).
|
|
232
242
|
test_files: []
|
checksums.yaml
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
SHA1:
|
|
3
|
-
metadata.gz: b0b0e3dda9642a618a998223568a92014fd55475
|
|
4
|
-
data.tar.gz: 719b6bde220e0abd79e27b6457c92d8ce8bb511b
|
|
5
|
-
SHA512:
|
|
6
|
-
metadata.gz: 53b2dbbc486b8b41dd447b6cdf66d1ca264c44ecc818cf1ef993ccc2e6a3d8b515f074c093f4d6e32908ca89bd6ed74673f97183c2c7310a065a06308b550bb7
|
|
7
|
-
data.tar.gz: b161018a09558875e58f7773aef6953f8bfeada2f21c98423de067f6c2d97e2680a43e6365426e040e5e79e49dd006687ada6c8941bcc7c214ac11f09ebbf5b7
|
|
@@ -1,200 +0,0 @@
|
|
|
1
|
-
require 'active_support/core_ext/array'
|
|
2
|
-
require 'active_support/core_ext/hash/except'
|
|
3
|
-
require 'active_support/core_ext/kernel/singleton_class'
|
|
4
|
-
require 'active_support/core_ext/object/blank'
|
|
5
|
-
require 'active_support/core_ext/class/attribute'
|
|
6
|
-
|
|
7
|
-
module ActiveRecord
|
|
8
|
-
# = Active Record Named \Scopes
|
|
9
|
-
module NamedScope
|
|
10
|
-
extend ActiveSupport::Concern
|
|
11
|
-
|
|
12
|
-
module ClassMethods
|
|
13
|
-
# Returns an anonymous \scope.
|
|
14
|
-
#
|
|
15
|
-
# posts = Post.scoped
|
|
16
|
-
# posts.size # Fires "select count(*) from posts" and returns the count
|
|
17
|
-
# posts.each {|p| puts p.name } # Fires "select * from posts" and loads post objects
|
|
18
|
-
#
|
|
19
|
-
# fruits = Fruit.scoped
|
|
20
|
-
# fruits = fruits.where(:colour => 'red') if options[:red_only]
|
|
21
|
-
# fruits = fruits.limit(10) if limited?
|
|
22
|
-
#
|
|
23
|
-
# Anonymous \scopes tend to be useful when procedurally generating complex
|
|
24
|
-
# queries, where passing intermediate values (\scopes) around as first-class
|
|
25
|
-
# objects is convenient.
|
|
26
|
-
#
|
|
27
|
-
# You can define a \scope that applies to all finders using
|
|
28
|
-
# ActiveRecord::Base.default_scope.
|
|
29
|
-
def scoped(options = nil)
|
|
30
|
-
if options
|
|
31
|
-
scoped.apply_finder_options(options)
|
|
32
|
-
else
|
|
33
|
-
if current_scope
|
|
34
|
-
current_scope.clone
|
|
35
|
-
else
|
|
36
|
-
scope = relation.clone
|
|
37
|
-
scope.default_scoped = true
|
|
38
|
-
scope
|
|
39
|
-
end
|
|
40
|
-
end
|
|
41
|
-
end
|
|
42
|
-
|
|
43
|
-
##
|
|
44
|
-
# Collects attributes from scopes that should be applied when creating
|
|
45
|
-
# an AR instance for the particular class this is called on.
|
|
46
|
-
def scope_attributes # :nodoc:
|
|
47
|
-
if current_scope
|
|
48
|
-
current_scope.scope_for_create
|
|
49
|
-
else
|
|
50
|
-
scope = relation.clone
|
|
51
|
-
scope.default_scoped = true
|
|
52
|
-
scope.scope_for_create
|
|
53
|
-
end
|
|
54
|
-
end
|
|
55
|
-
|
|
56
|
-
##
|
|
57
|
-
# Are there default attributes associated with this scope?
|
|
58
|
-
def scope_attributes? # :nodoc:
|
|
59
|
-
current_scope || default_scopes.any?
|
|
60
|
-
end
|
|
61
|
-
|
|
62
|
-
# Adds a class method for retrieving and querying objects. A \scope represents a narrowing of a database query,
|
|
63
|
-
# such as <tt>where(:color => :red).select('shirts.*').includes(:washing_instructions)</tt>.
|
|
64
|
-
#
|
|
65
|
-
# class Shirt < ActiveRecord::Base
|
|
66
|
-
# scope :red, where(:color => 'red')
|
|
67
|
-
# scope :dry_clean_only, joins(:washing_instructions).where('washing_instructions.dry_clean_only = ?', true)
|
|
68
|
-
# end
|
|
69
|
-
#
|
|
70
|
-
# The above calls to <tt>scope</tt> define class methods Shirt.red and Shirt.dry_clean_only. Shirt.red,
|
|
71
|
-
# in effect, represents the query <tt>Shirt.where(:color => 'red')</tt>.
|
|
72
|
-
#
|
|
73
|
-
# Note that this is simply 'syntactic sugar' for defining an actual class method:
|
|
74
|
-
#
|
|
75
|
-
# class Shirt < ActiveRecord::Base
|
|
76
|
-
# def self.red
|
|
77
|
-
# where(:color => 'red')
|
|
78
|
-
# end
|
|
79
|
-
# end
|
|
80
|
-
#
|
|
81
|
-
# Unlike <tt>Shirt.find(...)</tt>, however, the object returned by Shirt.red is not an Array; it
|
|
82
|
-
# resembles the association object constructed by a <tt>has_many</tt> declaration. For instance,
|
|
83
|
-
# you can invoke <tt>Shirt.red.first</tt>, <tt>Shirt.red.count</tt>, <tt>Shirt.red.where(:size => 'small')</tt>.
|
|
84
|
-
# Also, just as with the association objects, named \scopes act like an Array, implementing Enumerable;
|
|
85
|
-
# <tt>Shirt.red.each(&block)</tt>, <tt>Shirt.red.first</tt>, and <tt>Shirt.red.inject(memo, &block)</tt>
|
|
86
|
-
# all behave as if Shirt.red really was an Array.
|
|
87
|
-
#
|
|
88
|
-
# These named \scopes are composable. For instance, <tt>Shirt.red.dry_clean_only</tt> will produce
|
|
89
|
-
# all shirts that are both red and dry clean only.
|
|
90
|
-
# Nested finds and calculations also work with these compositions: <tt>Shirt.red.dry_clean_only.count</tt>
|
|
91
|
-
# returns the number of garments for which these criteria obtain. Similarly with
|
|
92
|
-
# <tt>Shirt.red.dry_clean_only.average(:thread_count)</tt>.
|
|
93
|
-
#
|
|
94
|
-
# All \scopes are available as class methods on the ActiveRecord::Base descendant upon which
|
|
95
|
-
# the \scopes were defined. But they are also available to <tt>has_many</tt> associations. If,
|
|
96
|
-
#
|
|
97
|
-
# class Person < ActiveRecord::Base
|
|
98
|
-
# has_many :shirts
|
|
99
|
-
# end
|
|
100
|
-
#
|
|
101
|
-
# then <tt>elton.shirts.red.dry_clean_only</tt> will return all of Elton's red, dry clean
|
|
102
|
-
# only shirts.
|
|
103
|
-
#
|
|
104
|
-
# Named \scopes can also be procedural:
|
|
105
|
-
#
|
|
106
|
-
# class Shirt < ActiveRecord::Base
|
|
107
|
-
# scope :colored, lambda { |color| where(:color => color) }
|
|
108
|
-
# end
|
|
109
|
-
#
|
|
110
|
-
# In this example, <tt>Shirt.colored('puce')</tt> finds all puce shirts.
|
|
111
|
-
#
|
|
112
|
-
# On Ruby 1.9 you can use the 'stabby lambda' syntax:
|
|
113
|
-
#
|
|
114
|
-
# scope :colored, ->(color) { where(:color => color) }
|
|
115
|
-
#
|
|
116
|
-
# Note that scopes defined with \scope will be evaluated when they are defined, rather than
|
|
117
|
-
# when they are used. For example, the following would be incorrect:
|
|
118
|
-
#
|
|
119
|
-
# class Post < ActiveRecord::Base
|
|
120
|
-
# scope :recent, where('published_at >= ?', Time.now - 1.week)
|
|
121
|
-
# end
|
|
122
|
-
#
|
|
123
|
-
# The example above would be 'frozen' to the <tt>Time.now</tt> value when the <tt>Post</tt>
|
|
124
|
-
# class was defined, and so the resultant SQL query would always be the same. The correct
|
|
125
|
-
# way to do this would be via a lambda, which will re-evaluate the scope each time
|
|
126
|
-
# it is called:
|
|
127
|
-
#
|
|
128
|
-
# class Post < ActiveRecord::Base
|
|
129
|
-
# scope :recent, lambda { where('published_at >= ?', Time.now - 1.week) }
|
|
130
|
-
# end
|
|
131
|
-
#
|
|
132
|
-
# Named \scopes can also have extensions, just as with <tt>has_many</tt> declarations:
|
|
133
|
-
#
|
|
134
|
-
# class Shirt < ActiveRecord::Base
|
|
135
|
-
# scope :red, where(:color => 'red') do
|
|
136
|
-
# def dom_id
|
|
137
|
-
# 'red_shirts'
|
|
138
|
-
# end
|
|
139
|
-
# end
|
|
140
|
-
# end
|
|
141
|
-
#
|
|
142
|
-
# Scopes can also be used while creating/building a record.
|
|
143
|
-
#
|
|
144
|
-
# class Article < ActiveRecord::Base
|
|
145
|
-
# scope :published, where(:published => true)
|
|
146
|
-
# end
|
|
147
|
-
#
|
|
148
|
-
# Article.published.new.published # => true
|
|
149
|
-
# Article.published.create.published # => true
|
|
150
|
-
#
|
|
151
|
-
# Class methods on your model are automatically available
|
|
152
|
-
# on scopes. Assuming the following setup:
|
|
153
|
-
#
|
|
154
|
-
# class Article < ActiveRecord::Base
|
|
155
|
-
# scope :published, where(:published => true)
|
|
156
|
-
# scope :featured, where(:featured => true)
|
|
157
|
-
#
|
|
158
|
-
# def self.latest_article
|
|
159
|
-
# order('published_at desc').first
|
|
160
|
-
# end
|
|
161
|
-
#
|
|
162
|
-
# def self.titles
|
|
163
|
-
# map(&:title)
|
|
164
|
-
# end
|
|
165
|
-
#
|
|
166
|
-
# end
|
|
167
|
-
#
|
|
168
|
-
# We are able to call the methods like this:
|
|
169
|
-
#
|
|
170
|
-
# Article.published.featured.latest_article
|
|
171
|
-
# Article.featured.titles
|
|
172
|
-
|
|
173
|
-
def scope(name, scope_options = {})
|
|
174
|
-
name = name.to_sym
|
|
175
|
-
valid_scope_name?(name)
|
|
176
|
-
extension = Module.new(&Proc.new) if block_given?
|
|
177
|
-
|
|
178
|
-
scope_proc = lambda do |*args|
|
|
179
|
-
options = scope_options.respond_to?(:call) ? unscoped { scope_options.call(*args) } : scope_options
|
|
180
|
-
options = scoped.apply_finder_options(options) if options.is_a?(Hash)
|
|
181
|
-
|
|
182
|
-
relation = scoped.merge(options)
|
|
183
|
-
|
|
184
|
-
extension ? relation.extending(extension) : relation
|
|
185
|
-
end
|
|
186
|
-
|
|
187
|
-
singleton_class.send(:redefine_method, name, &scope_proc)
|
|
188
|
-
end
|
|
189
|
-
|
|
190
|
-
protected
|
|
191
|
-
|
|
192
|
-
def valid_scope_name?(name)
|
|
193
|
-
if respond_to?(name, true)
|
|
194
|
-
logger.warn "Creating scope :#{name}. " \
|
|
195
|
-
"Overwriting existing method #{self.name}.#{name}."
|
|
196
|
-
end
|
|
197
|
-
end
|
|
198
|
-
end
|
|
199
|
-
end
|
|
200
|
-
end
|