activerecord 3.1.9 → 3.2.12

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.
Files changed (112) hide show
  1. checksums.yaml +6 -6
  2. data/CHANGELOG.md +317 -336
  3. data/README.rdoc +3 -3
  4. data/examples/performance.rb +20 -1
  5. data/lib/active_record/aggregations.rb +1 -1
  6. data/lib/active_record/associations/alias_tracker.rb +3 -6
  7. data/lib/active_record/associations/association.rb +3 -42
  8. data/lib/active_record/associations/association_scope.rb +3 -15
  9. data/lib/active_record/associations/builder/association.rb +6 -4
  10. data/lib/active_record/associations/builder/belongs_to.rb +3 -3
  11. data/lib/active_record/associations/builder/collection_association.rb +2 -2
  12. data/lib/active_record/associations/builder/has_many.rb +4 -4
  13. data/lib/active_record/associations/builder/has_one.rb +5 -6
  14. data/lib/active_record/associations/builder/singular_association.rb +3 -16
  15. data/lib/active_record/associations/collection_association.rb +64 -31
  16. data/lib/active_record/associations/collection_proxy.rb +2 -37
  17. data/lib/active_record/associations/has_and_belongs_to_many_association.rb +1 -0
  18. data/lib/active_record/associations/has_many_association.rb +5 -1
  19. data/lib/active_record/associations/has_many_through_association.rb +28 -9
  20. data/lib/active_record/associations/has_one_association.rb +15 -13
  21. data/lib/active_record/associations/join_dependency.rb +2 -2
  22. data/lib/active_record/associations/preloader.rb +14 -10
  23. data/lib/active_record/associations/through_association.rb +7 -3
  24. data/lib/active_record/associations.rb +92 -76
  25. data/lib/active_record/attribute_assignment.rb +221 -0
  26. data/lib/active_record/attribute_methods/deprecated_underscore_read.rb +32 -0
  27. data/lib/active_record/attribute_methods/dirty.rb +21 -11
  28. data/lib/active_record/attribute_methods/primary_key.rb +62 -25
  29. data/lib/active_record/attribute_methods/read.rb +73 -83
  30. data/lib/active_record/attribute_methods/serialization.rb +102 -0
  31. data/lib/active_record/attribute_methods/time_zone_conversion.rb +23 -17
  32. data/lib/active_record/attribute_methods/write.rb +31 -6
  33. data/lib/active_record/attribute_methods.rb +231 -30
  34. data/lib/active_record/autosave_association.rb +43 -22
  35. data/lib/active_record/base.rb +227 -1708
  36. data/lib/active_record/connection_adapters/abstract/connection_pool.rb +150 -148
  37. data/lib/active_record/connection_adapters/abstract/connection_specification.rb +85 -29
  38. data/lib/active_record/connection_adapters/abstract/database_statements.rb +6 -33
  39. data/lib/active_record/connection_adapters/abstract/query_cache.rb +10 -2
  40. data/lib/active_record/connection_adapters/abstract/quoting.rb +15 -6
  41. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +37 -26
  42. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +48 -19
  43. data/lib/active_record/connection_adapters/abstract_adapter.rb +77 -42
  44. data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +674 -0
  45. data/lib/active_record/connection_adapters/column.rb +37 -11
  46. data/lib/active_record/connection_adapters/mysql2_adapter.rb +129 -581
  47. data/lib/active_record/connection_adapters/mysql_adapter.rb +137 -696
  48. data/lib/active_record/connection_adapters/postgresql_adapter.rb +184 -86
  49. data/lib/active_record/connection_adapters/schema_cache.rb +50 -0
  50. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +2 -6
  51. data/lib/active_record/connection_adapters/sqlite_adapter.rb +55 -32
  52. data/lib/active_record/counter_cache.rb +9 -4
  53. data/lib/active_record/dynamic_finder_match.rb +12 -0
  54. data/lib/active_record/dynamic_matchers.rb +84 -0
  55. data/lib/active_record/errors.rb +11 -1
  56. data/lib/active_record/explain.rb +85 -0
  57. data/lib/active_record/explain_subscriber.rb +25 -0
  58. data/lib/active_record/fixtures/file.rb +65 -0
  59. data/lib/active_record/fixtures.rb +56 -85
  60. data/lib/active_record/identity_map.rb +3 -4
  61. data/lib/active_record/inheritance.rb +174 -0
  62. data/lib/active_record/integration.rb +49 -0
  63. data/lib/active_record/locking/optimistic.rb +30 -25
  64. data/lib/active_record/locking/pessimistic.rb +23 -1
  65. data/lib/active_record/log_subscriber.rb +3 -3
  66. data/lib/active_record/migration/command_recorder.rb +8 -8
  67. data/lib/active_record/migration.rb +68 -35
  68. data/lib/active_record/model_schema.rb +366 -0
  69. data/lib/active_record/nested_attributes.rb +3 -2
  70. data/lib/active_record/persistence.rb +57 -11
  71. data/lib/active_record/querying.rb +58 -0
  72. data/lib/active_record/railtie.rb +31 -29
  73. data/lib/active_record/railties/controller_runtime.rb +3 -1
  74. data/lib/active_record/railties/databases.rake +191 -110
  75. data/lib/active_record/railties/jdbcmysql_error.rb +1 -1
  76. data/lib/active_record/readonly_attributes.rb +26 -0
  77. data/lib/active_record/reflection.rb +7 -15
  78. data/lib/active_record/relation/batches.rb +5 -2
  79. data/lib/active_record/relation/calculations.rb +47 -15
  80. data/lib/active_record/relation/delegation.rb +49 -0
  81. data/lib/active_record/relation/finder_methods.rb +9 -7
  82. data/lib/active_record/relation/predicate_builder.rb +18 -7
  83. data/lib/active_record/relation/query_methods.rb +75 -9
  84. data/lib/active_record/relation/spawn_methods.rb +11 -2
  85. data/lib/active_record/relation.rb +78 -32
  86. data/lib/active_record/result.rb +1 -1
  87. data/lib/active_record/sanitization.rb +194 -0
  88. data/lib/active_record/schema_dumper.rb +12 -5
  89. data/lib/active_record/scoping/default.rb +142 -0
  90. data/lib/active_record/scoping/named.rb +202 -0
  91. data/lib/active_record/scoping.rb +152 -0
  92. data/lib/active_record/serialization.rb +1 -43
  93. data/lib/active_record/serializers/xml_serializer.rb +4 -45
  94. data/lib/active_record/session_store.rb +17 -15
  95. data/lib/active_record/store.rb +52 -0
  96. data/lib/active_record/test_case.rb +11 -7
  97. data/lib/active_record/timestamp.rb +17 -3
  98. data/lib/active_record/transactions.rb +27 -6
  99. data/lib/active_record/translation.rb +22 -0
  100. data/lib/active_record/validations/associated.rb +5 -4
  101. data/lib/active_record/validations/uniqueness.rb +7 -7
  102. data/lib/active_record/validations.rb +1 -1
  103. data/lib/active_record/version.rb +2 -2
  104. data/lib/active_record.rb +38 -3
  105. data/lib/rails/generators/active_record/migration/migration_generator.rb +1 -1
  106. data/lib/rails/generators/active_record/migration/templates/migration.rb +12 -3
  107. data/lib/rails/generators/active_record/model/model_generator.rb +9 -1
  108. data/lib/rails/generators/active_record/model/templates/migration.rb +3 -5
  109. data/lib/rails/generators/active_record/model/templates/model.rb +5 -0
  110. data/lib/rails/generators/active_record/session_migration/templates/migration.rb +1 -5
  111. metadata +30 -10
  112. data/lib/active_record/named_scope.rb +0 -200
