activerecord 1.0.0 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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
@@ -1,70 +0,0 @@
|
|
1
|
-
module ActiveRecord
|
2
|
-
module Associations
|
3
|
-
class AssociationCollection #:nodoc:
|
4
|
-
alias_method :proxy_respond_to?, :respond_to?
|
5
|
-
instance_methods.each { |m| undef_method m unless m =~ /(^__|^nil\?|^proxy_respond_to\?)/ }
|
6
|
-
|
7
|
-
def initialize(owner, association_name, association_class_name, association_class_primary_key_name, options)
|
8
|
-
@owner = owner
|
9
|
-
@options = options
|
10
|
-
@association_name = association_name
|
11
|
-
@association_class = eval(association_class_name)
|
12
|
-
@association_class_primary_key_name = association_class_primary_key_name
|
13
|
-
end
|
14
|
-
|
15
|
-
def method_missing(symbol, *args, &block)
|
16
|
-
load_collection_to_array
|
17
|
-
@collection_array.send(symbol, *args, &block)
|
18
|
-
end
|
19
|
-
|
20
|
-
def to_ary
|
21
|
-
load_collection_to_array
|
22
|
-
@collection_array.to_ary
|
23
|
-
end
|
24
|
-
|
25
|
-
def respond_to?(symbol)
|
26
|
-
proxy_respond_to?(symbol) || [].respond_to?(symbol)
|
27
|
-
end
|
28
|
-
|
29
|
-
def reload
|
30
|
-
@collection_array = nil
|
31
|
-
end
|
32
|
-
|
33
|
-
def concat(*records)
|
34
|
-
records.flatten!
|
35
|
-
records.each {|record| self << record; }
|
36
|
-
end
|
37
|
-
|
38
|
-
def destroy_all
|
39
|
-
load_collection_to_array
|
40
|
-
@collection_array.each { |object| object.destroy }
|
41
|
-
@collection_array = []
|
42
|
-
end
|
43
|
-
|
44
|
-
def size
|
45
|
-
(@collection_array.nil?) ? count_records : @collection_array.size
|
46
|
-
end
|
47
|
-
|
48
|
-
def empty?
|
49
|
-
size == 0
|
50
|
-
end
|
51
|
-
|
52
|
-
alias_method :length, :size
|
53
|
-
|
54
|
-
private
|
55
|
-
def load_collection_to_array
|
56
|
-
return unless @collection_array.nil?
|
57
|
-
begin
|
58
|
-
@collection_array = find_all_records
|
59
|
-
rescue ActiveRecord::StatementInvalid, ActiveRecord::RecordNotFound
|
60
|
-
@collection_array = []
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
|
-
def duplicated_records_array(records)
|
65
|
-
records = [records] unless records.is_a?(Array) || records.is_a?(ActiveRecord::Associations::AssociationCollection)
|
66
|
-
records.dup
|
67
|
-
end
|
68
|
-
end
|
69
|
-
end
|
70
|
-
end
|
@@ -1,107 +0,0 @@
|
|
1
|
-
# sqlite_adapter.rb
|
2
|
-
# author: Luke Holden <lholden@cablelan.net>
|
3
|
-
|
4
|
-
require 'active_record/connection_adapters/abstract_adapter'
|
5
|
-
|
6
|
-
begin
|
7
|
-
require 'sqlite' unless self.class.const_defined?(:SQLite)
|
8
|
-
|
9
|
-
module ActiveRecord
|
10
|
-
class Base
|
11
|
-
# Establishes a connection to the database that's used by all Active Record objects
|
12
|
-
def self.sqlite_connection(config) # :nodoc:
|
13
|
-
symbolize_strings_in_hash(config)
|
14
|
-
unless config.has_key?(:dbfile)
|
15
|
-
raise ArgumentError, "No database file specified. Missing argument: dbfile"
|
16
|
-
end
|
17
|
-
|
18
|
-
db = SQLite::Database.new(config[:dbfile], 0)
|
19
|
-
|
20
|
-
db.show_datatypes = "ON" if !defined? SQLite::Version
|
21
|
-
db.results_as_hash = true if defined? SQLite::Version
|
22
|
-
db.type_translation = false
|
23
|
-
|
24
|
-
ConnectionAdapters::SQLiteAdapter.new(db, logger)
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
module ConnectionAdapters
|
29
|
-
class SQLiteAdapter < AbstractAdapter # :nodoc:
|
30
|
-
def select_all(sql, name = nil)
|
31
|
-
select(sql, name)
|
32
|
-
end
|
33
|
-
|
34
|
-
def select_one(sql, name = nil)
|
35
|
-
result = select(sql, name)
|
36
|
-
result.nil? ? nil : result.first
|
37
|
-
end
|
38
|
-
|
39
|
-
def columns(table_name, name = nil)
|
40
|
-
table_structure(table_name).inject([]) do |columns, field|
|
41
|
-
columns << Column.new(field['name'], field['dflt_value'], field['type'])
|
42
|
-
columns
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
def insert(sql, name = nil, pk = nil, id_value = nil)
|
47
|
-
execute(sql, name = nil)
|
48
|
-
id_value || @connection.send( defined?( SQLite::Version ) ? :last_insert_row_id : :last_insert_rowid )
|
49
|
-
end
|
50
|
-
|
51
|
-
def execute(sql, name = nil)
|
52
|
-
log(sql, name, @connection) do |connection|
|
53
|
-
if defined?( SQLite::Version )
|
54
|
-
case sql
|
55
|
-
when "BEGIN" then connection.transaction
|
56
|
-
when "COMMIT" then connection.commit
|
57
|
-
when "ROLLBACK" then connection.rollback
|
58
|
-
else connection.execute(sql)
|
59
|
-
end
|
60
|
-
else
|
61
|
-
connection.execute( sql )
|
62
|
-
end
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
alias_method :update, :execute
|
67
|
-
alias_method :delete, :execute
|
68
|
-
|
69
|
-
def begin_db_transaction() execute "BEGIN" end
|
70
|
-
def commit_db_transaction() execute "COMMIT" end
|
71
|
-
def rollback_db_transaction() execute "ROLLBACK" end
|
72
|
-
|
73
|
-
def quote_column_name(name)
|
74
|
-
return "'#{name}'"
|
75
|
-
end
|
76
|
-
|
77
|
-
private
|
78
|
-
def select(sql, name = nil)
|
79
|
-
results = nil
|
80
|
-
log(sql, name, @connection) { |connection| results = connection.execute(sql) }
|
81
|
-
|
82
|
-
rows = []
|
83
|
-
|
84
|
-
results.each do |row|
|
85
|
-
hash_only_row = {}
|
86
|
-
row.each_key do |key|
|
87
|
-
hash_only_row[key.gsub(/\w\./, "")] = row[key] unless key.class == Fixnum
|
88
|
-
end
|
89
|
-
rows << hash_only_row
|
90
|
-
end
|
91
|
-
|
92
|
-
return rows
|
93
|
-
end
|
94
|
-
|
95
|
-
def table_structure(table_name)
|
96
|
-
sql = "PRAGMA table_info(#{table_name});"
|
97
|
-
results = nil
|
98
|
-
log(sql, nil, @connection) { |connection| results = connection.execute(sql) }
|
99
|
-
return results
|
100
|
-
end
|
101
|
-
end
|
102
|
-
end
|
103
|
-
end
|
104
|
-
rescue LoadError
|
105
|
-
retry if require('rubygems') rescue LoadError
|
106
|
-
# SQLite driver is not availible
|
107
|
-
end
|
@@ -1,70 +0,0 @@
|
|
1
|
-
module ActiveRecord
|
2
|
-
module Associations # :nodoc:
|
3
|
-
module ClassMethods
|
4
|
-
def deprecated_collection_count_method(collection_name)# :nodoc:
|
5
|
-
module_eval <<-"end_eval", __FILE__, __LINE__
|
6
|
-
def #{collection_name}_count(force_reload = false)
|
7
|
-
#{collection_name}.reload if force_reload
|
8
|
-
#{collection_name}.size
|
9
|
-
end
|
10
|
-
end_eval
|
11
|
-
end
|
12
|
-
|
13
|
-
def deprecated_add_association_relation(association_name)# :nodoc:
|
14
|
-
module_eval <<-"end_eval", __FILE__, __LINE__
|
15
|
-
def add_#{association_name}(*items)
|
16
|
-
#{association_name}.concat(items)
|
17
|
-
end
|
18
|
-
end_eval
|
19
|
-
end
|
20
|
-
|
21
|
-
def deprecated_remove_association_relation(association_name)# :nodoc:
|
22
|
-
module_eval <<-"end_eval", __FILE__, __LINE__
|
23
|
-
def remove_#{association_name}(items)
|
24
|
-
#{association_name}.delete(items)
|
25
|
-
end
|
26
|
-
end_eval
|
27
|
-
end
|
28
|
-
|
29
|
-
def deprecated_has_collection_method(collection_name)# :nodoc:
|
30
|
-
module_eval <<-"end_eval", __FILE__, __LINE__
|
31
|
-
def has_#{collection_name}?(force_reload = false)
|
32
|
-
!#{collection_name}(force_reload).empty?
|
33
|
-
end
|
34
|
-
end_eval
|
35
|
-
end
|
36
|
-
|
37
|
-
def deprecated_find_in_collection_method(collection_name)# :nodoc:
|
38
|
-
module_eval <<-"end_eval", __FILE__, __LINE__
|
39
|
-
def find_in_#{collection_name}(association_id)
|
40
|
-
#{collection_name}.find(association_id)
|
41
|
-
end
|
42
|
-
end_eval
|
43
|
-
end
|
44
|
-
|
45
|
-
def deprecated_find_all_in_collection_method(collection_name)# :nodoc:
|
46
|
-
module_eval <<-"end_eval", __FILE__, __LINE__
|
47
|
-
def find_all_in_#{collection_name}(runtime_conditions = nil, orderings = nil, limit = nil, joins = nil)
|
48
|
-
#{collection_name}.find_all(runtime_conditions, orderings, limit, joins)
|
49
|
-
end
|
50
|
-
end_eval
|
51
|
-
end
|
52
|
-
|
53
|
-
def deprecated_create_method(collection_name)# :nodoc:
|
54
|
-
module_eval <<-"end_eval", __FILE__, __LINE__
|
55
|
-
def create_in_#{collection_name}(attributes = {})
|
56
|
-
#{collection_name}.create(attributes)
|
57
|
-
end
|
58
|
-
end_eval
|
59
|
-
end
|
60
|
-
|
61
|
-
def deprecated_build_method(collection_name)# :nodoc:
|
62
|
-
module_eval <<-"end_eval", __FILE__, __LINE__
|
63
|
-
def build_to_#{collection_name}(attributes = {})
|
64
|
-
#{collection_name}.build(attributes)
|
65
|
-
end
|
66
|
-
end_eval
|
67
|
-
end
|
68
|
-
end
|
69
|
-
end
|
70
|
-
end
|
@@ -1,71 +0,0 @@
|
|
1
|
-
require 'singleton'
|
2
|
-
|
3
|
-
module ActiveRecord
|
4
|
-
# Observers can be programmed to react to lifecycle callbacks in another class to implement
|
5
|
-
# trigger-like behavior outside the original class. This is a great way to reduce the clutter that
|
6
|
-
# normally comes when the model class is burdened with excess responsibility that doesn't pertain to
|
7
|
-
# the core and nature of the class. Example:
|
8
|
-
#
|
9
|
-
# class CommentObserver < ActiveRecord::Observer
|
10
|
-
# def after_save(comment)
|
11
|
-
# NotificationServer.send_email("admin@do.com", "New comment was posted", comment)
|
12
|
-
# end
|
13
|
-
# end
|
14
|
-
#
|
15
|
-
# This Observer is triggered when a Comment#save is finished and sends a notification about it to the administrator.
|
16
|
-
#
|
17
|
-
# == Observing a class that can't be infered
|
18
|
-
#
|
19
|
-
# Observers will by default be mapped to the class with which they share a name. So CommentObserver will
|
20
|
-
# be tied to observing Comment, ProductManagerObserver to ProductManager, and so on. If you want to name your observer
|
21
|
-
# something else than the class you're interested in observing, you can implement the observed_class class method. Like this:
|
22
|
-
#
|
23
|
-
# class AuditObserver < ActiveRecord::Observer
|
24
|
-
# def self.observed_class() Account end
|
25
|
-
# def after_update(account)
|
26
|
-
# AuditTrail.new(account, "UPDATED")
|
27
|
-
# end
|
28
|
-
# end
|
29
|
-
#
|
30
|
-
# == Observing multiple classes at once
|
31
|
-
#
|
32
|
-
# If the audit observer needs to watch more than one kind of object, this can be specified in an array, like this:
|
33
|
-
#
|
34
|
-
# class AuditObserver < ActiveRecord::Observer
|
35
|
-
# def self.observed_class() [ Account, Balance ] end
|
36
|
-
# def after_update(record)
|
37
|
-
# AuditTrail.new(record, "UPDATED")
|
38
|
-
# end
|
39
|
-
# end
|
40
|
-
#
|
41
|
-
# The AuditObserver will now act on both updates to Account and Balance by treating them both as records.
|
42
|
-
#
|
43
|
-
# The observer can implement callback methods for each of the methods described in the Callbacks module.
|
44
|
-
class Observer
|
45
|
-
include Singleton
|
46
|
-
|
47
|
-
def initialize
|
48
|
-
[ observed_class ].flatten.each do |klass|
|
49
|
-
klass.add_observer(self)
|
50
|
-
klass.send(:define_method, :after_find) unless klass.respond_to?(:after_find)
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
def update(callback_method, object)
|
55
|
-
send(callback_method, object) if respond_to?(callback_method)
|
56
|
-
end
|
57
|
-
|
58
|
-
private
|
59
|
-
def observed_class
|
60
|
-
if self.class.respond_to? "observed_class"
|
61
|
-
self.class.observed_class
|
62
|
-
else
|
63
|
-
Object.const_get(infer_observed_class_name)
|
64
|
-
end
|
65
|
-
end
|
66
|
-
|
67
|
-
def infer_observed_class_name
|
68
|
-
self.class.name.scan(/(.*)Observer/)[0][0]
|
69
|
-
end
|
70
|
-
end
|
71
|
-
end
|
@@ -1,43 +0,0 @@
|
|
1
|
-
# attr_* style accessors for class-variables that can accessed both on an instance and class level.
|
2
|
-
class Class #:nodoc:
|
3
|
-
def cattr_reader(*syms)
|
4
|
-
syms.each do |sym|
|
5
|
-
class_eval <<-EOS
|
6
|
-
if ! defined? @@#{sym.id2name}
|
7
|
-
@@#{sym.id2name} = nil
|
8
|
-
end
|
9
|
-
|
10
|
-
def self.#{sym.id2name}
|
11
|
-
@@#{sym}
|
12
|
-
end
|
13
|
-
|
14
|
-
def #{sym.id2name}
|
15
|
-
self.class.#{sym.id2name}
|
16
|
-
end
|
17
|
-
EOS
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
def cattr_writer(*syms)
|
22
|
-
syms.each do |sym|
|
23
|
-
class_eval <<-EOS
|
24
|
-
if ! defined? @@#{sym.id2name}
|
25
|
-
@@#{sym.id2name} = nil
|
26
|
-
end
|
27
|
-
|
28
|
-
def self.#{sym.id2name}=(obj)
|
29
|
-
@@#{sym.id2name} = obj
|
30
|
-
end
|
31
|
-
|
32
|
-
def #{sym.id2name}=(obj)
|
33
|
-
self.class.#{sym.id2name}=(obj)
|
34
|
-
end
|
35
|
-
EOS
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
def cattr_accessor(*syms)
|
40
|
-
cattr_reader(*syms)
|
41
|
-
cattr_writer(*syms)
|
42
|
-
end
|
43
|
-
end
|
@@ -1,37 +0,0 @@
|
|
1
|
-
# Allows attributes to be shared within an inheritance hierarchy, but where each descentent gets a copy of
|
2
|
-
# their parents' attributes, instead of just a pointer to the same. This means that the child can add elements
|
3
|
-
# to, for example, an array without those additions being shared with either their parent, siblings, or
|
4
|
-
# children, which is unlike the regular class-level attributes that are shared across the entire hierarchy.
|
5
|
-
module ClassInheritableAttributes # :nodoc:
|
6
|
-
def self.append_features(base)
|
7
|
-
super
|
8
|
-
base.extend(ClassMethods)
|
9
|
-
end
|
10
|
-
|
11
|
-
module ClassMethods # :nodoc:
|
12
|
-
@@classes ||= {}
|
13
|
-
|
14
|
-
def inheritable_attributes
|
15
|
-
@@classes[self] ||= {}
|
16
|
-
end
|
17
|
-
|
18
|
-
def write_inheritable_attribute(key, value)
|
19
|
-
inheritable_attributes[key] = value
|
20
|
-
end
|
21
|
-
|
22
|
-
def write_inheritable_array(key, elements)
|
23
|
-
write_inheritable_attribute(key, []) if read_inheritable_attribute(key).nil?
|
24
|
-
write_inheritable_attribute(key, read_inheritable_attribute(key) + elements)
|
25
|
-
end
|
26
|
-
|
27
|
-
def read_inheritable_attribute(key)
|
28
|
-
inheritable_attributes[key]
|
29
|
-
end
|
30
|
-
|
31
|
-
private
|
32
|
-
def inherited(child)
|
33
|
-
@@classes[child] = inheritable_attributes.dup
|
34
|
-
end
|
35
|
-
|
36
|
-
end
|
37
|
-
end
|
@@ -1,70 +0,0 @@
|
|
1
|
-
# The Inflector transforms words from singular to plural, class names to table names, modulized class names to ones without,
|
2
|
-
# and class names to foreign keys.
|
3
|
-
module Inflector
|
4
|
-
extend self
|
5
|
-
|
6
|
-
def pluralize(word)
|
7
|
-
result = word.dup
|
8
|
-
plural_rules.each do |(rule, replacement)|
|
9
|
-
break if result.gsub!(rule, replacement)
|
10
|
-
end
|
11
|
-
return result
|
12
|
-
end
|
13
|
-
|
14
|
-
def singularize(word)
|
15
|
-
result = word.dup
|
16
|
-
singular_rules.each do |(rule, replacement)|
|
17
|
-
break if result.gsub!(rule, replacement)
|
18
|
-
end
|
19
|
-
return result
|
20
|
-
end
|
21
|
-
|
22
|
-
def camelize(lower_case_and_underscored_word)
|
23
|
-
lower_case_and_underscored_word.gsub(/(^|_)(.)/){$2.upcase}
|
24
|
-
end
|
25
|
-
|
26
|
-
def underscore(camel_cased_word)
|
27
|
-
camel_cased_word.gsub(/([A-Z]+)([A-Z])/,'\1_\2').gsub(/([a-z])([A-Z])/,'\1_\2').downcase
|
28
|
-
end
|
29
|
-
|
30
|
-
def demodulize(class_name_in_module)
|
31
|
-
class_name_in_module.gsub(/^.*::/, '')
|
32
|
-
end
|
33
|
-
|
34
|
-
def foreign_key(class_name, separate_class_name_and_id_with_underscore = true)
|
35
|
-
Inflector.underscore(Inflector.demodulize(class_name)) +
|
36
|
-
(separate_class_name_and_id_with_underscore ? "_id" : "id")
|
37
|
-
end
|
38
|
-
|
39
|
-
private
|
40
|
-
def plural_rules #:doc:
|
41
|
-
[
|
42
|
-
[/(x|ch|ss)$/, '\1es'], # search, switch, fix, box, process, address
|
43
|
-
[/([^aeiouy]|qu)y$/, '\1ies'], # query, ability, agency
|
44
|
-
[/(?:([^f])fe|([lr])f)$/, '\1\2ves'], # half, safe, wife
|
45
|
-
[/sis$/, 'ses'], # basis, diagnosis
|
46
|
-
[/([ti])um$/, '\1a'], # datum, medium
|
47
|
-
[/person$/, 'people'], # person, salesperson
|
48
|
-
[/man$/, 'men'], # man, woman, spokesman
|
49
|
-
[/child$/, 'children'], # child
|
50
|
-
[/s$/, 's'], # no change (compatibility)
|
51
|
-
[/$/, 's']
|
52
|
-
]
|
53
|
-
end
|
54
|
-
|
55
|
-
def singular_rules #:doc:
|
56
|
-
[
|
57
|
-
[/(x|ch|ss)es$/, '\1'],
|
58
|
-
[/([^aeiouy]|qu)ies$/, '\1y'],
|
59
|
-
[/([lr])ves$/, '\1f'],
|
60
|
-
[/([^f])ves$/, '\1fe'],
|
61
|
-
[/(analy|ba|diagno|parenthe|progno|synop|the)ses$/, '\1sis'],
|
62
|
-
[/([ti])a$/, '\1um'],
|
63
|
-
[/people$/, 'person'],
|
64
|
-
[/men$/, 'man'],
|
65
|
-
[/status$/, 'status'],
|
66
|
-
[/children$/, 'child'],
|
67
|
-
[/s$/, '']
|
68
|
-
]
|
69
|
-
end
|
70
|
-
end
|