activerecord 3.2.22.5 → 4.0.0.beta1
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of activerecord might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/CHANGELOG.md +1024 -543
- data/MIT-LICENSE +1 -1
- data/README.rdoc +20 -29
- data/examples/performance.rb +1 -1
- data/lib/active_record.rb +55 -44
- data/lib/active_record/aggregations.rb +40 -34
- data/lib/active_record/associations.rb +204 -276
- data/lib/active_record/associations/alias_tracker.rb +1 -1
- data/lib/active_record/associations/association.rb +30 -35
- data/lib/active_record/associations/association_scope.rb +40 -40
- data/lib/active_record/associations/belongs_to_association.rb +15 -2
- data/lib/active_record/associations/builder/association.rb +81 -28
- data/lib/active_record/associations/builder/belongs_to.rb +35 -57
- data/lib/active_record/associations/builder/collection_association.rb +54 -40
- data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +23 -41
- data/lib/active_record/associations/builder/has_many.rb +8 -64
- data/lib/active_record/associations/builder/has_one.rb +13 -50
- data/lib/active_record/associations/builder/singular_association.rb +13 -13
- data/lib/active_record/associations/collection_association.rb +92 -88
- data/lib/active_record/associations/collection_proxy.rb +913 -63
- data/lib/active_record/associations/has_and_belongs_to_many_association.rb +12 -10
- data/lib/active_record/associations/has_many_association.rb +35 -9
- data/lib/active_record/associations/has_many_through_association.rb +24 -14
- data/lib/active_record/associations/has_one_association.rb +33 -13
- data/lib/active_record/associations/has_one_through_association.rb +1 -1
- data/lib/active_record/associations/join_dependency.rb +2 -2
- data/lib/active_record/associations/join_dependency/join_association.rb +17 -22
- data/lib/active_record/associations/join_dependency/join_part.rb +1 -1
- data/lib/active_record/associations/join_helper.rb +1 -11
- data/lib/active_record/associations/preloader.rb +14 -17
- data/lib/active_record/associations/preloader/association.rb +29 -33
- data/lib/active_record/associations/preloader/collection_association.rb +1 -1
- data/lib/active_record/associations/preloader/has_and_belongs_to_many.rb +1 -1
- data/lib/active_record/associations/preloader/has_many_through.rb +1 -1
- data/lib/active_record/associations/preloader/has_one.rb +1 -1
- data/lib/active_record/associations/preloader/through_association.rb +13 -17
- data/lib/active_record/associations/singular_association.rb +11 -11
- data/lib/active_record/associations/through_association.rb +2 -2
- data/lib/active_record/attribute_assignment.rb +133 -153
- data/lib/active_record/attribute_methods.rb +196 -93
- data/lib/active_record/attribute_methods/before_type_cast.rb +44 -5
- data/lib/active_record/attribute_methods/dirty.rb +31 -28
- data/lib/active_record/attribute_methods/primary_key.rb +38 -30
- data/lib/active_record/attribute_methods/query.rb +5 -4
- data/lib/active_record/attribute_methods/read.rb +62 -91
- data/lib/active_record/attribute_methods/serialization.rb +97 -66
- data/lib/active_record/attribute_methods/time_zone_conversion.rb +39 -45
- data/lib/active_record/attribute_methods/write.rb +32 -39
- data/lib/active_record/autosave_association.rb +56 -70
- data/lib/active_record/base.rb +53 -450
- data/lib/active_record/callbacks.rb +53 -18
- data/lib/active_record/coders/yaml_column.rb +11 -9
- data/lib/active_record/connection_adapters/abstract/connection_pool.rb +353 -197
- data/lib/active_record/connection_adapters/abstract/database_limits.rb +9 -0
- data/lib/active_record/connection_adapters/abstract/database_statements.rb +130 -131
- data/lib/active_record/connection_adapters/abstract/query_cache.rb +24 -19
- data/lib/active_record/connection_adapters/abstract/quoting.rb +23 -3
- data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +101 -91
- data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +59 -0
- data/lib/active_record/connection_adapters/abstract/schema_statements.rb +225 -96
- data/lib/active_record/connection_adapters/abstract/transaction.rb +203 -0
- data/lib/active_record/connection_adapters/abstract_adapter.rb +99 -46
- data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +114 -36
- data/lib/active_record/connection_adapters/column.rb +46 -24
- data/lib/active_record/connection_adapters/connection_specification.rb +96 -0
- data/lib/active_record/connection_adapters/mysql2_adapter.rb +16 -32
- data/lib/active_record/connection_adapters/mysql_adapter.rb +181 -64
- data/lib/active_record/connection_adapters/postgresql/array_parser.rb +97 -0
- data/lib/active_record/connection_adapters/postgresql/cast.rb +132 -0
- data/lib/active_record/connection_adapters/postgresql/database_statements.rb +242 -0
- data/lib/active_record/connection_adapters/postgresql/oid.rb +347 -0
- data/lib/active_record/connection_adapters/postgresql/quoting.rb +158 -0
- data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +30 -0
- data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +448 -0
- data/lib/active_record/connection_adapters/postgresql_adapter.rb +454 -885
- data/lib/active_record/connection_adapters/schema_cache.rb +48 -16
- data/lib/active_record/connection_adapters/sqlite3_adapter.rb +574 -13
- data/lib/active_record/connection_handling.rb +98 -0
- data/lib/active_record/core.rb +428 -0
- data/lib/active_record/counter_cache.rb +106 -108
- data/lib/active_record/dynamic_matchers.rb +110 -63
- data/lib/active_record/errors.rb +25 -8
- data/lib/active_record/explain.rb +8 -58
- data/lib/active_record/explain_subscriber.rb +6 -3
- data/lib/active_record/fixture_set/file.rb +56 -0
- data/lib/active_record/fixtures.rb +146 -148
- data/lib/active_record/inheritance.rb +77 -59
- data/lib/active_record/integration.rb +5 -5
- data/lib/active_record/locale/en.yml +8 -1
- data/lib/active_record/locking/optimistic.rb +38 -42
- data/lib/active_record/locking/pessimistic.rb +4 -4
- data/lib/active_record/log_subscriber.rb +19 -9
- data/lib/active_record/migration.rb +318 -153
- data/lib/active_record/migration/command_recorder.rb +90 -31
- data/lib/active_record/migration/join_table.rb +15 -0
- data/lib/active_record/model_schema.rb +69 -92
- data/lib/active_record/nested_attributes.rb +113 -148
- data/lib/active_record/null_relation.rb +65 -0
- data/lib/active_record/persistence.rb +188 -97
- data/lib/active_record/query_cache.rb +18 -36
- data/lib/active_record/querying.rb +19 -15
- data/lib/active_record/railtie.rb +91 -36
- data/lib/active_record/railties/console_sandbox.rb +0 -2
- data/lib/active_record/railties/controller_runtime.rb +2 -2
- data/lib/active_record/railties/databases.rake +90 -309
- data/lib/active_record/railties/jdbcmysql_error.rb +1 -1
- data/lib/active_record/readonly_attributes.rb +7 -3
- data/lib/active_record/reflection.rb +72 -56
- data/lib/active_record/relation.rb +241 -157
- data/lib/active_record/relation/batches.rb +25 -22
- data/lib/active_record/relation/calculations.rb +143 -121
- data/lib/active_record/relation/delegation.rb +96 -18
- data/lib/active_record/relation/finder_methods.rb +117 -183
- data/lib/active_record/relation/merger.rb +133 -0
- data/lib/active_record/relation/predicate_builder.rb +90 -42
- data/lib/active_record/relation/query_methods.rb +666 -136
- data/lib/active_record/relation/spawn_methods.rb +43 -150
- data/lib/active_record/result.rb +33 -6
- data/lib/active_record/sanitization.rb +24 -50
- data/lib/active_record/schema.rb +19 -12
- data/lib/active_record/schema_dumper.rb +31 -39
- data/lib/active_record/schema_migration.rb +36 -0
- data/lib/active_record/scoping.rb +0 -124
- data/lib/active_record/scoping/default.rb +48 -45
- data/lib/active_record/scoping/named.rb +74 -103
- data/lib/active_record/serialization.rb +6 -2
- data/lib/active_record/serializers/xml_serializer.rb +9 -15
- data/lib/active_record/store.rb +119 -15
- data/lib/active_record/tasks/database_tasks.rb +158 -0
- data/lib/active_record/tasks/mysql_database_tasks.rb +138 -0
- data/lib/active_record/tasks/postgresql_database_tasks.rb +90 -0
- data/lib/active_record/tasks/sqlite_database_tasks.rb +51 -0
- data/lib/active_record/test_case.rb +61 -38
- data/lib/active_record/timestamp.rb +8 -9
- data/lib/active_record/transactions.rb +65 -51
- data/lib/active_record/validations.rb +17 -15
- data/lib/active_record/validations/associated.rb +20 -14
- data/lib/active_record/validations/presence.rb +65 -0
- data/lib/active_record/validations/uniqueness.rb +93 -52
- data/lib/active_record/version.rb +4 -4
- data/lib/rails/generators/active_record.rb +3 -5
- data/lib/rails/generators/active_record/migration/migration_generator.rb +37 -7
- data/lib/rails/generators/active_record/migration/templates/migration.rb +20 -15
- data/lib/rails/generators/active_record/model/model_generator.rb +4 -3
- data/lib/rails/generators/active_record/model/templates/model.rb +1 -6
- data/lib/rails/generators/active_record/model/templates/module.rb +1 -1
- metadata +53 -46
- data/lib/active_record/attribute_methods/deprecated_underscore_read.rb +0 -32
- data/lib/active_record/connection_adapters/abstract/connection_specification.rb +0 -191
- data/lib/active_record/connection_adapters/sqlite_adapter.rb +0 -583
- data/lib/active_record/dynamic_finder_match.rb +0 -68
- data/lib/active_record/dynamic_scope_match.rb +0 -23
- data/lib/active_record/fixtures/file.rb +0 -65
- data/lib/active_record/identity_map.rb +0 -162
- data/lib/active_record/observer.rb +0 -121
- data/lib/active_record/session_store.rb +0 -360
- data/lib/rails/generators/active_record/migration.rb +0 -15
- data/lib/rails/generators/active_record/observer/observer_generator.rb +0 -15
- data/lib/rails/generators/active_record/observer/templates/observer.rb +0 -4
- data/lib/rails/generators/active_record/session_migration/session_migration_generator.rb +0 -25
- data/lib/rails/generators/active_record/session_migration/templates/migration.rb +0 -12
@@ -8,66 +8,125 @@ module ActiveRecord
|
|
8
8
|
# * add_index
|
9
9
|
# * add_timestamps
|
10
10
|
# * create_table
|
11
|
+
# * create_join_table
|
11
12
|
# * remove_timestamps
|
12
13
|
# * rename_column
|
13
14
|
# * rename_index
|
14
15
|
# * rename_table
|
15
16
|
class CommandRecorder
|
16
|
-
|
17
|
+
include JoinTable
|
18
|
+
|
19
|
+
attr_accessor :commands, :delegate, :reverting
|
17
20
|
|
18
21
|
def initialize(delegate = nil)
|
19
22
|
@commands = []
|
20
23
|
@delegate = delegate
|
24
|
+
@reverting = false
|
25
|
+
end
|
26
|
+
|
27
|
+
# While executing the given block, the recorded will be in reverting mode.
|
28
|
+
# All commands recorded will end up being recorded reverted
|
29
|
+
# and in reverse order.
|
30
|
+
# For example:
|
31
|
+
#
|
32
|
+
# recorder.revert{ recorder.record(:rename_table, [:old, :new]) }
|
33
|
+
# # same effect as recorder.record(:rename_table, [:new, :old])
|
34
|
+
def revert
|
35
|
+
@reverting = !@reverting
|
36
|
+
previous = @commands
|
37
|
+
@commands = []
|
38
|
+
yield
|
39
|
+
ensure
|
40
|
+
@commands = previous.concat(@commands.reverse)
|
41
|
+
@reverting = !@reverting
|
21
42
|
end
|
22
43
|
|
23
44
|
# record +command+. +command+ should be a method name and arguments.
|
24
45
|
# For example:
|
25
46
|
#
|
26
47
|
# recorder.record(:method_name, [:arg1, :arg2])
|
27
|
-
def record(*command)
|
28
|
-
@
|
48
|
+
def record(*command, &block)
|
49
|
+
if @reverting
|
50
|
+
@commands << inverse_of(*command, &block)
|
51
|
+
else
|
52
|
+
@commands << (command << block)
|
53
|
+
end
|
29
54
|
end
|
30
55
|
|
31
|
-
# Returns
|
32
|
-
# commands stored in +commands+. For example:
|
56
|
+
# Returns the inverse of the given command. For example:
|
33
57
|
#
|
34
|
-
# recorder.
|
35
|
-
#
|
58
|
+
# recorder.inverse_of(:rename_table, [:old, :new])
|
59
|
+
# # => [:rename_table, [:new, :old]]
|
36
60
|
#
|
37
61
|
# This method will raise an +IrreversibleMigration+ exception if it cannot
|
38
|
-
# invert the +
|
39
|
-
def
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
send(method, args)
|
44
|
-
}
|
62
|
+
# invert the +command+.
|
63
|
+
def inverse_of(command, args, &block)
|
64
|
+
method = :"invert_#{command}"
|
65
|
+
raise IrreversibleMigration unless respond_to?(method, true)
|
66
|
+
send(method, args, &block)
|
45
67
|
end
|
46
68
|
|
47
69
|
def respond_to?(*args) # :nodoc:
|
48
70
|
super || delegate.respond_to?(*args)
|
49
71
|
end
|
50
72
|
|
51
|
-
[:create_table, :
|
73
|
+
[:create_table, :create_join_table, :rename_table, :add_column, :remove_column,
|
74
|
+
:rename_index, :rename_column, :add_index, :remove_index, :add_timestamps, :remove_timestamps,
|
75
|
+
:change_column_default, :add_reference, :remove_reference, :transaction,
|
76
|
+
:drop_join_table, :drop_table, :execute_block,
|
77
|
+
:change_column, :execute, :remove_columns, # irreversible methods need to be here too
|
78
|
+
].each do |method|
|
52
79
|
class_eval <<-EOV, __FILE__, __LINE__ + 1
|
53
|
-
def #{method}(*args) # def create_table(*args)
|
54
|
-
record(:"#{method}", args) # record(:create_table, args)
|
55
|
-
end
|
80
|
+
def #{method}(*args, &block) # def create_table(*args, &block)
|
81
|
+
record(:"#{method}", args, &block) # record(:create_table, args, &block)
|
82
|
+
end # end
|
56
83
|
EOV
|
57
84
|
end
|
85
|
+
alias :add_belongs_to :add_reference
|
86
|
+
alias :remove_belongs_to :remove_reference
|
87
|
+
|
88
|
+
def change_table(table_name, options = {})
|
89
|
+
yield ConnectionAdapters::Table.new(table_name, self)
|
90
|
+
end
|
58
91
|
|
59
92
|
private
|
60
93
|
|
61
|
-
|
62
|
-
|
94
|
+
module StraightReversions
|
95
|
+
private
|
96
|
+
{ transaction: :transaction,
|
97
|
+
execute_block: :execute_block,
|
98
|
+
create_table: :drop_table,
|
99
|
+
create_join_table: :drop_join_table,
|
100
|
+
add_column: :remove_column,
|
101
|
+
add_timestamps: :remove_timestamps,
|
102
|
+
add_reference: :remove_reference,
|
103
|
+
}.each do |cmd, inv|
|
104
|
+
[[inv, cmd], [cmd, inv]].uniq.each do |method, inverse|
|
105
|
+
class_eval <<-EOV, __FILE__, __LINE__ + 1
|
106
|
+
def invert_#{method}(args, &block) # def invert_create_table(args, &block)
|
107
|
+
[:#{inverse}, args, block] # [:drop_table, args, block]
|
108
|
+
end # end
|
109
|
+
EOV
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
include StraightReversions
|
115
|
+
|
116
|
+
def invert_drop_table(args, &block)
|
117
|
+
if args.size == 1 && block == nil
|
118
|
+
raise ActiveRecord::IrreversibleMigration, "To avoid mistakes, drop_table is only reversible if given options or a block (can be empty)."
|
119
|
+
end
|
120
|
+
super
|
63
121
|
end
|
64
122
|
|
65
123
|
def invert_rename_table(args)
|
66
124
|
[:rename_table, args.reverse]
|
67
125
|
end
|
68
126
|
|
69
|
-
def
|
70
|
-
|
127
|
+
def invert_remove_column(args)
|
128
|
+
raise ActiveRecord::IrreversibleMigration, "remove_column is only reversible if given a type." if args.size <= 2
|
129
|
+
super
|
71
130
|
end
|
72
131
|
|
73
132
|
def invert_rename_index(args)
|
@@ -80,26 +139,26 @@ module ActiveRecord
|
|
80
139
|
|
81
140
|
def invert_add_index(args)
|
82
141
|
table, columns, options = *args
|
83
|
-
|
84
|
-
options_hash = index_name ? {:name => index_name} : {:column => columns}
|
85
|
-
[:remove_index, [table, options_hash]]
|
142
|
+
[:remove_index, [table, (options || {}).merge(column: columns)]]
|
86
143
|
end
|
87
144
|
|
88
|
-
def
|
89
|
-
|
90
|
-
|
145
|
+
def invert_remove_index(args)
|
146
|
+
table, options = *args
|
147
|
+
raise ActiveRecord::IrreversibleMigration, "remove_index is only reversible if given a :column option." unless options && options[:column]
|
91
148
|
|
92
|
-
|
93
|
-
[:
|
149
|
+
options = options.dup
|
150
|
+
[:add_index, [table, options.delete(:column), options]]
|
94
151
|
end
|
95
152
|
|
153
|
+
alias :invert_add_belongs_to :invert_add_reference
|
154
|
+
alias :invert_remove_belongs_to :invert_remove_reference
|
155
|
+
|
96
156
|
# Forwards any missing method call to the \target.
|
97
157
|
def method_missing(method, *args, &block)
|
98
158
|
@delegate.send(method, *args, &block)
|
99
159
|
rescue NoMethodError => e
|
100
160
|
raise e, e.message.sub(/ for #<.*$/, " via proxy for #{@delegate}")
|
101
161
|
end
|
102
|
-
|
103
162
|
end
|
104
163
|
end
|
105
164
|
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
class Migration
|
3
|
+
module JoinTable #:nodoc:
|
4
|
+
private
|
5
|
+
|
6
|
+
def find_join_table_name(table_1, table_2, options = {})
|
7
|
+
options.delete(:table_name) || join_table_name(table_1, table_2)
|
8
|
+
end
|
9
|
+
|
10
|
+
def join_table_name(table_1, table_2)
|
11
|
+
[table_1.to_s, table_2.to_s].sort.join("_").to_sym
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -1,5 +1,3 @@
|
|
1
|
-
require 'active_support/concern'
|
2
|
-
|
3
1
|
module ActiveRecord
|
4
2
|
module ModelSchema
|
5
3
|
extend ActiveSupport::Concern
|
@@ -12,8 +10,7 @@ module ActiveRecord
|
|
12
10
|
# the Product class will look for "productid" instead of "id" as the primary column. If the
|
13
11
|
# latter is specified, the Product class will look for "product_id" instead of "id". Remember
|
14
12
|
# that this is a global setting for all Active Records.
|
15
|
-
|
16
|
-
self.primary_key_prefix_type = nil
|
13
|
+
mattr_accessor :primary_key_prefix_type, instance_writer: false
|
17
14
|
|
18
15
|
##
|
19
16
|
# :singleton-method:
|
@@ -25,14 +22,14 @@ module ActiveRecord
|
|
25
22
|
# If you are organising your models within modules you can add a prefix to the models within
|
26
23
|
# a namespace by defining a singleton method in the parent module called table_name_prefix which
|
27
24
|
# returns your chosen prefix.
|
28
|
-
class_attribute :table_name_prefix, :
|
25
|
+
class_attribute :table_name_prefix, instance_writer: false
|
29
26
|
self.table_name_prefix = ""
|
30
27
|
|
31
28
|
##
|
32
29
|
# :singleton-method:
|
33
30
|
# Works like +table_name_prefix+, but appends instead of prepends (set to "_basecamp" gives "projects_basecamp",
|
34
31
|
# "people_basecamp"). By default, the suffix is the empty string.
|
35
|
-
class_attribute :table_name_suffix, :
|
32
|
+
class_attribute :table_name_suffix, instance_writer: false
|
36
33
|
self.table_name_suffix = ""
|
37
34
|
|
38
35
|
##
|
@@ -40,8 +37,10 @@ module ActiveRecord
|
|
40
37
|
# Indicates whether table names should be the pluralized versions of the corresponding class names.
|
41
38
|
# If true, the default table name for a Product class will be +products+. If false, it would just be +product+.
|
42
39
|
# See table_name for the full rules on table/class naming. This is true, by default.
|
43
|
-
class_attribute :pluralize_table_names, :
|
40
|
+
class_attribute :pluralize_table_names, instance_writer: false
|
44
41
|
self.pluralize_table_names = true
|
42
|
+
|
43
|
+
self.inheritance_column = 'type'
|
45
44
|
end
|
46
45
|
|
47
46
|
module ClassMethods
|
@@ -105,10 +104,6 @@ module ActiveRecord
|
|
105
104
|
@table_name
|
106
105
|
end
|
107
106
|
|
108
|
-
def original_table_name #:nodoc:
|
109
|
-
deprecated_original_property_getter :table_name
|
110
|
-
end
|
111
|
-
|
112
107
|
# Sets the table name explicitly. Example:
|
113
108
|
#
|
114
109
|
# class Project < ActiveRecord::Base
|
@@ -118,17 +113,17 @@ module ActiveRecord
|
|
118
113
|
# You can also just define your own <tt>self.table_name</tt> method; see
|
119
114
|
# the documentation for ActiveRecord::Base#table_name.
|
120
115
|
def table_name=(value)
|
121
|
-
|
122
|
-
@table_name = value && value.to_s
|
123
|
-
@quoted_table_name = nil
|
124
|
-
@arel_table = nil
|
125
|
-
@relation = Relation.new(self, arel_table)
|
126
|
-
end
|
116
|
+
value = value && value.to_s
|
127
117
|
|
128
|
-
|
129
|
-
|
118
|
+
if defined?(@table_name)
|
119
|
+
return if value == @table_name
|
120
|
+
reset_column_information if connected?
|
121
|
+
end
|
122
|
+
|
123
|
+
@table_name = value
|
130
124
|
@quoted_table_name = nil
|
131
125
|
@arel_table = nil
|
126
|
+
@sequence_name = nil unless defined?(@explicit_sequence_name) && @explicit_sequence_name
|
132
127
|
@relation = Relation.new(self, arel_table)
|
133
128
|
end
|
134
129
|
|
@@ -139,16 +134,12 @@ module ActiveRecord
|
|
139
134
|
|
140
135
|
# Computes the table name, (re)sets it internally, and returns it.
|
141
136
|
def reset_table_name #:nodoc:
|
142
|
-
if abstract_class?
|
143
|
-
|
144
|
-
nil
|
145
|
-
else
|
146
|
-
superclass.table_name
|
147
|
-
end
|
137
|
+
self.table_name = if abstract_class?
|
138
|
+
superclass == Base ? nil : superclass.table_name
|
148
139
|
elsif superclass.abstract_class?
|
149
|
-
|
140
|
+
superclass.table_name || compute_table_name
|
150
141
|
else
|
151
|
-
|
142
|
+
compute_table_name
|
152
143
|
end
|
153
144
|
end
|
154
145
|
|
@@ -156,30 +147,25 @@ module ActiveRecord
|
|
156
147
|
(parents.detect{ |p| p.respond_to?(:table_name_prefix) } || self).table_name_prefix
|
157
148
|
end
|
158
149
|
|
159
|
-
#
|
150
|
+
# Defines the name of the table column which will store the class name on single-table
|
151
|
+
# inheritance situations.
|
152
|
+
#
|
153
|
+
# The default inheritance column name is +type+, which means it's a
|
154
|
+
# reserved word inside Active Record. To be able to use single-table
|
155
|
+
# inheritance with another column name, or to use the column +type+ in
|
156
|
+
# your own model for something else, you can set +inheritance_column+:
|
157
|
+
#
|
158
|
+
# self.inheritance_column = 'zoink'
|
160
159
|
def inheritance_column
|
161
|
-
|
162
|
-
(@inheritance_column ||= nil) || 'type'
|
163
|
-
else
|
164
|
-
(@inheritance_column ||= nil) || superclass.inheritance_column
|
165
|
-
end
|
166
|
-
end
|
167
|
-
|
168
|
-
def original_inheritance_column #:nodoc:
|
169
|
-
deprecated_original_property_getter :inheritance_column
|
160
|
+
(@inheritance_column ||= nil) || superclass.inheritance_column
|
170
161
|
end
|
171
162
|
|
172
163
|
# Sets the value of inheritance_column
|
173
164
|
def inheritance_column=(value)
|
174
|
-
@
|
175
|
-
@inheritance_column = value.to_s
|
165
|
+
@inheritance_column = value.to_s
|
176
166
|
@explicit_inheritance_column = true
|
177
167
|
end
|
178
168
|
|
179
|
-
def set_inheritance_column(value = nil, &block) #:nodoc:
|
180
|
-
deprecated_property_setter :inheritance_column, value, block
|
181
|
-
end
|
182
|
-
|
183
169
|
def sequence_name
|
184
170
|
if base_class == self
|
185
171
|
@sequence_name ||= reset_sequence_name
|
@@ -188,12 +174,9 @@ module ActiveRecord
|
|
188
174
|
end
|
189
175
|
end
|
190
176
|
|
191
|
-
def original_sequence_name #:nodoc:
|
192
|
-
deprecated_original_property_getter :sequence_name
|
193
|
-
end
|
194
|
-
|
195
177
|
def reset_sequence_name #:nodoc:
|
196
|
-
|
178
|
+
@explicit_sequence_name = false
|
179
|
+
@sequence_name = connection.default_sequence_name(table_name, primary_key)
|
197
180
|
end
|
198
181
|
|
199
182
|
# Sets the name of the sequence to use when generating ids to the given
|
@@ -211,12 +194,8 @@ module ActiveRecord
|
|
211
194
|
# self.sequence_name = "projectseq" # default would have been "project_seq"
|
212
195
|
# end
|
213
196
|
def sequence_name=(value)
|
214
|
-
@original_sequence_name = @sequence_name if defined?(@sequence_name)
|
215
197
|
@sequence_name = value.to_s
|
216
|
-
|
217
|
-
|
218
|
-
def set_sequence_name(value = nil, &block) #:nodoc:
|
219
|
-
deprecated_property_setter :sequence_name, value, block
|
198
|
+
@explicit_sequence_name = true
|
220
199
|
end
|
221
200
|
|
222
201
|
# Indicates whether the table associated with this class exists
|
@@ -238,6 +217,25 @@ module ActiveRecord
|
|
238
217
|
@columns_hash ||= Hash[columns.map { |c| [c.name, c] }]
|
239
218
|
end
|
240
219
|
|
220
|
+
def column_types # :nodoc:
|
221
|
+
@column_types ||= decorate_columns(columns_hash.dup)
|
222
|
+
end
|
223
|
+
|
224
|
+
def decorate_columns(columns_hash) # :nodoc:
|
225
|
+
return if columns_hash.empty?
|
226
|
+
|
227
|
+
columns_hash.each do |name, col|
|
228
|
+
if serialized_attributes.key?(name)
|
229
|
+
columns_hash[name] = AttributeMethods::Serialization::Type.new(col)
|
230
|
+
end
|
231
|
+
if create_time_zone_conversion_attribute?(name, col)
|
232
|
+
columns_hash[name] = AttributeMethods::TimeZoneConversion::Type.new(col)
|
233
|
+
end
|
234
|
+
end
|
235
|
+
|
236
|
+
columns_hash
|
237
|
+
end
|
238
|
+
|
241
239
|
# Returns a hash where the keys are column names and the values are
|
242
240
|
# default values when instantiating the AR object for this table.
|
243
241
|
def column_defaults
|
@@ -259,13 +257,12 @@ module ActiveRecord
|
|
259
257
|
# and true as the value. This makes it possible to do O(1) lookups in respond_to? to check if a given method for attribute
|
260
258
|
# is available.
|
261
259
|
def column_methods_hash #:nodoc:
|
262
|
-
@dynamic_methods_hash ||= column_names.
|
260
|
+
@dynamic_methods_hash ||= column_names.each_with_object(Hash.new(false)) do |attr, methods|
|
263
261
|
attr_name = attr.to_s
|
264
262
|
methods[attr.to_sym] = attr_name
|
265
263
|
methods["#{attr}=".to_sym] = attr_name
|
266
264
|
methods["#{attr}?".to_sym] = attr_name
|
267
265
|
methods["#{attr}_before_type_cast".to_sym] = attr_name
|
268
|
-
methods
|
269
266
|
end
|
270
267
|
end
|
271
268
|
|
@@ -287,7 +284,7 @@ module ActiveRecord
|
|
287
284
|
#
|
288
285
|
# JobLevel.reset_column_information
|
289
286
|
# %w{assistant executive manager director}.each do |type|
|
290
|
-
# JobLevel.create(:
|
287
|
+
# JobLevel.create(name: type)
|
291
288
|
# end
|
292
289
|
# end
|
293
290
|
#
|
@@ -300,14 +297,23 @@ module ActiveRecord
|
|
300
297
|
undefine_attribute_methods
|
301
298
|
connection.schema_cache.clear_table_cache!(table_name) if table_exists?
|
302
299
|
|
303
|
-
@
|
300
|
+
@arel_engine = nil
|
301
|
+
@column_defaults = nil
|
302
|
+
@column_names = nil
|
303
|
+
@columns = nil
|
304
|
+
@columns_hash = nil
|
305
|
+
@column_types = nil
|
306
|
+
@content_columns = nil
|
304
307
|
@dynamic_methods_hash = nil
|
305
|
-
@inheritance_column
|
306
|
-
@
|
308
|
+
@inheritance_column = nil unless defined?(@explicit_inheritance_column) && @explicit_inheritance_column
|
309
|
+
@relation = nil
|
307
310
|
end
|
308
311
|
|
309
|
-
|
310
|
-
|
312
|
+
# This is a hook for use by modules that need to do extra stuff to
|
313
|
+
# attributes when they are initialized. (e.g. attribute
|
314
|
+
# serialization)
|
315
|
+
def initialize_attributes(attributes, options = {}) #:nodoc:
|
316
|
+
attributes
|
311
317
|
end
|
312
318
|
|
313
319
|
private
|
@@ -315,8 +321,7 @@ module ActiveRecord
|
|
315
321
|
# Guesses the table name, but does not decorate it with prefix and suffix information.
|
316
322
|
def undecorated_table_name(class_name = base_class.name)
|
317
323
|
table_name = class_name.to_s.demodulize.underscore
|
318
|
-
|
319
|
-
table_name
|
324
|
+
pluralize_table_names ? table_name.pluralize : table_name
|
320
325
|
end
|
321
326
|
|
322
327
|
# Computes and returns a table name according to default conventions.
|
@@ -324,7 +329,7 @@ module ActiveRecord
|
|
324
329
|
base = base_class
|
325
330
|
if self == base
|
326
331
|
# Nested classes are prefixed with singular parent table name.
|
327
|
-
if parent <
|
332
|
+
if parent < Base && !parent.abstract_class?
|
328
333
|
contained = parent.table_name
|
329
334
|
contained = contained.singularize if parent.pluralize_table_names
|
330
335
|
contained += '_'
|
@@ -335,34 +340,6 @@ module ActiveRecord
|
|
335
340
|
base.table_name
|
336
341
|
end
|
337
342
|
end
|
338
|
-
|
339
|
-
def deprecated_property_setter(property, value, block)
|
340
|
-
if block
|
341
|
-
ActiveSupport::Deprecation.warn(
|
342
|
-
"Calling set_#{property} is deprecated. If you need to lazily evaluate " \
|
343
|
-
"the #{property}, define your own `self.#{property}` class method. You can use `super` " \
|
344
|
-
"to get the default #{property} where you would have called `original_#{property}`."
|
345
|
-
)
|
346
|
-
|
347
|
-
define_attr_method property, value, false, &block
|
348
|
-
else
|
349
|
-
ActiveSupport::Deprecation.warn(
|
350
|
-
"Calling set_#{property} is deprecated. Please use `self.#{property} = 'the_name'` instead."
|
351
|
-
)
|
352
|
-
|
353
|
-
define_attr_method property, value, false
|
354
|
-
end
|
355
|
-
end
|
356
|
-
|
357
|
-
def deprecated_original_property_getter(property)
|
358
|
-
ActiveSupport::Deprecation.warn("original_#{property} is deprecated. Define self.#{property} and call super instead.")
|
359
|
-
|
360
|
-
if !instance_variable_defined?("@original_#{property}") && respond_to?("reset_#{property}")
|
361
|
-
send("reset_#{property}")
|
362
|
-
else
|
363
|
-
instance_variable_get("@original_#{property}")
|
364
|
-
end
|
365
|
-
end
|
366
343
|
end
|
367
344
|
end
|
368
345
|
end
|