@@ -54,13 +54,13 @@ module ActiveRecord
54
54
 
55
55
  def build_relation(klass, table, attribute, value) #:nodoc:
56
56
  column = klass.columns_hash[attribute.to_s]
57
- value = column.limit ? value.to_s.mb_chars[0, column.limit] : value.to_s if column.text?
57
+ value = column.limit ? value.to_s.mb_chars[0, column.limit] : value.to_s if value && column.text?
58
58
 
59
59
  if !options[:case_sensitive] && value && column.text?
60
- # will use SQL LOWER function before comparison
61
- relation = table[attribute].lower.eq(table.lower(value))
60
+ # will use SQL LOWER function before comparison, unless it detects a case insensitive collation
61
+ relation = klass.connection.case_insensitive_comparison(table, attribute, column, value)
62
62
  else
63
- value = klass.connection.case_sensitive_modifier(value)
63
+ value = klass.connection.case_sensitive_modifier(value) if value
64
64
  relation = table[attribute].eq(value)
65
65
  end
66
66
 
@@ -81,9 +81,9 @@ module ActiveRecord
81
81
  #
82
82
  # class Person < ActiveRecord::Base
83
83
  # validates_uniqueness_of :user_name, :scope => :account_id
84
- # end
84
+ # end
85
85
  #
