activerecord 3.2.22.4 → 4.0.13
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 +2799 -617
- data/MIT-LICENSE +1 -1
- data/README.rdoc +23 -32
- data/examples/performance.rb +1 -1
- data/lib/active_record/aggregations.rb +40 -34
- data/lib/active_record/association_relation.rb +22 -0
- data/lib/active_record/associations/alias_tracker.rb +4 -2
- data/lib/active_record/associations/association.rb +60 -46
- data/lib/active_record/associations/association_scope.rb +46 -40
- data/lib/active_record/associations/belongs_to_association.rb +17 -4
- data/lib/active_record/associations/belongs_to_polymorphic_association.rb +1 -1
- data/lib/active_record/associations/builder/association.rb +81 -28
- data/lib/active_record/associations/builder/belongs_to.rb +73 -56
- 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 +130 -96
- data/lib/active_record/associations/collection_proxy.rb +916 -63
- data/lib/active_record/associations/has_and_belongs_to_many_association.rb +15 -13
- data/lib/active_record/associations/has_many_association.rb +35 -8
- data/lib/active_record/associations/has_many_through_association.rb +37 -17
- data/lib/active_record/associations/has_one_association.rb +42 -19
- data/lib/active_record/associations/has_one_through_association.rb +1 -1
- data/lib/active_record/associations/join_dependency/join_association.rb +39 -22
- data/lib/active_record/associations/join_dependency/join_base.rb +2 -2
- data/lib/active_record/associations/join_dependency/join_part.rb +21 -8
- data/lib/active_record/associations/join_dependency.rb +30 -9
- data/lib/active_record/associations/join_helper.rb +1 -11
- 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 +2 -2
- data/lib/active_record/associations/preloader/has_many_through.rb +6 -2
- 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/preloader.rb +20 -43
- data/lib/active_record/associations/singular_association.rb +11 -11
- data/lib/active_record/associations/through_association.rb +3 -3
- data/lib/active_record/associations.rb +223 -282
- data/lib/active_record/attribute_assignment.rb +134 -154
- data/lib/active_record/attribute_methods/before_type_cast.rb +44 -5
- data/lib/active_record/attribute_methods/dirty.rb +36 -29
- data/lib/active_record/attribute_methods/primary_key.rb +45 -31
- data/lib/active_record/attribute_methods/query.rb +5 -4
- data/lib/active_record/attribute_methods/read.rb +67 -90
- data/lib/active_record/attribute_methods/serialization.rb +133 -70
- data/lib/active_record/attribute_methods/time_zone_conversion.rb +51 -45
- data/lib/active_record/attribute_methods/write.rb +34 -39
- data/lib/active_record/attribute_methods.rb +268 -108
- data/lib/active_record/autosave_association.rb +80 -73
- data/lib/active_record/base.rb +54 -451
- data/lib/active_record/callbacks.rb +60 -22
- data/lib/active_record/coders/yaml_column.rb +18 -21
- data/lib/active_record/connection_adapters/abstract/connection_pool.rb +347 -197
- data/lib/active_record/connection_adapters/abstract/database_limits.rb +9 -0
- data/lib/active_record/connection_adapters/abstract/database_statements.rb +146 -138
- data/lib/active_record/connection_adapters/abstract/query_cache.rb +25 -19
- data/lib/active_record/connection_adapters/abstract/quoting.rb +19 -3
- data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +151 -142
- data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +70 -0
- data/lib/active_record/connection_adapters/abstract/schema_statements.rb +499 -217
- data/lib/active_record/connection_adapters/abstract/transaction.rb +208 -0
- data/lib/active_record/connection_adapters/abstract_adapter.rb +209 -44
- data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +169 -61
- data/lib/active_record/connection_adapters/column.rb +67 -36
- data/lib/active_record/connection_adapters/connection_specification.rb +96 -0
- data/lib/active_record/connection_adapters/mysql2_adapter.rb +28 -29
- data/lib/active_record/connection_adapters/mysql_adapter.rb +200 -73
- data/lib/active_record/connection_adapters/postgresql/array_parser.rb +98 -0
- data/lib/active_record/connection_adapters/postgresql/cast.rb +160 -0
- data/lib/active_record/connection_adapters/postgresql/database_statements.rb +240 -0
- data/lib/active_record/connection_adapters/postgresql/oid.rb +374 -0
- data/lib/active_record/connection_adapters/postgresql/quoting.rb +183 -0
- data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +30 -0
- data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +508 -0
- data/lib/active_record/connection_adapters/postgresql_adapter.rb +544 -899
- data/lib/active_record/connection_adapters/schema_cache.rb +76 -16
- data/lib/active_record/connection_adapters/sqlite3_adapter.rb +595 -16
- data/lib/active_record/connection_handling.rb +98 -0
- data/lib/active_record/core.rb +472 -0
- data/lib/active_record/counter_cache.rb +107 -108
- data/lib/active_record/dynamic_matchers.rb +115 -63
- data/lib/active_record/errors.rb +36 -18
- data/lib/active_record/explain.rb +15 -63
- data/lib/active_record/explain_registry.rb +30 -0
- data/lib/active_record/explain_subscriber.rb +8 -4
- data/lib/active_record/fixture_set/file.rb +55 -0
- data/lib/active_record/fixtures.rb +159 -155
- data/lib/active_record/inheritance.rb +93 -59
- data/lib/active_record/integration.rb +8 -8
- data/lib/active_record/locale/en.yml +8 -1
- data/lib/active_record/locking/optimistic.rb +39 -43
- data/lib/active_record/locking/pessimistic.rb +4 -4
- data/lib/active_record/log_subscriber.rb +19 -9
- data/lib/active_record/migration/command_recorder.rb +102 -33
- data/lib/active_record/migration/join_table.rb +15 -0
- data/lib/active_record/migration.rb +411 -173
- data/lib/active_record/model_schema.rb +81 -94
- data/lib/active_record/nested_attributes.rb +173 -131
- data/lib/active_record/null_relation.rb +67 -0
- data/lib/active_record/persistence.rb +254 -106
- data/lib/active_record/query_cache.rb +18 -36
- data/lib/active_record/querying.rb +19 -15
- data/lib/active_record/railtie.rb +113 -38
- data/lib/active_record/railties/console_sandbox.rb +3 -4
- data/lib/active_record/railties/controller_runtime.rb +4 -3
- data/lib/active_record/railties/databases.rake +115 -368
- 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 +110 -61
- data/lib/active_record/relation/batches.rb +29 -29
- data/lib/active_record/relation/calculations.rb +155 -125
- data/lib/active_record/relation/delegation.rb +94 -18
- data/lib/active_record/relation/finder_methods.rb +151 -203
- data/lib/active_record/relation/merger.rb +188 -0
- data/lib/active_record/relation/predicate_builder.rb +85 -42
- data/lib/active_record/relation/query_methods.rb +793 -146
- data/lib/active_record/relation/spawn_methods.rb +43 -150
- data/lib/active_record/relation.rb +293 -173
- data/lib/active_record/result.rb +48 -7
- data/lib/active_record/runtime_registry.rb +17 -0
- data/lib/active_record/sanitization.rb +41 -54
- data/lib/active_record/schema.rb +19 -12
- data/lib/active_record/schema_dumper.rb +41 -41
- data/lib/active_record/schema_migration.rb +46 -0
- data/lib/active_record/scoping/default.rb +56 -52
- data/lib/active_record/scoping/named.rb +78 -103
- data/lib/active_record/scoping.rb +54 -124
- data/lib/active_record/serialization.rb +6 -2
- data/lib/active_record/serializers/xml_serializer.rb +9 -15
- data/lib/active_record/statement_cache.rb +26 -0
- data/lib/active_record/store.rb +131 -15
- data/lib/active_record/tasks/database_tasks.rb +204 -0
- data/lib/active_record/tasks/firebird_database_tasks.rb +56 -0
- data/lib/active_record/tasks/mysql_database_tasks.rb +144 -0
- data/lib/active_record/tasks/oracle_database_tasks.rb +45 -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/tasks/sqlserver_database_tasks.rb +48 -0
- data/lib/active_record/test_case.rb +67 -38
- data/lib/active_record/timestamp.rb +16 -11
- data/lib/active_record/transactions.rb +73 -51
- data/lib/active_record/validations/associated.rb +19 -13
- data/lib/active_record/validations/presence.rb +65 -0
- data/lib/active_record/validations/uniqueness.rb +110 -57
- data/lib/active_record/validations.rb +18 -17
- data/lib/active_record/version.rb +7 -6
- data/lib/active_record.rb +63 -45
- data/lib/rails/generators/active_record/migration/migration_generator.rb +45 -8
- data/lib/rails/generators/active_record/{model/templates/migration.rb → migration/templates/create_table_migration.rb} +4 -0
- data/lib/rails/generators/active_record/migration/templates/migration.rb +20 -15
- data/lib/rails/generators/active_record/model/model_generator.rb +5 -4
- data/lib/rails/generators/active_record/model/templates/model.rb +4 -6
- data/lib/rails/generators/active_record/model/templates/module.rb +1 -1
- data/lib/rails/generators/active_record.rb +3 -5
- metadata +43 -29
- data/examples/associations.png +0 -0
- 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
@@ -10,11 +10,11 @@ module ActiveRecord
|
|
10
10
|
# puts invalid.record.errors
|
11
11
|
# end
|
12
12
|
class RecordInvalid < ActiveRecordError
|
13
|
-
attr_reader :record
|
14
|
-
def initialize(record)
|
13
|
+
attr_reader :record # :nodoc:
|
14
|
+
def initialize(record) # :nodoc:
|
15
15
|
@record = record
|
16
16
|
errors = @record.errors.full_messages.join(", ")
|
17
|
-
super(I18n.t("
|
17
|
+
super(I18n.t(:"#{@record.class.i18n_scope}.errors.messages.record_invalid", :errors => errors, :default => :"errors.messages.record_invalid"))
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
@@ -32,11 +32,11 @@ module ActiveRecord
|
|
32
32
|
module ClassMethods
|
33
33
|
# Creates an object just like Base.create but calls <tt>save!</tt> instead of +save+
|
34
34
|
# so an exception is raised if the record is invalid.
|
35
|
-
def create!(attributes = nil,
|
35
|
+
def create!(attributes = nil, &block)
|
36
36
|
if attributes.is_a?(Array)
|
37
|
-
attributes.collect { |attr| create!(attr,
|
37
|
+
attributes.collect { |attr| create!(attr, &block) }
|
38
38
|
else
|
39
|
-
object = new(attributes
|
39
|
+
object = new(attributes)
|
40
40
|
yield(object) if block_given?
|
41
41
|
object.save!
|
42
42
|
object
|
@@ -44,23 +44,24 @@ module ActiveRecord
|
|
44
44
|
end
|
45
45
|
end
|
46
46
|
|
47
|
-
# The validation process on save can be skipped by passing <tt
|
48
|
-
# replaced with this when the validations
|
47
|
+
# The validation process on save can be skipped by passing <tt>validate: false</tt>.
|
48
|
+
# The regular Base#save method is replaced with this when the validations
|
49
|
+
# module is mixed in, which it is by default.
|
49
50
|
def save(options={})
|
50
51
|
perform_validations(options) ? super : false
|
51
52
|
end
|
52
53
|
|
53
|
-
# Attempts to save the record just like Base#save but will raise a +RecordInvalid+
|
54
|
-
# if the record is not valid.
|
54
|
+
# Attempts to save the record just like Base#save but will raise a +RecordInvalid+
|
55
|
+
# exception instead of returning +false+ if the record is not valid.
|
55
56
|
def save!(options={})
|
56
57
|
perform_validations(options) ? super : raise(RecordInvalid.new(self))
|
57
58
|
end
|
58
59
|
|
59
|
-
# Runs all the validations within the specified context. Returns true if
|
60
|
-
# false otherwise.
|
60
|
+
# Runs all the validations within the specified context. Returns +true+ if
|
61
|
+
# no errors are found, +false+ otherwise.
|
61
62
|
#
|
62
|
-
# If the argument is false (default is +nil+), the context is set to <tt>:create</tt> if
|
63
|
-
# <tt>new_record?</tt> is true
|
63
|
+
# If the argument is +false+ (default is +nil+), the context is set to <tt>:create</tt> if
|
64
|
+
# <tt>new_record?</tt> is +true+, and to <tt>:update</tt> if it is not.
|
64
65
|
#
|
65
66
|
# Validations with no <tt>:on</tt> option will run no matter the context. Validations with
|
66
67
|
# some <tt>:on</tt> option will only run in the specified context.
|
@@ -72,12 +73,12 @@ module ActiveRecord
|
|
72
73
|
|
73
74
|
protected
|
74
75
|
|
75
|
-
def perform_validations(options={})
|
76
|
-
|
77
|
-
perform_validation ? valid?(options[:context]) : true
|
76
|
+
def perform_validations(options={}) # :nodoc:
|
77
|
+
options[:validate] == false || valid?(options[:context])
|
78
78
|
end
|
79
79
|
end
|
80
80
|
end
|
81
81
|
|
82
82
|
require "active_record/validations/associated"
|
83
83
|
require "active_record/validations/uniqueness"
|
84
|
+
require "active_record/validations/presence"
|
@@ -1,10 +1,11 @@
|
|
1
1
|
module ActiveRecord
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
PRE = "4"
|
2
|
+
# Returns the version of the currently loaded ActiveRecord as a Gem::Version
|
3
|
+
def self.version
|
4
|
+
Gem::Version.new "4.0.13"
|
5
|
+
end
|
7
6
|
|
8
|
-
|
7
|
+
module VERSION #:nodoc:
|
8
|
+
MAJOR, MINOR, TINY, PRE = ActiveRecord.version.segments
|
9
|
+
STRING = ActiveRecord.version.to_s
|
9
10
|
end
|
10
11
|
end
|
data/lib/active_record.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
#--
|
2
|
-
# Copyright (c) 2004-
|
2
|
+
# Copyright (c) 2004-2013 David Heinemeier Hansson
|
3
3
|
#
|
4
4
|
# Permission is hereby granted, free of charge, to any person obtaining
|
5
5
|
# a copy of this software and associated documentation files (the
|
@@ -22,24 +22,47 @@
|
|
22
22
|
#++
|
23
23
|
|
24
24
|
require 'active_support'
|
25
|
-
require 'active_support/
|
25
|
+
require 'active_support/rails'
|
26
26
|
require 'active_model'
|
27
27
|
require 'arel'
|
28
|
+
require 'active_record/deprecated_finders'
|
28
29
|
|
29
30
|
require 'active_record/version'
|
30
31
|
|
31
32
|
module ActiveRecord
|
32
33
|
extend ActiveSupport::Autoload
|
33
34
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
autoload :
|
35
|
+
autoload :Base
|
36
|
+
autoload :Callbacks
|
37
|
+
autoload :Core
|
38
|
+
autoload :ConnectionHandling
|
39
|
+
autoload :CounterCache
|
40
|
+
autoload :DynamicMatchers
|
41
|
+
autoload :Explain
|
42
|
+
autoload :Inheritance
|
43
|
+
autoload :Integration
|
44
|
+
autoload :Migration
|
45
|
+
autoload :Migrator, 'active_record/migration'
|
46
|
+
autoload :ModelSchema
|
47
|
+
autoload :NestedAttributes
|
48
|
+
autoload :Persistence
|
49
|
+
autoload :QueryCache
|
50
|
+
autoload :Querying
|
51
|
+
autoload :ReadonlyAttributes
|
52
|
+
autoload :Reflection
|
53
|
+
autoload :RuntimeRegistry
|
54
|
+
autoload :Sanitization
|
55
|
+
autoload :Schema
|
56
|
+
autoload :SchemaDumper
|
57
|
+
autoload :SchemaMigration
|
58
|
+
autoload :Scoping
|
59
|
+
autoload :Serialization
|
60
|
+
autoload :StatementCache
|
61
|
+
autoload :Store
|
62
|
+
autoload :Timestamp
|
63
|
+
autoload :Transactions
|
64
|
+
autoload :Translation
|
65
|
+
autoload :Validations
|
43
66
|
|
44
67
|
eager_autoload do
|
45
68
|
autoload :ActiveRecordError, 'active_record/errors'
|
@@ -48,11 +71,13 @@ module ActiveRecord
|
|
48
71
|
|
49
72
|
autoload :Aggregations
|
50
73
|
autoload :Associations
|
51
|
-
autoload :AttributeMethods
|
52
74
|
autoload :AttributeAssignment
|
75
|
+
autoload :AttributeMethods
|
53
76
|
autoload :AutosaveAssociation
|
54
77
|
|
55
78
|
autoload :Relation
|
79
|
+
autoload :AssociationRelation
|
80
|
+
autoload :NullRelation
|
56
81
|
|
57
82
|
autoload_under 'relation' do
|
58
83
|
autoload :QueryMethods
|
@@ -61,41 +86,10 @@ module ActiveRecord
|
|
61
86
|
autoload :PredicateBuilder
|
62
87
|
autoload :SpawnMethods
|
63
88
|
autoload :Batches
|
64
|
-
autoload :Explain
|
65
89
|
autoload :Delegation
|
66
90
|
end
|
67
91
|
|
68
|
-
autoload :Base
|
69
|
-
autoload :Callbacks
|
70
|
-
autoload :CounterCache
|
71
|
-
autoload :DynamicMatchers
|
72
|
-
autoload :DynamicFinderMatch
|
73
|
-
autoload :DynamicScopeMatch
|
74
|
-
autoload :Explain
|
75
|
-
autoload :IdentityMap
|
76
|
-
autoload :Inheritance
|
77
|
-
autoload :Integration
|
78
|
-
autoload :Migration
|
79
|
-
autoload :Migrator, 'active_record/migration'
|
80
|
-
autoload :ModelSchema
|
81
|
-
autoload :NestedAttributes
|
82
|
-
autoload :Observer
|
83
|
-
autoload :Persistence
|
84
|
-
autoload :QueryCache
|
85
|
-
autoload :Querying
|
86
|
-
autoload :ReadonlyAttributes
|
87
|
-
autoload :Reflection
|
88
92
|
autoload :Result
|
89
|
-
autoload :Sanitization
|
90
|
-
autoload :Schema
|
91
|
-
autoload :SchemaDumper
|
92
|
-
autoload :Scoping
|
93
|
-
autoload :Serialization
|
94
|
-
autoload :Store
|
95
|
-
autoload :Timestamp
|
96
|
-
autoload :Transactions
|
97
|
-
autoload :Translation
|
98
|
-
autoload :Validations
|
99
93
|
end
|
100
94
|
|
101
95
|
module Coders
|
@@ -114,7 +108,6 @@ module ActiveRecord
|
|
114
108
|
autoload :TimeZoneConversion
|
115
109
|
autoload :Write
|
116
110
|
autoload :Serialization
|
117
|
-
autoload :DeprecatedUnderscoreRead
|
118
111
|
end
|
119
112
|
end
|
120
113
|
|
@@ -145,12 +138,37 @@ module ActiveRecord
|
|
145
138
|
end
|
146
139
|
end
|
147
140
|
|
141
|
+
module Tasks
|
142
|
+
extend ActiveSupport::Autoload
|
143
|
+
|
144
|
+
autoload :DatabaseTasks
|
145
|
+
autoload :SQLiteDatabaseTasks, 'active_record/tasks/sqlite_database_tasks'
|
146
|
+
autoload :MySQLDatabaseTasks, 'active_record/tasks/mysql_database_tasks'
|
147
|
+
autoload :PostgreSQLDatabaseTasks,
|
148
|
+
'active_record/tasks/postgresql_database_tasks'
|
149
|
+
|
150
|
+
autoload :FirebirdDatabaseTasks, 'active_record/tasks/firebird_database_tasks'
|
151
|
+
autoload :SqlserverDatabaseTasks, 'active_record/tasks/sqlserver_database_tasks'
|
152
|
+
autoload :OracleDatabaseTasks, 'active_record/tasks/oracle_database_tasks'
|
153
|
+
end
|
154
|
+
|
148
155
|
autoload :TestCase
|
149
156
|
autoload :TestFixtures, 'active_record/fixtures'
|
157
|
+
|
158
|
+
def self.eager_load!
|
159
|
+
super
|
160
|
+
ActiveRecord::Locking.eager_load!
|
161
|
+
ActiveRecord::Scoping.eager_load!
|
162
|
+
ActiveRecord::Associations.eager_load!
|
163
|
+
ActiveRecord::AttributeMethods.eager_load!
|
164
|
+
ActiveRecord::ConnectionAdapters.eager_load!
|
165
|
+
end
|
150
166
|
end
|
151
167
|
|
152
168
|
ActiveSupport.on_load(:active_record) do
|
153
169
|
Arel::Table.engine = self
|
154
170
|
end
|
155
171
|
|
156
|
-
|
172
|
+
ActiveSupport.on_load(:i18n) do
|
173
|
+
I18n.load_path << File.dirname(__FILE__) + '/active_record/locale/en.yml'
|
174
|
+
end
|
@@ -1,25 +1,62 @@
|
|
1
1
|
require 'rails/generators/active_record'
|
2
2
|
|
3
3
|
module ActiveRecord
|
4
|
-
module Generators
|
5
|
-
class MigrationGenerator < Base
|
4
|
+
module Generators # :nodoc:
|
5
|
+
class MigrationGenerator < Base # :nodoc:
|
6
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!
|
10
|
-
|
10
|
+
validate_file_name!
|
11
|
+
migration_template @migration_template, "db/migrate/#{file_name}.rb"
|
11
12
|
end
|
12
13
|
|
13
14
|
protected
|
14
|
-
|
15
|
+
attr_reader :migration_action, :join_tables
|
15
16
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
17
|
+
def set_local_assigns!
|
18
|
+
@migration_template = "migration.rb"
|
19
|
+
case file_name
|
20
|
+
when /^(add|remove)_.*_(?:to|from)_(.*)/
|
21
|
+
@migration_action = $1
|
22
|
+
@table_name = $2.pluralize
|
23
|
+
when /join_table/
|
24
|
+
if attributes.length == 2
|
25
|
+
@migration_action = 'join'
|
26
|
+
@join_tables = attributes.map(&:plural_name)
|
27
|
+
|
28
|
+
set_index_names
|
20
29
|
end
|
30
|
+
when /^create_(.+)/
|
31
|
+
@table_name = $1.pluralize
|
32
|
+
@migration_template = "create_table_migration.rb"
|
21
33
|
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def set_index_names
|
37
|
+
attributes.each_with_index do |attr, i|
|
38
|
+
attr.index_name = [attr, attributes[i - 1]].map{ |a| index_name_for(a) }
|
39
|
+
end
|
40
|
+
end
|
22
41
|
|
42
|
+
def index_name_for(attribute)
|
43
|
+
if attribute.foreign_key?
|
44
|
+
attribute.name
|
45
|
+
else
|
46
|
+
attribute.name.singularize.foreign_key
|
47
|
+
end.to_sym
|
48
|
+
end
|
49
|
+
|
50
|
+
private
|
51
|
+
def attributes_with_index
|
52
|
+
attributes.select { |a| !a.reference? && a.has_index? }
|
53
|
+
end
|
54
|
+
|
55
|
+
def validate_file_name!
|
56
|
+
unless file_name =~ /^[_a-z0-9]+$/
|
57
|
+
raise IllegalMigrationNameError.new(file_name)
|
58
|
+
end
|
59
|
+
end
|
23
60
|
end
|
24
61
|
end
|
25
62
|
end
|
@@ -2,8 +2,12 @@ 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
|
+
<% if attribute.password_digest? -%>
|
6
|
+
t.string :password_digest<%= attribute.inject_options %>
|
7
|
+
<% else -%>
|
5
8
|
t.<%= attribute.type %> :<%= attribute.name %><%= attribute.inject_options %>
|
6
9
|
<% end -%>
|
10
|
+
<% end -%>
|
7
11
|
<% if options[:timestamps] %>
|
8
12
|
t.timestamps
|
9
13
|
<% end -%>
|
@@ -2,32 +2,37 @@ class <%= migration_class_name %> < ActiveRecord::Migration
|
|
2
2
|
<%- if migration_action == 'add' -%>
|
3
3
|
def change
|
4
4
|
<% attributes.each do |attribute| -%>
|
5
|
+
<%- if attribute.reference? -%>
|
6
|
+
add_reference :<%= table_name %>, :<%= attribute.name %><%= attribute.inject_options %>
|
7
|
+
<%- else -%>
|
5
8
|
add_column :<%= table_name %>, :<%= attribute.name %>, :<%= attribute.type %><%= attribute.inject_options %>
|
6
9
|
<%- if attribute.has_index? -%>
|
7
10
|
add_index :<%= table_name %>, :<%= attribute.index_name %><%= attribute.inject_index_options %>
|
8
11
|
<%- end -%>
|
12
|
+
<%- end -%>
|
9
13
|
<%- end -%>
|
10
14
|
end
|
15
|
+
<%- elsif migration_action == 'join' -%>
|
16
|
+
def change
|
17
|
+
create_join_table :<%= join_tables.first %>, :<%= join_tables.second %> do |t|
|
18
|
+
<%- attributes.each do |attribute| -%>
|
19
|
+
<%= '# ' unless attribute.has_index? -%>t.index <%= attribute.index_name %><%= attribute.inject_index_options %>
|
20
|
+
<%- end -%>
|
21
|
+
end
|
22
|
+
end
|
11
23
|
<%- else -%>
|
12
|
-
def
|
24
|
+
def change
|
13
25
|
<% attributes.each do |attribute| -%>
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
26
|
+
<%- if migration_action -%>
|
27
|
+
<%- if attribute.reference? -%>
|
28
|
+
remove_reference :<%= table_name %>, :<%= attribute.name %><%= attribute.inject_options %>
|
29
|
+
<%- else -%>
|
30
|
+
<%- if attribute.has_index? -%>
|
31
|
+
remove_index :<%= table_name %>, :<%= attribute.index_name %><%= attribute.inject_index_options %>
|
18
32
|
<%- end -%>
|
33
|
+
remove_column :<%= table_name %>, :<%= attribute.name %>, :<%= attribute.type %><%= attribute.inject_options %>
|
19
34
|
<%- end -%>
|
20
35
|
<%- end -%>
|
21
|
-
end
|
22
|
-
|
23
|
-
def down
|
24
|
-
<% attributes.reverse.each do |attribute| -%>
|
25
|
-
<%- if migration_action -%>
|
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 -%>
|
30
|
-
<%- end -%>
|
31
36
|
<%- end -%>
|
32
37
|
end
|
33
38
|
<%- end -%>
|
@@ -1,8 +1,8 @@
|
|
1
1
|
require 'rails/generators/active_record'
|
2
2
|
|
3
3
|
module ActiveRecord
|
4
|
-
module Generators
|
5
|
-
class ModelGenerator < Base
|
4
|
+
module Generators # :nodoc:
|
5
|
+
class ModelGenerator < Base # :nodoc:
|
6
6
|
argument :attributes, :type => :array, :default => [], :banner => "field[:type][:index] field[:type][:index]"
|
7
7
|
|
8
8
|
check_class_collision
|
@@ -14,7 +14,8 @@ module ActiveRecord
|
|
14
14
|
|
15
15
|
def create_migration_file
|
16
16
|
return unless options[:migration] && options[:parent].nil?
|
17
|
-
|
17
|
+
attributes.each { |a| a.attr_options.delete(:index) if a.reference? && !a.has_index? } if options[:indexes] == false
|
18
|
+
migration_template "../../migration/templates/create_table_migration.rb", "db/migrate/create_#{table_name}.rb"
|
18
19
|
end
|
19
20
|
|
20
21
|
def create_model_file
|
@@ -27,7 +28,7 @@ module ActiveRecord
|
|
27
28
|
end
|
28
29
|
|
29
30
|
def attributes_with_index
|
30
|
-
attributes.select { |a| a.
|
31
|
+
attributes.select { |a| !a.reference? && a.has_index? }
|
31
32
|
end
|
32
33
|
|
33
34
|
def accessible_attributes
|
@@ -1,12 +1,10 @@
|
|
1
1
|
<% module_namespacing do -%>
|
2
2
|
class <%= class_name %> < <%= parent_class_name.classify %>
|
3
|
-
<% attributes.select
|
4
|
-
belongs_to :<%= attribute.name %>
|
3
|
+
<% attributes.select(&:reference?).each do |attribute| -%>
|
4
|
+
belongs_to :<%= attribute.name %><%= ', polymorphic: true' if attribute.polymorphic? %>
|
5
5
|
<% end -%>
|
6
|
-
<% if
|
7
|
-
|
8
|
-
<% else -%>
|
9
|
-
# attr_accessible :title, :body
|
6
|
+
<% if attributes.any?(&:password_digest?) -%>
|
7
|
+
has_secure_password
|
10
8
|
<% end -%>
|
11
9
|
end
|
12
10
|
<% end -%>
|
@@ -1,14 +1,12 @@
|
|
1
1
|
require 'rails/generators/named_base'
|
2
2
|
require 'rails/generators/migration'
|
3
3
|
require 'rails/generators/active_model'
|
4
|
-
require 'rails/generators/active_record/migration'
|
5
4
|
require 'active_record'
|
6
5
|
|
7
6
|
module ActiveRecord
|
8
|
-
module Generators
|
9
|
-
class Base < Rails::Generators::NamedBase
|
7
|
+
module Generators # :nodoc:
|
8
|
+
class Base < Rails::Generators::NamedBase # :nodoc:
|
10
9
|
include Rails::Generators::Migration
|
11
|
-
extend ActiveRecord::Generators::Migration
|
12
10
|
|
13
11
|
# Set the current directory as base for the inherited generators.
|
14
12
|
def self.base_root
|
@@ -16,7 +14,7 @@ module ActiveRecord
|
|
16
14
|
end
|
17
15
|
|
18
16
|
# Implement the required interface for Rails::Generators::Migration.
|
19
|
-
def self.next_migration_number(dirname)
|
17
|
+
def self.next_migration_number(dirname)
|
20
18
|
next_migration_number = current_migration_number(dirname) + 1
|
21
19
|
ActiveRecord::Migration.next_migration_number(next_migration_number)
|
22
20
|
end
|