activerecord 1.0.0 → 2.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.
- data/CHANGELOG +4928 -3
- data/README +45 -46
- data/RUNNING_UNIT_TESTS +8 -11
- data/Rakefile +247 -0
- data/install.rb +8 -38
- data/lib/active_record/aggregations.rb +64 -49
- data/lib/active_record/associations/association_collection.rb +217 -47
- data/lib/active_record/associations/association_proxy.rb +159 -0
- data/lib/active_record/associations/belongs_to_association.rb +56 -0
- data/lib/active_record/associations/belongs_to_polymorphic_association.rb +50 -0
- data/lib/active_record/associations/has_and_belongs_to_many_association.rb +155 -37
- data/lib/active_record/associations/has_many_association.rb +145 -75
- data/lib/active_record/associations/has_many_through_association.rb +283 -0
- data/lib/active_record/associations/has_one_association.rb +96 -0
- data/lib/active_record/associations.rb +1537 -304
- data/lib/active_record/attribute_methods.rb +328 -0
- data/lib/active_record/base.rb +2001 -588
- data/lib/active_record/calculations.rb +269 -0
- data/lib/active_record/callbacks.rb +169 -165
- data/lib/active_record/connection_adapters/abstract/connection_specification.rb +308 -0
- data/lib/active_record/connection_adapters/abstract/database_statements.rb +171 -0
- data/lib/active_record/connection_adapters/abstract/query_cache.rb +87 -0
- data/lib/active_record/connection_adapters/abstract/quoting.rb +69 -0
- data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +472 -0
- data/lib/active_record/connection_adapters/abstract/schema_statements.rb +306 -0
- data/lib/active_record/connection_adapters/abstract_adapter.rb +125 -279
- data/lib/active_record/connection_adapters/mysql_adapter.rb +442 -77
- data/lib/active_record/connection_adapters/postgresql_adapter.rb +805 -135
- data/lib/active_record/connection_adapters/sqlite3_adapter.rb +34 -0
- data/lib/active_record/connection_adapters/sqlite_adapter.rb +353 -69
- data/lib/active_record/fixtures.rb +946 -100
- data/lib/active_record/locking/optimistic.rb +144 -0
- data/lib/active_record/locking/pessimistic.rb +77 -0
- data/lib/active_record/migration.rb +417 -0
- data/lib/active_record/observer.rb +142 -32
- data/lib/active_record/query_cache.rb +23 -0
- data/lib/active_record/reflection.rb +163 -70
- data/lib/active_record/schema.rb +58 -0
- data/lib/active_record/schema_dumper.rb +171 -0
- data/lib/active_record/serialization.rb +98 -0
- data/lib/active_record/serializers/json_serializer.rb +71 -0
- data/lib/active_record/serializers/xml_serializer.rb +315 -0
- data/lib/active_record/timestamp.rb +41 -0
- data/lib/active_record/transactions.rb +87 -57
- data/lib/active_record/validations.rb +909 -122
- data/lib/active_record/vendor/db2.rb +362 -0
- data/lib/active_record/vendor/mysql.rb +126 -29
- data/lib/active_record/version.rb +9 -0
- data/lib/active_record.rb +35 -7
- data/lib/activerecord.rb +1 -0
- data/test/aaa_create_tables_test.rb +72 -0
- data/test/abstract_unit.rb +73 -5
- data/test/active_schema_test_mysql.rb +43 -0
- data/test/adapter_test.rb +105 -0
- data/test/adapter_test_sqlserver.rb +95 -0
- data/test/aggregations_test.rb +110 -16
- data/test/all.sh +2 -2
- data/test/ar_schema_test.rb +33 -0
- data/test/association_inheritance_reload.rb +14 -0
- data/test/associations/ar_joins_test.rb +0 -0
- data/test/associations/callbacks_test.rb +147 -0
- data/test/associations/cascaded_eager_loading_test.rb +110 -0
- data/test/associations/eager_singularization_test.rb +145 -0
- data/test/associations/eager_test.rb +442 -0
- data/test/associations/extension_test.rb +47 -0
- data/test/associations/inner_join_association_test.rb +88 -0
- data/test/associations/join_model_test.rb +553 -0
- data/test/associations_test.rb +1930 -267
- data/test/attribute_methods_test.rb +146 -0
- data/test/base_test.rb +1316 -84
- data/test/binary_test.rb +32 -0
- data/test/calculations_test.rb +251 -0
- data/test/callbacks_test.rb +400 -0
- data/test/class_inheritable_attributes_test.rb +3 -4
- data/test/column_alias_test.rb +17 -0
- data/test/connection_test_firebird.rb +8 -0
- data/test/connection_test_mysql.rb +30 -0
- data/test/connections/native_db2/connection.rb +25 -0
- data/test/connections/native_firebird/connection.rb +26 -0
- data/test/connections/native_frontbase/connection.rb +27 -0
- data/test/connections/native_mysql/connection.rb +21 -18
- data/test/connections/native_openbase/connection.rb +21 -0
- data/test/connections/native_oracle/connection.rb +27 -0
- data/test/connections/native_postgresql/connection.rb +17 -18
- data/test/connections/native_sqlite/connection.rb +17 -16
- data/test/connections/native_sqlite3/connection.rb +25 -0
- data/test/connections/native_sqlite3/in_memory_connection.rb +18 -0
- data/test/connections/native_sybase/connection.rb +23 -0
- data/test/copy_table_test_sqlite.rb +69 -0
- data/test/datatype_test_postgresql.rb +203 -0
- data/test/date_time_test.rb +37 -0
- data/test/default_test_firebird.rb +16 -0
- data/test/defaults_test.rb +67 -0
- data/test/deprecated_finder_test.rb +30 -0
- data/test/finder_test.rb +607 -32
- data/test/fixtures/accounts.yml +28 -0
- data/test/fixtures/all/developers.yml +0 -0
- data/test/fixtures/all/people.csv +0 -0
- data/test/fixtures/all/tasks.yml +0 -0
- data/test/fixtures/author.rb +107 -0
- data/test/fixtures/author_favorites.yml +4 -0
- data/test/fixtures/authors.yml +7 -0
- data/test/fixtures/bad_fixtures/attr_with_numeric_first_char +1 -0
- data/test/fixtures/bad_fixtures/attr_with_spaces +1 -0
- data/test/fixtures/bad_fixtures/blank_line +3 -0
- data/test/fixtures/bad_fixtures/duplicate_attributes +3 -0
- data/test/fixtures/bad_fixtures/missing_value +1 -0
- data/test/fixtures/binaries.yml +132 -0
- data/test/fixtures/binary.rb +2 -0
- data/test/fixtures/book.rb +4 -0
- data/test/fixtures/books.yml +7 -0
- data/test/fixtures/categories/special_categories.yml +9 -0
- data/test/fixtures/categories/subsubdir/arbitrary_filename.yml +4 -0
- data/test/fixtures/categories.yml +14 -0
- data/test/fixtures/categories_ordered.yml +7 -0
- data/test/fixtures/categories_posts.yml +23 -0
- data/test/fixtures/categorization.rb +5 -0
- data/test/fixtures/categorizations.yml +17 -0
- data/test/fixtures/category.rb +26 -0
- data/test/fixtures/citation.rb +6 -0
- data/test/fixtures/comment.rb +23 -0
- data/test/fixtures/comments.yml +59 -0
- data/test/fixtures/companies.yml +55 -0
- data/test/fixtures/company.rb +81 -4
- data/test/fixtures/company_in_module.rb +32 -6
- data/test/fixtures/computer.rb +4 -0
- data/test/fixtures/computers.yml +4 -0
- data/test/fixtures/contact.rb +16 -0
- data/test/fixtures/courses.yml +7 -0
- data/test/fixtures/customer.rb +28 -3
- data/test/fixtures/customers.yml +17 -0
- data/test/fixtures/db_definitions/db2.drop.sql +33 -0
- data/test/fixtures/db_definitions/db2.sql +235 -0
- data/test/fixtures/db_definitions/db22.drop.sql +2 -0
- data/test/fixtures/db_definitions/db22.sql +5 -0
- data/test/fixtures/db_definitions/firebird.drop.sql +65 -0
- data/test/fixtures/db_definitions/firebird.sql +310 -0
- data/test/fixtures/db_definitions/firebird2.drop.sql +2 -0
- data/test/fixtures/db_definitions/firebird2.sql +6 -0
- data/test/fixtures/db_definitions/frontbase.drop.sql +33 -0
- data/test/fixtures/db_definitions/frontbase.sql +273 -0
- data/test/fixtures/db_definitions/frontbase2.drop.sql +1 -0
- data/test/fixtures/db_definitions/frontbase2.sql +4 -0
- data/test/fixtures/db_definitions/openbase.drop.sql +2 -0
- data/test/fixtures/db_definitions/openbase.sql +318 -0
- data/test/fixtures/db_definitions/openbase2.drop.sql +2 -0
- data/test/fixtures/db_definitions/openbase2.sql +7 -0
- data/test/fixtures/db_definitions/oracle.drop.sql +67 -0
- data/test/fixtures/db_definitions/oracle.sql +330 -0
- data/test/fixtures/db_definitions/oracle2.drop.sql +2 -0
- data/test/fixtures/db_definitions/oracle2.sql +6 -0
- data/test/fixtures/db_definitions/postgresql.drop.sql +44 -0
- data/test/fixtures/db_definitions/postgresql.sql +217 -38
- data/test/fixtures/db_definitions/postgresql2.drop.sql +2 -0
- data/test/fixtures/db_definitions/postgresql2.sql +2 -2
- data/test/fixtures/db_definitions/schema.rb +354 -0
- data/test/fixtures/db_definitions/schema2.rb +11 -0
- data/test/fixtures/db_definitions/sqlite.drop.sql +33 -0
- data/test/fixtures/db_definitions/sqlite.sql +139 -5
- data/test/fixtures/db_definitions/sqlite2.drop.sql +2 -0
- data/test/fixtures/db_definitions/sqlite2.sql +1 -0
- data/test/fixtures/db_definitions/sybase.drop.sql +35 -0
- data/test/fixtures/db_definitions/sybase.sql +222 -0
- data/test/fixtures/db_definitions/sybase2.drop.sql +4 -0
- data/test/fixtures/db_definitions/sybase2.sql +5 -0
- data/test/fixtures/developer.rb +70 -6
- data/test/fixtures/developers.yml +21 -0
- data/test/fixtures/developers_projects/david_action_controller +2 -1
- data/test/fixtures/developers_projects/david_active_record +2 -1
- data/test/fixtures/developers_projects.yml +17 -0
- data/test/fixtures/edge.rb +5 -0
- data/test/fixtures/edges.yml +6 -0
- data/test/fixtures/entrants.yml +14 -0
- data/test/fixtures/example.log +1 -0
- data/test/fixtures/fk_test_has_fk.yml +3 -0
- data/test/fixtures/fk_test_has_pk.yml +2 -0
- data/test/fixtures/flowers.jpg +0 -0
- data/test/fixtures/funny_jokes.yml +10 -0
- data/test/fixtures/item.rb +7 -0
- data/test/fixtures/items.yml +4 -0
- data/test/fixtures/joke.rb +3 -0
- data/test/fixtures/keyboard.rb +3 -0
- data/test/fixtures/legacy_thing.rb +3 -0
- data/test/fixtures/legacy_things.yml +3 -0
- data/test/fixtures/matey.rb +4 -0
- data/test/fixtures/mateys.yml +4 -0
- data/test/fixtures/migrations/1_people_have_last_names.rb +9 -0
- data/test/fixtures/migrations/2_we_need_reminders.rb +12 -0
- data/test/fixtures/migrations/3_innocent_jointable.rb +12 -0
- data/test/fixtures/migrations_with_decimal/1_give_me_big_numbers.rb +15 -0
- data/test/fixtures/migrations_with_duplicate/1_people_have_last_names.rb +9 -0
- data/test/fixtures/migrations_with_duplicate/2_we_need_reminders.rb +12 -0
- data/test/fixtures/migrations_with_duplicate/3_foo.rb +7 -0
- data/test/fixtures/migrations_with_duplicate/3_innocent_jointable.rb +12 -0
- data/test/fixtures/migrations_with_missing_versions/1000_people_have_middle_names.rb +9 -0
- data/test/fixtures/migrations_with_missing_versions/1_people_have_last_names.rb +9 -0
- data/test/fixtures/migrations_with_missing_versions/3_we_need_reminders.rb +12 -0
- data/test/fixtures/migrations_with_missing_versions/4_innocent_jointable.rb +12 -0
- data/test/fixtures/minimalistic.rb +2 -0
- data/test/fixtures/minimalistics.yml +2 -0
- data/test/fixtures/mixed_case_monkey.rb +3 -0
- data/test/fixtures/mixed_case_monkeys.yml +6 -0
- data/test/fixtures/mixins.yml +29 -0
- data/test/fixtures/movies.yml +7 -0
- data/test/fixtures/naked/csv/accounts.csv +1 -0
- data/test/fixtures/naked/yml/accounts.yml +1 -0
- data/test/fixtures/naked/yml/companies.yml +1 -0
- data/test/fixtures/naked/yml/courses.yml +1 -0
- data/test/fixtures/order.rb +4 -0
- data/test/fixtures/parrot.rb +13 -0
- data/test/fixtures/parrots.yml +27 -0
- data/test/fixtures/parrots_pirates.yml +7 -0
- data/test/fixtures/people.yml +3 -0
- data/test/fixtures/person.rb +4 -0
- data/test/fixtures/pirate.rb +5 -0
- data/test/fixtures/pirates.yml +9 -0
- data/test/fixtures/post.rb +59 -0
- data/test/fixtures/posts.yml +48 -0
- data/test/fixtures/project.rb +27 -2
- data/test/fixtures/projects.yml +7 -0
- data/test/fixtures/reader.rb +4 -0
- data/test/fixtures/readers.yml +4 -0
- data/test/fixtures/reply.rb +18 -2
- data/test/fixtures/reserved_words/distinct.yml +5 -0
- data/test/fixtures/reserved_words/distincts_selects.yml +11 -0
- data/test/fixtures/reserved_words/group.yml +14 -0
- data/test/fixtures/reserved_words/select.yml +8 -0
- data/test/fixtures/reserved_words/values.yml +7 -0
- data/test/fixtures/ship.rb +3 -0
- data/test/fixtures/ships.yml +5 -0
- data/test/fixtures/subject.rb +4 -0
- data/test/fixtures/subscriber.rb +4 -3
- data/test/fixtures/tag.rb +7 -0
- data/test/fixtures/tagging.rb +10 -0
- data/test/fixtures/taggings.yml +25 -0
- data/test/fixtures/tags.yml +7 -0
- data/test/fixtures/task.rb +3 -0
- data/test/fixtures/tasks.yml +7 -0
- data/test/fixtures/topic.rb +20 -3
- data/test/fixtures/topics.yml +22 -0
- data/test/fixtures/treasure.rb +4 -0
- data/test/fixtures/treasures.yml +10 -0
- data/test/fixtures/vertex.rb +9 -0
- data/test/fixtures/vertices.yml +4 -0
- data/test/fixtures_test.rb +574 -8
- data/test/inheritance_test.rb +113 -27
- data/test/json_serialization_test.rb +180 -0
- data/test/lifecycle_test.rb +56 -29
- data/test/locking_test.rb +273 -0
- data/test/method_scoping_test.rb +416 -0
- data/test/migration_test.rb +933 -0
- data/test/migration_test_firebird.rb +124 -0
- data/test/mixin_test.rb +95 -0
- data/test/modules_test.rb +23 -10
- data/test/multiple_db_test.rb +17 -3
- data/test/pk_test.rb +59 -15
- data/test/query_cache_test.rb +104 -0
- data/test/readonly_test.rb +107 -0
- data/test/reflection_test.rb +124 -27
- data/test/reserved_word_test_mysql.rb +177 -0
- data/test/schema_authorization_test_postgresql.rb +75 -0
- data/test/schema_dumper_test.rb +131 -0
- data/test/schema_test_postgresql.rb +64 -0
- data/test/serialization_test.rb +47 -0
- data/test/synonym_test_oracle.rb +17 -0
- data/test/table_name_test_sqlserver.rb +23 -0
- data/test/threaded_connections_test.rb +48 -0
- data/test/transactions_test.rb +227 -29
- data/test/unconnected_test.rb +14 -6
- data/test/validations_test.rb +1293 -32
- data/test/xml_serialization_test.rb +202 -0
- metadata +347 -143
- data/dev-utils/eval_debugger.rb +0 -9
- data/examples/associations.rb +0 -87
- data/examples/shared_setup.rb +0 -15
- data/examples/validation.rb +0 -88
- data/lib/active_record/deprecated_associations.rb +0 -70
- 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/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/deprecated_associations_test.rb +0 -336
- data/test/fixtures/accounts/signals37 +0 -3
- data/test/fixtures/accounts/unknown +0 -2
- 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/courses/java +0 -2
- data/test/fixtures/courses/ruby +0 -2
- 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/developers/david +0 -2
- data/test/fixtures/developers/jamis +0 -2
- 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/movies/first +0 -2
- data/test/fixtures/movies/second +0 -2
- data/test/fixtures/projects/action_controller +0 -2
- data/test/fixtures/projects/active_record +0 -2
- data/test/fixtures/topics/first +0 -9
- data/test/fixtures/topics/second +0 -8
- data/test/inflector_test.rb +0 -104
- data/test/thread_safety_test.rb +0 -33
data/lib/active_record/base.rb
CHANGED
@@ -1,11 +1,12 @@
|
|
1
|
-
require '
|
2
|
-
require 'active_record/support/class_inheritable_attributes'
|
3
|
-
require 'active_record/support/inflector'
|
1
|
+
require 'base64'
|
4
2
|
require 'yaml'
|
3
|
+
require 'set'
|
5
4
|
|
6
5
|
module ActiveRecord #:nodoc:
|
7
6
|
class ActiveRecordError < StandardError #:nodoc:
|
8
7
|
end
|
8
|
+
class SubclassNotFound < ActiveRecordError #:nodoc:
|
9
|
+
end
|
9
10
|
class AssociationTypeMismatch < ActiveRecordError #:nodoc:
|
10
11
|
end
|
11
12
|
class SerializationTypeMismatch < ActiveRecordError #:nodoc:
|
@@ -20,102 +21,228 @@ module ActiveRecord #:nodoc:
|
|
20
21
|
end
|
21
22
|
class RecordNotFound < ActiveRecordError #:nodoc:
|
22
23
|
end
|
24
|
+
class RecordNotSaved < ActiveRecordError #:nodoc:
|
25
|
+
end
|
23
26
|
class StatementInvalid < ActiveRecordError #:nodoc:
|
24
27
|
end
|
28
|
+
class PreparedStatementInvalid < ActiveRecordError #:nodoc:
|
29
|
+
end
|
30
|
+
class StaleObjectError < ActiveRecordError #:nodoc:
|
31
|
+
end
|
32
|
+
class ConfigurationError < ActiveRecordError #:nodoc:
|
33
|
+
end
|
34
|
+
class ReadOnlyRecord < ActiveRecordError #:nodoc:
|
35
|
+
end
|
36
|
+
class Rollback < ActiveRecordError #:nodoc:
|
37
|
+
end
|
38
|
+
class DangerousAttributeError < ActiveRecordError #:nodoc:
|
39
|
+
end
|
40
|
+
|
41
|
+
# Raised when you've tried to access a column which wasn't
|
42
|
+
# loaded by your finder. Typically this is because :select
|
43
|
+
# has been specified
|
44
|
+
class MissingAttributeError < NoMethodError
|
45
|
+
end
|
25
46
|
|
26
|
-
|
47
|
+
class AttributeAssignmentError < ActiveRecordError #:nodoc:
|
48
|
+
attr_reader :exception, :attribute
|
49
|
+
def initialize(message, exception, attribute)
|
50
|
+
@exception = exception
|
51
|
+
@attribute = attribute
|
52
|
+
@message = message
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
class MultiparameterAssignmentErrors < ActiveRecordError #:nodoc:
|
57
|
+
attr_reader :errors
|
58
|
+
def initialize(errors)
|
59
|
+
@errors = errors
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
# Active Record objects don't specify their attributes directly, but rather infer them from the table definition with
|
27
64
|
# which they're linked. Adding, removing, and changing attributes and their type is done directly in the database. Any change
|
28
65
|
# is instantly reflected in the Active Record objects. The mapping that binds a given Active Record class to a certain
|
29
|
-
# database table will happen automatically in most common cases, but can be overwritten for the uncommon ones.
|
30
|
-
#
|
66
|
+
# database table will happen automatically in most common cases, but can be overwritten for the uncommon ones.
|
67
|
+
#
|
31
68
|
# See the mapping rules in table_name and the full example in link:files/README.html for more insight.
|
32
|
-
#
|
69
|
+
#
|
33
70
|
# == Creation
|
34
|
-
#
|
35
|
-
# Active Records
|
36
|
-
# you're receiving the data from somewhere else, like
|
37
|
-
#
|
38
|
-
# user = User.new(
|
71
|
+
#
|
72
|
+
# Active Records accept constructor parameters either in a hash or as a block. The hash method is especially useful when
|
73
|
+
# you're receiving the data from somewhere else, like an HTTP request. It works like this:
|
74
|
+
#
|
75
|
+
# user = User.new(:name => "David", :occupation => "Code Artist")
|
39
76
|
# user.name # => "David"
|
40
|
-
#
|
77
|
+
#
|
41
78
|
# You can also use block initialization:
|
42
|
-
#
|
79
|
+
#
|
43
80
|
# user = User.new do |u|
|
44
81
|
# u.name = "David"
|
45
82
|
# u.occupation = "Code Artist"
|
46
83
|
# end
|
47
|
-
#
|
84
|
+
#
|
48
85
|
# And of course you can just create a bare object and specify the attributes after the fact:
|
49
|
-
#
|
86
|
+
#
|
50
87
|
# user = User.new
|
51
88
|
# user.name = "David"
|
52
89
|
# user.occupation = "Code Artist"
|
53
|
-
#
|
90
|
+
#
|
54
91
|
# == Conditions
|
55
|
-
#
|
56
|
-
# Conditions can either be specified as a string or
|
92
|
+
#
|
93
|
+
# Conditions can either be specified as a string, array, or hash representing the WHERE-part of an SQL statement.
|
57
94
|
# The array form is to be used when the condition input is tainted and requires sanitization. The string form can
|
58
|
-
# be used for statements that
|
59
|
-
#
|
60
|
-
#
|
95
|
+
# be used for statements that don't involve tainted data. The hash form works much like the array form, except
|
96
|
+
# only equality and range is possible. Examples:
|
97
|
+
#
|
98
|
+
# class User < ActiveRecord::Base
|
61
99
|
# def self.authenticate_unsafely(user_name, password)
|
62
|
-
#
|
100
|
+
# find(:first, :conditions => "user_name = '#{user_name}' AND password = '#{password}'")
|
63
101
|
# end
|
64
|
-
#
|
102
|
+
#
|
65
103
|
# def self.authenticate_safely(user_name, password)
|
66
|
-
#
|
104
|
+
# find(:first, :conditions => [ "user_name = ? AND password = ?", user_name, password ])
|
105
|
+
# end
|
106
|
+
#
|
107
|
+
# def self.authenticate_safely_simply(user_name, password)
|
108
|
+
# find(:first, :conditions => { :user_name => user_name, :password => password })
|
67
109
|
# end
|
68
110
|
# end
|
69
|
-
#
|
70
|
-
# The
|
71
|
-
# attacks if the
|
72
|
-
#
|
73
|
-
# an attacker can't escape the query and fake the login (or worse).
|
74
|
-
#
|
111
|
+
#
|
112
|
+
# The <tt>authenticate_unsafely</tt> method inserts the parameters directly into the query and is thus susceptible to SQL-injection
|
113
|
+
# attacks if the <tt>user_name</tt> and +password+ parameters come directly from an HTTP request. The <tt>authenticate_safely</tt> and
|
114
|
+
# <tt>authenticate_safely_simply</tt> both will sanitize the <tt>user_name</tt> and +password+ before inserting them in the query,
|
115
|
+
# which will ensure that an attacker can't escape the query and fake the login (or worse).
|
116
|
+
#
|
117
|
+
# When using multiple parameters in the conditions, it can easily become hard to read exactly what the fourth or fifth
|
118
|
+
# question mark is supposed to represent. In those cases, you can resort to named bind variables instead. That's done by replacing
|
119
|
+
# the question marks with symbols and supplying a hash with values for the matching symbol keys:
|
120
|
+
#
|
121
|
+
# Company.find(:first, :conditions => [
|
122
|
+
# "id = :id AND name = :name AND division = :division AND created_at > :accounting_date",
|
123
|
+
# { :id => 3, :name => "37signals", :division => "First", :accounting_date => '2005-01-01' }
|
124
|
+
# ])
|
125
|
+
#
|
126
|
+
# Similarly, a simple hash without a statement will generate conditions based on equality with the SQL AND
|
127
|
+
# operator. For instance:
|
128
|
+
#
|
129
|
+
# Student.find(:all, :conditions => { :first_name => "Harvey", :status => 1 })
|
130
|
+
# Student.find(:all, :conditions => params[:student])
|
131
|
+
#
|
132
|
+
# A range may be used in the hash to use the SQL BETWEEN operator:
|
133
|
+
#
|
134
|
+
# Student.find(:all, :conditions => { :grade => 9..12 })
|
135
|
+
#
|
75
136
|
# == Overwriting default accessors
|
76
|
-
#
|
77
|
-
# All column values are automatically available through basic accessors on the Active Record object, but
|
78
|
-
# want to specialize this behavior. This can be done by
|
79
|
-
# name as the attribute) calling read_attribute(attr_name) and write_attribute(attr_name, value) to actually change things.
|
137
|
+
#
|
138
|
+
# All column values are automatically available through basic accessors on the Active Record object, but sometimes you
|
139
|
+
# want to specialize this behavior. This can be done by overwriting the default accessors (using the same
|
140
|
+
# name as the attribute) and calling read_attribute(attr_name) and write_attribute(attr_name, value) to actually change things.
|
80
141
|
# Example:
|
81
|
-
#
|
142
|
+
#
|
82
143
|
# class Song < ActiveRecord::Base
|
83
144
|
# # Uses an integer of seconds to hold the length of the song
|
84
|
-
#
|
145
|
+
#
|
85
146
|
# def length=(minutes)
|
86
|
-
# write_attribute(
|
147
|
+
# write_attribute(:length, minutes * 60)
|
87
148
|
# end
|
88
|
-
#
|
149
|
+
#
|
89
150
|
# def length
|
90
|
-
# read_attribute(
|
151
|
+
# read_attribute(:length) / 60
|
91
152
|
# end
|
92
153
|
# end
|
154
|
+
#
|
155
|
+
# You can alternatively use self[:attribute]=(value) and self[:attribute] instead of write_attribute(:attribute, value) and
|
156
|
+
# read_attribute(:attribute) as a shorter form.
|
157
|
+
#
|
158
|
+
# == Attribute query methods
|
159
|
+
#
|
160
|
+
# In addition to the basic accessors, query methods are also automatically available on the Active Record object.
|
161
|
+
# Query methods allow you to test whether an attribute value is present.
|
93
162
|
#
|
94
|
-
#
|
95
|
-
#
|
96
|
-
#
|
97
|
-
#
|
98
|
-
#
|
163
|
+
# For example, an Active Record User with the <tt>name</tt> attribute has a <tt>name?</tt> method that you can call
|
164
|
+
# to determine whether the user has a name:
|
165
|
+
#
|
166
|
+
# user = User.new(:name => "David")
|
167
|
+
# user.name? # => true
|
168
|
+
#
|
169
|
+
# anonymous = User.new(:name => "")
|
170
|
+
# anonymous.name? # => false
|
171
|
+
#
|
172
|
+
# == Accessing attributes before they have been typecasted
|
173
|
+
#
|
174
|
+
# Sometimes you want to be able to read the raw attribute data without having the column-determined typecast run its course first.
|
175
|
+
# That can be done by using the <attribute>_before_type_cast accessors that all attributes have. For example, if your Account model
|
176
|
+
# has a balance attribute, you can call account.balance_before_type_cast or account.id_before_type_cast.
|
177
|
+
#
|
178
|
+
# This is especially useful in validation situations where the user might supply a string for an integer field and you want to display
|
179
|
+
# the original string back in an error message. Accessing the attribute normally would typecast the string to 0, which isn't what you
|
180
|
+
# want.
|
181
|
+
#
|
182
|
+
# == Dynamic attribute-based finders
|
183
|
+
#
|
184
|
+
# Dynamic attribute-based finders are a cleaner way of getting (and/or creating) objects by simple queries without turning to SQL. They work by
|
185
|
+
# appending the name of an attribute to <tt>find_by_</tt> or <tt>find_all_by_</tt>, so you get finders like Person.find_by_user_name,
|
186
|
+
# Person.find_all_by_last_name, Payment.find_by_transaction_id. So instead of writing
|
187
|
+
# <tt>Person.find(:first, :conditions => ["user_name = ?", user_name])</tt>, you just do <tt>Person.find_by_user_name(user_name)</tt>.
|
188
|
+
# And instead of writing <tt>Person.find(:all, :conditions => ["last_name = ?", last_name])</tt>, you just do <tt>Person.find_all_by_last_name(last_name)</tt>.
|
189
|
+
#
|
190
|
+
# It's also possible to use multiple attributes in the same find by separating them with "_and_", so you get finders like
|
191
|
+
# <tt>Person.find_by_user_name_and_password</tt> or even <tt>Payment.find_by_purchaser_and_state_and_country</tt>. So instead of writing
|
192
|
+
# <tt>Person.find(:first, :conditions => ["user_name = ? AND password = ?", user_name, password])</tt>, you just do
|
193
|
+
# <tt>Person.find_by_user_name_and_password(user_name, password)</tt>.
|
194
|
+
#
|
195
|
+
# It's even possible to use all the additional parameters to find. For example, the full interface for Payment.find_all_by_amount
|
196
|
+
# is actually Payment.find_all_by_amount(amount, options). And the full interface to Person.find_by_user_name is
|
197
|
+
# actually Person.find_by_user_name(user_name, options). So you could call <tt>Payment.find_all_by_amount(50, :order => "created_on")</tt>.
|
198
|
+
#
|
199
|
+
# The same dynamic finder style can be used to create the object if it doesn't already exist. This dynamic finder is called with
|
200
|
+
# <tt>find_or_create_by_</tt> and will return the object if it already exists and otherwise creates it, then returns it. Example:
|
201
|
+
#
|
202
|
+
# # No 'Summer' tag exists
|
203
|
+
# Tag.find_or_create_by_name("Summer") # equal to Tag.create(:name => "Summer")
|
204
|
+
#
|
205
|
+
# # Now the 'Summer' tag does exist
|
206
|
+
# Tag.find_or_create_by_name("Summer") # equal to Tag.find_by_name("Summer")
|
207
|
+
#
|
208
|
+
# Use the <tt>find_or_initialize_by_</tt> finder if you want to return a new record without saving it first. Example:
|
209
|
+
#
|
210
|
+
# # No 'Winter' tag exists
|
211
|
+
# winter = Tag.find_or_initialize_by_name("Winter")
|
212
|
+
# winter.new_record? # true
|
213
|
+
#
|
214
|
+
# To find by a subset of the attributes to be used for instantiating a new object, pass a hash instead of
|
215
|
+
# a list of parameters. For example:
|
216
|
+
#
|
217
|
+
# Tag.find_or_create_by_name(:name => "rails", :creator => current_user)
|
218
|
+
#
|
219
|
+
# That will either find an existing tag named "rails", or create a new one while setting the user that created it.
|
220
|
+
#
|
221
|
+
# == Saving arrays, hashes, and other non-mappable objects in text columns
|
222
|
+
#
|
223
|
+
# Active Record can serialize any object in text columns using YAML. To do so, you must specify this with a call to the class method +serialize+.
|
224
|
+
# This makes it possible to store arrays, hashes, and other non-mappable objects without doing any additional work. Example:
|
225
|
+
#
|
99
226
|
# class User < ActiveRecord::Base
|
100
227
|
# serialize :preferences
|
101
228
|
# end
|
102
|
-
#
|
103
|
-
# user = User.create(
|
229
|
+
#
|
230
|
+
# user = User.create(:preferences => { "background" => "black", "display" => large })
|
104
231
|
# User.find(user.id).preferences # => { "background" => "black", "display" => large }
|
105
|
-
#
|
106
|
-
# You can also specify
|
232
|
+
#
|
233
|
+
# You can also specify a class option as the second parameter that'll raise an exception if a serialized object is retrieved as a
|
107
234
|
# descendent of a class not in the hierarchy. Example:
|
108
|
-
#
|
235
|
+
#
|
109
236
|
# class User < ActiveRecord::Base
|
110
|
-
# serialize :preferences,
|
237
|
+
# serialize :preferences, Hash
|
111
238
|
# end
|
112
|
-
#
|
113
|
-
# user = User.create(
|
114
|
-
# User.find(user.id).preferences
|
115
|
-
#
|
239
|
+
#
|
240
|
+
# user = User.create(:preferences => %w( one two three ))
|
241
|
+
# User.find(user.id).preferences # raises SerializationTypeMismatch
|
242
|
+
#
|
116
243
|
# == Single table inheritance
|
117
244
|
#
|
118
|
-
# Active Record allows inheritance by storing the name of the class in a column that by default is
|
245
|
+
# Active Record allows inheritance by storing the name of the class in a column that by default is named "type" (can be changed
|
119
246
|
# by overwriting <tt>Base.inheritance_column</tt>). This means that an inheritance looking like this:
|
120
247
|
#
|
121
248
|
# class Company < ActiveRecord::Base; end
|
@@ -123,61 +250,52 @@ module ActiveRecord #:nodoc:
|
|
123
250
|
# class Client < Company; end
|
124
251
|
# class PriorityClient < Client; end
|
125
252
|
#
|
126
|
-
# When you do Firm.create(
|
127
|
-
# fetch this row again using Company.
|
253
|
+
# When you do Firm.create(:name => "37signals"), this record will be saved in the companies table with type = "Firm". You can then
|
254
|
+
# fetch this row again using Company.find(:first, "name = '37signals'") and it will return a Firm object.
|
255
|
+
#
|
256
|
+
# If you don't have a type column defined in your table, single-table inheritance won't be triggered. In that case, it'll work just
|
257
|
+
# like normal subclasses with no special magic for differentiating between them or reloading the right type with find.
|
128
258
|
#
|
129
259
|
# Note, all the attributes for all the cases are kept in the same table. Read more:
|
130
260
|
# http://www.martinfowler.com/eaaCatalog/singleTableInheritance.html
|
131
|
-
#
|
261
|
+
#
|
132
262
|
# == Connection to multiple databases in different models
|
133
263
|
#
|
134
264
|
# Connections are usually created through ActiveRecord::Base.establish_connection and retrieved by ActiveRecord::Base.connection.
|
135
|
-
# All classes inheriting from ActiveRecord::Base will use this connection. But you can also set a class-specific connection.
|
136
|
-
# For example, if Course is
|
265
|
+
# All classes inheriting from ActiveRecord::Base will use this connection. But you can also set a class-specific connection.
|
266
|
+
# For example, if Course is an ActiveRecord::Base, but resides in a different database, you can just say Course.establish_connection
|
137
267
|
# and Course *and all its subclasses* will use this connection instead.
|
138
268
|
#
|
139
269
|
# This feature is implemented by keeping a connection pool in ActiveRecord::Base that is a Hash indexed by the class. If a connection is
|
140
270
|
# requested, the retrieve_connection method will go up the class-hierarchy until a connection is found in the connection pool.
|
141
271
|
#
|
142
272
|
# == Exceptions
|
143
|
-
#
|
273
|
+
#
|
144
274
|
# * +ActiveRecordError+ -- generic error class and superclass of all other errors raised by Active Record
|
145
|
-
# * +AdapterNotSpecified+ -- the configuration hash used in <tt>establish_connection</tt> didn't include
|
275
|
+
# * +AdapterNotSpecified+ -- the configuration hash used in <tt>establish_connection</tt> didn't include an
|
146
276
|
# <tt>:adapter</tt> key.
|
147
|
-
# * +
|
148
|
-
# (or a bad spelling of an existing one).
|
149
|
-
# * +AssociationTypeMismatch+ -- the object assigned to the association wasn't of the type specified in the association definition.
|
150
|
-
# * +SerializationTypeMismatch+ -- the object
|
151
|
-
# the serialize definition.
|
277
|
+
# * +AdapterNotFound+ -- the <tt>:adapter</tt> key used in <tt>establish_connection</tt> specified a non-existent adapter
|
278
|
+
# (or a bad spelling of an existing one).
|
279
|
+
# * +AssociationTypeMismatch+ -- the object assigned to the association wasn't of the type specified in the association definition.
|
280
|
+
# * +SerializationTypeMismatch+ -- the serialized object wasn't of the class specified as the second parameter.
|
152
281
|
# * +ConnectionNotEstablished+ -- no connection has been established. Use <tt>establish_connection</tt> before querying.
|
153
|
-
# * +RecordNotFound+ -- no record responded to the find* method.
|
282
|
+
# * +RecordNotFound+ -- no record responded to the find* method.
|
154
283
|
# Either the row with the given ID doesn't exist or the row didn't meet the additional restrictions.
|
155
284
|
# * +StatementInvalid+ -- the database server rejected the SQL statement. The precise error is added in the message.
|
156
285
|
# Either the record with the given ID doesn't exist or the record didn't meet the additional restrictions.
|
157
|
-
#
|
158
|
-
#
|
286
|
+
# * +MultiparameterAssignmentErrors+ -- collection of errors that occurred during a mass assignment using the
|
287
|
+
# +attributes=+ method. The +errors+ property of this exception contains an array of +AttributeAssignmentError+
|
288
|
+
# objects that should be inspected to determine which attributes triggered the errors.
|
289
|
+
# * +AttributeAssignmentError+ -- an error occurred while doing a mass assignment through the +attributes=+ method.
|
290
|
+
# You can inspect the +attribute+ property of the exception object to determine which attribute triggered the error.
|
291
|
+
#
|
292
|
+
# *Note*: The attributes listed are class-level attributes (accessible from both the class and instance level).
|
159
293
|
# So it's possible to assign a logger to the class through Base.logger= which will then be used by all
|
160
294
|
# instances in the current object space.
|
161
295
|
class Base
|
162
|
-
include ClassInheritableAttributes
|
163
|
-
|
164
296
|
# Accepts a logger conforming to the interface of Log4r or the default Ruby 1.8+ Logger class, which is then passed
|
165
297
|
# on to any new database connections made and which can be retrieved on both a class and instance level by calling +logger+.
|
166
|
-
cattr_accessor :logger
|
167
|
-
|
168
|
-
# Returns the connection currently associated with the class. This can
|
169
|
-
# also be used to "borrow" the connection to do database work unrelated
|
170
|
-
# to any of the specific Active Records.
|
171
|
-
def self.connection
|
172
|
-
retrieve_connection
|
173
|
-
end
|
174
|
-
|
175
|
-
# Returns the connection currently associated with the class. This can
|
176
|
-
# also be used to "borrow" the connection to do database work that isn't
|
177
|
-
# easily done without going straight to SQL.
|
178
|
-
def connection
|
179
|
-
self.class.connection
|
180
|
-
end
|
298
|
+
cattr_accessor :logger, :instance_writer => false
|
181
299
|
|
182
300
|
def self.inherited(child) #:nodoc:
|
183
301
|
@@subclasses[self] ||= []
|
@@ -185,186 +303,432 @@ module ActiveRecord #:nodoc:
|
|
185
303
|
super
|
186
304
|
end
|
187
305
|
|
306
|
+
def self.reset_subclasses #:nodoc:
|
307
|
+
nonreloadables = []
|
308
|
+
subclasses.each do |klass|
|
309
|
+
unless Dependencies.autoloaded? klass
|
310
|
+
nonreloadables << klass
|
311
|
+
next
|
312
|
+
end
|
313
|
+
klass.instance_variables.each { |var| klass.send(:remove_instance_variable, var) }
|
314
|
+
klass.instance_methods(false).each { |m| klass.send :undef_method, m }
|
315
|
+
end
|
316
|
+
@@subclasses = {}
|
317
|
+
nonreloadables.each { |klass| (@@subclasses[klass.superclass] ||= []) << klass }
|
318
|
+
end
|
319
|
+
|
188
320
|
@@subclasses = {}
|
189
|
-
|
190
|
-
|
321
|
+
|
322
|
+
cattr_accessor :configurations, :instance_writer => false
|
323
|
+
@@configurations = {}
|
324
|
+
|
325
|
+
# Accessor for the prefix type that will be prepended to every primary key column name. The options are :table_name and
|
191
326
|
# :table_name_with_underscore. If the first is specified, the Product class will look for "productid" instead of "id" as
|
192
327
|
# the primary column. If the latter is specified, the Product class will look for "product_id" instead of "id". Remember
|
193
|
-
# that this is a global setting for all Active Records.
|
194
|
-
cattr_accessor :primary_key_prefix_type
|
328
|
+
# that this is a global setting for all Active Records.
|
329
|
+
cattr_accessor :primary_key_prefix_type, :instance_writer => false
|
195
330
|
@@primary_key_prefix_type = nil
|
196
331
|
|
197
|
-
# Accessor for the name of the prefix string to prepend to every table name. So if set to "basecamp_", all
|
198
|
-
# table names will be named like "basecamp_projects", "basecamp_people", etc. This is a
|
332
|
+
# Accessor for the name of the prefix string to prepend to every table name. So if set to "basecamp_", all
|
333
|
+
# table names will be named like "basecamp_projects", "basecamp_people", etc. This is a convenient way of creating a namespace
|
199
334
|
# for tables in a shared database. By default, the prefix is the empty string.
|
200
|
-
cattr_accessor :table_name_prefix
|
335
|
+
cattr_accessor :table_name_prefix, :instance_writer => false
|
201
336
|
@@table_name_prefix = ""
|
202
337
|
|
203
338
|
# Works like +table_name_prefix+, but appends instead of prepends (set to "_basecamp" gives "projects_basecamp",
|
204
339
|
# "people_basecamp"). By default, the suffix is the empty string.
|
205
|
-
cattr_accessor :table_name_suffix
|
340
|
+
cattr_accessor :table_name_suffix, :instance_writer => false
|
206
341
|
@@table_name_suffix = ""
|
207
342
|
|
208
|
-
#
|
209
|
-
# If true,
|
343
|
+
# Indicates whether table names should be the pluralized versions of the corresponding class names.
|
344
|
+
# If true, the default table name for a +Product+ class will be +products+. If false, it would just be +product+.
|
210
345
|
# See table_name for the full rules on table/class naming. This is true, by default.
|
211
|
-
cattr_accessor :pluralize_table_names
|
346
|
+
cattr_accessor :pluralize_table_names, :instance_writer => false
|
212
347
|
@@pluralize_table_names = true
|
213
348
|
|
349
|
+
# Determines whether to use ANSI codes to colorize the logging statements committed by the connection adapter. These colors
|
350
|
+
# make it much easier to overview things during debugging (when used through a reader like +tail+ and on a black background), but
|
351
|
+
# may complicate matters if you use software like syslog. This is true, by default.
|
352
|
+
cattr_accessor :colorize_logging, :instance_writer => false
|
353
|
+
@@colorize_logging = true
|
354
|
+
|
355
|
+
# Determines whether to use Time.local (using :local) or Time.utc (using :utc) when pulling dates and times from the database.
|
356
|
+
# This is set to :local by default.
|
357
|
+
cattr_accessor :default_timezone, :instance_writer => false
|
358
|
+
@@default_timezone = :local
|
359
|
+
|
360
|
+
# Determines whether to use a connection for each thread, or a single shared connection for all threads.
|
361
|
+
# Defaults to false. Set to true if you're writing a threaded application.
|
362
|
+
cattr_accessor :allow_concurrency, :instance_writer => false
|
363
|
+
@@allow_concurrency = false
|
364
|
+
|
365
|
+
# Specifies the format to use when dumping the database schema with Rails'
|
366
|
+
# Rakefile. If :sql, the schema is dumped as (potentially database-
|
367
|
+
# specific) SQL statements. If :ruby, the schema is dumped as an
|
368
|
+
# ActiveRecord::Schema file which can be loaded into any database that
|
369
|
+
# supports migrations. Use :ruby if you want to have different database
|
370
|
+
# adapters for, e.g., your development and test environments.
|
371
|
+
cattr_accessor :schema_format , :instance_writer => false
|
372
|
+
@@schema_format = :ruby
|
373
|
+
|
214
374
|
class << self # Class methods
|
215
|
-
#
|
216
|
-
#
|
217
|
-
#
|
375
|
+
# Find operates with three different retrieval approaches:
|
376
|
+
#
|
377
|
+
# * Find by id: This can either be a specific id (1), a list of ids (1, 5, 6), or an array of ids ([5, 6, 10]).
|
378
|
+
# If no record can be found for all of the listed ids, then RecordNotFound will be raised.
|
379
|
+
# * Find first: This will return the first record matched by the options used. These options can either be specific
|
380
|
+
# conditions or merely an order. If no record can be matched, nil is returned.
|
381
|
+
# * Find all: This will return all the records matched by the options used. If no records are found, an empty array is returned.
|
382
|
+
#
|
383
|
+
# All approaches accept an options hash as their last parameter. The options are:
|
384
|
+
#
|
385
|
+
# * <tt>:conditions</tt>: An SQL fragment like "administrator = 1" or [ "user_name = ?", username ]. See conditions in the intro.
|
386
|
+
# * <tt>:order</tt>: An SQL fragment like "created_at DESC, name".
|
387
|
+
# * <tt>:group</tt>: An attribute name by which the result should be grouped. Uses the GROUP BY SQL-clause.
|
388
|
+
# * <tt>:limit</tt>: An integer determining the limit on the number of rows that should be returned.
|
389
|
+
# * <tt>:offset</tt>: An integer determining the offset from where the rows should be fetched. So at 5, it would skip rows 0 through 4.
|
390
|
+
# * <tt>:joins</tt>: An SQL fragment for additional joins like "LEFT JOIN comments ON comments.post_id = id" (Rarely needed).
|
391
|
+
# Accepts named associations in the form of :include, which will perform an INNER JOIN on the associated table(s).
|
392
|
+
# The records will be returned read-only since they will have attributes that do not correspond to the table's columns.
|
393
|
+
# Pass :readonly => false to override.
|
394
|
+
# See adding joins for associations under Associations.
|
395
|
+
# * <tt>:include</tt>: Names associations that should be loaded alongside using LEFT OUTER JOINs. The symbols named refer
|
396
|
+
# to already defined associations. See eager loading under Associations.
|
397
|
+
# * <tt>:select</tt>: By default, this is * as in SELECT * FROM, but can be changed if you, for example, want to do a join but not
|
398
|
+
# include the joined columns.
|
399
|
+
# * <tt>:from</tt>: By default, this is the table name of the class, but can be changed to an alternate table name (or even the name
|
400
|
+
# of a database view).
|
401
|
+
# * <tt>:readonly</tt>: Mark the returned records read-only so they cannot be saved or updated.
|
402
|
+
# * <tt>:lock</tt>: An SQL fragment like "FOR UPDATE" or "LOCK IN SHARE MODE".
|
403
|
+
# :lock => true gives connection's default exclusive lock, usually "FOR UPDATE".
|
404
|
+
#
|
405
|
+
# Examples for find by id:
|
218
406
|
# Person.find(1) # returns the object for ID = 1
|
219
407
|
# Person.find(1, 2, 6) # returns an array for objects with IDs in (1, 2, 6)
|
220
408
|
# Person.find([7, 17]) # returns an array for objects with IDs in (7, 17)
|
221
|
-
#
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
409
|
+
# Person.find([1]) # returns an array for the object with ID = 1
|
410
|
+
# Person.find(1, :conditions => "administrator = 1", :order => "created_on DESC")
|
411
|
+
#
|
412
|
+
# Note that returned records may not be in the same order as the ids you
|
413
|
+
# provide since database rows are unordered. Give an explicit :order
|
414
|
+
# to ensure the results are sorted.
|
415
|
+
#
|
416
|
+
# Examples for find first:
|
417
|
+
# Person.find(:first) # returns the first object fetched by SELECT * FROM people
|
418
|
+
# Person.find(:first, :conditions => [ "user_name = ?", user_name])
|
419
|
+
# Person.find(:first, :order => "created_on DESC", :offset => 5)
|
420
|
+
#
|
421
|
+
# Examples for find all:
|
422
|
+
# Person.find(:all) # returns an array of objects for all the rows fetched by SELECT * FROM people
|
423
|
+
# Person.find(:all, :conditions => [ "category IN (?)", categories], :limit => 50)
|
424
|
+
# Person.find(:all, :offset => 10, :limit => 10)
|
425
|
+
# Person.find(:all, :include => [ :account, :friends ])
|
426
|
+
# Person.find(:all, :group => "category")
|
427
|
+
#
|
428
|
+
# Example for find with a lock. Imagine two concurrent transactions:
|
429
|
+
# each will read person.visits == 2, add 1 to it, and save, resulting
|
430
|
+
# in two saves of person.visits = 3. By locking the row, the second
|
431
|
+
# transaction has to wait until the first is finished; we get the
|
432
|
+
# expected person.visits == 4.
|
433
|
+
# Person.transaction do
|
434
|
+
# person = Person.find(1, :lock => true)
|
435
|
+
# person.visits += 1
|
436
|
+
# person.save!
|
437
|
+
# end
|
438
|
+
def find(*args)
|
439
|
+
options = args.extract_options!
|
440
|
+
# Note: we extract any :joins option with a non-string value from the options, and turn it into
|
441
|
+
# an internal option :ar_joins. This allows code called from here to find the ar_joins, and
|
442
|
+
# it bypasses marking the result as read_only.
|
443
|
+
# A normal string join marks the result as read-only because it contains attributes from joined tables
|
444
|
+
# which are not in the base table and therefore prevent the result from being saved.
|
445
|
+
# In the case of an ar_join, the JoinDependency created to instantiate the results eliminates these
|
446
|
+
# bogus attributes. See JoinDependency#instantiate, and JoinBase#instantiate in associations.rb.
|
447
|
+
validate_find_options(options)
|
448
|
+
set_readonly_option!(options)
|
449
|
+
|
450
|
+
case args.first
|
451
|
+
when :first then find_initial(options)
|
452
|
+
when :all then find_every(options)
|
453
|
+
else find_from_ids(args, options)
|
246
454
|
end
|
247
455
|
end
|
248
|
-
|
249
|
-
#
|
250
|
-
#
|
251
|
-
#
|
252
|
-
#
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
#
|
259
|
-
#
|
260
|
-
#
|
261
|
-
#
|
262
|
-
#
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
# Works like find_all, but requires a complete SQL string. Example:
|
274
|
-
# Post.find_by_sql "SELECT p.*, c.author FROM posts p, comments c WHERE p.id = c.post_id"
|
456
|
+
|
457
|
+
#
|
458
|
+
# Executes a custom sql query against your database and returns all the results. The results will
|
459
|
+
# be returned as an array with columns requested encapsulated as attributes of the model you call
|
460
|
+
# this method from. If you call +Product.find_by_sql+ then the results will be returned in a Product
|
461
|
+
# object with the attributes you specified in the SQL query.
|
462
|
+
#
|
463
|
+
# If you call a complicated SQL query which spans multiple tables the columns specified by the
|
464
|
+
# SELECT will be attributes of the model, whether or not they are columns of the corresponding
|
465
|
+
# table.
|
466
|
+
#
|
467
|
+
# The +sql+ parameter is a full sql query as a string. It will be called as is, there will be
|
468
|
+
# no database agnostic conversions performed. This should be a last resort because using, for example,
|
469
|
+
# MySQL specific terms will lock you to using that particular database engine or require you to
|
470
|
+
# change your call if you switch engines
|
471
|
+
#
|
472
|
+
# ==== Examples
|
473
|
+
# # A simple sql query spanning multiple tables
|
474
|
+
# Post.find_by_sql "SELECT p.title, c.author FROM posts p, comments c WHERE p.id = c.post_id"
|
475
|
+
# > [#<Post:0x36bff9c @attributes={"title"=>"Ruby Meetup", "first_name"=>"Quentin"}>, ...]
|
476
|
+
#
|
477
|
+
# # You can use the same string replacement techniques as you can with ActiveRecord#find
|
478
|
+
# Post.find_by_sql ["SELECT title FROM posts WHERE author = ? AND created > ?", author_id, start_date]
|
479
|
+
# > [#<Post:0x36bff9c @attributes={"first_name"=>"The Cheap Man Buys Twice"}>, ...]
|
275
480
|
def find_by_sql(sql)
|
276
|
-
connection.select_all(sql, "#{name} Load").
|
277
|
-
end
|
278
|
-
|
279
|
-
#
|
280
|
-
#
|
281
|
-
#
|
282
|
-
#
|
283
|
-
#
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
#
|
295
|
-
|
481
|
+
connection.select_all(sanitize_sql(sql), "#{name} Load").collect! { |record| instantiate(record) }
|
482
|
+
end
|
483
|
+
|
484
|
+
# Checks whether a record exists in the database that matches conditions given. These conditions
|
485
|
+
# can either be a single integer representing a primary key id to be found, or a condition to be
|
486
|
+
# matched like using ActiveRecord#find.
|
487
|
+
#
|
488
|
+
# The +id_or_conditions+ parameter can be an Integer or a String if you want to search the primary key
|
489
|
+
# column of the table for a matching id, or if you're looking to match against a condition you can use
|
490
|
+
# an Array or a Hash.
|
491
|
+
#
|
492
|
+
# Possible gotcha: You can't pass in a condition as a string e.g. "name = 'Jamie'", this would be
|
493
|
+
# sanitized and then queried against the primary key column as "id = 'name = \'Jamie"
|
494
|
+
#
|
495
|
+
# ==== Examples
|
496
|
+
# Person.exists?(5)
|
497
|
+
# Person.exists?('5')
|
498
|
+
# Person.exists?(:name => "David")
|
499
|
+
# Person.exists?(['name LIKE ?', "%#{query}%"])
|
500
|
+
def exists?(id_or_conditions)
|
501
|
+
!find(:first, :select => "#{table_name}.#{primary_key}", :conditions => expand_id_conditions(id_or_conditions)).nil?
|
502
|
+
rescue ActiveRecord::ActiveRecordError
|
503
|
+
false
|
504
|
+
end
|
505
|
+
|
506
|
+
# Creates an object (or multiple objects) and saves it to the database, if validations pass.
|
507
|
+
# The resulting object is returned whether the object was saved successfully to the database or not.
|
508
|
+
#
|
509
|
+
# The +attributes+ parameter can be either be a Hash or an Array of Hashes. These Hashes describe the
|
510
|
+
# attributes on the objects that are to be created.
|
511
|
+
#
|
512
|
+
# ==== Examples
|
513
|
+
# # Create a single new object
|
514
|
+
# User.create(:first_name => 'Jamie')
|
515
|
+
# # Create an Array of new objects
|
516
|
+
# User.create([{:first_name => 'Jamie'}, {:first_name => 'Jeremy'}])
|
296
517
|
def create(attributes = nil)
|
297
|
-
|
298
|
-
|
299
|
-
|
518
|
+
if attributes.is_a?(Array)
|
519
|
+
attributes.collect { |attr| create(attr) }
|
520
|
+
else
|
521
|
+
object = new(attributes)
|
522
|
+
object.save
|
523
|
+
object
|
524
|
+
end
|
300
525
|
end
|
301
526
|
|
302
|
-
#
|
303
|
-
#
|
527
|
+
# Updates an object (or multiple objects) and saves it to the database, if validations pass.
|
528
|
+
# The resulting object is returned whether the object was saved successfully to the database or not.
|
529
|
+
#
|
530
|
+
# ==== Options
|
531
|
+
#
|
532
|
+
# +id+ This should be the id or an array of ids to be updated
|
533
|
+
# +attributes+ This should be a Hash of attributes to be set on the object, or an array of Hashes.
|
534
|
+
#
|
535
|
+
# ==== Examples
|
536
|
+
#
|
537
|
+
# # Updating one record:
|
538
|
+
# Person.update(15, {:user_name => 'Samuel', :group => 'expert'})
|
539
|
+
#
|
540
|
+
# # Updating multiple records:
|
541
|
+
# people = { 1 => { "first_name" => "David" }, 2 => { "first_name" => "Jeremy"} }
|
542
|
+
# Person.update(people.keys, people.values)
|
304
543
|
def update(id, attributes)
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
544
|
+
if id.is_a?(Array)
|
545
|
+
idx = -1
|
546
|
+
id.collect { |id| idx += 1; update(id, attributes[idx]) }
|
547
|
+
else
|
548
|
+
object = find(id)
|
549
|
+
object.update_attributes(attributes)
|
550
|
+
object
|
551
|
+
end
|
552
|
+
end
|
553
|
+
|
554
|
+
# Delete an object (or multiple objects) where the +id+ given matches the primary_key. A SQL +DELETE+ command
|
555
|
+
# is executed on the database which means that no callbacks are fired off running this. This is an efficient method
|
556
|
+
# of deleting records that don't need cleaning up after or other actions to be taken.
|
557
|
+
#
|
558
|
+
# Objects are _not_ instantiated with this method.
|
559
|
+
#
|
560
|
+
# ==== Options
|
561
|
+
#
|
562
|
+
# +id+ Can be either an Integer or an Array of Integers
|
563
|
+
#
|
564
|
+
# ==== Examples
|
565
|
+
#
|
566
|
+
# # Delete a single object
|
567
|
+
# Todo.delete(1)
|
568
|
+
#
|
569
|
+
# # Delete multiple objects
|
570
|
+
# todos = [1,2,3]
|
571
|
+
# Todo.delete(todos)
|
572
|
+
def delete(id)
|
573
|
+
delete_all([ "#{connection.quote_column_name(primary_key)} IN (?)", id ])
|
574
|
+
end
|
575
|
+
|
576
|
+
# Destroy an object (or multiple objects) that has the given id, the object is instantiated first,
|
577
|
+
# therefore all callbacks and filters are fired off before the object is deleted. This method is
|
578
|
+
# less efficient than ActiveRecord#delete but allows cleanup methods and other actions to be run.
|
579
|
+
#
|
580
|
+
# This essentially finds the object (or multiple objects) with the given id, creates a new object
|
581
|
+
# from the attributes, and then calls destroy on it.
|
582
|
+
#
|
583
|
+
# ==== Options
|
584
|
+
#
|
585
|
+
# +id+ Can be either an Integer or an Array of Integers
|
586
|
+
#
|
587
|
+
# ==== Examples
|
588
|
+
#
|
589
|
+
# # Destroy a single object
|
590
|
+
# Todo.destroy(1)
|
591
|
+
#
|
592
|
+
# # Destroy multiple objects
|
593
|
+
# todos = [1,2,3]
|
594
|
+
# Todo.destroy(todos)
|
595
|
+
def destroy(id)
|
596
|
+
id.is_a?(Array) ? id.each { |id| destroy(id) } : find(id).destroy
|
597
|
+
end
|
598
|
+
|
599
|
+
# Updates all records with details given if they match a set of conditions supplied, limits and order can
|
600
|
+
# also be supplied.
|
601
|
+
#
|
602
|
+
# ==== Options
|
603
|
+
#
|
604
|
+
# +updates+ A String of column and value pairs that will be set on any records that match conditions
|
605
|
+
# +conditions+ An SQL fragment like "administrator = 1" or [ "user_name = ?", username ].
|
606
|
+
# See conditions in the intro for more info.
|
607
|
+
# +options+ Additional options are :limit and/or :order, see the examples for usage.
|
608
|
+
#
|
609
|
+
# ==== Examples
|
610
|
+
#
|
611
|
+
# # Update all billing objects with the 3 different attributes given
|
612
|
+
# Billing.update_all( "category = 'authorized', approved = 1, author = 'David'" )
|
613
|
+
#
|
614
|
+
# # Update records that match our conditions
|
615
|
+
# Billing.update_all( "author = 'David'", "title LIKE '%Rails%'" )
|
616
|
+
#
|
617
|
+
# # Update records that match our conditions but limit it to 5 ordered by date
|
618
|
+
# Billing.update_all( "author = 'David'", "title LIKE '%Rails%'",
|
619
|
+
# :order => 'created_at', :limit => 5 )
|
620
|
+
def update_all(updates, conditions = nil, options = {})
|
621
|
+
sql = "UPDATE #{table_name} SET #{sanitize_sql_for_assignment(updates)} "
|
622
|
+
scope = scope(:find)
|
623
|
+
add_conditions!(sql, conditions, scope)
|
624
|
+
add_order!(sql, options[:order], scope)
|
625
|
+
add_limit!(sql, options, scope)
|
317
626
|
connection.update(sql, "#{name} Update")
|
318
627
|
end
|
319
|
-
|
320
|
-
# Destroys the objects for all the records that
|
628
|
+
|
629
|
+
# Destroys the objects for all the records that match the +conditions+ by instantiating each object and calling
|
321
630
|
# the destroy method. Example:
|
322
631
|
# Person.destroy_all "last_login < '2004-04-04'"
|
323
632
|
def destroy_all(conditions = nil)
|
324
|
-
|
633
|
+
find(:all, :conditions => conditions).each { |object| object.destroy }
|
325
634
|
end
|
326
|
-
|
327
|
-
# Deletes all the records that
|
635
|
+
|
636
|
+
# Deletes all the records that match the +conditions+ without instantiating the objects first (and hence not
|
328
637
|
# calling the destroy method). Example:
|
329
|
-
# Post.
|
638
|
+
# Post.delete_all "person_id = 5 AND (category = 'Something' OR category = 'Else')"
|
330
639
|
def delete_all(conditions = nil)
|
331
|
-
sql = "DELETE FROM #{
|
332
|
-
add_conditions!(sql, conditions)
|
640
|
+
sql = "DELETE FROM #{quoted_table_name} "
|
641
|
+
add_conditions!(sql, conditions, scope(:find))
|
333
642
|
connection.delete(sql, "#{name} Delete all")
|
334
643
|
end
|
335
|
-
|
336
|
-
# Returns the number of records that meets the +conditions+. Zero is returned if no records match. Example:
|
337
|
-
# Product.count "sales > 1"
|
338
|
-
def count(conditions = nil)
|
339
|
-
sql = "SELECT COUNT(*) FROM #{table_name} "
|
340
|
-
add_conditions!(sql, conditions)
|
341
|
-
count_by_sql(sql)
|
342
|
-
end
|
343
644
|
|
344
645
|
# Returns the result of an SQL statement that should only include a COUNT(*) in the SELECT part.
|
345
|
-
#
|
646
|
+
# The use of this method should be restricted to complicated SQL queries that can't be executed
|
647
|
+
# using the ActiveRecord::Calculations class methods. Look into those before using this.
|
648
|
+
#
|
649
|
+
# ==== Options
|
650
|
+
#
|
651
|
+
# +sql+: An SQL statement which should return a count query from the database, see the example below
|
652
|
+
#
|
653
|
+
# ==== Examples
|
654
|
+
#
|
655
|
+
# Product.count_by_sql "SELECT COUNT(*) FROM sales s, customers c WHERE s.customer_id = c.id"
|
346
656
|
def count_by_sql(sql)
|
347
|
-
|
348
|
-
|
657
|
+
sql = sanitize_conditions(sql)
|
658
|
+
connection.select_value(sql, "#{name} Count").to_i
|
349
659
|
end
|
350
|
-
|
351
|
-
#
|
352
|
-
#
|
353
|
-
#
|
354
|
-
#
|
355
|
-
#
|
660
|
+
|
661
|
+
# A generic "counter updater" implementation, intended primarily to be
|
662
|
+
# used by increment_counter and decrement_counter, but which may also
|
663
|
+
# be useful on its own. It simply does a direct SQL update for the record
|
664
|
+
# with the given ID, altering the given hash of counters by the amount
|
665
|
+
# given by the corresponding value:
|
666
|
+
#
|
667
|
+
# ==== Options
|
668
|
+
#
|
669
|
+
# +id+ The id of the object you wish to update a counter on
|
670
|
+
# +counters+ An Array of Hashes containing the names of the fields
|
671
|
+
# to update as keys and the amount to update the field by as
|
672
|
+
# values
|
673
|
+
#
|
674
|
+
# ==== Examples
|
675
|
+
#
|
676
|
+
# # For the Post with id of 5, decrement the comment_count by 1, and
|
677
|
+
# # increment the action_count by 1
|
678
|
+
# Post.update_counters 5, :comment_count => -1, :action_count => 1
|
679
|
+
# # Executes the following SQL:
|
680
|
+
# # UPDATE posts
|
681
|
+
# # SET comment_count = comment_count - 1,
|
682
|
+
# # action_count = action_count + 1
|
683
|
+
# # WHERE id = 5
|
684
|
+
def update_counters(id, counters)
|
685
|
+
updates = counters.inject([]) { |list, (counter_name, increment)|
|
686
|
+
sign = increment < 0 ? "-" : "+"
|
687
|
+
list << "#{connection.quote_column_name(counter_name)} = #{connection.quote_column_name(counter_name)} #{sign} #{increment.abs}"
|
688
|
+
}.join(", ")
|
689
|
+
update_all(updates, "#{connection.quote_column_name(primary_key)} = #{quote_value(id)}")
|
690
|
+
end
|
691
|
+
|
692
|
+
# Increment a number field by one, usually representing a count.
|
693
|
+
#
|
694
|
+
# This is used for caching aggregate values, so that they don't need to be computed every time.
|
695
|
+
# For example, a DiscussionBoard may cache post_count and comment_count otherwise every time the board is
|
696
|
+
# shown it would have to run an SQL query to find how many posts and comments there are.
|
697
|
+
#
|
698
|
+
# ==== Options
|
699
|
+
#
|
700
|
+
# +counter_name+ The name of the field that should be incremented
|
701
|
+
# +id+ The id of the object that should be incremented
|
702
|
+
#
|
703
|
+
# ==== Examples
|
704
|
+
#
|
705
|
+
# # Increment the post_count column for the record with an id of 5
|
706
|
+
# DiscussionBoard.increment_counter(:post_count, 5)
|
356
707
|
def increment_counter(counter_name, id)
|
357
|
-
|
708
|
+
update_counters(id, counter_name => 1)
|
358
709
|
end
|
359
710
|
|
360
|
-
#
|
711
|
+
# Decrement a number field by one, usually representing a count.
|
712
|
+
#
|
713
|
+
# This works the same as increment_counter but reduces the column value by 1 instead of increasing it.
|
714
|
+
#
|
715
|
+
# ==== Options
|
716
|
+
#
|
717
|
+
# +counter_name+ The name of the field that should be decremented
|
718
|
+
# +id+ The id of the object that should be decremented
|
719
|
+
#
|
720
|
+
# ==== Examples
|
721
|
+
#
|
722
|
+
# # Decrement the post_count column for the record with an id of 5
|
723
|
+
# DiscussionBoard.decrement_counter(:post_count, 5)
|
361
724
|
def decrement_counter(counter_name, id)
|
362
|
-
|
725
|
+
update_counters(id, counter_name => -1)
|
363
726
|
end
|
364
727
|
|
365
|
-
|
728
|
+
|
729
|
+
# Attributes named in this macro are protected from mass-assignment, such as <tt>new(attributes)</tt> and
|
366
730
|
# <tt>attributes=(attributes)</tt>. Their assignment will simply be ignored. Instead, you can use the direct writer
|
367
|
-
# methods to do assignment. This is meant to protect sensitive attributes
|
731
|
+
# methods to do assignment. This is meant to protect sensitive attributes from being overwritten by URL/form hackers. Example:
|
368
732
|
#
|
369
733
|
# class Customer < ActiveRecord::Base
|
370
734
|
# attr_protected :credit_rating
|
@@ -377,334 +741,1362 @@ module ActiveRecord #:nodoc:
|
|
377
741
|
#
|
378
742
|
# customer.credit_rating = "Average"
|
379
743
|
# customer.credit_rating # => "Average"
|
744
|
+
#
|
745
|
+
# To start from an all-closed default and enable attributes as needed, have a look at attr_accessible.
|
380
746
|
def attr_protected(*attributes)
|
381
|
-
|
747
|
+
write_inheritable_attribute("attr_protected", Set.new(attributes.map(&:to_s)) + (protected_attributes || []))
|
382
748
|
end
|
383
|
-
|
384
|
-
# Returns an array of all the attributes that have been protected from mass-
|
749
|
+
|
750
|
+
# Returns an array of all the attributes that have been protected from mass-assignment.
|
385
751
|
def protected_attributes # :nodoc:
|
386
752
|
read_inheritable_attribute("attr_protected")
|
387
753
|
end
|
388
754
|
|
389
|
-
#
|
390
|
-
# <tt>new(attributes)</tt> and <tt>attributes=(attributes)</tt
|
391
|
-
#
|
392
|
-
#
|
755
|
+
# Similar to the attr_protected macro, this protects attributes of your model from mass-assignment,
|
756
|
+
# such as <tt>new(attributes)</tt> and <tt>attributes=(attributes)</tt>
|
757
|
+
# however, it does it in the opposite way. This locks all attributes and only allows access to the
|
758
|
+
# attributes specified. Assignment to attributes not in this list will be ignored and need to be set
|
759
|
+
# using the direct writer methods instead. This is meant to protect sensitive attributes from being
|
760
|
+
# overwritten by URL/form hackers. If you'd rather start from an all-open default and restrict
|
761
|
+
# attributes as needed, have a look at attr_protected.
|
762
|
+
#
|
763
|
+
# ==== Options
|
764
|
+
#
|
765
|
+
# <tt>*attributes</tt> A comma separated list of symbols that represent columns _not_ to be protected
|
766
|
+
#
|
767
|
+
# ==== Examples
|
768
|
+
#
|
769
|
+
# class Customer < ActiveRecord::Base
|
770
|
+
# attr_accessible :name, :nickname
|
771
|
+
# end
|
772
|
+
#
|
773
|
+
# customer = Customer.new(:name => "David", :nickname => "Dave", :credit_rating => "Excellent")
|
774
|
+
# customer.credit_rating # => nil
|
775
|
+
# customer.attributes = { :name => "Jolly fellow", :credit_rating => "Superb" }
|
776
|
+
# customer.credit_rating # => nil
|
777
|
+
#
|
778
|
+
# customer.credit_rating = "Average"
|
779
|
+
# customer.credit_rating # => "Average"
|
393
780
|
def attr_accessible(*attributes)
|
394
|
-
|
781
|
+
write_inheritable_attribute("attr_accessible", Set.new(attributes.map(&:to_s)) + (accessible_attributes || []))
|
395
782
|
end
|
396
|
-
|
397
|
-
# Returns an array of all the attributes that have been made accessible to mass-
|
783
|
+
|
784
|
+
# Returns an array of all the attributes that have been made accessible to mass-assignment.
|
398
785
|
def accessible_attributes # :nodoc:
|
399
786
|
read_inheritable_attribute("attr_accessible")
|
400
787
|
end
|
401
788
|
|
402
|
-
|
403
|
-
|
404
|
-
|
789
|
+
# Attributes listed as readonly can be set for a new record, but will be ignored in database updates afterwards.
|
790
|
+
def attr_readonly(*attributes)
|
791
|
+
write_inheritable_attribute("attr_readonly", Set.new(attributes.map(&:to_s)) + (readonly_attributes || []))
|
792
|
+
end
|
793
|
+
|
794
|
+
# Returns an array of all the attributes that have been specified as readonly.
|
795
|
+
def readonly_attributes
|
796
|
+
read_inheritable_attribute("attr_readonly")
|
797
|
+
end
|
798
|
+
|
799
|
+
# If you have an attribute that needs to be saved to the database as an object, and retrieved as the same object,
|
800
|
+
# then specify the name of that attribute using this method and it will be handled automatically.
|
801
|
+
# The serialization is done through YAML. If +class_name+ is specified, the serialized object must be of that
|
802
|
+
# class on retrieval or +SerializationTypeMismatch+ will be raised.
|
803
|
+
#
|
804
|
+
# ==== Options
|
805
|
+
#
|
806
|
+
# +attr_name+ The field name that should be serialized
|
807
|
+
# +class_name+ Optional, class name that the object type should be equal to
|
808
|
+
#
|
809
|
+
# ==== Example
|
810
|
+
# # Serialize a preferences attribute
|
811
|
+
# class User
|
812
|
+
# serialize :preferences
|
813
|
+
# end
|
405
814
|
def serialize(attr_name, class_name = Object)
|
406
|
-
|
815
|
+
serialized_attributes[attr_name.to_s] = class_name
|
407
816
|
end
|
408
|
-
|
817
|
+
|
409
818
|
# Returns a hash of all the attributes that have been specified for serialization as keys and their class restriction as values.
|
410
819
|
def serialized_attributes
|
411
|
-
read_inheritable_attribute("attr_serialized")
|
820
|
+
read_inheritable_attribute("attr_serialized") or write_inheritable_attribute("attr_serialized", {})
|
821
|
+
end
|
822
|
+
|
823
|
+
|
824
|
+
# Guesses the table name (in forced lower-case) based on the name of the class in the inheritance hierarchy descending
|
825
|
+
# directly from ActiveRecord. So if the hierarchy looks like: Reply < Message < ActiveRecord, then Message is used
|
826
|
+
# to guess the table name even when called on Reply. The rules used to do the guess are handled by the Inflector class
|
827
|
+
# in Active Support, which knows almost all common English inflections. You can add new inflections in config/initializers/inflections.rb.
|
828
|
+
#
|
829
|
+
# Nested classes are given table names prefixed by the singular form of
|
830
|
+
# the parent's table name. Enclosing modules are not considered. Examples:
|
831
|
+
#
|
832
|
+
# class Invoice < ActiveRecord::Base; end;
|
833
|
+
# file class table_name
|
834
|
+
# invoice.rb Invoice invoices
|
835
|
+
#
|
836
|
+
# class Invoice < ActiveRecord::Base; class Lineitem < ActiveRecord::Base; end; end;
|
837
|
+
# file class table_name
|
838
|
+
# invoice.rb Invoice::Lineitem invoice_lineitems
|
839
|
+
#
|
840
|
+
# module Invoice; class Lineitem < ActiveRecord::Base; end; end;
|
841
|
+
# file class table_name
|
842
|
+
# invoice/lineitem.rb Invoice::Lineitem lineitems
|
843
|
+
#
|
844
|
+
# Additionally, the class-level table_name_prefix is prepended and the
|
845
|
+
# table_name_suffix is appended. So if you have "myapp_" as a prefix,
|
846
|
+
# the table name guess for an Invoice class becomes "myapp_invoices".
|
847
|
+
# Invoice::Lineitem becomes "myapp_invoice_lineitems".
|
848
|
+
#
|
849
|
+
# You can also overwrite this class method to allow for unguessable
|
850
|
+
# links, such as a Mouse class with a link to a "mice" table. Example:
|
851
|
+
#
|
852
|
+
# class Mouse < ActiveRecord::Base
|
853
|
+
# set_table_name "mice"
|
854
|
+
# end
|
855
|
+
def table_name
|
856
|
+
reset_table_name
|
857
|
+
end
|
858
|
+
|
859
|
+
def reset_table_name #:nodoc:
|
860
|
+
base = base_class
|
861
|
+
|
862
|
+
name =
|
863
|
+
# STI subclasses always use their superclass' table.
|
864
|
+
unless self == base
|
865
|
+
base.table_name
|
866
|
+
else
|
867
|
+
# Nested classes are prefixed with singular parent table name.
|
868
|
+
if parent < ActiveRecord::Base && !parent.abstract_class?
|
869
|
+
contained = parent.table_name
|
870
|
+
contained = contained.singularize if parent.pluralize_table_names
|
871
|
+
contained << '_'
|
872
|
+
end
|
873
|
+
name = "#{table_name_prefix}#{contained}#{undecorated_table_name(base.name)}#{table_name_suffix}"
|
874
|
+
end
|
875
|
+
|
876
|
+
set_table_name(name)
|
877
|
+
name
|
878
|
+
end
|
879
|
+
|
880
|
+
# Defines the primary key field -- can be overridden in subclasses. Overwriting will negate any effect of the
|
881
|
+
# primary_key_prefix_type setting, though.
|
882
|
+
def primary_key
|
883
|
+
reset_primary_key
|
884
|
+
end
|
885
|
+
|
886
|
+
def reset_primary_key #:nodoc:
|
887
|
+
key = 'id'
|
888
|
+
case primary_key_prefix_type
|
889
|
+
when :table_name
|
890
|
+
key = Inflector.foreign_key(base_class.name, false)
|
891
|
+
when :table_name_with_underscore
|
892
|
+
key = Inflector.foreign_key(base_class.name)
|
893
|
+
end
|
894
|
+
set_primary_key(key)
|
895
|
+
key
|
896
|
+
end
|
897
|
+
|
898
|
+
# Defines the column name for use with single table inheritance
|
899
|
+
# -- can be set in subclasses like so: self.inheritance_column = "type_id"
|
900
|
+
def inheritance_column
|
901
|
+
@inheritance_column ||= "type".freeze
|
902
|
+
end
|
903
|
+
|
904
|
+
# Lazy-set the sequence name to the connection's default. This method
|
905
|
+
# is only ever called once since set_sequence_name overrides it.
|
906
|
+
def sequence_name #:nodoc:
|
907
|
+
reset_sequence_name
|
908
|
+
end
|
909
|
+
|
910
|
+
def reset_sequence_name #:nodoc:
|
911
|
+
default = connection.default_sequence_name(table_name, primary_key)
|
912
|
+
set_sequence_name(default)
|
913
|
+
default
|
914
|
+
end
|
915
|
+
|
916
|
+
# Sets the table name to use to the given value, or (if the value
|
917
|
+
# is nil or false) to the value returned by the given block.
|
918
|
+
#
|
919
|
+
# Example:
|
920
|
+
#
|
921
|
+
# class Project < ActiveRecord::Base
|
922
|
+
# set_table_name "project"
|
923
|
+
# end
|
924
|
+
def set_table_name(value = nil, &block)
|
925
|
+
define_attr_method :table_name, value, &block
|
926
|
+
end
|
927
|
+
alias :table_name= :set_table_name
|
928
|
+
|
929
|
+
# Sets the name of the primary key column to use to the given value,
|
930
|
+
# or (if the value is nil or false) to the value returned by the given
|
931
|
+
# block.
|
932
|
+
#
|
933
|
+
# Example:
|
934
|
+
#
|
935
|
+
# class Project < ActiveRecord::Base
|
936
|
+
# set_primary_key "sysid"
|
937
|
+
# end
|
938
|
+
def set_primary_key(value = nil, &block)
|
939
|
+
define_attr_method :primary_key, value, &block
|
940
|
+
end
|
941
|
+
alias :primary_key= :set_primary_key
|
942
|
+
|
943
|
+
# Sets the name of the inheritance column to use to the given value,
|
944
|
+
# or (if the value # is nil or false) to the value returned by the
|
945
|
+
# given block.
|
946
|
+
#
|
947
|
+
# Example:
|
948
|
+
#
|
949
|
+
# class Project < ActiveRecord::Base
|
950
|
+
# set_inheritance_column do
|
951
|
+
# original_inheritance_column + "_id"
|
952
|
+
# end
|
953
|
+
# end
|
954
|
+
def set_inheritance_column(value = nil, &block)
|
955
|
+
define_attr_method :inheritance_column, value, &block
|
956
|
+
end
|
957
|
+
alias :inheritance_column= :set_inheritance_column
|
958
|
+
|
959
|
+
# Sets the name of the sequence to use when generating ids to the given
|
960
|
+
# value, or (if the value is nil or false) to the value returned by the
|
961
|
+
# given block. This is required for Oracle and is useful for any
|
962
|
+
# database which relies on sequences for primary key generation.
|
963
|
+
#
|
964
|
+
# If a sequence name is not explicitly set when using Oracle or Firebird,
|
965
|
+
# it will default to the commonly used pattern of: #{table_name}_seq
|
966
|
+
#
|
967
|
+
# If a sequence name is not explicitly set when using PostgreSQL, it
|
968
|
+
# will discover the sequence corresponding to your primary key for you.
|
969
|
+
#
|
970
|
+
# Example:
|
971
|
+
#
|
972
|
+
# class Project < ActiveRecord::Base
|
973
|
+
# set_sequence_name "projectseq" # default would have been "project_seq"
|
974
|
+
# end
|
975
|
+
def set_sequence_name(value = nil, &block)
|
976
|
+
define_attr_method :sequence_name, value, &block
|
977
|
+
end
|
978
|
+
alias :sequence_name= :set_sequence_name
|
979
|
+
|
980
|
+
# Turns the +table_name+ back into a class name following the reverse rules of +table_name+.
|
981
|
+
def class_name(table_name = table_name) # :nodoc:
|
982
|
+
# remove any prefix and/or suffix from the table name
|
983
|
+
class_name = table_name[table_name_prefix.length..-(table_name_suffix.length + 1)].camelize
|
984
|
+
class_name = class_name.singularize if pluralize_table_names
|
985
|
+
class_name
|
986
|
+
end
|
987
|
+
|
988
|
+
# Indicates whether the table associated with this class exists
|
989
|
+
def table_exists?
|
990
|
+
if connection.respond_to?(:tables)
|
991
|
+
connection.tables.include? table_name
|
992
|
+
else
|
993
|
+
# if the connection adapter hasn't implemented tables, there are two crude tests that can be
|
994
|
+
# used - see if getting column info raises an error, or if the number of columns returned is zero
|
995
|
+
begin
|
996
|
+
reset_column_information
|
997
|
+
columns.size > 0
|
998
|
+
rescue ActiveRecord::StatementInvalid
|
999
|
+
false
|
1000
|
+
end
|
1001
|
+
end
|
1002
|
+
end
|
1003
|
+
|
1004
|
+
# Returns an array of column objects for the table associated with this class.
|
1005
|
+
def columns
|
1006
|
+
unless @columns
|
1007
|
+
@columns = connection.columns(table_name, "#{name} Columns")
|
1008
|
+
@columns.each {|column| column.primary = column.name == primary_key}
|
1009
|
+
end
|
1010
|
+
@columns
|
1011
|
+
end
|
1012
|
+
|
1013
|
+
# Returns a hash of column objects for the table associated with this class.
|
1014
|
+
def columns_hash
|
1015
|
+
@columns_hash ||= columns.inject({}) { |hash, column| hash[column.name] = column; hash }
|
1016
|
+
end
|
1017
|
+
|
1018
|
+
# Returns an array of column names as strings.
|
1019
|
+
def column_names
|
1020
|
+
@column_names ||= columns.map { |column| column.name }
|
1021
|
+
end
|
1022
|
+
|
1023
|
+
# Returns an array of column objects where the primary id, all columns ending in "_id" or "_count",
|
1024
|
+
# and columns used for single table inheritance have been removed.
|
1025
|
+
def content_columns
|
1026
|
+
@content_columns ||= columns.reject { |c| c.primary || c.name =~ /(_id|_count)$/ || c.name == inheritance_column }
|
1027
|
+
end
|
1028
|
+
|
1029
|
+
# Returns a hash of all the methods added to query each of the columns in the table with the name of the method as the key
|
1030
|
+
# 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
|
1031
|
+
# is available.
|
1032
|
+
def column_methods_hash #:nodoc:
|
1033
|
+
@dynamic_methods_hash ||= column_names.inject(Hash.new(false)) do |methods, attr|
|
1034
|
+
attr_name = attr.to_s
|
1035
|
+
methods[attr.to_sym] = attr_name
|
1036
|
+
methods["#{attr}=".to_sym] = attr_name
|
1037
|
+
methods["#{attr}?".to_sym] = attr_name
|
1038
|
+
methods["#{attr}_before_type_cast".to_sym] = attr_name
|
1039
|
+
methods
|
1040
|
+
end
|
1041
|
+
end
|
1042
|
+
|
1043
|
+
# Resets all the cached information about columns, which will cause them to be reloaded on the next request.
|
1044
|
+
def reset_column_information
|
1045
|
+
generated_methods.each { |name| undef_method(name) }
|
1046
|
+
@column_names = @columns = @columns_hash = @content_columns = @dynamic_methods_hash = @generated_methods = @inheritance_column = nil
|
1047
|
+
end
|
1048
|
+
|
1049
|
+
def reset_column_information_and_inheritable_attributes_for_all_subclasses#:nodoc:
|
1050
|
+
subclasses.each { |klass| klass.reset_inheritable_attributes; klass.reset_column_information }
|
1051
|
+
end
|
1052
|
+
|
1053
|
+
# Transforms attribute key names into a more humane format, such as "First name" instead of "first_name". Example:
|
1054
|
+
# Person.human_attribute_name("first_name") # => "First name"
|
1055
|
+
# Deprecated in favor of just calling "first_name".humanize
|
1056
|
+
def human_attribute_name(attribute_key_name) #:nodoc:
|
1057
|
+
attribute_key_name.humanize
|
1058
|
+
end
|
1059
|
+
|
1060
|
+
# True if this isn't a concrete subclass needing a STI type condition.
|
1061
|
+
def descends_from_active_record?
|
1062
|
+
if superclass.abstract_class?
|
1063
|
+
superclass.descends_from_active_record?
|
1064
|
+
else
|
1065
|
+
superclass == Base || !columns_hash.include?(inheritance_column)
|
1066
|
+
end
|
1067
|
+
end
|
1068
|
+
|
1069
|
+
def finder_needs_type_condition? #:nodoc:
|
1070
|
+
# This is like this because benchmarking justifies the strange :false stuff
|
1071
|
+
:true == (@finder_needs_type_condition ||= descends_from_active_record? ? :false : :true)
|
1072
|
+
end
|
1073
|
+
|
1074
|
+
# Returns a string like 'Post id:integer, title:string, body:text'
|
1075
|
+
def inspect
|
1076
|
+
if self == Base
|
1077
|
+
super
|
1078
|
+
elsif abstract_class?
|
1079
|
+
"#{super}(abstract)"
|
1080
|
+
elsif table_exists?
|
1081
|
+
attr_list = columns.map { |c| "#{c.name}: #{c.type}" } * ', '
|
1082
|
+
"#{super}(#{attr_list})"
|
1083
|
+
else
|
1084
|
+
"#{super}(Table doesn't exist)"
|
1085
|
+
end
|
1086
|
+
end
|
1087
|
+
|
1088
|
+
|
1089
|
+
def quote_value(value, column = nil) #:nodoc:
|
1090
|
+
connection.quote(value,column)
|
1091
|
+
end
|
1092
|
+
|
1093
|
+
# Used to sanitize objects before they're used in an SQL SELECT statement. Delegates to <tt>connection.quote</tt>.
|
1094
|
+
def sanitize(object) #:nodoc:
|
1095
|
+
connection.quote(object)
|
1096
|
+
end
|
1097
|
+
|
1098
|
+
# Log and benchmark multiple statements in a single block. Example:
|
1099
|
+
#
|
1100
|
+
# Project.benchmark("Creating project") do
|
1101
|
+
# project = Project.create("name" => "stuff")
|
1102
|
+
# project.create_manager("name" => "David")
|
1103
|
+
# project.milestones << Milestone.find(:all)
|
1104
|
+
# end
|
1105
|
+
#
|
1106
|
+
# The benchmark is only recorded if the current level of the logger matches the <tt>log_level</tt>, which makes it
|
1107
|
+
# easy to include benchmarking statements in production software that will remain inexpensive because the benchmark
|
1108
|
+
# will only be conducted if the log level is low enough.
|
1109
|
+
#
|
1110
|
+
# The logging of the multiple statements is turned off unless <tt>use_silence</tt> is set to false.
|
1111
|
+
def benchmark(title, log_level = Logger::DEBUG, use_silence = true)
|
1112
|
+
if logger && logger.level == log_level
|
1113
|
+
result = nil
|
1114
|
+
seconds = Benchmark.realtime { result = use_silence ? silence { yield } : yield }
|
1115
|
+
logger.add(log_level, "#{title} (#{'%.5f' % seconds})")
|
1116
|
+
result
|
1117
|
+
else
|
1118
|
+
yield
|
1119
|
+
end
|
1120
|
+
end
|
1121
|
+
|
1122
|
+
# Silences the logger for the duration of the block.
|
1123
|
+
def silence
|
1124
|
+
old_logger_level, logger.level = logger.level, Logger::ERROR if logger
|
1125
|
+
yield
|
1126
|
+
ensure
|
1127
|
+
logger.level = old_logger_level if logger
|
1128
|
+
end
|
1129
|
+
|
1130
|
+
# Overwrite the default class equality method to provide support for association proxies.
|
1131
|
+
def ===(object)
|
1132
|
+
object.is_a?(self)
|
1133
|
+
end
|
1134
|
+
|
1135
|
+
# Returns the base AR subclass that this class descends from. If A
|
1136
|
+
# extends AR::Base, A.base_class will return A. If B descends from A
|
1137
|
+
# through some arbitrarily deep hierarchy, B.base_class will return A.
|
1138
|
+
def base_class
|
1139
|
+
class_of_active_record_descendant(self)
|
412
1140
|
end
|
413
1141
|
|
414
|
-
#
|
415
|
-
|
416
|
-
|
417
|
-
#
|
418
|
-
#
|
419
|
-
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
|
428
|
-
|
429
|
-
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
|
440
|
-
|
441
|
-
|
442
|
-
|
443
|
-
|
444
|
-
|
445
|
-
|
1142
|
+
# Set this to true if this is an abstract class (see #abstract_class?).
|
1143
|
+
attr_accessor :abstract_class
|
1144
|
+
|
1145
|
+
# Returns whether this class is a base AR class. If A is a base class and
|
1146
|
+
# B descends from A, then B.base_class will return B.
|
1147
|
+
def abstract_class?
|
1148
|
+
abstract_class == true
|
1149
|
+
end
|
1150
|
+
|
1151
|
+
private
|
1152
|
+
def find_initial(options)
|
1153
|
+
options.update(:limit => 1) unless options[:include]
|
1154
|
+
find_every(options).first
|
1155
|
+
end
|
1156
|
+
|
1157
|
+
def find_every(options)
|
1158
|
+
records = scoped?(:find, :include) || options[:include] ?
|
1159
|
+
find_with_associations(options) :
|
1160
|
+
find_by_sql(construct_finder_sql(options))
|
1161
|
+
|
1162
|
+
records.each { |record| record.readonly! } if options[:readonly]
|
1163
|
+
|
1164
|
+
records
|
1165
|
+
end
|
1166
|
+
|
1167
|
+
def find_from_ids(ids, options)
|
1168
|
+
expects_array = ids.first.kind_of?(Array)
|
1169
|
+
return ids.first if expects_array && ids.first.empty?
|
1170
|
+
|
1171
|
+
ids = ids.flatten.compact.uniq
|
1172
|
+
|
1173
|
+
case ids.size
|
1174
|
+
when 0
|
1175
|
+
raise RecordNotFound, "Couldn't find #{name} without an ID"
|
1176
|
+
when 1
|
1177
|
+
result = find_one(ids.first, options)
|
1178
|
+
expects_array ? [ result ] : result
|
1179
|
+
else
|
1180
|
+
find_some(ids, options)
|
1181
|
+
end
|
1182
|
+
end
|
1183
|
+
|
1184
|
+
def find_one(id, options)
|
1185
|
+
conditions = " AND (#{sanitize_sql(options[:conditions])})" if options[:conditions]
|
1186
|
+
options.update :conditions => "#{quoted_table_name}.#{connection.quote_column_name(primary_key)} = #{quote_value(id,columns_hash[primary_key])}#{conditions}"
|
1187
|
+
|
1188
|
+
# Use find_every(options).first since the primary key condition
|
1189
|
+
# already ensures we have a single record. Using find_initial adds
|
1190
|
+
# a superfluous :limit => 1.
|
1191
|
+
if result = find_every(options).first
|
1192
|
+
result
|
1193
|
+
else
|
1194
|
+
raise RecordNotFound, "Couldn't find #{name} with ID=#{id}#{conditions}"
|
1195
|
+
end
|
1196
|
+
end
|
1197
|
+
|
1198
|
+
def find_some(ids, options)
|
1199
|
+
conditions = " AND (#{sanitize_sql(options[:conditions])})" if options[:conditions]
|
1200
|
+
ids_list = ids.map { |id| quote_value(id,columns_hash[primary_key]) }.join(',')
|
1201
|
+
options.update :conditions => "#{quoted_table_name}.#{connection.quote_column_name(primary_key)} IN (#{ids_list})#{conditions}"
|
1202
|
+
|
1203
|
+
result = find_every(options)
|
1204
|
+
|
1205
|
+
# Determine expected size from limit and offset, not just ids.size.
|
1206
|
+
expected_size =
|
1207
|
+
if options[:limit] && ids.size > options[:limit]
|
1208
|
+
options[:limit]
|
1209
|
+
else
|
1210
|
+
ids.size
|
1211
|
+
end
|
1212
|
+
|
1213
|
+
# 11 ids with limit 3, offset 9 should give 2 results.
|
1214
|
+
if options[:offset] && (ids.size - options[:offset] < expected_size)
|
1215
|
+
expected_size = ids.size - options[:offset]
|
1216
|
+
end
|
1217
|
+
|
1218
|
+
if result.size == expected_size
|
1219
|
+
result
|
1220
|
+
else
|
1221
|
+
raise RecordNotFound, "Couldn't find all #{name.pluralize} with IDs (#{ids_list})#{conditions} (found #{result.size} results, but was looking for #{expected_size})"
|
1222
|
+
end
|
1223
|
+
end
|
1224
|
+
|
1225
|
+
# Finder methods must instantiate through this method to work with the
|
1226
|
+
# single-table inheritance model that makes it possible to create
|
1227
|
+
# objects of different types from the same table.
|
1228
|
+
def instantiate(record)
|
1229
|
+
object =
|
1230
|
+
if subclass_name = record[inheritance_column]
|
1231
|
+
# No type given.
|
1232
|
+
if subclass_name.empty?
|
1233
|
+
allocate
|
1234
|
+
|
1235
|
+
else
|
1236
|
+
# Ignore type if no column is present since it was probably
|
1237
|
+
# pulled in from a sloppy join.
|
1238
|
+
unless columns_hash.include?(inheritance_column)
|
1239
|
+
allocate
|
1240
|
+
|
1241
|
+
else
|
1242
|
+
begin
|
1243
|
+
compute_type(subclass_name).allocate
|
1244
|
+
rescue NameError
|
1245
|
+
raise SubclassNotFound,
|
1246
|
+
"The single-table inheritance mechanism failed to locate the subclass: '#{record[inheritance_column]}'. " +
|
1247
|
+
"This error is raised because the column '#{inheritance_column}' is reserved for storing the class in case of inheritance. " +
|
1248
|
+
"Please rename this column if you didn't intend it to be used for storing the inheritance class " +
|
1249
|
+
"or overwrite #{self.to_s}.inheritance_column to use another column for that information."
|
1250
|
+
end
|
1251
|
+
end
|
1252
|
+
end
|
1253
|
+
else
|
1254
|
+
allocate
|
1255
|
+
end
|
1256
|
+
|
1257
|
+
object.instance_variable_set("@attributes", record)
|
1258
|
+
object.instance_variable_set("@attributes_cache", Hash.new)
|
1259
|
+
|
1260
|
+
if object.respond_to_without_attributes?(:after_find)
|
1261
|
+
object.send(:callback, :after_find)
|
1262
|
+
end
|
1263
|
+
|
1264
|
+
if object.respond_to_without_attributes?(:after_initialize)
|
1265
|
+
object.send(:callback, :after_initialize)
|
1266
|
+
end
|
1267
|
+
|
1268
|
+
object
|
1269
|
+
end
|
1270
|
+
|
1271
|
+
# Nest the type name in the same module as this class.
|
1272
|
+
# Bar is "MyApp::Business::Bar" relative to MyApp::Business::Foo
|
1273
|
+
def type_name_with_module(type_name)
|
1274
|
+
(/^::/ =~ type_name) ? type_name : "#{parent.name}::#{type_name}"
|
1275
|
+
end
|
1276
|
+
|
1277
|
+
def construct_finder_sql(options)
|
1278
|
+
scope = scope(:find)
|
1279
|
+
sql = "SELECT #{(scope && scope[:select]) || options[:select] || (options[:joins] && quoted_table_name + '.*') || '*'} "
|
1280
|
+
sql << "FROM #{(scope && scope[:from]) || options[:from] || quoted_table_name} "
|
1281
|
+
|
1282
|
+
add_joins!(sql, options, scope)
|
1283
|
+
add_conditions!(sql, options[:conditions], scope)
|
1284
|
+
|
1285
|
+
add_group!(sql, options[:group], scope)
|
1286
|
+
add_order!(sql, options[:order], scope)
|
1287
|
+
add_limit!(sql, options, scope)
|
1288
|
+
add_lock!(sql, options, scope)
|
1289
|
+
|
1290
|
+
sql
|
1291
|
+
end
|
1292
|
+
|
1293
|
+
# Merges includes so that the result is a valid +include+
|
1294
|
+
def merge_includes(first, second)
|
1295
|
+
(safe_to_array(first) + safe_to_array(second)).uniq
|
1296
|
+
end
|
1297
|
+
|
1298
|
+
# Object#to_a is deprecated, though it does have the desired behavior
|
1299
|
+
def safe_to_array(o)
|
1300
|
+
case o
|
1301
|
+
when NilClass
|
1302
|
+
[]
|
1303
|
+
when Array
|
1304
|
+
o
|
1305
|
+
else
|
1306
|
+
[o]
|
1307
|
+
end
|
1308
|
+
end
|
1309
|
+
|
1310
|
+
def add_order!(sql, order, scope = :auto)
|
1311
|
+
scope = scope(:find) if :auto == scope
|
1312
|
+
scoped_order = scope[:order] if scope
|
1313
|
+
if order
|
1314
|
+
sql << " ORDER BY #{order}"
|
1315
|
+
sql << ", #{scoped_order}" if scoped_order
|
1316
|
+
else
|
1317
|
+
sql << " ORDER BY #{scoped_order}" if scoped_order
|
1318
|
+
end
|
1319
|
+
end
|
1320
|
+
|
1321
|
+
def add_group!(sql, group, scope = :auto)
|
1322
|
+
if group
|
1323
|
+
sql << " GROUP BY #{group}"
|
1324
|
+
else
|
1325
|
+
scope = scope(:find) if :auto == scope
|
1326
|
+
if scope && (scoped_group = scope[:group])
|
1327
|
+
sql << " GROUP BY #{scoped_group}"
|
1328
|
+
end
|
1329
|
+
end
|
1330
|
+
end
|
1331
|
+
|
1332
|
+
# The optional scope argument is for the current :find scope.
|
1333
|
+
def add_limit!(sql, options, scope = :auto)
|
1334
|
+
scope = scope(:find) if :auto == scope
|
1335
|
+
|
1336
|
+
if scope
|
1337
|
+
options[:limit] ||= scope[:limit]
|
1338
|
+
options[:offset] ||= scope[:offset]
|
1339
|
+
end
|
1340
|
+
|
1341
|
+
connection.add_limit_offset!(sql, options)
|
1342
|
+
end
|
1343
|
+
|
1344
|
+
# The optional scope argument is for the current :find scope.
|
1345
|
+
# The :lock option has precedence over a scoped :lock.
|
1346
|
+
def add_lock!(sql, options, scope = :auto)
|
1347
|
+
scope = scope(:find) if :auto == scope
|
1348
|
+
options = options.reverse_merge(:lock => scope[:lock]) if scope
|
1349
|
+
connection.add_lock!(sql, options)
|
1350
|
+
end
|
1351
|
+
|
1352
|
+
# The optional scope argument is for the current :find scope.
|
1353
|
+
def add_joins!(sql, options, scope = :auto)
|
1354
|
+
scope = scope(:find) if :auto == scope
|
1355
|
+
join = (scope && scope[:joins]) || options[:joins]
|
1356
|
+
case join
|
1357
|
+
when Symbol, Hash, Array
|
1358
|
+
join_dependency = ActiveRecord::Associations::ClassMethods::InnerJoinDependency.new(self, join, nil)
|
1359
|
+
sql << " #{join_dependency.join_associations.collect{|join| join.association_join }.join} "
|
1360
|
+
else
|
1361
|
+
sql << " #{join} "
|
1362
|
+
end
|
1363
|
+
end
|
1364
|
+
|
1365
|
+
# Adds a sanitized version of +conditions+ to the +sql+ string. Note that the passed-in +sql+ string is changed.
|
1366
|
+
# The optional scope argument is for the current :find scope.
|
1367
|
+
def add_conditions!(sql, conditions, scope = :auto)
|
1368
|
+
scope = scope(:find) if :auto == scope
|
1369
|
+
segments = []
|
1370
|
+
segments << sanitize_sql(scope[:conditions]) if scope && !scope[:conditions].blank?
|
1371
|
+
segments << sanitize_sql(conditions) unless conditions.blank?
|
1372
|
+
segments << type_condition if finder_needs_type_condition?
|
1373
|
+
segments.delete_if{|s| s.blank?}
|
1374
|
+
sql << "WHERE (#{segments.join(") AND (")}) " unless segments.empty?
|
1375
|
+
end
|
1376
|
+
|
1377
|
+
def type_condition
|
1378
|
+
quoted_inheritance_column = connection.quote_column_name(inheritance_column)
|
1379
|
+
type_condition = subclasses.inject("#{quoted_table_name}.#{quoted_inheritance_column} = '#{name.demodulize}' ") do |condition, subclass|
|
1380
|
+
condition << "OR #{quoted_table_name}.#{quoted_inheritance_column} = '#{subclass.name.demodulize}' "
|
1381
|
+
end
|
1382
|
+
|
1383
|
+
" (#{type_condition}) "
|
1384
|
+
end
|
1385
|
+
|
1386
|
+
# Guesses the table name, but does not decorate it with prefix and suffix information.
|
1387
|
+
def undecorated_table_name(class_name = base_class.name)
|
1388
|
+
table_name = Inflector.underscore(Inflector.demodulize(class_name))
|
1389
|
+
table_name = Inflector.pluralize(table_name) if pluralize_table_names
|
1390
|
+
table_name
|
1391
|
+
end
|
1392
|
+
|
1393
|
+
# Enables dynamic finders like find_by_user_name(user_name) and find_by_user_name_and_password(user_name, password) that are turned into
|
1394
|
+
# find(:first, :conditions => ["user_name = ?", user_name]) and find(:first, :conditions => ["user_name = ? AND password = ?", user_name, password])
|
1395
|
+
# respectively. Also works for find(:all) by using find_all_by_amount(50) that is turned into find(:all, :conditions => ["amount = ?", 50]).
|
1396
|
+
#
|
1397
|
+
# It's even possible to use all the additional parameters to find. For example, the full interface for find_all_by_amount
|
1398
|
+
# is actually find_all_by_amount(amount, options).
|
1399
|
+
#
|
1400
|
+
# This also enables you to initialize a record if it is not found, such as find_or_initialize_by_amount(amount)
|
1401
|
+
# or find_or_create_by_user_and_password(user, password).
|
1402
|
+
#
|
1403
|
+
# Each dynamic finder or initializer/creator is also defined in the class after it is first invoked, so that future
|
1404
|
+
# attempts to use it do not run through method_missing.
|
1405
|
+
def method_missing(method_id, *arguments)
|
1406
|
+
if match = /^find_(all_by|by)_([_a-zA-Z]\w*)$/.match(method_id.to_s)
|
1407
|
+
finder = determine_finder(match)
|
1408
|
+
|
1409
|
+
attribute_names = extract_attribute_names_from_match(match)
|
1410
|
+
super unless all_attributes_exists?(attribute_names)
|
1411
|
+
|
1412
|
+
self.class_eval %{
|
1413
|
+
def self.#{method_id}(*args)
|
1414
|
+
options = args.last.is_a?(Hash) ? args.pop : {}
|
1415
|
+
attributes = construct_attributes_from_arguments([:#{attribute_names.join(',:')}], args)
|
1416
|
+
finder_options = { :conditions => attributes }
|
1417
|
+
validate_find_options(options)
|
1418
|
+
set_readonly_option!(options)
|
1419
|
+
|
1420
|
+
if options[:conditions]
|
1421
|
+
with_scope(:find => finder_options) do
|
1422
|
+
ActiveSupport::Deprecation.silence { send(:#{finder}, options) }
|
1423
|
+
end
|
1424
|
+
else
|
1425
|
+
ActiveSupport::Deprecation.silence { send(:#{finder}, options.merge(finder_options)) }
|
1426
|
+
end
|
1427
|
+
end
|
1428
|
+
}, __FILE__, __LINE__
|
1429
|
+
send(method_id, *arguments)
|
1430
|
+
elsif match = /^find_or_(initialize|create)_by_([_a-zA-Z]\w*)$/.match(method_id.to_s)
|
1431
|
+
instantiator = determine_instantiator(match)
|
1432
|
+
attribute_names = extract_attribute_names_from_match(match)
|
1433
|
+
super unless all_attributes_exists?(attribute_names)
|
1434
|
+
|
1435
|
+
self.class_eval %{
|
1436
|
+
def self.#{method_id}(*args)
|
1437
|
+
if args[0].is_a?(Hash)
|
1438
|
+
attributes = args[0].with_indifferent_access
|
1439
|
+
find_attributes = attributes.slice(*[:#{attribute_names.join(',:')}])
|
1440
|
+
else
|
1441
|
+
find_attributes = attributes = construct_attributes_from_arguments([:#{attribute_names.join(',:')}], args)
|
1442
|
+
end
|
1443
|
+
|
1444
|
+
options = { :conditions => find_attributes }
|
1445
|
+
set_readonly_option!(options)
|
1446
|
+
|
1447
|
+
record = find_initial(options)
|
1448
|
+
if record.nil?
|
1449
|
+
record = self.new { |r| r.send(:attributes=, attributes, false) }
|
1450
|
+
#{'record.save' if instantiator == :create}
|
1451
|
+
record
|
1452
|
+
else
|
1453
|
+
record
|
1454
|
+
end
|
1455
|
+
end
|
1456
|
+
}, __FILE__, __LINE__
|
1457
|
+
send(method_id, *arguments)
|
1458
|
+
else
|
1459
|
+
super
|
1460
|
+
end
|
1461
|
+
end
|
1462
|
+
|
1463
|
+
def determine_finder(match)
|
1464
|
+
match.captures.first == 'all_by' ? :find_every : :find_initial
|
1465
|
+
end
|
1466
|
+
|
1467
|
+
def determine_instantiator(match)
|
1468
|
+
match.captures.first == 'initialize' ? :new : :create
|
1469
|
+
end
|
1470
|
+
|
1471
|
+
def extract_attribute_names_from_match(match)
|
1472
|
+
match.captures.last.split('_and_')
|
1473
|
+
end
|
1474
|
+
|
1475
|
+
def construct_attributes_from_arguments(attribute_names, arguments)
|
1476
|
+
attributes = {}
|
1477
|
+
attribute_names.each_with_index { |name, idx| attributes[name] = arguments[idx] }
|
1478
|
+
attributes
|
1479
|
+
end
|
1480
|
+
|
1481
|
+
def all_attributes_exists?(attribute_names)
|
1482
|
+
attribute_names.all? { |name| column_methods_hash.include?(name.to_sym) }
|
1483
|
+
end
|
1484
|
+
|
1485
|
+
def attribute_condition(argument)
|
1486
|
+
case argument
|
1487
|
+
when nil then "IS ?"
|
1488
|
+
when Array, ActiveRecord::Associations::AssociationCollection then "IN (?)"
|
1489
|
+
when Range then "BETWEEN ? AND ?"
|
1490
|
+
else "= ?"
|
1491
|
+
end
|
446
1492
|
end
|
447
|
-
end
|
448
1493
|
|
449
|
-
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
|
454
|
-
|
455
|
-
|
456
|
-
|
1494
|
+
# Interpret Array and Hash as conditions and anything else as an id.
|
1495
|
+
def expand_id_conditions(id_or_conditions)
|
1496
|
+
case id_or_conditions
|
1497
|
+
when Array, Hash then id_or_conditions
|
1498
|
+
else sanitize_sql(primary_key => id_or_conditions)
|
1499
|
+
end
|
1500
|
+
end
|
1501
|
+
|
1502
|
+
|
1503
|
+
# Defines an "attribute" method (like #inheritance_column or
|
1504
|
+
# #table_name). A new (class) method will be created with the
|
1505
|
+
# given name. If a value is specified, the new method will
|
1506
|
+
# return that value (as a string). Otherwise, the given block
|
1507
|
+
# will be used to compute the value of the method.
|
1508
|
+
#
|
1509
|
+
# The original method will be aliased, with the new name being
|
1510
|
+
# prefixed with "original_". This allows the new method to
|
1511
|
+
# access the original value.
|
1512
|
+
#
|
1513
|
+
# Example:
|
1514
|
+
#
|
1515
|
+
# class A < ActiveRecord::Base
|
1516
|
+
# define_attr_method :primary_key, "sysid"
|
1517
|
+
# define_attr_method( :inheritance_column ) do
|
1518
|
+
# original_inheritance_column + "_id"
|
1519
|
+
# end
|
1520
|
+
# end
|
1521
|
+
def define_attr_method(name, value=nil, &block)
|
1522
|
+
sing = class << self; self; end
|
1523
|
+
sing.send :alias_method, "original_#{name}", name
|
1524
|
+
if block_given?
|
1525
|
+
sing.send :define_method, name, &block
|
457
1526
|
else
|
458
|
-
|
1527
|
+
# use eval instead of a block to work around a memory leak in dev
|
1528
|
+
# mode in fcgi
|
1529
|
+
sing.class_eval "def #{name}; #{value.to_s.inspect}; end"
|
1530
|
+
end
|
459
1531
|
end
|
460
|
-
end
|
461
1532
|
|
462
|
-
|
463
|
-
|
464
|
-
|
465
|
-
|
1533
|
+
protected
|
1534
|
+
# Scope parameters to method calls within the block. Takes a hash of method_name => parameters hash.
|
1535
|
+
# method_name may be :find or :create. :find parameters may include the <tt>:conditions</tt>, <tt>:joins</tt>,
|
1536
|
+
# <tt>:include</tt>, <tt>:offset</tt>, <tt>:limit</tt>, and <tt>:readonly</tt> options. :create parameters are an attributes hash.
|
1537
|
+
#
|
1538
|
+
# class Article < ActiveRecord::Base
|
1539
|
+
# def self.create_with_scope
|
1540
|
+
# with_scope(:find => { :conditions => "blog_id = 1" }, :create => { :blog_id => 1 }) do
|
1541
|
+
# find(1) # => SELECT * from articles WHERE blog_id = 1 AND id = 1
|
1542
|
+
# a = create(1)
|
1543
|
+
# a.blog_id # => 1
|
1544
|
+
# end
|
1545
|
+
# end
|
1546
|
+
# end
|
1547
|
+
#
|
1548
|
+
# In nested scopings, all previous parameters are overwritten by the innermost rule, with the exception of
|
1549
|
+
# :conditions and :include options in :find, which are merged.
|
1550
|
+
#
|
1551
|
+
# class Article < ActiveRecord::Base
|
1552
|
+
# def self.find_with_scope
|
1553
|
+
# with_scope(:find => { :conditions => "blog_id = 1", :limit => 1 }, :create => { :blog_id => 1 }) do
|
1554
|
+
# with_scope(:find => { :limit => 10})
|
1555
|
+
# find(:all) # => SELECT * from articles WHERE blog_id = 1 LIMIT 10
|
1556
|
+
# end
|
1557
|
+
# with_scope(:find => { :conditions => "author_id = 3" })
|
1558
|
+
# find(:all) # => SELECT * from articles WHERE blog_id = 1 AND author_id = 3 LIMIT 1
|
1559
|
+
# end
|
1560
|
+
# end
|
1561
|
+
# end
|
1562
|
+
# end
|
1563
|
+
#
|
1564
|
+
# You can ignore any previous scopings by using the <tt>with_exclusive_scope</tt> method.
|
1565
|
+
#
|
1566
|
+
# class Article < ActiveRecord::Base
|
1567
|
+
# def self.find_with_exclusive_scope
|
1568
|
+
# with_scope(:find => { :conditions => "blog_id = 1", :limit => 1 }) do
|
1569
|
+
# with_exclusive_scope(:find => { :limit => 10 })
|
1570
|
+
# find(:all) # => SELECT * from articles LIMIT 10
|
1571
|
+
# end
|
1572
|
+
# end
|
1573
|
+
# end
|
1574
|
+
# end
|
1575
|
+
def with_scope(method_scoping = {}, action = :merge, &block)
|
1576
|
+
method_scoping = method_scoping.method_scoping if method_scoping.respond_to?(:method_scoping)
|
1577
|
+
|
1578
|
+
# Dup first and second level of hash (method and params).
|
1579
|
+
method_scoping = method_scoping.inject({}) do |hash, (method, params)|
|
1580
|
+
hash[method] = (params == true) ? params : params.dup
|
1581
|
+
hash
|
1582
|
+
end
|
466
1583
|
|
467
|
-
|
468
|
-
def class_name(table_name = table_name) # :nodoc:
|
469
|
-
# remove any prefix and/or suffix from the table name
|
470
|
-
class_name = Inflector.camelize(table_name[table_name_prefix.length..-(table_name_suffix.length + 1)])
|
471
|
-
class_name = Inflector.singularize(class_name) if pluralize_table_names
|
472
|
-
return class_name
|
473
|
-
end
|
1584
|
+
method_scoping.assert_valid_keys([ :find, :create ])
|
474
1585
|
|
475
|
-
|
476
|
-
|
477
|
-
|
478
|
-
|
479
|
-
|
480
|
-
# Returns an array of column objects for the table associated with this class.
|
481
|
-
def columns_hash
|
482
|
-
@columns_hash ||= columns.inject({}) { |hash, column| hash[column.name] = column; hash }
|
483
|
-
end
|
1586
|
+
if f = method_scoping[:find]
|
1587
|
+
f.assert_valid_keys(VALID_FIND_OPTIONS)
|
1588
|
+
set_readonly_option! f
|
1589
|
+
end
|
484
1590
|
|
485
|
-
|
486
|
-
|
487
|
-
|
488
|
-
|
489
|
-
|
1591
|
+
# Merge scopings
|
1592
|
+
if action == :merge && current_scoped_methods
|
1593
|
+
method_scoping = current_scoped_methods.inject(method_scoping) do |hash, (method, params)|
|
1594
|
+
case hash[method]
|
1595
|
+
when Hash
|
1596
|
+
if method == :find
|
1597
|
+
(hash[method].keys + params.keys).uniq.each do |key|
|
1598
|
+
merge = hash[method][key] && params[key] # merge if both scopes have the same key
|
1599
|
+
if key == :conditions && merge
|
1600
|
+
hash[method][key] = [params[key], hash[method][key]].collect{ |sql| "( %s )" % sanitize_sql(sql) }.join(" AND ")
|
1601
|
+
elsif key == :include && merge
|
1602
|
+
hash[method][key] = merge_includes(hash[method][key], params[key]).uniq
|
1603
|
+
else
|
1604
|
+
hash[method][key] = hash[method][key] || params[key]
|
1605
|
+
end
|
1606
|
+
end
|
1607
|
+
else
|
1608
|
+
hash[method] = params.merge(hash[method])
|
1609
|
+
end
|
1610
|
+
else
|
1611
|
+
hash[method] = params
|
1612
|
+
end
|
1613
|
+
hash
|
1614
|
+
end
|
1615
|
+
end
|
490
1616
|
|
491
|
-
|
492
|
-
# Person.human_attribute_name("first_name") # => "First name"
|
493
|
-
def human_attribute_name(attribute_key_name)
|
494
|
-
attribute_key_name.gsub(/_/, " ").capitalize unless attribute_key_name.nil?
|
495
|
-
end
|
496
|
-
|
497
|
-
def descents_from_active_record? # :nodoc:
|
498
|
-
superclass == Base
|
499
|
-
end
|
1617
|
+
self.scoped_methods << method_scoping
|
500
1618
|
|
501
|
-
|
502
|
-
|
503
|
-
|
504
|
-
|
505
|
-
|
1619
|
+
begin
|
1620
|
+
yield
|
1621
|
+
ensure
|
1622
|
+
self.scoped_methods.pop
|
1623
|
+
end
|
1624
|
+
end
|
506
1625
|
|
507
|
-
|
508
|
-
|
509
|
-
|
510
|
-
|
511
|
-
# project = Project.create("name" => "stuff")
|
512
|
-
# project.create_manager("name" => "David")
|
513
|
-
# project.milestones << Milestone.find_all
|
514
|
-
# end
|
515
|
-
def benchmark(title)
|
516
|
-
logger.level = Logger::ERROR
|
517
|
-
bm = Benchmark.measure { yield }
|
518
|
-
logger.level = Logger::DEBUG
|
519
|
-
logger.info "#{title} (#{sprintf("%f", bm.real)})"
|
520
|
-
end
|
1626
|
+
# Works like with_scope, but discards any nested properties.
|
1627
|
+
def with_exclusive_scope(method_scoping = {}, &block)
|
1628
|
+
with_scope(method_scoping, :overwrite, &block)
|
1629
|
+
end
|
521
1630
|
|
522
|
-
|
523
|
-
|
524
|
-
|
525
|
-
def instantiate(record)
|
526
|
-
object = record_with_type?(record) ? compute_type(record[inheritance_column]).allocate : allocate
|
527
|
-
object.instance_variable_set("@attributes", record)
|
528
|
-
return object
|
1631
|
+
def subclasses #:nodoc:
|
1632
|
+
@@subclasses[self] ||= []
|
1633
|
+
@@subclasses[self] + extra = @@subclasses[self].inject([]) {|list, subclass| list + subclass.subclasses }
|
529
1634
|
end
|
530
|
-
|
531
|
-
#
|
532
|
-
def
|
533
|
-
|
534
|
-
|
1635
|
+
|
1636
|
+
# Test whether the given method and optional key are scoped.
|
1637
|
+
def scoped?(method, key = nil) #:nodoc:
|
1638
|
+
if current_scoped_methods && (scope = current_scoped_methods[method])
|
1639
|
+
!key || scope.has_key?(key)
|
1640
|
+
end
|
535
1641
|
end
|
536
|
-
|
537
|
-
#
|
538
|
-
|
539
|
-
|
540
|
-
|
1642
|
+
|
1643
|
+
# Retrieve the scope for the given method and optional key.
|
1644
|
+
def scope(method, key = nil) #:nodoc:
|
1645
|
+
if current_scoped_methods && (scope = current_scoped_methods[method])
|
1646
|
+
key ? scope[key] : scope
|
1647
|
+
end
|
541
1648
|
end
|
542
1649
|
|
543
|
-
|
544
|
-
|
545
|
-
|
546
|
-
sql << (conditions.nil? ? "WHERE " : " AND ") + type_condition unless descents_from_active_record?
|
1650
|
+
def thread_safe_scoped_methods #:nodoc:
|
1651
|
+
scoped_methods = (Thread.current[:scoped_methods] ||= {})
|
1652
|
+
scoped_methods[self] ||= []
|
547
1653
|
end
|
548
1654
|
|
549
|
-
def
|
550
|
-
|
551
|
-
condition << "OR #{inheritance_column} = '#{Inflector.demodulize(subclass.name)}' "
|
552
|
-
end + ") "
|
1655
|
+
def single_threaded_scoped_methods #:nodoc:
|
1656
|
+
@scoped_methods ||= []
|
553
1657
|
end
|
554
|
-
|
555
|
-
#
|
556
|
-
|
557
|
-
|
558
|
-
|
559
|
-
|
1658
|
+
|
1659
|
+
# pick up the correct scoped_methods version from @@allow_concurrency
|
1660
|
+
if @@allow_concurrency
|
1661
|
+
alias_method :scoped_methods, :thread_safe_scoped_methods
|
1662
|
+
else
|
1663
|
+
alias_method :scoped_methods, :single_threaded_scoped_methods
|
560
1664
|
end
|
561
|
-
|
562
|
-
|
563
|
-
|
564
|
-
def subclasses
|
565
|
-
@@subclasses[self] ||= []
|
566
|
-
@@subclasses[self] + extra = @@subclasses[self].inject([]) {|list, subclass| list + subclass.subclasses }
|
1665
|
+
|
1666
|
+
def current_scoped_methods #:nodoc:
|
1667
|
+
scoped_methods.last
|
567
1668
|
end
|
568
|
-
|
1669
|
+
|
569
1670
|
# Returns the class type of the record using the current module as a prefix. So descendents of
|
570
|
-
# MyApp::Business::Account would
|
1671
|
+
# MyApp::Business::Account would appear as MyApp::Business::AccountSubclass.
|
571
1672
|
def compute_type(type_name)
|
572
|
-
type_name_with_module(type_name)
|
573
|
-
|
1673
|
+
modularized_name = type_name_with_module(type_name)
|
1674
|
+
begin
|
1675
|
+
class_eval(modularized_name, __FILE__, __LINE__)
|
1676
|
+
rescue NameError
|
1677
|
+
class_eval(type_name, __FILE__, __LINE__)
|
574
1678
|
end
|
575
1679
|
end
|
576
1680
|
|
577
|
-
# Returns the
|
578
|
-
def
|
579
|
-
if klass.superclass == Base
|
580
|
-
|
1681
|
+
# Returns the class descending directly from ActiveRecord in the inheritance hierarchy.
|
1682
|
+
def class_of_active_record_descendant(klass)
|
1683
|
+
if klass.superclass == Base || klass.superclass.abstract_class?
|
1684
|
+
klass
|
581
1685
|
elsif klass.superclass.nil?
|
582
1686
|
raise ActiveRecordError, "#{name} doesn't belong in a hierarchy descending from ActiveRecord"
|
583
1687
|
else
|
584
|
-
|
1688
|
+
class_of_active_record_descendant(klass.superclass)
|
1689
|
+
end
|
1690
|
+
end
|
1691
|
+
|
1692
|
+
# Returns the name of the class descending directly from ActiveRecord in the inheritance hierarchy.
|
1693
|
+
def class_name_of_active_record_descendant(klass) #:nodoc:
|
1694
|
+
klass.base_class.name
|
1695
|
+
end
|
1696
|
+
|
1697
|
+
# Accepts an array, hash, or string of sql conditions and sanitizes
|
1698
|
+
# them into a valid SQL fragment for a WHERE clause.
|
1699
|
+
# ["name='%s' and group_id='%s'", "foo'bar", 4] returns "name='foo''bar' and group_id='4'"
|
1700
|
+
# { :name => "foo'bar", :group_id => 4 } returns "name='foo''bar' and group_id='4'"
|
1701
|
+
# "name='foo''bar' and group_id='4'" returns "name='foo''bar' and group_id='4'"
|
1702
|
+
def sanitize_sql_for_conditions(condition)
|
1703
|
+
case condition
|
1704
|
+
when Array; sanitize_sql_array(condition)
|
1705
|
+
when Hash; sanitize_sql_hash_for_conditions(condition)
|
1706
|
+
else condition
|
1707
|
+
end
|
1708
|
+
end
|
1709
|
+
alias_method :sanitize_sql, :sanitize_sql_for_conditions
|
1710
|
+
|
1711
|
+
# Accepts an array, hash, or string of sql conditions and sanitizes
|
1712
|
+
# them into a valid SQL fragment for a SET clause.
|
1713
|
+
# { :name => nil, :group_id => 4 } returns "name = NULL , group_id='4'"
|
1714
|
+
def sanitize_sql_for_assignment(assignments)
|
1715
|
+
case assignments
|
1716
|
+
when Array; sanitize_sql_array(assignments)
|
1717
|
+
when Hash; sanitize_sql_hash_for_assignment(assignments)
|
1718
|
+
else assignments
|
1719
|
+
end
|
1720
|
+
end
|
1721
|
+
|
1722
|
+
# Sanitizes a hash of attribute/value pairs into SQL conditions for a WHERE clause.
|
1723
|
+
# { :name => "foo'bar", :group_id => 4 }
|
1724
|
+
# # => "name='foo''bar' and group_id= 4"
|
1725
|
+
# { :status => nil, :group_id => [1,2,3] }
|
1726
|
+
# # => "status IS NULL and group_id IN (1,2,3)"
|
1727
|
+
# { :age => 13..18 }
|
1728
|
+
# # => "age BETWEEN 13 AND 18"
|
1729
|
+
# { 'other_records.id' => 7 }
|
1730
|
+
# # => "`other_records`.`id` = 7"
|
1731
|
+
def sanitize_sql_hash_for_conditions(attrs)
|
1732
|
+
conditions = attrs.map do |attr, value|
|
1733
|
+
attr = attr.to_s
|
1734
|
+
|
1735
|
+
# Extract table name from qualified attribute names.
|
1736
|
+
if attr.include?('.')
|
1737
|
+
table_name, attr = attr.split('.', 2)
|
1738
|
+
table_name = connection.quote_table_name(table_name)
|
1739
|
+
else
|
1740
|
+
table_name = quoted_table_name
|
1741
|
+
end
|
1742
|
+
|
1743
|
+
"#{table_name}.#{connection.quote_column_name(attr)} #{attribute_condition(value)}"
|
1744
|
+
end.join(' AND ')
|
1745
|
+
|
1746
|
+
replace_bind_variables(conditions, expand_range_bind_variables(attrs.values))
|
1747
|
+
end
|
1748
|
+
alias_method :sanitize_sql_hash, :sanitize_sql_hash_for_conditions
|
1749
|
+
|
1750
|
+
# Sanitizes a hash of attribute/value pairs into SQL conditions for a SET clause.
|
1751
|
+
# { :status => nil, :group_id => 1 }
|
1752
|
+
# # => "status = NULL , group_id = 1"
|
1753
|
+
def sanitize_sql_hash_for_assignment(attrs)
|
1754
|
+
conditions = attrs.map do |attr, value|
|
1755
|
+
"#{connection.quote_column_name(attr)} = #{quote_bound_value(value)}"
|
1756
|
+
end.join(', ')
|
1757
|
+
end
|
1758
|
+
|
1759
|
+
# Accepts an array of conditions. The array has each value
|
1760
|
+
# sanitized and interpolated into the sql statement.
|
1761
|
+
# ["name='%s' and group_id='%s'", "foo'bar", 4] returns "name='foo''bar' and group_id='4'"
|
1762
|
+
def sanitize_sql_array(ary)
|
1763
|
+
statement, *values = ary
|
1764
|
+
if values.first.is_a?(Hash) and statement =~ /:\w+/
|
1765
|
+
replace_named_bind_variables(statement, values.first)
|
1766
|
+
elsif statement.include?('?')
|
1767
|
+
replace_bind_variables(statement, values)
|
1768
|
+
else
|
1769
|
+
statement % values.collect { |value| connection.quote_string(value.to_s) }
|
1770
|
+
end
|
1771
|
+
end
|
1772
|
+
|
1773
|
+
alias_method :sanitize_conditions, :sanitize_sql
|
1774
|
+
|
1775
|
+
def replace_bind_variables(statement, values) #:nodoc:
|
1776
|
+
raise_if_bind_arity_mismatch(statement, statement.count('?'), values.size)
|
1777
|
+
bound = values.dup
|
1778
|
+
statement.gsub('?') { quote_bound_value(bound.shift) }
|
1779
|
+
end
|
1780
|
+
|
1781
|
+
def replace_named_bind_variables(statement, bind_vars) #:nodoc:
|
1782
|
+
statement.gsub(/:(\w+)/) do
|
1783
|
+
match = $1.to_sym
|
1784
|
+
if bind_vars.include?(match)
|
1785
|
+
quote_bound_value(bind_vars[match])
|
1786
|
+
else
|
1787
|
+
raise PreparedStatementInvalid, "missing value for :#{match} in #{statement}"
|
1788
|
+
end
|
1789
|
+
end
|
1790
|
+
end
|
1791
|
+
|
1792
|
+
def expand_range_bind_variables(bind_vars) #:nodoc:
|
1793
|
+
bind_vars.each_with_index do |var, index|
|
1794
|
+
bind_vars[index, 1] = [var.first, var.last] if var.is_a?(Range)
|
1795
|
+
end
|
1796
|
+
bind_vars
|
1797
|
+
end
|
1798
|
+
|
1799
|
+
def quote_bound_value(value) #:nodoc:
|
1800
|
+
if value.respond_to?(:map) && !value.is_a?(String)
|
1801
|
+
if value.respond_to?(:empty?) && value.empty?
|
1802
|
+
connection.quote(nil)
|
1803
|
+
else
|
1804
|
+
value.map { |v| connection.quote(v) }.join(',')
|
1805
|
+
end
|
1806
|
+
else
|
1807
|
+
connection.quote(value)
|
1808
|
+
end
|
1809
|
+
end
|
1810
|
+
|
1811
|
+
def raise_if_bind_arity_mismatch(statement, expected, provided) #:nodoc:
|
1812
|
+
unless expected == provided
|
1813
|
+
raise PreparedStatementInvalid, "wrong number of bind variables (#{provided} for #{expected}) in: #{statement}"
|
585
1814
|
end
|
586
1815
|
end
|
587
1816
|
|
588
|
-
|
589
|
-
|
590
|
-
|
591
|
-
|
592
|
-
|
593
|
-
|
594
|
-
|
1817
|
+
VALID_FIND_OPTIONS = [ :conditions, :include, :joins, :limit, :offset,
|
1818
|
+
:order, :select, :readonly, :group, :from, :lock ]
|
1819
|
+
|
1820
|
+
def validate_find_options(options) #:nodoc:
|
1821
|
+
options.assert_valid_keys(VALID_FIND_OPTIONS)
|
1822
|
+
end
|
1823
|
+
|
1824
|
+
def set_readonly_option!(options) #:nodoc:
|
1825
|
+
# Inherit :readonly from finder scope if set. Otherwise,
|
1826
|
+
# if :joins is not blank then :readonly defaults to true.
|
1827
|
+
unless options.has_key?(:readonly)
|
1828
|
+
if scoped_readonly = scope(:find, :readonly)
|
1829
|
+
options[:readonly] = scoped_readonly
|
1830
|
+
elsif !options[:joins].blank? && !options[:select]
|
1831
|
+
options[:readonly] = true
|
1832
|
+
end
|
595
1833
|
end
|
596
|
-
|
597
|
-
|
1834
|
+
end
|
1835
|
+
|
1836
|
+
def encode_quoted_value(value) #:nodoc:
|
1837
|
+
quoted_value = connection.quote(value)
|
1838
|
+
quoted_value = "'#{quoted_value[1..-2].gsub(/\'/, "\\\\'")}'" if quoted_value.include?("\\\'") # (for ruby mode) "
|
1839
|
+
quoted_value
|
598
1840
|
end
|
599
1841
|
end
|
600
1842
|
|
601
1843
|
public
|
602
1844
|
# New objects can be instantiated as either empty (pass no construction parameter) or pre-set with
|
603
1845
|
# attributes but not yet saved (pass a hash with key names matching the associated table column names).
|
604
|
-
# In both instances, valid attribute keys are determined by the column names of the associated table --
|
1846
|
+
# In both instances, valid attribute keys are determined by the column names of the associated table --
|
605
1847
|
# hence you can't have attributes that aren't part of the table columns.
|
606
1848
|
def initialize(attributes = nil)
|
607
1849
|
@attributes = attributes_from_column_definition
|
1850
|
+
@attributes_cache = {}
|
608
1851
|
@new_record = true
|
609
1852
|
ensure_proper_type
|
610
1853
|
self.attributes = attributes unless attributes.nil?
|
611
|
-
|
1854
|
+
self.class.send(:scope, :create).each { |att,value| self.send("#{att}=", value) } if self.class.send(:scoped?, :create)
|
1855
|
+
result = yield self if block_given?
|
1856
|
+
callback(:after_initialize) if respond_to_without_attributes?(:after_initialize)
|
1857
|
+
result
|
612
1858
|
end
|
613
|
-
|
614
|
-
#
|
615
|
-
#
|
1859
|
+
|
1860
|
+
# A model instance's primary key is always available as model.id
|
1861
|
+
# whether you name it the default 'id' or set it to something else.
|
616
1862
|
def id
|
617
|
-
|
1863
|
+
attr_name = self.class.primary_key
|
1864
|
+
column = column_for_attribute(attr_name)
|
1865
|
+
|
1866
|
+
self.class.send(:define_read_method, :id, attr_name, column)
|
1867
|
+
# now that the method exists, call it
|
1868
|
+
self.send attr_name.to_sym
|
1869
|
+
|
618
1870
|
end
|
619
|
-
|
1871
|
+
|
1872
|
+
# Enables Active Record objects to be used as URL parameters in Action Pack automatically.
|
1873
|
+
def to_param
|
1874
|
+
# We can't use alias_method here, because method 'id' optimizes itself on the fly.
|
1875
|
+
(id = self.id) ? id.to_s : nil # Be sure to stringify the id for routes
|
1876
|
+
end
|
1877
|
+
|
1878
|
+
def id_before_type_cast #:nodoc:
|
1879
|
+
read_attribute_before_type_cast(self.class.primary_key)
|
1880
|
+
end
|
1881
|
+
|
1882
|
+
def quoted_id #:nodoc:
|
1883
|
+
quote_value(id, column_for_attribute(self.class.primary_key))
|
1884
|
+
end
|
1885
|
+
|
620
1886
|
# Sets the primary ID.
|
621
1887
|
def id=(value)
|
622
1888
|
write_attribute(self.class.primary_key, value)
|
623
1889
|
end
|
624
|
-
|
1890
|
+
|
625
1891
|
# Returns true if this object hasn't been saved yet -- that is, a record for the object doesn't exist yet.
|
626
1892
|
def new_record?
|
627
1893
|
@new_record
|
628
1894
|
end
|
629
|
-
|
1895
|
+
|
630
1896
|
# * No record exists: Creates a new record with values matching those of the object attributes.
|
631
1897
|
# * A record does exist: Updates the record with values matching those of the object attributes.
|
632
1898
|
def save
|
633
1899
|
create_or_update
|
634
|
-
return true
|
635
1900
|
end
|
636
|
-
|
1901
|
+
|
1902
|
+
# Attempts to save the record, but instead of just returning false if it couldn't happen, it raises a
|
1903
|
+
# RecordNotSaved exception
|
1904
|
+
def save!
|
1905
|
+
create_or_update || raise(RecordNotSaved)
|
1906
|
+
end
|
1907
|
+
|
637
1908
|
# Deletes the record in the database and freezes this instance to reflect that no changes should
|
638
1909
|
# be made (since they can't be persisted).
|
639
1910
|
def destroy
|
640
1911
|
unless new_record?
|
641
|
-
connection.delete
|
642
|
-
|
643
|
-
|
644
|
-
|
645
|
-
)
|
1912
|
+
connection.delete <<-end_sql, "#{self.class.name} Destroy"
|
1913
|
+
DELETE FROM #{self.class.quoted_table_name}
|
1914
|
+
WHERE #{connection.quote_column_name(self.class.primary_key)} = #{quoted_id}
|
1915
|
+
end_sql
|
646
1916
|
end
|
647
1917
|
|
648
1918
|
freeze
|
649
1919
|
end
|
650
1920
|
|
651
|
-
# Returns a clone of the record that hasn't been assigned an id yet and
|
1921
|
+
# Returns a clone of the record that hasn't been assigned an id yet and
|
1922
|
+
# is treated as a new record. Note that this is a "shallow" clone:
|
1923
|
+
# it copies the object's attributes only, not its associations.
|
1924
|
+
# The extent of a "deep" clone is application-specific and is therefore
|
1925
|
+
# left to the application to implement according to its need.
|
652
1926
|
def clone
|
653
|
-
|
1927
|
+
attrs = self.attributes_before_type_cast
|
1928
|
+
attrs.delete(self.class.primary_key)
|
1929
|
+
record = self.class.new
|
1930
|
+
record.send :instance_variable_set, '@attributes', attrs
|
1931
|
+
record
|
1932
|
+
end
|
654
1933
|
|
655
|
-
|
656
|
-
|
657
|
-
|
658
|
-
|
659
|
-
|
660
|
-
|
1934
|
+
# Returns an instance of the specified klass with the attributes of the current record. This is mostly useful in relation to
|
1935
|
+
# single-table inheritance structures where you want a subclass to appear as the superclass. This can be used along with record
|
1936
|
+
# identification in Action Pack to allow, say, Client < Company to do something like render :partial => @client.becomes(Company)
|
1937
|
+
# to render that instance using the companies/company partial instead of clients/client.
|
1938
|
+
#
|
1939
|
+
# Note: The new instance will share a link to the same attributes as the original class. So any change to the attributes in either
|
1940
|
+
# instance will affect the other.
|
1941
|
+
def becomes(klass)
|
1942
|
+
returning klass.new do |became|
|
1943
|
+
became.instance_variable_set("@attributes", @attributes)
|
1944
|
+
became.instance_variable_set("@attributes_cache", @attributes_cache)
|
1945
|
+
became.instance_variable_set("@new_record", new_record?)
|
661
1946
|
end
|
662
|
-
|
663
|
-
cloned_record = self.class.new(attr)
|
664
|
-
cloned_record.instance_variable_set "@new_record", true
|
665
|
-
cloned_record.id = nil
|
666
|
-
cloned_record
|
667
1947
|
end
|
668
|
-
|
1948
|
+
|
669
1949
|
# Updates a single attribute and saves the record. This is especially useful for boolean flags on existing records.
|
1950
|
+
# Note: This method is overwritten by the Validation module that'll make sure that updates made with this method
|
1951
|
+
# aren't subjected to validation checks. Hence, attributes can be updated even if the full object isn't valid.
|
670
1952
|
def update_attribute(name, value)
|
671
|
-
|
1953
|
+
send(name.to_s + '=', value)
|
1954
|
+
save
|
1955
|
+
end
|
1956
|
+
|
1957
|
+
# Updates all the attributes from the passed-in Hash and saves the record. If the object is invalid, the saving will
|
1958
|
+
# fail and false will be returned.
|
1959
|
+
def update_attributes(attributes)
|
1960
|
+
self.attributes = attributes
|
672
1961
|
save
|
673
1962
|
end
|
1963
|
+
|
1964
|
+
# Updates an object just like Base.update_attributes but calls save! instead of save so an exception is raised if the record is invalid.
|
1965
|
+
def update_attributes!(attributes)
|
1966
|
+
self.attributes = attributes
|
1967
|
+
save!
|
1968
|
+
end
|
1969
|
+
|
1970
|
+
# Initializes the +attribute+ to zero if nil and adds one. Only makes sense for number-based attributes. Returns self.
|
1971
|
+
def increment(attribute)
|
1972
|
+
self[attribute] ||= 0
|
1973
|
+
self[attribute] += 1
|
1974
|
+
self
|
1975
|
+
end
|
1976
|
+
|
1977
|
+
# Increments the +attribute+ and saves the record.
|
1978
|
+
def increment!(attribute)
|
1979
|
+
increment(attribute).update_attribute(attribute, self[attribute])
|
1980
|
+
end
|
1981
|
+
|
1982
|
+
# Initializes the +attribute+ to zero if nil and subtracts one. Only makes sense for number-based attributes. Returns self.
|
1983
|
+
def decrement(attribute)
|
1984
|
+
self[attribute] ||= 0
|
1985
|
+
self[attribute] -= 1
|
1986
|
+
self
|
1987
|
+
end
|
1988
|
+
|
1989
|
+
# Decrements the +attribute+ and saves the record.
|
1990
|
+
def decrement!(attribute)
|
1991
|
+
decrement(attribute).update_attribute(attribute, self[attribute])
|
1992
|
+
end
|
674
1993
|
|
675
|
-
#
|
1994
|
+
# Turns an +attribute+ that's currently true into false and vice versa. Returns self.
|
1995
|
+
def toggle(attribute)
|
1996
|
+
self[attribute] = !send("#{attribute}?")
|
1997
|
+
self
|
1998
|
+
end
|
1999
|
+
|
2000
|
+
# Toggles the +attribute+ and saves the record.
|
2001
|
+
def toggle!(attribute)
|
2002
|
+
toggle(attribute).update_attribute(attribute, self[attribute])
|
2003
|
+
end
|
2004
|
+
|
2005
|
+
# Reloads the attributes of this object from the database.
|
2006
|
+
# The optional options argument is passed to find when reloading so you
|
2007
|
+
# may do e.g. record.reload(:lock => true) to reload the same record with
|
2008
|
+
# an exclusive row lock.
|
2009
|
+
def reload(options = nil)
|
2010
|
+
clear_aggregation_cache
|
2011
|
+
clear_association_cache
|
2012
|
+
@attributes.update(self.class.find(self.id, options).instance_variable_get('@attributes'))
|
2013
|
+
@attributes_cache = {}
|
2014
|
+
self
|
2015
|
+
end
|
2016
|
+
|
2017
|
+
# Returns the value of the attribute identified by <tt>attr_name</tt> after it has been typecast (for example,
|
676
2018
|
# "2004-12-12" in a data column is cast to a date object, like Date.new(2004, 12, 12)).
|
677
2019
|
# (Alias for the protected read_attribute method).
|
678
|
-
def [](attr_name)
|
2020
|
+
def [](attr_name)
|
679
2021
|
read_attribute(attr_name)
|
680
2022
|
end
|
681
|
-
|
2023
|
+
|
682
2024
|
# Updates the attribute identified by <tt>attr_name</tt> with the specified +value+.
|
683
2025
|
# (Alias for the protected write_attribute method).
|
684
|
-
def []=
|
2026
|
+
def []=(attr_name, value)
|
685
2027
|
write_attribute(attr_name, value)
|
686
2028
|
end
|
687
2029
|
|
688
2030
|
# Allows you to set all the attributes at once by passing in a hash with keys
|
689
2031
|
# matching the attribute names (which again matches the column names). Sensitive attributes can be protected
|
690
2032
|
# from this form of mass-assignment by using the +attr_protected+ macro. Or you can alternatively
|
691
|
-
# specify which attributes *can* be accessed
|
692
|
-
# attributes not included in that won't be allowed to be mass-assigned.
|
693
|
-
def attributes=(
|
694
|
-
return if
|
2033
|
+
# specify which attributes *can* be accessed with the +attr_accessible+ macro. Then all the
|
2034
|
+
# attributes not included in that won't be allowed to be mass-assigned.
|
2035
|
+
def attributes=(new_attributes, guard_protected_attributes = true)
|
2036
|
+
return if new_attributes.nil?
|
2037
|
+
attributes = new_attributes.dup
|
2038
|
+
attributes.stringify_keys!
|
695
2039
|
|
696
2040
|
multi_parameter_attributes = []
|
697
|
-
remove_attributes_protected_from_mass_assignment(attributes)
|
2041
|
+
attributes = remove_attributes_protected_from_mass_assignment(attributes) if guard_protected_attributes
|
2042
|
+
|
2043
|
+
attributes.each do |k, v|
|
698
2044
|
k.include?("(") ? multi_parameter_attributes << [ k, v ] : send(k + "=", v)
|
699
2045
|
end
|
2046
|
+
|
700
2047
|
assign_multiparameter_attributes(multi_parameter_attributes)
|
701
2048
|
end
|
702
2049
|
|
2050
|
+
|
2051
|
+
# Returns a hash of all the attributes with their names as keys and clones of their objects as values.
|
2052
|
+
def attributes(options = nil)
|
2053
|
+
attributes = clone_attributes :read_attribute
|
2054
|
+
|
2055
|
+
if options.nil?
|
2056
|
+
attributes
|
2057
|
+
else
|
2058
|
+
if except = options[:except]
|
2059
|
+
except = Array(except).collect { |attribute| attribute.to_s }
|
2060
|
+
except.each { |attribute_name| attributes.delete(attribute_name) }
|
2061
|
+
attributes
|
2062
|
+
elsif only = options[:only]
|
2063
|
+
only = Array(only).collect { |attribute| attribute.to_s }
|
2064
|
+
attributes.delete_if { |key, value| !only.include?(key) }
|
2065
|
+
attributes
|
2066
|
+
else
|
2067
|
+
raise ArgumentError, "Options does not specify :except or :only (#{options.keys.inspect})"
|
2068
|
+
end
|
2069
|
+
end
|
2070
|
+
end
|
2071
|
+
|
2072
|
+
# Returns a hash of cloned attributes before typecasting and deserialization.
|
2073
|
+
def attributes_before_type_cast
|
2074
|
+
clone_attributes :read_attribute_before_type_cast
|
2075
|
+
end
|
2076
|
+
|
2077
|
+
# Format attributes nicely for inspect.
|
2078
|
+
def attribute_for_inspect(attr_name)
|
2079
|
+
value = read_attribute(attr_name)
|
2080
|
+
|
2081
|
+
if value.is_a?(String) && value.length > 50
|
2082
|
+
"#{value[0..50]}...".inspect
|
2083
|
+
elsif value.is_a?(Date) || value.is_a?(Time)
|
2084
|
+
%("#{value.to_s(:db)}")
|
2085
|
+
else
|
2086
|
+
value.inspect
|
2087
|
+
end
|
2088
|
+
end
|
2089
|
+
|
703
2090
|
# Returns true if the specified +attribute+ has been set by the user or by a database load and is neither
|
704
|
-
# nil nor empty? (the latter only applies to objects that
|
2091
|
+
# nil nor empty? (the latter only applies to objects that respond to empty?, most notably Strings).
|
705
2092
|
def attribute_present?(attribute)
|
706
|
-
|
707
|
-
|
2093
|
+
value = read_attribute(attribute)
|
2094
|
+
!value.blank?
|
2095
|
+
end
|
2096
|
+
|
2097
|
+
# Returns true if the given attribute is in the attributes hash
|
2098
|
+
def has_attribute?(attr_name)
|
2099
|
+
@attributes.has_key?(attr_name.to_s)
|
708
2100
|
end
|
709
2101
|
|
710
2102
|
# Returns an array of names for the attributes available on this object sorted alphabetically.
|
@@ -714,174 +2106,180 @@ module ActiveRecord #:nodoc:
|
|
714
2106
|
|
715
2107
|
# Returns the column object for the named attribute.
|
716
2108
|
def column_for_attribute(name)
|
717
|
-
self.class.columns_hash[name]
|
2109
|
+
self.class.columns_hash[name.to_s]
|
718
2110
|
end
|
719
|
-
|
720
|
-
# Returns true if the +comparison_object+ is of the same type and has the same id.
|
2111
|
+
|
2112
|
+
# Returns true if the +comparison_object+ is the same object, or is of the same type and has the same id.
|
721
2113
|
def ==(comparison_object)
|
722
|
-
comparison_object.
|
2114
|
+
comparison_object.equal?(self) ||
|
2115
|
+
(comparison_object.instance_of?(self.class) &&
|
2116
|
+
comparison_object.id == id &&
|
2117
|
+
!comparison_object.new_record?)
|
2118
|
+
end
|
2119
|
+
|
2120
|
+
# Delegates to ==
|
2121
|
+
def eql?(comparison_object)
|
2122
|
+
self == (comparison_object)
|
723
2123
|
end
|
724
2124
|
|
725
|
-
#
|
726
|
-
|
2125
|
+
# Delegates to id in order to allow two records of the same type and id to work with something like:
|
2126
|
+
# [ Person.find(1), Person.find(2), Person.find(3) ] & [ Person.find(1), Person.find(4) ] # => [ Person.find(1) ]
|
2127
|
+
def hash
|
2128
|
+
id.hash
|
2129
|
+
end
|
2130
|
+
|
2131
|
+
# Freeze the attributes hash such that associations are still accessible, even on destroyed records.
|
2132
|
+
def freeze
|
2133
|
+
@attributes.freeze; self
|
2134
|
+
end
|
2135
|
+
|
2136
|
+
# Returns +true+ if the attributes hash has been frozen.
|
2137
|
+
def frozen?
|
2138
|
+
@attributes.frozen?
|
2139
|
+
end
|
2140
|
+
|
2141
|
+
# Returns +true+ if the record is read only. Records loaded through joins with piggy-back
|
2142
|
+
# attributes will be marked as read only since they cannot be saved.
|
2143
|
+
def readonly?
|
2144
|
+
@readonly == true
|
2145
|
+
end
|
2146
|
+
|
2147
|
+
# Marks this record as read only.
|
2148
|
+
def readonly!
|
2149
|
+
@readonly = true
|
2150
|
+
end
|
727
2151
|
|
728
|
-
#
|
729
|
-
|
730
|
-
|
731
|
-
|
732
|
-
|
2152
|
+
# Returns the contents of the record as a nicely formatted string.
|
2153
|
+
def inspect
|
2154
|
+
attributes_as_nice_string = self.class.column_names.collect { |name|
|
2155
|
+
if has_attribute?(name) || new_record?
|
2156
|
+
"#{name}: #{attribute_for_inspect(name)}"
|
2157
|
+
end
|
2158
|
+
}.compact.join(", ")
|
2159
|
+
"#<#{self.class} #{attributes_as_nice_string}>"
|
733
2160
|
end
|
734
2161
|
|
735
2162
|
private
|
736
2163
|
def create_or_update
|
737
|
-
if
|
2164
|
+
raise ReadOnlyRecord if readonly?
|
2165
|
+
result = new_record? ? create : update
|
2166
|
+
result != false
|
738
2167
|
end
|
739
2168
|
|
740
|
-
# Updates the associated record with values matching those of the
|
2169
|
+
# Updates the associated record with values matching those of the instance attributes.
|
2170
|
+
# Returns the number of affected rows.
|
741
2171
|
def update
|
2172
|
+
quoted_attributes = attributes_with_quotes(false, false)
|
2173
|
+
return 0 if quoted_attributes.empty?
|
742
2174
|
connection.update(
|
743
|
-
"UPDATE #{self.class.
|
744
|
-
"SET #{quoted_comma_pair_list(connection,
|
745
|
-
"WHERE #{self.class.primary_key} =
|
2175
|
+
"UPDATE #{self.class.quoted_table_name} " +
|
2176
|
+
"SET #{quoted_comma_pair_list(connection, quoted_attributes)} " +
|
2177
|
+
"WHERE #{connection.quote_column_name(self.class.primary_key)} = #{quote_value(id)}",
|
746
2178
|
"#{self.class.name} Update"
|
747
2179
|
)
|
748
2180
|
end
|
749
2181
|
|
750
|
-
# Creates a
|
2182
|
+
# Creates a record with values matching those of the instance attributes
|
2183
|
+
# and returns its id.
|
751
2184
|
def create
|
752
|
-
self.id
|
753
|
-
|
2185
|
+
if self.id.nil? && connection.prefetch_primary_key?(self.class.table_name)
|
2186
|
+
self.id = connection.next_sequence_value(self.class.sequence_name)
|
2187
|
+
end
|
2188
|
+
|
2189
|
+
quoted_attributes = attributes_with_quotes
|
2190
|
+
|
2191
|
+
statement = if quoted_attributes.empty?
|
2192
|
+
connection.empty_insert_statement(self.class.table_name)
|
2193
|
+
else
|
2194
|
+
"INSERT INTO #{self.class.quoted_table_name} " +
|
754
2195
|
"(#{quoted_column_names.join(', ')}) " +
|
755
|
-
"VALUES(#{
|
756
|
-
|
757
|
-
|
758
|
-
|
759
|
-
|
2196
|
+
"VALUES(#{quoted_attributes.values.join(', ')})"
|
2197
|
+
end
|
2198
|
+
|
2199
|
+
self.id = connection.insert(statement, "#{self.class.name} Create",
|
2200
|
+
self.class.primary_key, self.id, self.class.sequence_name)
|
2201
|
+
|
760
2202
|
@new_record = false
|
2203
|
+
id
|
761
2204
|
end
|
762
2205
|
|
763
|
-
# Sets the attribute used for single table inheritance to this class name if this is not the ActiveRecord
|
764
|
-
# Considering the hierarchy Reply < Message < ActiveRecord, this makes it possible to do Reply.new without having to
|
765
|
-
# set Reply[Reply.inheritance_column] = "Reply" yourself. No such attribute would be set for objects of the
|
2206
|
+
# Sets the attribute used for single table inheritance to this class name if this is not the ActiveRecord descendent.
|
2207
|
+
# Considering the hierarchy Reply < Message < ActiveRecord, this makes it possible to do Reply.new without having to
|
2208
|
+
# set Reply[Reply.inheritance_column] = "Reply" yourself. No such attribute would be set for objects of the
|
766
2209
|
# Message class in that example.
|
767
2210
|
def ensure_proper_type
|
768
|
-
unless self.class.
|
769
|
-
|
770
|
-
end
|
771
|
-
end
|
772
|
-
|
773
|
-
# Allows access to the object attributes, which are held in the @attributes hash, as were
|
774
|
-
# they first-class methods. So a Person class with a name attribute can use Person#name and
|
775
|
-
# Person#name= and never directly use the attributes hash -- except for multiple assigns with
|
776
|
-
# ActiveRecord#attributes=. A Milestone class can also ask Milestone#completed? to test that
|
777
|
-
# the completed attribute is not nil or 0.
|
778
|
-
#
|
779
|
-
# It's also possible to instantiate related objects, so a Client class belonging to the clients
|
780
|
-
# table with a master_id foreign key can instantiate master through Client#master.
|
781
|
-
def method_missing(method_id, *arguments)
|
782
|
-
method_name = method_id.id2name
|
783
|
-
|
784
|
-
if method_name =~ read_method? && @attributes.include?($1)
|
785
|
-
return read_attribute($1)
|
786
|
-
elsif method_name =~ write_method?
|
787
|
-
write_attribute($1, arguments[0])
|
788
|
-
elsif method_name =~ query_method?
|
789
|
-
return query_attribute($1)
|
790
|
-
else
|
791
|
-
super
|
2211
|
+
unless self.class.descends_from_active_record?
|
2212
|
+
write_attribute(self.class.inheritance_column, Inflector.demodulize(self.class.name))
|
792
2213
|
end
|
793
2214
|
end
|
794
2215
|
|
795
|
-
def
|
796
|
-
|
797
|
-
|
798
|
-
|
799
|
-
|
800
|
-
|
801
|
-
def read_attribute(attr_name) #:doc:
|
802
|
-
if column = column_for_attribute(attr_name)
|
803
|
-
@attributes[attr_name] = unserializable_attribute?(attr_name, column) ?
|
804
|
-
unserialize_attribute(attr_name) : column.type_cast(@attributes[attr_name])
|
2216
|
+
def convert_number_column_value(value)
|
2217
|
+
case value
|
2218
|
+
when FalseClass; 0
|
2219
|
+
when TrueClass; 1
|
2220
|
+
when ''; nil
|
2221
|
+
else value
|
805
2222
|
end
|
806
|
-
|
807
|
-
@attributes[attr_name]
|
808
2223
|
end
|
809
2224
|
|
810
|
-
|
811
|
-
|
812
|
-
|
813
|
-
|
2225
|
+
def remove_attributes_protected_from_mass_assignment(attributes)
|
2226
|
+
safe_attributes =
|
2227
|
+
if self.class.accessible_attributes.nil? && self.class.protected_attributes.nil?
|
2228
|
+
attributes.reject { |key, value| attributes_protected_by_default.include?(key.gsub(/\(.+/, "")) }
|
2229
|
+
elsif self.class.protected_attributes.nil?
|
2230
|
+
attributes.reject { |key, value| !self.class.accessible_attributes.include?(key.gsub(/\(.+/, "")) || attributes_protected_by_default.include?(key.gsub(/\(.+/, "")) }
|
2231
|
+
elsif self.class.accessible_attributes.nil?
|
2232
|
+
attributes.reject { |key, value| self.class.protected_attributes.include?(key.gsub(/\(.+/,"")) || attributes_protected_by_default.include?(key.gsub(/\(.+/, "")) }
|
2233
|
+
else
|
2234
|
+
raise "Declare either attr_protected or attr_accessible for #{self.class}, but not both."
|
2235
|
+
end
|
814
2236
|
|
815
|
-
|
816
|
-
def unserialize_attribute(attr_name)
|
817
|
-
unserialized_object = object_from_yaml(@attributes[attr_name])
|
2237
|
+
removed_attributes = attributes.keys - safe_attributes.keys
|
818
2238
|
|
819
|
-
if
|
820
|
-
|
821
|
-
else
|
822
|
-
raise(
|
823
|
-
SerializationTypeMismatch,
|
824
|
-
"#{attr_name} was supposed to be a #{self.class.serialized_attributes[attr_name]}, " +
|
825
|
-
"but was a #{unserialized_object.class.to_s}"
|
826
|
-
)
|
2239
|
+
if removed_attributes.any?
|
2240
|
+
logger.debug "WARNING: Can't mass-assign these protected attributes: #{removed_attributes.join(', ')}"
|
827
2241
|
end
|
828
|
-
end
|
829
2242
|
|
830
|
-
|
831
|
-
# columns are turned into nil.
|
832
|
-
def write_attribute(attr_name, value) #:doc:
|
833
|
-
@attributes[attr_name] = empty_string_for_number_column?(attr_name, value) ? nil : value
|
2243
|
+
safe_attributes
|
834
2244
|
end
|
835
2245
|
|
836
|
-
|
837
|
-
|
838
|
-
|
839
|
-
|
840
|
-
|
841
|
-
def query_attribute(attr_name)
|
842
|
-
attribute = @attributes[attr_name]
|
843
|
-
if attribute.kind_of?(Fixnum) && attribute == 0
|
844
|
-
false
|
845
|
-
elsif attribute.kind_of?(String) && attribute == "0"
|
846
|
-
false
|
847
|
-
elsif attribute.kind_of?(String) && attribute.empty?
|
848
|
-
false
|
849
|
-
elsif attribute.nil?
|
850
|
-
false
|
851
|
-
elsif attribute == false
|
852
|
-
false
|
853
|
-
elsif attribute == "f"
|
854
|
-
false
|
855
|
-
elsif attribute == "false"
|
856
|
-
false
|
2246
|
+
# Removes attributes which have been marked as readonly.
|
2247
|
+
def remove_readonly_attributes(attributes)
|
2248
|
+
unless self.class.readonly_attributes.nil?
|
2249
|
+
attributes.delete_if { |key, value| self.class.readonly_attributes.include?(key.gsub(/\(.+/,"")) }
|
857
2250
|
else
|
858
|
-
|
2251
|
+
attributes
|
859
2252
|
end
|
860
2253
|
end
|
861
2254
|
|
862
|
-
|
863
|
-
|
864
|
-
|
865
|
-
|
866
|
-
|
867
|
-
elsif self.class.accessible_attributes.nil?
|
868
|
-
attributes.reject { |key, value| self.class.protected_attributes.include?(key.intern) || key == self.class.primary_key }
|
869
|
-
end
|
2255
|
+
# The primary key and inheritance column can never be set by mass-assignment for security reasons.
|
2256
|
+
def attributes_protected_by_default
|
2257
|
+
default = [ self.class.primary_key, self.class.inheritance_column ]
|
2258
|
+
default << 'id' unless self.class.primary_key.eql? 'id'
|
2259
|
+
default
|
870
2260
|
end
|
871
2261
|
|
872
|
-
# Returns copy of the attributes hash where all the values have been safely quoted for use in
|
873
|
-
# an SQL statement.
|
874
|
-
def attributes_with_quotes
|
875
|
-
|
876
|
-
|
877
|
-
|
878
|
-
|
2262
|
+
# Returns a copy of the attributes hash where all the values have been safely quoted for use in
|
2263
|
+
# an SQL statement.
|
2264
|
+
def attributes_with_quotes(include_primary_key = true, include_readonly_attributes = true)
|
2265
|
+
quoted = attributes.inject({}) do |quoted, (name, value)|
|
2266
|
+
if column = column_for_attribute(name)
|
2267
|
+
quoted[name] = quote_value(value, column) unless !include_primary_key && column.primary
|
2268
|
+
end
|
2269
|
+
quoted
|
879
2270
|
end
|
2271
|
+
include_readonly_attributes ? quoted : remove_readonly_attributes(quoted)
|
880
2272
|
end
|
881
|
-
|
2273
|
+
|
882
2274
|
# Quote strings appropriately for SQL statements.
|
883
|
-
def
|
884
|
-
connection.quote(value, column)
|
2275
|
+
def quote_value(value, column = nil)
|
2276
|
+
self.class.connection.quote(value, column)
|
2277
|
+
end
|
2278
|
+
|
2279
|
+
# Interpolate custom sql string in instance context.
|
2280
|
+
# Optional record argument is meant for custom insert_sql.
|
2281
|
+
def interpolate_sql(sql, record = nil)
|
2282
|
+
instance_eval("%@#{sql.gsub('@', '\@')}@")
|
885
2283
|
end
|
886
2284
|
|
887
2285
|
# Initializes the attributes array with keys matching the columns from the linked table and
|
@@ -889,7 +2287,7 @@ module ActiveRecord #:nodoc:
|
|
889
2287
|
# that a new instance, or one populated from a passed-in Hash, still has all the attributes
|
890
2288
|
# that instances loaded from the database would.
|
891
2289
|
def attributes_from_column_definition
|
892
|
-
|
2290
|
+
self.class.columns.inject({}) do |attributes, column|
|
893
2291
|
attributes[column.name] = column.default unless column.name == self.class.primary_key
|
894
2292
|
attributes
|
895
2293
|
end
|
@@ -899,26 +2297,34 @@ module ActiveRecord #:nodoc:
|
|
899
2297
|
# by calling new on the column type or aggregation type (through composed_of) object with these parameters.
|
900
2298
|
# So having the pairs written_on(1) = "2004", written_on(2) = "6", written_on(3) = "24", will instantiate
|
901
2299
|
# written_on (a date type) with Date.new("2004", "6", "24"). You can also specify a typecast character in the
|
902
|
-
#
|
903
|
-
# s for String, and a for Array. If all the values for a given attribute
|
2300
|
+
# parentheses to have the parameters typecasted before they're used in the constructor. Use i for Fixnum, f for Float,
|
2301
|
+
# s for String, and a for Array. If all the values for a given attribute are empty, the attribute will be set to nil.
|
904
2302
|
def assign_multiparameter_attributes(pairs)
|
905
2303
|
execute_callstack_for_multiparameter_attributes(
|
906
2304
|
extract_callstack_for_multiparameter_attributes(pairs)
|
907
2305
|
)
|
908
2306
|
end
|
909
|
-
|
2307
|
+
|
910
2308
|
# Includes an ugly hack for Time.local instead of Time.new because the latter is reserved by Time itself.
|
911
2309
|
def execute_callstack_for_multiparameter_attributes(callstack)
|
2310
|
+
errors = []
|
912
2311
|
callstack.each do |name, values|
|
913
|
-
klass = (self.class.reflect_on_aggregation(name) || column_for_attribute(name)).klass
|
2312
|
+
klass = (self.class.reflect_on_aggregation(name.to_sym) || column_for_attribute(name)).klass
|
914
2313
|
if values.empty?
|
915
2314
|
send(name + "=", nil)
|
916
2315
|
else
|
917
|
-
|
2316
|
+
begin
|
2317
|
+
send(name + "=", Time == klass ? (@@default_timezone == :utc ? klass.utc(*values) : klass.local(*values)) : klass.new(*values))
|
2318
|
+
rescue => ex
|
2319
|
+
errors << AttributeAssignmentError.new("error on assignment #{values.inspect} to #{name}", ex, name)
|
2320
|
+
end
|
918
2321
|
end
|
919
2322
|
end
|
2323
|
+
unless errors.empty?
|
2324
|
+
raise MultiparameterAssignmentErrors.new(errors), "#{errors.size} error(s) on assignment of multiparameter attributes"
|
2325
|
+
end
|
920
2326
|
end
|
921
|
-
|
2327
|
+
|
922
2328
|
def extract_callstack_for_multiparameter_attributes(pairs)
|
923
2329
|
attributes = { }
|
924
2330
|
|
@@ -928,58 +2334,65 @@ module ActiveRecord #:nodoc:
|
|
928
2334
|
attributes[attribute_name] = [] unless attributes.include?(attribute_name)
|
929
2335
|
|
930
2336
|
unless value.empty?
|
931
|
-
attributes[attribute_name] <<
|
932
|
-
[find_parameter_position(multiparameter_name), type_cast_attribute_value(multiparameter_name, value)]
|
2337
|
+
attributes[attribute_name] <<
|
2338
|
+
[ find_parameter_position(multiparameter_name), type_cast_attribute_value(multiparameter_name, value) ]
|
933
2339
|
end
|
934
2340
|
end
|
935
2341
|
|
936
2342
|
attributes.each { |name, values| attributes[name] = values.sort_by{ |v| v.first }.collect { |v| v.last } }
|
937
2343
|
end
|
938
|
-
|
2344
|
+
|
939
2345
|
def type_cast_attribute_value(multiparameter_name, value)
|
940
2346
|
multiparameter_name =~ /\([0-9]*([a-z])\)/ ? value.send("to_" + $1) : value
|
941
2347
|
end
|
942
|
-
|
2348
|
+
|
943
2349
|
def find_parameter_position(multiparameter_name)
|
944
2350
|
multiparameter_name.scan(/\(([0-9]*).*\)/).first.first
|
945
2351
|
end
|
946
|
-
|
2352
|
+
|
947
2353
|
# Returns a comma-separated pair list, like "key1 = val1, key2 = val2".
|
948
2354
|
def comma_pair_list(hash)
|
949
2355
|
hash.inject([]) { |list, pair| list << "#{pair.first} = #{pair.last}" }.join(", ")
|
950
2356
|
end
|
951
2357
|
|
952
|
-
def quoted_column_names
|
953
|
-
|
2358
|
+
def quoted_column_names(attributes = attributes_with_quotes)
|
2359
|
+
attributes.keys.collect do |column_name|
|
2360
|
+
self.class.connection.quote_column_name(column_name)
|
2361
|
+
end
|
2362
|
+
end
|
2363
|
+
|
2364
|
+
def self.quoted_table_name
|
2365
|
+
self.connection.quote_table_name(self.table_name)
|
954
2366
|
end
|
955
2367
|
|
956
|
-
def quote_columns(
|
957
|
-
hash.inject({})
|
958
|
-
|
959
|
-
|
960
|
-
|
2368
|
+
def quote_columns(quoter, hash)
|
2369
|
+
hash.inject({}) do |quoted, (name, value)|
|
2370
|
+
quoted[quoter.quote_column_name(name)] = value
|
2371
|
+
quoted
|
2372
|
+
end
|
961
2373
|
end
|
962
2374
|
|
963
|
-
def quoted_comma_pair_list(
|
964
|
-
comma_pair_list(quote_columns(
|
2375
|
+
def quoted_comma_pair_list(quoter, hash)
|
2376
|
+
comma_pair_list(quote_columns(quoter, hash))
|
965
2377
|
end
|
966
2378
|
|
967
2379
|
def object_from_yaml(string)
|
968
|
-
return string unless String
|
969
|
-
|
970
|
-
|
971
|
-
|
972
|
-
|
973
|
-
|
974
|
-
|
975
|
-
|
976
|
-
else
|
977
|
-
string
|
2380
|
+
return string unless string.is_a?(String)
|
2381
|
+
YAML::load(string) rescue string
|
2382
|
+
end
|
2383
|
+
|
2384
|
+
def clone_attributes(reader_method = :read_attribute, attributes = {})
|
2385
|
+
self.attribute_names.inject(attributes) do |attributes, name|
|
2386
|
+
attributes[name] = clone_attribute_value(reader_method, name)
|
2387
|
+
attributes
|
978
2388
|
end
|
979
2389
|
end
|
980
2390
|
|
981
|
-
def
|
982
|
-
|
2391
|
+
def clone_attribute_value(reader_method, attribute_name)
|
2392
|
+
value = send(reader_method, attribute_name)
|
2393
|
+
value.duplicable? ? value.clone : value
|
2394
|
+
rescue TypeError, NoMethodError
|
2395
|
+
value
|
983
2396
|
end
|
984
2397
|
end
|
985
|
-
end
|
2398
|
+
end
|