86
- # Or even multiple scope parameters. For example, making sure that a teacher can only be on the schedule once
86
+ # Or even multiple scope parameters. For example, making sure that a teacher can only be on the schedule once
87
87
  # per semester for a particular class.
88
88
  #
89
89
  # class TeacherSchedule < ActiveRecord::Base
@@ -105,7 +105,7 @@ module ActiveRecord
105
105
  # The method, proc or string should return or evaluate to a true or false value.
106
106
  # * <tt>:unless</tt> - Specifies a method, proc or string to call to determine if the validation should
107
107
  # not occur (e.g. <tt>:unless => :skip_validation</tt>, or
108
- # <tt>:unless => Proc.new { |user| user.signup_step <= 2 }</tt>). The method, proc or string should
108
+ # <tt>:unless => Proc.new { |user| user.signup_step <= 2 }</tt>). The method, proc or string should
109
109
  # return or evaluate to a true or false value.
110
110
  #
111
111
  # === Concurrency and integrity
@@ -1,7 +1,7 @@
1
1
  module ActiveRecord
2
2
  # = Active Record RecordInvalid
3
3
  #
4
- # Raised by <tt>save!</tt> and <tt>create!</tt> when the record is invalid. Use the
4
+ # Raised by <tt>save!</tt> and <tt>create!</tt> when the record is invalid. Use the
5
5
  # +record+ method to retrieve the record which did not validate.
6
6
  #
7
7
  # begin
@@ -1,8 +1,8 @@
1
1
  module ActiveRecord
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 3
4
- MINOR = 1
5
- TINY = 9
4
+ MINOR = 2
5
+ TINY = 12
6
6
  PRE = nil
7
7
 
8
8
  STRING = [MAJOR, MINOR, TINY, PRE].compact.join('.')
data/lib/active_record.rb CHANGED
@@ -31,13 +31,25 @@ require 'active_record/version'
31
31
  module ActiveRecord
32
32
  extend ActiveSupport::Autoload
33
33
 
34
+ # ActiveRecord::SessionStore depends on the abstract store in Action Pack.
35
+ # Eager loading this class would break client code that eager loads Active
36
+ # Record standalone.
37
+ #
38
+ # Note that the Rails application generator creates an initializer specific
39
+ # for setting the session store. Thus, albeit in theory this autoload would
40
+ # not be thread-safe, in practice it is because if the application uses this
41
+ # session store its autoload happens at boot time.
42
+ autoload :SessionStore
43
+
34
44
  eager_autoload do
35
45
  autoload :ActiveRecordError, 'active_record/errors'
36
46
  autoload :ConnectionNotEstablished, 'active_record/errors'
47
+ autoload :ConnectionAdapters, 'active_record/connection_adapters/abstract_adapter'
37
48
 
38
49
  autoload :Aggregations
39
50
  autoload :Associations
40
51
  autoload :AttributeMethods
52
+ autoload :AttributeAssignment
41
53
  autoload :AutosaveAssociation
42
54
 
43
55
  autoload :Relation
@@ -49,29 +61,41 @@ module ActiveRecord
49
61
  autoload :PredicateBuilder
50
62
  autoload :SpawnMethods
51
63
  autoload :Batches
64
+ autoload :Explain
65
+ autoload :Delegation
52
66
  end
53
67
 
54
68
  autoload :Base
55
69
  autoload :Callbacks
56
70
  autoload :CounterCache
71
+ autoload :DynamicMatchers
57
72
  autoload :DynamicFinderMatch
58
73
  autoload :DynamicScopeMatch
74
+ autoload :Explain
75
+ autoload :IdentityMap
76
+ autoload :Inheritance
77
+ autoload :Integration
59
78
  autoload :Migration
60
79
  autoload :Migrator, 'active_record/migration'
61
- autoload :NamedScope
80
+ autoload :ModelSchema
62
81
  autoload :NestedAttributes
63
82
  autoload :Observer
64
83
  autoload :Persistence
65
84
  autoload :QueryCache
85
+ autoload :Querying
86
+ autoload :ReadonlyAttributes
66
87
  autoload :Reflection
88
+ autoload :Result
89
+ autoload :Sanitization
67
90
  autoload :Schema
68
91
  autoload :SchemaDumper
