activerecord 4.2.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of activerecord might be problematic. Click here for more details.
- checksums.yaml +7 -0
- data/CHANGELOG.md +1372 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +218 -0
- data/examples/performance.rb +184 -0
- data/examples/simple.rb +14 -0
- data/lib/active_record.rb +173 -0
- data/lib/active_record/aggregations.rb +266 -0
- data/lib/active_record/association_relation.rb +22 -0
- data/lib/active_record/associations.rb +1724 -0
- data/lib/active_record/associations/alias_tracker.rb +87 -0
- data/lib/active_record/associations/association.rb +253 -0
- data/lib/active_record/associations/association_scope.rb +194 -0
- data/lib/active_record/associations/belongs_to_association.rb +111 -0
- data/lib/active_record/associations/belongs_to_polymorphic_association.rb +40 -0
- data/lib/active_record/associations/builder/association.rb +149 -0
- data/lib/active_record/associations/builder/belongs_to.rb +116 -0
- data/lib/active_record/associations/builder/collection_association.rb +91 -0
- data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +124 -0
- data/lib/active_record/associations/builder/has_many.rb +15 -0
- data/lib/active_record/associations/builder/has_one.rb +23 -0
- data/lib/active_record/associations/builder/singular_association.rb +38 -0
- data/lib/active_record/associations/collection_association.rb +634 -0
- data/lib/active_record/associations/collection_proxy.rb +1027 -0
- data/lib/active_record/associations/has_many_association.rb +184 -0
- data/lib/active_record/associations/has_many_through_association.rb +238 -0
- data/lib/active_record/associations/has_one_association.rb +105 -0
- data/lib/active_record/associations/has_one_through_association.rb +36 -0
- data/lib/active_record/associations/join_dependency.rb +282 -0
- data/lib/active_record/associations/join_dependency/join_association.rb +122 -0
- data/lib/active_record/associations/join_dependency/join_base.rb +22 -0
- data/lib/active_record/associations/join_dependency/join_part.rb +71 -0
- data/lib/active_record/associations/preloader.rb +203 -0
- data/lib/active_record/associations/preloader/association.rb +162 -0
- data/lib/active_record/associations/preloader/belongs_to.rb +17 -0
- data/lib/active_record/associations/preloader/collection_association.rb +24 -0
- data/lib/active_record/associations/preloader/has_many.rb +17 -0
- data/lib/active_record/associations/preloader/has_many_through.rb +19 -0
- data/lib/active_record/associations/preloader/has_one.rb +23 -0
- data/lib/active_record/associations/preloader/has_one_through.rb +9 -0
- data/lib/active_record/associations/preloader/singular_association.rb +21 -0
- data/lib/active_record/associations/preloader/through_association.rb +96 -0
- data/lib/active_record/associations/singular_association.rb +86 -0
- data/lib/active_record/associations/through_association.rb +96 -0
- data/lib/active_record/attribute.rb +149 -0
- data/lib/active_record/attribute_assignment.rb +212 -0
- data/lib/active_record/attribute_decorators.rb +66 -0
- data/lib/active_record/attribute_methods.rb +439 -0
- data/lib/active_record/attribute_methods/before_type_cast.rb +71 -0
- data/lib/active_record/attribute_methods/dirty.rb +181 -0
- data/lib/active_record/attribute_methods/primary_key.rb +128 -0
- data/lib/active_record/attribute_methods/query.rb +40 -0
- data/lib/active_record/attribute_methods/read.rb +103 -0
- data/lib/active_record/attribute_methods/serialization.rb +70 -0
- data/lib/active_record/attribute_methods/time_zone_conversion.rb +65 -0
- data/lib/active_record/attribute_methods/write.rb +83 -0
- data/lib/active_record/attribute_set.rb +77 -0
- data/lib/active_record/attribute_set/builder.rb +86 -0
- data/lib/active_record/attributes.rb +139 -0
- data/lib/active_record/autosave_association.rb +439 -0
- data/lib/active_record/base.rb +317 -0
- data/lib/active_record/callbacks.rb +313 -0
- data/lib/active_record/coders/json.rb +13 -0
- data/lib/active_record/coders/yaml_column.rb +38 -0
- data/lib/active_record/connection_adapters/abstract/connection_pool.rb +659 -0
- data/lib/active_record/connection_adapters/abstract/database_limits.rb +67 -0
- data/lib/active_record/connection_adapters/abstract/database_statements.rb +373 -0
- data/lib/active_record/connection_adapters/abstract/query_cache.rb +95 -0
- data/lib/active_record/connection_adapters/abstract/quoting.rb +133 -0
- data/lib/active_record/connection_adapters/abstract/savepoints.rb +21 -0
- data/lib/active_record/connection_adapters/abstract/schema_creation.rb +125 -0
- data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +574 -0
- data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +50 -0
- data/lib/active_record/connection_adapters/abstract/schema_statements.rb +991 -0
- data/lib/active_record/connection_adapters/abstract/transaction.rb +219 -0
- data/lib/active_record/connection_adapters/abstract_adapter.rb +487 -0
- data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +883 -0
- data/lib/active_record/connection_adapters/column.rb +82 -0
- data/lib/active_record/connection_adapters/connection_specification.rb +275 -0
- data/lib/active_record/connection_adapters/mysql2_adapter.rb +282 -0
- data/lib/active_record/connection_adapters/mysql_adapter.rb +491 -0
- data/lib/active_record/connection_adapters/postgresql/array_parser.rb +93 -0
- data/lib/active_record/connection_adapters/postgresql/column.rb +20 -0
- data/lib/active_record/connection_adapters/postgresql/database_statements.rb +232 -0
- data/lib/active_record/connection_adapters/postgresql/oid.rb +36 -0
- data/lib/active_record/connection_adapters/postgresql/oid/array.rb +99 -0
- data/lib/active_record/connection_adapters/postgresql/oid/bit.rb +52 -0
- data/lib/active_record/connection_adapters/postgresql/oid/bit_varying.rb +13 -0
- data/lib/active_record/connection_adapters/postgresql/oid/bytea.rb +14 -0
- data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +46 -0
- data/lib/active_record/connection_adapters/postgresql/oid/date.rb +11 -0
- data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +27 -0
- data/lib/active_record/connection_adapters/postgresql/oid/decimal.rb +13 -0
- data/lib/active_record/connection_adapters/postgresql/oid/enum.rb +17 -0
- data/lib/active_record/connection_adapters/postgresql/oid/float.rb +21 -0
- data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +59 -0
- data/lib/active_record/connection_adapters/postgresql/oid/inet.rb +13 -0
- data/lib/active_record/connection_adapters/postgresql/oid/infinity.rb +13 -0
- data/lib/active_record/connection_adapters/postgresql/oid/integer.rb +11 -0
- data/lib/active_record/connection_adapters/postgresql/oid/json.rb +35 -0
- data/lib/active_record/connection_adapters/postgresql/oid/jsonb.rb +23 -0
- data/lib/active_record/connection_adapters/postgresql/oid/money.rb +43 -0
- data/lib/active_record/connection_adapters/postgresql/oid/point.rb +43 -0
- data/lib/active_record/connection_adapters/postgresql/oid/range.rb +79 -0
- data/lib/active_record/connection_adapters/postgresql/oid/specialized_string.rb +15 -0
- data/lib/active_record/connection_adapters/postgresql/oid/time.rb +11 -0
- data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +97 -0
- data/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +21 -0
- data/lib/active_record/connection_adapters/postgresql/oid/vector.rb +26 -0
- data/lib/active_record/connection_adapters/postgresql/oid/xml.rb +28 -0
- data/lib/active_record/connection_adapters/postgresql/quoting.rb +108 -0
- data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +30 -0
- data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +152 -0
- data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +588 -0
- data/lib/active_record/connection_adapters/postgresql/utils.rb +77 -0
- data/lib/active_record/connection_adapters/postgresql_adapter.rb +754 -0
- data/lib/active_record/connection_adapters/schema_cache.rb +94 -0
- data/lib/active_record/connection_adapters/sqlite3_adapter.rb +628 -0
- data/lib/active_record/connection_adapters/statement_pool.rb +40 -0
- data/lib/active_record/connection_handling.rb +132 -0
- data/lib/active_record/core.rb +566 -0
- data/lib/active_record/counter_cache.rb +175 -0
- data/lib/active_record/dynamic_matchers.rb +140 -0
- data/lib/active_record/enum.rb +198 -0
- data/lib/active_record/errors.rb +252 -0
- data/lib/active_record/explain.rb +38 -0
- data/lib/active_record/explain_registry.rb +30 -0
- data/lib/active_record/explain_subscriber.rb +29 -0
- data/lib/active_record/fixture_set/file.rb +56 -0
- data/lib/active_record/fixtures.rb +1007 -0
- data/lib/active_record/gem_version.rb +15 -0
- data/lib/active_record/inheritance.rb +247 -0
- data/lib/active_record/integration.rb +113 -0
- data/lib/active_record/locale/en.yml +47 -0
- data/lib/active_record/locking/optimistic.rb +204 -0
- data/lib/active_record/locking/pessimistic.rb +77 -0
- data/lib/active_record/log_subscriber.rb +75 -0
- data/lib/active_record/migration.rb +1051 -0
- data/lib/active_record/migration/command_recorder.rb +197 -0
- data/lib/active_record/migration/join_table.rb +15 -0
- data/lib/active_record/model_schema.rb +340 -0
- data/lib/active_record/nested_attributes.rb +548 -0
- data/lib/active_record/no_touching.rb +52 -0
- data/lib/active_record/null_relation.rb +81 -0
- data/lib/active_record/persistence.rb +532 -0
- data/lib/active_record/query_cache.rb +56 -0
- data/lib/active_record/querying.rb +68 -0
- data/lib/active_record/railtie.rb +162 -0
- data/lib/active_record/railties/console_sandbox.rb +5 -0
- data/lib/active_record/railties/controller_runtime.rb +50 -0
- data/lib/active_record/railties/databases.rake +391 -0
- data/lib/active_record/railties/jdbcmysql_error.rb +16 -0
- data/lib/active_record/readonly_attributes.rb +23 -0
- data/lib/active_record/reflection.rb +881 -0
- data/lib/active_record/relation.rb +681 -0
- data/lib/active_record/relation/batches.rb +138 -0
- data/lib/active_record/relation/calculations.rb +403 -0
- data/lib/active_record/relation/delegation.rb +140 -0
- data/lib/active_record/relation/finder_methods.rb +528 -0
- data/lib/active_record/relation/merger.rb +170 -0
- data/lib/active_record/relation/predicate_builder.rb +126 -0
- data/lib/active_record/relation/predicate_builder/array_handler.rb +47 -0
- data/lib/active_record/relation/predicate_builder/relation_handler.rb +13 -0
- data/lib/active_record/relation/query_methods.rb +1176 -0
- data/lib/active_record/relation/spawn_methods.rb +75 -0
- data/lib/active_record/result.rb +131 -0
- data/lib/active_record/runtime_registry.rb +22 -0
- data/lib/active_record/sanitization.rb +191 -0
- data/lib/active_record/schema.rb +64 -0
- data/lib/active_record/schema_dumper.rb +251 -0
- data/lib/active_record/schema_migration.rb +56 -0
- data/lib/active_record/scoping.rb +87 -0
- data/lib/active_record/scoping/default.rb +134 -0
- data/lib/active_record/scoping/named.rb +164 -0
- data/lib/active_record/serialization.rb +22 -0
- data/lib/active_record/serializers/xml_serializer.rb +193 -0
- data/lib/active_record/statement_cache.rb +111 -0
- data/lib/active_record/store.rb +205 -0
- data/lib/active_record/tasks/database_tasks.rb +296 -0
- data/lib/active_record/tasks/mysql_database_tasks.rb +145 -0
- data/lib/active_record/tasks/postgresql_database_tasks.rb +90 -0
- data/lib/active_record/tasks/sqlite_database_tasks.rb +55 -0
- data/lib/active_record/timestamp.rb +121 -0
- data/lib/active_record/transactions.rb +417 -0
- data/lib/active_record/translation.rb +22 -0
- data/lib/active_record/type.rb +23 -0
- data/lib/active_record/type/big_integer.rb +13 -0
- data/lib/active_record/type/binary.rb +50 -0
- data/lib/active_record/type/boolean.rb +30 -0
- data/lib/active_record/type/date.rb +46 -0
- data/lib/active_record/type/date_time.rb +43 -0
- data/lib/active_record/type/decimal.rb +40 -0
- data/lib/active_record/type/decimal_without_scale.rb +11 -0
- data/lib/active_record/type/decorator.rb +14 -0
- data/lib/active_record/type/float.rb +19 -0
- data/lib/active_record/type/hash_lookup_type_map.rb +17 -0
- data/lib/active_record/type/integer.rb +55 -0
- data/lib/active_record/type/mutable.rb +16 -0
- data/lib/active_record/type/numeric.rb +36 -0
- data/lib/active_record/type/serialized.rb +56 -0
- data/lib/active_record/type/string.rb +36 -0
- data/lib/active_record/type/text.rb +11 -0
- data/lib/active_record/type/time.rb +26 -0
- data/lib/active_record/type/time_value.rb +38 -0
- data/lib/active_record/type/type_map.rb +64 -0
- data/lib/active_record/type/unsigned_integer.rb +15 -0
- data/lib/active_record/type/value.rb +101 -0
- data/lib/active_record/validations.rb +90 -0
- data/lib/active_record/validations/associated.rb +51 -0
- data/lib/active_record/validations/presence.rb +67 -0
- data/lib/active_record/validations/uniqueness.rb +229 -0
- data/lib/active_record/version.rb +8 -0
- data/lib/rails/generators/active_record.rb +17 -0
- data/lib/rails/generators/active_record/migration.rb +18 -0
- data/lib/rails/generators/active_record/migration/migration_generator.rb +70 -0
- data/lib/rails/generators/active_record/migration/templates/create_table_migration.rb +22 -0
- data/lib/rails/generators/active_record/migration/templates/migration.rb +45 -0
- data/lib/rails/generators/active_record/model/model_generator.rb +52 -0
- data/lib/rails/generators/active_record/model/templates/model.rb +10 -0
- data/lib/rails/generators/active_record/model/templates/module.rb +7 -0
- metadata +309 -0
@@ -0,0 +1,56 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module Type
|
3
|
+
class Serialized < DelegateClass(Type::Value) # :nodoc:
|
4
|
+
include Mutable
|
5
|
+
include Decorator
|
6
|
+
|
7
|
+
attr_reader :subtype, :coder
|
8
|
+
|
9
|
+
def initialize(subtype, coder)
|
10
|
+
@subtype = subtype
|
11
|
+
@coder = coder
|
12
|
+
super(subtype)
|
13
|
+
end
|
14
|
+
|
15
|
+
def type_cast_from_database(value)
|
16
|
+
if default_value?(value)
|
17
|
+
value
|
18
|
+
else
|
19
|
+
coder.load(super)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def type_cast_for_database(value)
|
24
|
+
return if value.nil?
|
25
|
+
unless default_value?(value)
|
26
|
+
super coder.dump(value)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def changed_in_place?(raw_old_value, value)
|
31
|
+
return false if value.nil?
|
32
|
+
subtype.changed_in_place?(raw_old_value, coder.dump(value))
|
33
|
+
end
|
34
|
+
|
35
|
+
def accessor
|
36
|
+
ActiveRecord::Store::IndifferentHashAccessor
|
37
|
+
end
|
38
|
+
|
39
|
+
def init_with(coder)
|
40
|
+
@coder = coder['coder']
|
41
|
+
super
|
42
|
+
end
|
43
|
+
|
44
|
+
def encode_with(coder)
|
45
|
+
coder['coder'] = @coder
|
46
|
+
super
|
47
|
+
end
|
48
|
+
|
49
|
+
private
|
50
|
+
|
51
|
+
def default_value?(value)
|
52
|
+
value == coder.load(nil)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module Type
|
3
|
+
class String < Value # :nodoc:
|
4
|
+
def type
|
5
|
+
:string
|
6
|
+
end
|
7
|
+
|
8
|
+
def changed_in_place?(raw_old_value, new_value)
|
9
|
+
if new_value.is_a?(::String)
|
10
|
+
raw_old_value != new_value
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def type_cast_for_database(value)
|
15
|
+
case value
|
16
|
+
when ::Numeric, ActiveSupport::Duration then value.to_s
|
17
|
+
when ::String then ::String.new(value)
|
18
|
+
when true then "t"
|
19
|
+
when false then "f"
|
20
|
+
else super
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def cast_value(value)
|
27
|
+
case value
|
28
|
+
when true then "t"
|
29
|
+
when false then "f"
|
30
|
+
# String.new is slightly faster than dup
|
31
|
+
else ::String.new(value.to_s)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module Type
|
3
|
+
class Time < Value # :nodoc:
|
4
|
+
include TimeValue
|
5
|
+
|
6
|
+
def type
|
7
|
+
:time
|
8
|
+
end
|
9
|
+
|
10
|
+
private
|
11
|
+
|
12
|
+
def cast_value(value)
|
13
|
+
return value unless value.is_a?(::String)
|
14
|
+
return if value.empty?
|
15
|
+
|
16
|
+
dummy_time_value = "2000-01-01 #{value}"
|
17
|
+
|
18
|
+
fast_string_to_time(dummy_time_value) || begin
|
19
|
+
time_hash = ::Date._parse(dummy_time_value)
|
20
|
+
return if time_hash[:hour].nil?
|
21
|
+
new_time(*time_hash.values_at(:year, :mon, :mday, :hour, :min, :sec, :sec_fraction))
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module Type
|
3
|
+
module TimeValue # :nodoc:
|
4
|
+
def klass
|
5
|
+
::Time
|
6
|
+
end
|
7
|
+
|
8
|
+
def type_cast_for_schema(value)
|
9
|
+
"'#{value.to_s(:db)}'"
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
def new_time(year, mon, mday, hour, min, sec, microsec, offset = nil)
|
15
|
+
# Treat 0000-00-00 00:00:00 as nil.
|
16
|
+
return if year.nil? || (year == 0 && mon == 0 && mday == 0)
|
17
|
+
|
18
|
+
if offset
|
19
|
+
time = ::Time.utc(year, mon, mday, hour, min, sec, microsec) rescue nil
|
20
|
+
return unless time
|
21
|
+
|
22
|
+
time -= offset
|
23
|
+
Base.default_timezone == :utc ? time : time.getlocal
|
24
|
+
else
|
25
|
+
::Time.public_send(Base.default_timezone, year, mon, mday, hour, min, sec, microsec) rescue nil
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
# Doesn't handle time zones.
|
30
|
+
def fast_string_to_time(string)
|
31
|
+
if string =~ ConnectionAdapters::Column::Format::ISO_DATETIME
|
32
|
+
microsec = ($7.to_r * 1_000_000).to_i
|
33
|
+
new_time $1.to_i, $2.to_i, $3.to_i, $4.to_i, $5.to_i, $6.to_i, microsec
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
require 'thread_safe'
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
module Type
|
5
|
+
class TypeMap # :nodoc:
|
6
|
+
def initialize
|
7
|
+
@mapping = {}
|
8
|
+
@cache = ThreadSafe::Cache.new do |h, key|
|
9
|
+
h.fetch_or_store(key, ThreadSafe::Cache.new)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def lookup(lookup_key, *args)
|
14
|
+
fetch(lookup_key, *args) { default_value }
|
15
|
+
end
|
16
|
+
|
17
|
+
def fetch(lookup_key, *args, &block)
|
18
|
+
@cache[lookup_key].fetch_or_store(args) do
|
19
|
+
perform_fetch(lookup_key, *args, &block)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def register_type(key, value = nil, &block)
|
24
|
+
raise ::ArgumentError unless value || block
|
25
|
+
@cache.clear
|
26
|
+
|
27
|
+
if block
|
28
|
+
@mapping[key] = block
|
29
|
+
else
|
30
|
+
@mapping[key] = proc { value }
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def alias_type(key, target_key)
|
35
|
+
register_type(key) do |sql_type, *args|
|
36
|
+
metadata = sql_type[/\(.*\)/, 0]
|
37
|
+
lookup("#{target_key}#{metadata}", *args)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def clear
|
42
|
+
@mapping.clear
|
43
|
+
end
|
44
|
+
|
45
|
+
private
|
46
|
+
|
47
|
+
def perform_fetch(lookup_key, *args)
|
48
|
+
matching_pair = @mapping.reverse_each.detect do |key, _|
|
49
|
+
key === lookup_key
|
50
|
+
end
|
51
|
+
|
52
|
+
if matching_pair
|
53
|
+
matching_pair.last.call(lookup_key, *args)
|
54
|
+
else
|
55
|
+
yield lookup_key, *args
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def default_value
|
60
|
+
@default_value ||= Value.new
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,101 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module Type
|
3
|
+
class Value # :nodoc:
|
4
|
+
attr_reader :precision, :scale, :limit
|
5
|
+
|
6
|
+
# Valid options are +precision+, +scale+, and +limit+. They are only
|
7
|
+
# used when dumping schema.
|
8
|
+
def initialize(options = {})
|
9
|
+
options.assert_valid_keys(:precision, :scale, :limit)
|
10
|
+
@precision = options[:precision]
|
11
|
+
@scale = options[:scale]
|
12
|
+
@limit = options[:limit]
|
13
|
+
end
|
14
|
+
|
15
|
+
# The simplified type that this object represents. Returns a symbol such
|
16
|
+
# as +:string+ or +:integer+
|
17
|
+
def type; end
|
18
|
+
|
19
|
+
# Type casts a string from the database into the appropriate ruby type.
|
20
|
+
# Classes which do not need separate type casting behavior for database
|
21
|
+
# and user provided values should override +cast_value+ instead.
|
22
|
+
def type_cast_from_database(value)
|
23
|
+
type_cast(value)
|
24
|
+
end
|
25
|
+
|
26
|
+
# Type casts a value from user input (e.g. from a setter). This value may
|
27
|
+
# be a string from the form builder, or an already type cast value
|
28
|
+
# provided manually to a setter.
|
29
|
+
#
|
30
|
+
# Classes which do not need separate type casting behavior for database
|
31
|
+
# and user provided values should override +type_cast+ or +cast_value+
|
32
|
+
# instead.
|
33
|
+
def type_cast_from_user(value)
|
34
|
+
type_cast(value)
|
35
|
+
end
|
36
|
+
|
37
|
+
# Cast a value from the ruby type to a type that the database knows how
|
38
|
+
# to understand. The returned value from this method should be a
|
39
|
+
# +String+, +Numeric+, +Date+, +Time+, +Symbol+, +true+, +false+, or
|
40
|
+
# +nil+
|
41
|
+
def type_cast_for_database(value)
|
42
|
+
value
|
43
|
+
end
|
44
|
+
|
45
|
+
# Type cast a value for schema dumping. This method is private, as we are
|
46
|
+
# hoping to remove it entirely.
|
47
|
+
def type_cast_for_schema(value) # :nodoc:
|
48
|
+
value.inspect
|
49
|
+
end
|
50
|
+
|
51
|
+
# These predicates are not documented, as I need to look further into
|
52
|
+
# their use, and see if they can be removed entirely.
|
53
|
+
def number? # :nodoc:
|
54
|
+
false
|
55
|
+
end
|
56
|
+
|
57
|
+
def binary? # :nodoc:
|
58
|
+
false
|
59
|
+
end
|
60
|
+
|
61
|
+
def klass # :nodoc:
|
62
|
+
end
|
63
|
+
|
64
|
+
# Determines whether a value has changed for dirty checking. +old_value+
|
65
|
+
# and +new_value+ will always be type-cast. Types should not need to
|
66
|
+
# override this method.
|
67
|
+
def changed?(old_value, new_value, _new_value_before_type_cast)
|
68
|
+
old_value != new_value
|
69
|
+
end
|
70
|
+
|
71
|
+
# Determines whether the mutable value has been modified since it was
|
72
|
+
# read. Returns +false+ by default. This method should not be overridden
|
73
|
+
# directly. Types which return a mutable value should include
|
74
|
+
# +Type::Mutable+, which will define this method.
|
75
|
+
def changed_in_place?(*)
|
76
|
+
false
|
77
|
+
end
|
78
|
+
|
79
|
+
def ==(other)
|
80
|
+
self.class == other.class &&
|
81
|
+
precision == other.precision &&
|
82
|
+
scale == other.scale &&
|
83
|
+
limit == other.limit
|
84
|
+
end
|
85
|
+
|
86
|
+
private
|
87
|
+
|
88
|
+
def type_cast(value)
|
89
|
+
cast_value(value) unless value.nil?
|
90
|
+
end
|
91
|
+
|
92
|
+
# Convenience method for types which do not need separate type casting
|
93
|
+
# behavior for user and database inputs. Called by
|
94
|
+
# `type_cast_from_database` and `type_cast_from_user` for all values
|
95
|
+
# except `nil`.
|
96
|
+
def cast_value(value) # :doc:
|
97
|
+
value
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
@@ -0,0 +1,90 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
# = Active Record RecordInvalid
|
3
|
+
#
|
4
|
+
# Raised by <tt>save!</tt> and <tt>create!</tt> when the record is invalid. Use the
|
5
|
+
# +record+ method to retrieve the record which did not validate.
|
6
|
+
#
|
7
|
+
# begin
|
8
|
+
# complex_operation_that_internally_calls_save!
|
9
|
+
# rescue ActiveRecord::RecordInvalid => invalid
|
10
|
+
# puts invalid.record.errors
|
11
|
+
# end
|
12
|
+
class RecordInvalid < ActiveRecordError
|
13
|
+
attr_reader :record
|
14
|
+
|
15
|
+
def initialize(record)
|
16
|
+
@record = record
|
17
|
+
errors = @record.errors.full_messages.join(", ")
|
18
|
+
super(I18n.t(:"#{@record.class.i18n_scope}.errors.messages.record_invalid", :errors => errors, :default => :"errors.messages.record_invalid"))
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
# = Active Record Validations
|
23
|
+
#
|
24
|
+
# Active Record includes the majority of its validations from <tt>ActiveModel::Validations</tt>
|
25
|
+
# all of which accept the <tt>:on</tt> argument to define the context where the
|
26
|
+
# validations are active. Active Record will always supply either the context of
|
27
|
+
# <tt>:create</tt> or <tt>:update</tt> dependent on whether the model is a
|
28
|
+
# <tt>new_record?</tt>.
|
29
|
+
module Validations
|
30
|
+
extend ActiveSupport::Concern
|
31
|
+
include ActiveModel::Validations
|
32
|
+
|
33
|
+
# The validation process on save can be skipped by passing <tt>validate: false</tt>.
|
34
|
+
# The regular Base#save method is replaced with this when the validations
|
35
|
+
# module is mixed in, which it is by default.
|
36
|
+
def save(options={})
|
37
|
+
perform_validations(options) ? super : false
|
38
|
+
end
|
39
|
+
|
40
|
+
# Attempts to save the record just like Base#save but will raise a +RecordInvalid+
|
41
|
+
# exception instead of returning +false+ if the record is not valid.
|
42
|
+
def save!(options={})
|
43
|
+
perform_validations(options) ? super : raise_record_invalid
|
44
|
+
end
|
45
|
+
|
46
|
+
# Runs all the validations within the specified context. Returns +true+ if
|
47
|
+
# no errors are found, +false+ otherwise.
|
48
|
+
#
|
49
|
+
# Aliased as validate.
|
50
|
+
#
|
51
|
+
# If the argument is +false+ (default is +nil+), the context is set to <tt>:create</tt> if
|
52
|
+
# <tt>new_record?</tt> is +true+, and to <tt>:update</tt> if it is not.
|
53
|
+
#
|
54
|
+
# Validations with no <tt>:on</tt> option will run no matter the context. Validations with
|
55
|
+
# some <tt>:on</tt> option will only run in the specified context.
|
56
|
+
def valid?(context = nil)
|
57
|
+
context ||= (new_record? ? :create : :update)
|
58
|
+
output = super(context)
|
59
|
+
errors.empty? && output
|
60
|
+
end
|
61
|
+
|
62
|
+
alias_method :validate, :valid?
|
63
|
+
|
64
|
+
# Runs all the validations within the specified context. Returns +true+ if
|
65
|
+
# no errors are found, raises +RecordInvalid+ otherwise.
|
66
|
+
#
|
67
|
+
# If the argument is +false+ (default is +nil+), the context is set to <tt>:create</tt> if
|
68
|
+
# <tt>new_record?</tt> is +true+, and to <tt>:update</tt> if it is not.
|
69
|
+
#
|
70
|
+
# Validations with no <tt>:on</tt> option will run no matter the context. Validations with
|
71
|
+
# some <tt>:on</tt> option will only run in the specified context.
|
72
|
+
def validate!(context = nil)
|
73
|
+
valid?(context) || raise_record_invalid
|
74
|
+
end
|
75
|
+
|
76
|
+
protected
|
77
|
+
|
78
|
+
def raise_record_invalid
|
79
|
+
raise(RecordInvalid.new(self))
|
80
|
+
end
|
81
|
+
|
82
|
+
def perform_validations(options={}) # :nodoc:
|
83
|
+
options[:validate] == false || valid?(options[:context])
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
require "active_record/validations/associated"
|
89
|
+
require "active_record/validations/uniqueness"
|
90
|
+
require "active_record/validations/presence"
|