activerecord 1.0.0 → 4.0.0
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 +7 -0
- data/CHANGELOG.md +2102 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +213 -0
- data/examples/performance.rb +172 -0
- data/examples/simple.rb +14 -0
- data/lib/active_record/aggregations.rb +180 -84
- data/lib/active_record/associations/alias_tracker.rb +76 -0
- data/lib/active_record/associations/association.rb +248 -0
- data/lib/active_record/associations/association_scope.rb +135 -0
- data/lib/active_record/associations/belongs_to_association.rb +92 -0
- data/lib/active_record/associations/belongs_to_polymorphic_association.rb +35 -0
- data/lib/active_record/associations/builder/association.rb +108 -0
- data/lib/active_record/associations/builder/belongs_to.rb +98 -0
- data/lib/active_record/associations/builder/collection_association.rb +89 -0
- data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +39 -0
- data/lib/active_record/associations/builder/has_many.rb +15 -0
- data/lib/active_record/associations/builder/has_one.rb +25 -0
- data/lib/active_record/associations/builder/singular_association.rb +32 -0
- data/lib/active_record/associations/collection_association.rb +608 -0
- data/lib/active_record/associations/collection_proxy.rb +986 -0
- data/lib/active_record/associations/has_and_belongs_to_many_association.rb +58 -39
- data/lib/active_record/associations/has_many_association.rb +116 -85
- data/lib/active_record/associations/has_many_through_association.rb +197 -0
- data/lib/active_record/associations/has_one_association.rb +102 -0
- data/lib/active_record/associations/has_one_through_association.rb +36 -0
- data/lib/active_record/associations/join_dependency/join_association.rb +174 -0
- data/lib/active_record/associations/join_dependency/join_base.rb +24 -0
- data/lib/active_record/associations/join_dependency/join_part.rb +78 -0
- data/lib/active_record/associations/join_dependency.rb +235 -0
- data/lib/active_record/associations/join_helper.rb +45 -0
- data/lib/active_record/associations/preloader/association.rb +121 -0
- data/lib/active_record/associations/preloader/belongs_to.rb +17 -0
- data/lib/active_record/associations/preloader/collection_association.rb +24 -0
- data/lib/active_record/associations/preloader/has_and_belongs_to_many.rb +60 -0
- data/lib/active_record/associations/preloader/has_many.rb +17 -0
- data/lib/active_record/associations/preloader/has_many_through.rb +19 -0
- data/lib/active_record/associations/preloader/has_one.rb +23 -0
- data/lib/active_record/associations/preloader/has_one_through.rb +9 -0
- data/lib/active_record/associations/preloader/singular_association.rb +21 -0
- data/lib/active_record/associations/preloader/through_association.rb +63 -0
- data/lib/active_record/associations/preloader.rb +178 -0
- data/lib/active_record/associations/singular_association.rb +64 -0
- data/lib/active_record/associations/through_association.rb +87 -0
- data/lib/active_record/associations.rb +1437 -431
- data/lib/active_record/attribute_assignment.rb +201 -0
- data/lib/active_record/attribute_methods/before_type_cast.rb +70 -0
- data/lib/active_record/attribute_methods/dirty.rb +118 -0
- data/lib/active_record/attribute_methods/primary_key.rb +122 -0
- data/lib/active_record/attribute_methods/query.rb +40 -0
- data/lib/active_record/attribute_methods/read.rb +107 -0
- data/lib/active_record/attribute_methods/serialization.rb +162 -0
- data/lib/active_record/attribute_methods/time_zone_conversion.rb +59 -0
- data/lib/active_record/attribute_methods/write.rb +63 -0
- data/lib/active_record/attribute_methods.rb +393 -0
- data/lib/active_record/autosave_association.rb +426 -0
- data/lib/active_record/base.rb +268 -930
- data/lib/active_record/callbacks.rb +203 -230
- data/lib/active_record/coders/yaml_column.rb +38 -0
- data/lib/active_record/connection_adapters/abstract/connection_pool.rb +638 -0
- data/lib/active_record/connection_adapters/abstract/database_limits.rb +67 -0
- data/lib/active_record/connection_adapters/abstract/database_statements.rb +390 -0
- data/lib/active_record/connection_adapters/abstract/query_cache.rb +95 -0
- data/lib/active_record/connection_adapters/abstract/quoting.rb +129 -0
- data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +501 -0
- data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +70 -0
- data/lib/active_record/connection_adapters/abstract/schema_statements.rb +873 -0
- data/lib/active_record/connection_adapters/abstract/transaction.rb +203 -0
- data/lib/active_record/connection_adapters/abstract_adapter.rb +389 -275
- data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +782 -0
- data/lib/active_record/connection_adapters/column.rb +318 -0
- data/lib/active_record/connection_adapters/connection_specification.rb +96 -0
- data/lib/active_record/connection_adapters/mysql2_adapter.rb +273 -0
- data/lib/active_record/connection_adapters/mysql_adapter.rb +517 -90
- data/lib/active_record/connection_adapters/postgresql/array_parser.rb +97 -0
- data/lib/active_record/connection_adapters/postgresql/cast.rb +152 -0
- data/lib/active_record/connection_adapters/postgresql/database_statements.rb +242 -0
- data/lib/active_record/connection_adapters/postgresql/oid.rb +366 -0
- data/lib/active_record/connection_adapters/postgresql/quoting.rb +171 -0
- data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +30 -0
- data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +489 -0
- data/lib/active_record/connection_adapters/postgresql_adapter.rb +911 -138
- data/lib/active_record/connection_adapters/schema_cache.rb +129 -0
- data/lib/active_record/connection_adapters/sqlite3_adapter.rb +624 -0
- data/lib/active_record/connection_adapters/statement_pool.rb +40 -0
- data/lib/active_record/connection_handling.rb +98 -0
- data/lib/active_record/core.rb +463 -0
- data/lib/active_record/counter_cache.rb +122 -0
- data/lib/active_record/dynamic_matchers.rb +131 -0
- data/lib/active_record/errors.rb +213 -0
- data/lib/active_record/explain.rb +38 -0
- data/lib/active_record/explain_registry.rb +30 -0
- data/lib/active_record/explain_subscriber.rb +29 -0
- data/lib/active_record/fixture_set/file.rb +55 -0
- data/lib/active_record/fixtures.rb +892 -138
- data/lib/active_record/inheritance.rb +200 -0
- data/lib/active_record/integration.rb +60 -0
- data/lib/active_record/locale/en.yml +47 -0
- data/lib/active_record/locking/optimistic.rb +181 -0
- data/lib/active_record/locking/pessimistic.rb +77 -0
- data/lib/active_record/log_subscriber.rb +82 -0
- data/lib/active_record/migration/command_recorder.rb +164 -0
- data/lib/active_record/migration/join_table.rb +15 -0
- data/lib/active_record/migration.rb +1015 -0
- data/lib/active_record/model_schema.rb +345 -0
- data/lib/active_record/nested_attributes.rb +546 -0
- data/lib/active_record/null_relation.rb +65 -0
- data/lib/active_record/persistence.rb +509 -0
- data/lib/active_record/query_cache.rb +56 -0
- data/lib/active_record/querying.rb +62 -0
- data/lib/active_record/railtie.rb +205 -0
- data/lib/active_record/railties/console_sandbox.rb +5 -0
- data/lib/active_record/railties/controller_runtime.rb +50 -0
- data/lib/active_record/railties/databases.rake +402 -0
- data/lib/active_record/railties/jdbcmysql_error.rb +16 -0
- data/lib/active_record/readonly_attributes.rb +30 -0
- data/lib/active_record/reflection.rb +544 -87
- data/lib/active_record/relation/batches.rb +93 -0
- data/lib/active_record/relation/calculations.rb +399 -0
- data/lib/active_record/relation/delegation.rb +125 -0
- data/lib/active_record/relation/finder_methods.rb +349 -0
- data/lib/active_record/relation/merger.rb +161 -0
- data/lib/active_record/relation/predicate_builder.rb +106 -0
- data/lib/active_record/relation/query_methods.rb +1044 -0
- data/lib/active_record/relation/spawn_methods.rb +73 -0
- data/lib/active_record/relation.rb +655 -0
- data/lib/active_record/result.rb +67 -0
- data/lib/active_record/runtime_registry.rb +17 -0
- data/lib/active_record/sanitization.rb +168 -0
- data/lib/active_record/schema.rb +65 -0
- data/lib/active_record/schema_dumper.rb +204 -0
- data/lib/active_record/schema_migration.rb +39 -0
- data/lib/active_record/scoping/default.rb +146 -0
- data/lib/active_record/scoping/named.rb +175 -0
- data/lib/active_record/scoping.rb +82 -0
- data/lib/active_record/serialization.rb +22 -0
- data/lib/active_record/serializers/xml_serializer.rb +197 -0
- data/lib/active_record/statement_cache.rb +26 -0
- data/lib/active_record/store.rb +156 -0
- data/lib/active_record/tasks/database_tasks.rb +203 -0
- data/lib/active_record/tasks/firebird_database_tasks.rb +56 -0
- data/lib/active_record/tasks/mysql_database_tasks.rb +143 -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 +96 -0
- data/lib/active_record/timestamp.rb +119 -0
- data/lib/active_record/transactions.rb +366 -69
- data/lib/active_record/translation.rb +22 -0
- data/lib/active_record/validations/associated.rb +49 -0
- data/lib/active_record/validations/presence.rb +65 -0
- data/lib/active_record/validations/uniqueness.rb +225 -0
- data/lib/active_record/validations.rb +64 -185
- data/lib/active_record/version.rb +11 -0
- data/lib/active_record.rb +149 -24
- data/lib/rails/generators/active_record/migration/migration_generator.rb +62 -0
- data/lib/rails/generators/active_record/migration/templates/create_table_migration.rb +19 -0
- data/lib/rails/generators/active_record/migration/templates/migration.rb +39 -0
- data/lib/rails/generators/active_record/model/model_generator.rb +48 -0
- data/lib/rails/generators/active_record/model/templates/model.rb +10 -0
- data/lib/rails/generators/active_record/model/templates/module.rb +7 -0
- data/lib/rails/generators/active_record.rb +23 -0
- metadata +261 -161
- data/CHANGELOG +0 -581
- data/README +0 -361
- data/RUNNING_UNIT_TESTS +0 -36
- data/dev-utils/eval_debugger.rb +0 -9
- data/examples/associations.png +0 -0
- data/examples/associations.rb +0 -87
- data/examples/shared_setup.rb +0 -15
- data/examples/validation.rb +0 -88
- data/install.rb +0 -60
- data/lib/active_record/associations/association_collection.rb +0 -70
- data/lib/active_record/connection_adapters/sqlite_adapter.rb +0 -107
- data/lib/active_record/deprecated_associations.rb +0 -70
- data/lib/active_record/observer.rb +0 -71
- data/lib/active_record/support/class_attribute_accessors.rb +0 -43
- data/lib/active_record/support/class_inheritable_attributes.rb +0 -37
- data/lib/active_record/support/clean_logger.rb +0 -10
- data/lib/active_record/support/inflector.rb +0 -70
- data/lib/active_record/vendor/mysql.rb +0 -1117
- data/lib/active_record/vendor/simple.rb +0 -702
- data/lib/active_record/wrappers/yaml_wrapper.rb +0 -15
- data/lib/active_record/wrappings.rb +0 -59
- data/rakefile +0 -122
- data/test/abstract_unit.rb +0 -16
- data/test/aggregations_test.rb +0 -34
- data/test/all.sh +0 -8
- data/test/associations_test.rb +0 -477
- data/test/base_test.rb +0 -513
- data/test/class_inheritable_attributes_test.rb +0 -33
- data/test/connections/native_mysql/connection.rb +0 -24
- data/test/connections/native_postgresql/connection.rb +0 -24
- data/test/connections/native_sqlite/connection.rb +0 -24
- data/test/deprecated_associations_test.rb +0 -336
- data/test/finder_test.rb +0 -67
- data/test/fixtures/accounts/signals37 +0 -3
- data/test/fixtures/accounts/unknown +0 -2
- data/test/fixtures/auto_id.rb +0 -4
- data/test/fixtures/column_name.rb +0 -3
- data/test/fixtures/companies/first_client +0 -6
- data/test/fixtures/companies/first_firm +0 -4
- data/test/fixtures/companies/second_client +0 -6
- data/test/fixtures/company.rb +0 -37
- data/test/fixtures/company_in_module.rb +0 -33
- data/test/fixtures/course.rb +0 -3
- data/test/fixtures/courses/java +0 -2
- data/test/fixtures/courses/ruby +0 -2
- data/test/fixtures/customer.rb +0 -30
- data/test/fixtures/customers/david +0 -6
- data/test/fixtures/db_definitions/mysql.sql +0 -96
- data/test/fixtures/db_definitions/mysql2.sql +0 -4
- data/test/fixtures/db_definitions/postgresql.sql +0 -113
- data/test/fixtures/db_definitions/postgresql2.sql +0 -4
- data/test/fixtures/db_definitions/sqlite.sql +0 -85
- data/test/fixtures/db_definitions/sqlite2.sql +0 -4
- data/test/fixtures/default.rb +0 -2
- data/test/fixtures/developer.rb +0 -8
- data/test/fixtures/developers/david +0 -2
- data/test/fixtures/developers/jamis +0 -2
- data/test/fixtures/developers_projects/david_action_controller +0 -2
- data/test/fixtures/developers_projects/david_active_record +0 -2
- data/test/fixtures/developers_projects/jamis_active_record +0 -2
- data/test/fixtures/entrant.rb +0 -3
- data/test/fixtures/entrants/first +0 -3
- data/test/fixtures/entrants/second +0 -3
- data/test/fixtures/entrants/third +0 -3
- data/test/fixtures/fixture_database.sqlite +0 -0
- data/test/fixtures/fixture_database_2.sqlite +0 -0
- data/test/fixtures/movie.rb +0 -5
- data/test/fixtures/movies/first +0 -2
- data/test/fixtures/movies/second +0 -2
- data/test/fixtures/project.rb +0 -3
- data/test/fixtures/projects/action_controller +0 -2
- data/test/fixtures/projects/active_record +0 -2
- data/test/fixtures/reply.rb +0 -21
- data/test/fixtures/subscriber.rb +0 -5
- data/test/fixtures/subscribers/first +0 -2
- data/test/fixtures/subscribers/second +0 -2
- data/test/fixtures/topic.rb +0 -20
- data/test/fixtures/topics/first +0 -9
- data/test/fixtures/topics/second +0 -8
- data/test/fixtures_test.rb +0 -20
- data/test/inflector_test.rb +0 -104
- data/test/inheritance_test.rb +0 -125
- data/test/lifecycle_test.rb +0 -110
- data/test/modules_test.rb +0 -21
- data/test/multiple_db_test.rb +0 -46
- data/test/pk_test.rb +0 -57
- data/test/reflection_test.rb +0 -78
- data/test/thread_safety_test.rb +0 -33
- data/test/transactions_test.rb +0 -83
- data/test/unconnected_test.rb +0 -24
- data/test/validations_test.rb +0 -126
data/lib/active_record.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
#--
|
2
|
-
# Copyright (c) 2004 David Heinemeier Hansson
|
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
|
@@ -21,28 +21,153 @@
|
|
21
21
|
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
22
22
|
#++
|
23
23
|
|
24
|
-
|
25
|
-
|
26
|
-
require '
|
27
|
-
|
28
|
-
require 'active_record/
|
29
|
-
|
30
|
-
require 'active_record/
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
24
|
+
require 'active_support'
|
25
|
+
require 'active_support/rails'
|
26
|
+
require 'active_model'
|
27
|
+
require 'arel'
|
28
|
+
require 'active_record/deprecated_finders'
|
29
|
+
|
30
|
+
require 'active_record/version'
|
31
|
+
|
32
|
+
module ActiveRecord
|
33
|
+
extend ActiveSupport::Autoload
|
34
|
+
|
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
|
66
|
+
|
67
|
+
eager_autoload do
|
68
|
+
autoload :ActiveRecordError, 'active_record/errors'
|
69
|
+
autoload :ConnectionNotEstablished, 'active_record/errors'
|
70
|
+
autoload :ConnectionAdapters, 'active_record/connection_adapters/abstract_adapter'
|
71
|
+
|
72
|
+
autoload :Aggregations
|
73
|
+
autoload :Associations
|
74
|
+
autoload :AttributeAssignment
|
75
|
+
autoload :AttributeMethods
|
76
|
+
autoload :AutosaveAssociation
|
77
|
+
|
78
|
+
autoload :Relation
|
79
|
+
autoload :NullRelation
|
80
|
+
|
81
|
+
autoload_under 'relation' do
|
82
|
+
autoload :QueryMethods
|
83
|
+
autoload :FinderMethods
|
84
|
+
autoload :Calculations
|
85
|
+
autoload :PredicateBuilder
|
86
|
+
autoload :SpawnMethods
|
87
|
+
autoload :Batches
|
88
|
+
autoload :Delegation
|
89
|
+
end
|
90
|
+
|
91
|
+
autoload :Result
|
92
|
+
end
|
93
|
+
|
94
|
+
module Coders
|
95
|
+
autoload :YAMLColumn, 'active_record/coders/yaml_column'
|
96
|
+
end
|
97
|
+
|
98
|
+
module AttributeMethods
|
99
|
+
extend ActiveSupport::Autoload
|
100
|
+
|
101
|
+
eager_autoload do
|
102
|
+
autoload :BeforeTypeCast
|
103
|
+
autoload :Dirty
|
104
|
+
autoload :PrimaryKey
|
105
|
+
autoload :Query
|
106
|
+
autoload :Read
|
107
|
+
autoload :TimeZoneConversion
|
108
|
+
autoload :Write
|
109
|
+
autoload :Serialization
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
module Locking
|
114
|
+
extend ActiveSupport::Autoload
|
115
|
+
|
116
|
+
eager_autoload do
|
117
|
+
autoload :Optimistic
|
118
|
+
autoload :Pessimistic
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
module ConnectionAdapters
|
123
|
+
extend ActiveSupport::Autoload
|
124
|
+
|
125
|
+
eager_autoload do
|
126
|
+
autoload :AbstractAdapter
|
127
|
+
autoload :ConnectionManagement, "active_record/connection_adapters/abstract/connection_pool"
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
module Scoping
|
132
|
+
extend ActiveSupport::Autoload
|
133
|
+
|
134
|
+
eager_autoload do
|
135
|
+
autoload :Named
|
136
|
+
autoload :Default
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
module Tasks
|
141
|
+
extend ActiveSupport::Autoload
|
142
|
+
|
143
|
+
autoload :DatabaseTasks
|
144
|
+
autoload :SQLiteDatabaseTasks, 'active_record/tasks/sqlite_database_tasks'
|
145
|
+
autoload :MySQLDatabaseTasks, 'active_record/tasks/mysql_database_tasks'
|
146
|
+
autoload :PostgreSQLDatabaseTasks,
|
147
|
+
'active_record/tasks/postgresql_database_tasks'
|
148
|
+
|
149
|
+
autoload :FirebirdDatabaseTasks, 'active_record/tasks/firebird_database_tasks'
|
150
|
+
autoload :SqlserverDatabaseTasks, 'active_record/tasks/sqlserver_database_tasks'
|
151
|
+
autoload :OracleDatabaseTasks, 'active_record/tasks/oracle_database_tasks'
|
152
|
+
end
|
153
|
+
|
154
|
+
autoload :TestCase
|
155
|
+
autoload :TestFixtures, 'active_record/fixtures'
|
156
|
+
|
157
|
+
def self.eager_load!
|
158
|
+
super
|
159
|
+
ActiveRecord::Locking.eager_load!
|
160
|
+
ActiveRecord::Scoping.eager_load!
|
161
|
+
ActiveRecord::Associations.eager_load!
|
162
|
+
ActiveRecord::AttributeMethods.eager_load!
|
163
|
+
ActiveRecord::ConnectionAdapters.eager_load!
|
164
|
+
end
|
44
165
|
end
|
45
166
|
|
46
|
-
|
47
|
-
|
48
|
-
|
167
|
+
ActiveSupport.on_load(:active_record) do
|
168
|
+
Arel::Table.engine = self
|
169
|
+
end
|
170
|
+
|
171
|
+
ActiveSupport.on_load(:i18n) do
|
172
|
+
I18n.load_path << File.dirname(__FILE__) + '/active_record/locale/en.yml'
|
173
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'rails/generators/active_record'
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
module Generators # :nodoc:
|
5
|
+
class MigrationGenerator < Base # :nodoc:
|
6
|
+
argument :attributes, :type => :array, :default => [], :banner => "field[:type][:index] field[:type][:index]"
|
7
|
+
|
8
|
+
def create_migration_file
|
9
|
+
set_local_assigns!
|
10
|
+
validate_file_name!
|
11
|
+
migration_template @migration_template, "db/migrate/#{file_name}.rb"
|
12
|
+
end
|
13
|
+
|
14
|
+
protected
|
15
|
+
attr_reader :migration_action, :join_tables
|
16
|
+
|
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
|
29
|
+
end
|
30
|
+
when /^create_(.+)/
|
31
|
+
@table_name = $1.pluralize
|
32
|
+
@migration_template = "create_table_migration.rb"
|
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
|
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
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
class <%= migration_class_name %> < ActiveRecord::Migration
|
2
|
+
def change
|
3
|
+
create_table :<%= table_name %> do |t|
|
4
|
+
<% attributes.each do |attribute| -%>
|
5
|
+
<% if attribute.password_digest? -%>
|
6
|
+
t.string :password_digest<%= attribute.inject_options %>
|
7
|
+
<% else -%>
|
8
|
+
t.<%= attribute.type %> :<%= attribute.name %><%= attribute.inject_options %>
|
9
|
+
<% end -%>
|
10
|
+
<% end -%>
|
11
|
+
<% if options[:timestamps] %>
|
12
|
+
t.timestamps
|
13
|
+
<% end -%>
|
14
|
+
end
|
15
|
+
<% attributes_with_index.each do |attribute| -%>
|
16
|
+
add_index :<%= table_name %>, :<%= attribute.index_name %><%= attribute.inject_index_options %>
|
17
|
+
<% end -%>
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
class <%= migration_class_name %> < ActiveRecord::Migration
|
2
|
+
<%- if migration_action == 'add' -%>
|
3
|
+
def change
|
4
|
+
<% attributes.each do |attribute| -%>
|
5
|
+
<%- if attribute.reference? -%>
|
6
|
+
add_reference :<%= table_name %>, :<%= attribute.name %><%= attribute.inject_options %>
|
7
|
+
<%- else -%>
|
8
|
+
add_column :<%= table_name %>, :<%= attribute.name %>, :<%= attribute.type %><%= attribute.inject_options %>
|
9
|
+
<%- if attribute.has_index? -%>
|
10
|
+
add_index :<%= table_name %>, :<%= attribute.index_name %><%= attribute.inject_index_options %>
|
11
|
+
<%- end -%>
|
12
|
+
<%- end -%>
|
13
|
+
<%- end -%>
|
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
|
23
|
+
<%- else -%>
|
24
|
+
def change
|
25
|
+
<% attributes.each do |attribute| -%>
|
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 %>
|
32
|
+
<%- end -%>
|
33
|
+
remove_column :<%= table_name %>, :<%= attribute.name %>, :<%= attribute.type %><%= attribute.inject_options %>
|
34
|
+
<%- end -%>
|
35
|
+
<%- end -%>
|
36
|
+
<%- end -%>
|
37
|
+
end
|
38
|
+
<%- end -%>
|
39
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'rails/generators/active_record'
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
module Generators # :nodoc:
|
5
|
+
class ModelGenerator < Base # :nodoc:
|
6
|
+
argument :attributes, :type => :array, :default => [], :banner => "field[:type][:index] field[:type][:index]"
|
7
|
+
|
8
|
+
check_class_collision
|
9
|
+
|
10
|
+
class_option :migration, :type => :boolean
|
11
|
+
class_option :timestamps, :type => :boolean
|
12
|
+
class_option :parent, :type => :string, :desc => "The parent class for the generated model"
|
13
|
+
class_option :indexes, :type => :boolean, :default => true, :desc => "Add indexes for references and belongs_to columns"
|
14
|
+
|
15
|
+
def create_migration_file
|
16
|
+
return unless options[:migration] && options[:parent].nil?
|
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"
|
19
|
+
end
|
20
|
+
|
21
|
+
def create_model_file
|
22
|
+
template 'model.rb', File.join('app/models', class_path, "#{file_name}.rb")
|
23
|
+
end
|
24
|
+
|
25
|
+
def create_module_file
|
26
|
+
return if regular_class_path.empty?
|
27
|
+
template 'module.rb', File.join('app/models', "#{class_path.join('/')}.rb") if behavior == :invoke
|
28
|
+
end
|
29
|
+
|
30
|
+
def attributes_with_index
|
31
|
+
attributes.select { |a| !a.reference? && a.has_index? }
|
32
|
+
end
|
33
|
+
|
34
|
+
def accessible_attributes
|
35
|
+
attributes.reject(&:reference?)
|
36
|
+
end
|
37
|
+
|
38
|
+
hook_for :test_framework
|
39
|
+
|
40
|
+
protected
|
41
|
+
|
42
|
+
def parent_class_name
|
43
|
+
options[:parent] || "ActiveRecord::Base"
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
<% module_namespacing do -%>
|
2
|
+
class <%= class_name %> < <%= parent_class_name.classify %>
|
3
|
+
<% attributes.select(&:reference?).each do |attribute| -%>
|
4
|
+
belongs_to :<%= attribute.name %><%= ', polymorphic: true' if attribute.polymorphic? %>
|
5
|
+
<% end -%>
|
6
|
+
<% if attributes.any?(&:password_digest?) -%>
|
7
|
+
has_secure_password
|
8
|
+
<% end -%>
|
9
|
+
end
|
10
|
+
<% end -%>
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'rails/generators/named_base'
|
2
|
+
require 'rails/generators/migration'
|
3
|
+
require 'rails/generators/active_model'
|
4
|
+
require 'active_record'
|
5
|
+
|
6
|
+
module ActiveRecord
|
7
|
+
module Generators # :nodoc:
|
8
|
+
class Base < Rails::Generators::NamedBase # :nodoc:
|
9
|
+
include Rails::Generators::Migration
|
10
|
+
|
11
|
+
# Set the current directory as base for the inherited generators.
|
12
|
+
def self.base_root
|
13
|
+
File.dirname(__FILE__)
|
14
|
+
end
|
15
|
+
|
16
|
+
# Implement the required interface for Rails::Generators::Migration.
|
17
|
+
def self.next_migration_number(dirname)
|
18
|
+
next_migration_number = current_migration_number(dirname) + 1
|
19
|
+
ActiveRecord::Migration.next_migration_number(next_migration_number)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|