92
+ autoload :Scoping
69
93
  autoload :Serialization
70
- autoload :SessionStore
94
+ autoload :Store
71
95
  autoload :Timestamp
72
96
  autoload :Transactions
97
+ autoload :Translation
73
98
  autoload :Validations
74
- autoload :IdentityMap
75
99
  end
76
100
 
77
101
  module Coders
@@ -89,6 +113,8 @@ module ActiveRecord
89
113
  autoload :Read
90
114
  autoload :TimeZoneConversion
91
115
  autoload :Write
116
+ autoload :Serialization
117
+ autoload :DeprecatedUnderscoreRead
92
118
  end
93
119
  end
94
120
 
@@ -110,6 +136,15 @@ module ActiveRecord
110
136
  end
111
137
  end
112
138
 
139
+ module Scoping
140
+ extend ActiveSupport::Autoload
141
+
142
+ eager_autoload do
143
+ autoload :Named
144
+ autoload :Default
145
+ end
146
+ end
147
+
113
148
  autoload :TestCase
114
149
  autoload :TestFixtures, 'active_record/fixtures'
115
150
  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,10 @@ 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 %>
27
+ <%- if attribute.has_index? && migration_action == 'remove' -%>
28
+ add_index :<%= table_name %>, :<%= attribute.index_name %><%= attribute.inject_index_options %>
29
+ <%- end -%>
21
30
  <%- end -%>
22
31
  <%- end -%>
23
32
  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,14 @@ 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
+
33
+ def accessible_attributes
34
+ attributes.reject(&:reference?)
35
+ end
36
+
29
37
  hook_for :test_framework
30
38
 
31
39
  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
- <% if options[:indexes] -%>
12
- <% attributes.select {|attr| attr.reference? }.each do |attribute| -%>
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
@@ -3,5 +3,10 @@ class <%= class_name %> < <%= parent_class_name.classify %>
3
3
  <% attributes.select {|attr| attr.reference? }.each do |attribute| -%>
4
4
  belongs_to :<%= attribute.name %>
5
5
  <% end -%>
6
+ <% if !accessible_attributes.empty? -%>
7
+ attr_accessible <%= accessible_attributes.map {|a| ":#{a.name}" }.sort.join(', ') %>
8
+ <% else -%>
9
+ # attr_accessible :title, :body
10
+ <% end -%>
6
11
  end
7
12
  <% end -%>
@@ -1,5 +1,5 @@
1
1
  class <%= migration_class_name %> < ActiveRecord::Migration
2
- def up
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,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.1.9
4
+ version: 3.2.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Heinemeier Hansson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2012-12-23 00:00:00.000000000 Z
11
+ date: 2013-02-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -16,42 +16,42 @@ dependencies:
16
16
  requirements:
17
17
  - - '='
18
18
  - !ruby/object:Gem::Version
19
- version: 3.1.9
19
+ version: 3.2.12
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - '='
25
25
  - !ruby/object:Gem::Version
26
- version: 3.1.9
26
+ version: 3.2.12
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: activemodel
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - '='
32
32
  - !ruby/object:Gem::Version
33
- version: 3.1.9
33
+ version: 3.2.12
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - '='
39
39
  - !ruby/object:Gem::Version
40
- version: 3.1.9
40
+ version: 3.2.12
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: arel
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: 2.2.3
47
+ version: 3.0.2
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: 2.2.3
54
+ version: 3.0.2
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: tzinfo
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -120,11 +120,14 @@ files:
120
120
  - lib/active_record/associations/singular_association.rb
121
121
  - lib/active_record/associations/through_association.rb
122
122
  - lib/active_record/associations.rb
123
+ - lib/active_record/attribute_assignment.rb
123
124
  - lib/active_record/attribute_methods/before_type_cast.rb
125
+ - lib/active_record/attribute_methods/deprecated_underscore_read.rb
124
126
  - lib/active_record/attribute_methods/dirty.rb
125
127
  - lib/active_record/attribute_methods/primary_key.rb
126
128
  - lib/active_record/attribute_methods/query.rb
127
129
  - lib/active_record/attribute_methods/read.rb
130
+ - lib/active_record/attribute_methods/serialization.rb
128
131
  - lib/active_record/attribute_methods/time_zone_conversion.rb
129
132
  - lib/active_record/attribute_methods/write.rb
130
133
  - lib/active_record/attribute_methods.rb
@@ -141,52 +144,69 @@ files:
141
144
  - lib/active_record/connection_adapters/abstract/schema_definitions.rb
142
145
  - lib/active_record/connection_adapters/abstract/schema_statements.rb
143
146
  - lib/active_record/connection_adapters/abstract_adapter.rb
147
+ - lib/active_record/connection_adapters/abstract_mysql_adapter.rb
144
148
  - lib/active_record/connection_adapters/column.rb
145
149
  - lib/active_record/connection_adapters/mysql2_adapter.rb
146
150
  - lib/active_record/connection_adapters/mysql_adapter.rb
147
151
  - lib/active_record/connection_adapters/postgresql_adapter.rb
152
+ - lib/active_record/connection_adapters/schema_cache.rb
148
153
  - lib/active_record/connection_adapters/sqlite3_adapter.rb
149
154
  - lib/active_record/connection_adapters/sqlite_adapter.rb
150
155
  - lib/active_record/connection_adapters/statement_pool.rb
151
156
  - lib/active_record/counter_cache.rb
152
157
  - lib/active_record/dynamic_finder_match.rb
158
+ - lib/active_record/dynamic_matchers.rb
153
159
  - lib/active_record/dynamic_scope_match.rb
154
160
  - lib/active_record/errors.rb
161
+ - lib/active_record/explain.rb
162
+ - lib/active_record/explain_subscriber.rb
163
+ - lib/active_record/fixtures/file.rb
155
164
  - lib/active_record/fixtures.rb
156
165
  - lib/active_record/identity_map.rb
166
+ - lib/active_record/inheritance.rb
167
+ - lib/active_record/integration.rb
157
168
  - lib/active_record/locale/en.yml
158
169
  - lib/active_record/locking/optimistic.rb
159
170
  - lib/active_record/locking/pessimistic.rb
160
171
  - lib/active_record/log_subscriber.rb
161
172
  - lib/active_record/migration/command_recorder.rb
162
173
  - lib/active_record/migration.rb
163
- - lib/active_record/named_scope.rb
174
+ - lib/active_record/model_schema.rb
164
175
  - lib/active_record/nested_attributes.rb
165
176
  - lib/active_record/observer.rb
166
177
  - lib/active_record/persistence.rb
167
178
  - lib/active_record/query_cache.rb
179
+ - lib/active_record/querying.rb
168
180
  - lib/active_record/railtie.rb
169
181
  - lib/active_record/railties/console_sandbox.rb
170
182
  - lib/active_record/railties/controller_runtime.rb
171
183
  - lib/active_record/railties/databases.rake
172
184
  - lib/active_record/railties/jdbcmysql_error.rb
185
+ - lib/active_record/readonly_attributes.rb
173
186
  - lib/active_record/reflection.rb
174
187
  - lib/active_record/relation/batches.rb
175
188
  - lib/active_record/relation/calculations.rb
189
+ - lib/active_record/relation/delegation.rb
176
190
  - lib/active_record/relation/finder_methods.rb
177
191
  - lib/active_record/relation/predicate_builder.rb
178
192
  - lib/active_record/relation/query_methods.rb
179
193
  - lib/active_record/relation/spawn_methods.rb
180
194
  - lib/active_record/relation.rb
181
195
  - lib/active_record/result.rb
196
+ - lib/active_record/sanitization.rb
182
197
  - lib/active_record/schema.rb
183
198
  - lib/active_record/schema_dumper.rb
199
+ - lib/active_record/scoping/default.rb
200
+ - lib/active_record/scoping/named.rb
201
+ - lib/active_record/scoping.rb
184
202
  - lib/active_record/serialization.rb
185
203
  - lib/active_record/serializers/xml_serializer.rb
186
204
  - lib/active_record/session_store.rb
205
+ - lib/active_record/store.rb
187
206
  - lib/active_record/test_case.rb
188
207
  - lib/active_record/timestamp.rb
189
208
  - lib/active_record/transactions.rb
209
+ - lib/active_record/translation.rb
190
210
  - lib/active_record/validations/associated.rb
191
211
  - lib/active_record/validations/uniqueness.rb
192
212
  - lib/active_record/validations.rb
@@ -225,7 +245,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
225
245
  version: '0'
226
246
  requirements: []
227
247
  rubyforge_project:
228
- rubygems_version: 2.0.0.preview2.1
248
+ rubygems_version: 2.0.0.rc.2
229
249
  signing_key:
230
250
  specification_version: 4
231
251
  summary: Object-relational mapper framework (part of Rails).
@@ -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