activerecord 4.1.15 → 4.2.11.3

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.

Files changed (185) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +1162 -1792
  3. data/README.rdoc +15 -10
  4. data/lib/active_record.rb +4 -0
  5. data/lib/active_record/aggregations.rb +15 -8
  6. data/lib/active_record/association_relation.rb +13 -0
  7. data/lib/active_record/associations.rb +158 -49
  8. data/lib/active_record/associations/alias_tracker.rb +3 -12
  9. data/lib/active_record/associations/association.rb +16 -4
  10. data/lib/active_record/associations/association_scope.rb +83 -38
  11. data/lib/active_record/associations/belongs_to_association.rb +28 -10
  12. data/lib/active_record/associations/builder/association.rb +15 -4
  13. data/lib/active_record/associations/builder/belongs_to.rb +7 -29
  14. data/lib/active_record/associations/builder/collection_association.rb +5 -1
  15. data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +8 -13
  16. data/lib/active_record/associations/builder/has_many.rb +1 -1
  17. data/lib/active_record/associations/builder/has_one.rb +2 -2
  18. data/lib/active_record/associations/builder/singular_association.rb +8 -1
  19. data/lib/active_record/associations/collection_association.rb +63 -27
  20. data/lib/active_record/associations/collection_proxy.rb +29 -35
  21. data/lib/active_record/associations/foreign_association.rb +11 -0
  22. data/lib/active_record/associations/has_many_association.rb +83 -22
  23. data/lib/active_record/associations/has_many_through_association.rb +49 -26
  24. data/lib/active_record/associations/has_one_association.rb +1 -1
  25. data/lib/active_record/associations/join_dependency.rb +26 -13
  26. data/lib/active_record/associations/join_dependency/join_association.rb +25 -15
  27. data/lib/active_record/associations/join_dependency/join_part.rb +0 -1
  28. data/lib/active_record/associations/preloader.rb +36 -26
  29. data/lib/active_record/associations/preloader/association.rb +14 -11
  30. data/lib/active_record/associations/preloader/through_association.rb +4 -3
  31. data/lib/active_record/associations/singular_association.rb +17 -2
  32. data/lib/active_record/associations/through_association.rb +5 -12
  33. data/lib/active_record/attribute.rb +163 -0
  34. data/lib/active_record/attribute_assignment.rb +19 -11
  35. data/lib/active_record/attribute_decorators.rb +66 -0
  36. data/lib/active_record/attribute_methods.rb +56 -94
  37. data/lib/active_record/attribute_methods/before_type_cast.rb +7 -2
  38. data/lib/active_record/attribute_methods/dirty.rb +107 -43
  39. data/lib/active_record/attribute_methods/primary_key.rb +7 -8
  40. data/lib/active_record/attribute_methods/query.rb +1 -1
  41. data/lib/active_record/attribute_methods/read.rb +22 -59
  42. data/lib/active_record/attribute_methods/serialization.rb +16 -150
  43. data/lib/active_record/attribute_methods/time_zone_conversion.rb +38 -40
  44. data/lib/active_record/attribute_methods/write.rb +9 -24
  45. data/lib/active_record/attribute_set.rb +81 -0
  46. data/lib/active_record/attribute_set/builder.rb +106 -0
  47. data/lib/active_record/attributes.rb +147 -0
  48. data/lib/active_record/autosave_association.rb +19 -12
  49. data/lib/active_record/base.rb +13 -24
  50. data/lib/active_record/callbacks.rb +6 -6
  51. data/lib/active_record/connection_adapters/abstract/connection_pool.rb +84 -52
  52. data/lib/active_record/connection_adapters/abstract/database_statements.rb +52 -50
  53. data/lib/active_record/connection_adapters/abstract/query_cache.rb +1 -1
  54. data/lib/active_record/connection_adapters/abstract/quoting.rb +60 -60
  55. data/lib/active_record/connection_adapters/abstract/savepoints.rb +1 -1
  56. data/lib/active_record/connection_adapters/abstract/schema_creation.rb +39 -4
  57. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +138 -56
  58. data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +14 -34
  59. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +268 -71
  60. data/lib/active_record/connection_adapters/abstract/transaction.rb +125 -118
  61. data/lib/active_record/connection_adapters/abstract_adapter.rb +171 -59
  62. data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +293 -139
  63. data/lib/active_record/connection_adapters/column.rb +29 -240
  64. data/lib/active_record/connection_adapters/connection_specification.rb +15 -24
  65. data/lib/active_record/connection_adapters/mysql2_adapter.rb +16 -32
  66. data/lib/active_record/connection_adapters/mysql_adapter.rb +67 -144
  67. data/lib/active_record/connection_adapters/postgresql/array_parser.rb +15 -27
  68. data/lib/active_record/connection_adapters/postgresql/column.rb +20 -0
  69. data/lib/active_record/connection_adapters/postgresql/database_statements.rb +40 -25
  70. data/lib/active_record/connection_adapters/postgresql/oid.rb +29 -388
  71. data/lib/active_record/connection_adapters/postgresql/oid/array.rb +100 -0
  72. data/lib/active_record/connection_adapters/postgresql/oid/bit.rb +52 -0
  73. data/lib/active_record/connection_adapters/postgresql/oid/bit_varying.rb +13 -0
  74. data/lib/active_record/connection_adapters/postgresql/oid/bytea.rb +15 -0
  75. data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +46 -0
  76. data/lib/active_record/connection_adapters/postgresql/oid/date.rb +11 -0
  77. data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +36 -0
  78. data/lib/active_record/connection_adapters/postgresql/oid/decimal.rb +13 -0
  79. data/lib/active_record/connection_adapters/postgresql/oid/enum.rb +19 -0
  80. data/lib/active_record/connection_adapters/postgresql/oid/float.rb +21 -0
  81. data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +59 -0
  82. data/lib/active_record/connection_adapters/postgresql/oid/inet.rb +13 -0
  83. data/lib/active_record/connection_adapters/postgresql/oid/infinity.rb +13 -0
  84. data/lib/active_record/connection_adapters/postgresql/oid/integer.rb +11 -0
  85. data/lib/active_record/connection_adapters/postgresql/oid/json.rb +35 -0
  86. data/lib/active_record/connection_adapters/postgresql/oid/jsonb.rb +23 -0
  87. data/lib/active_record/connection_adapters/postgresql/oid/money.rb +43 -0
  88. data/lib/active_record/connection_adapters/postgresql/oid/point.rb +43 -0
  89. data/lib/active_record/connection_adapters/postgresql/oid/range.rb +79 -0
  90. data/lib/active_record/connection_adapters/postgresql/oid/specialized_string.rb +19 -0
  91. data/lib/active_record/connection_adapters/postgresql/oid/time.rb +11 -0
  92. data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +109 -0
  93. data/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +21 -0
  94. data/lib/active_record/connection_adapters/postgresql/oid/vector.rb +26 -0
  95. data/lib/active_record/connection_adapters/postgresql/oid/xml.rb +28 -0
  96. data/lib/active_record/connection_adapters/postgresql/quoting.rb +46 -136
  97. data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +4 -4
  98. data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +152 -0
  99. data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +131 -43
  100. data/lib/active_record/connection_adapters/postgresql/utils.rb +77 -0
  101. data/lib/active_record/connection_adapters/postgresql_adapter.rb +224 -477
  102. data/lib/active_record/connection_adapters/schema_cache.rb +14 -28
  103. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +61 -75
  104. data/lib/active_record/connection_handling.rb +1 -1
  105. data/lib/active_record/core.rb +163 -39
  106. data/lib/active_record/counter_cache.rb +60 -6
  107. data/lib/active_record/enum.rb +9 -11
  108. data/lib/active_record/errors.rb +53 -30
  109. data/lib/active_record/explain.rb +1 -1
  110. data/lib/active_record/explain_subscriber.rb +1 -1
  111. data/lib/active_record/fixtures.rb +55 -69
  112. data/lib/active_record/gem_version.rb +4 -4
  113. data/lib/active_record/inheritance.rb +35 -10
  114. data/lib/active_record/integration.rb +4 -4
  115. data/lib/active_record/legacy_yaml_adapter.rb +30 -0
  116. data/lib/active_record/locking/optimistic.rb +46 -26
  117. data/lib/active_record/migration.rb +71 -46
  118. data/lib/active_record/migration/command_recorder.rb +19 -2
  119. data/lib/active_record/migration/join_table.rb +1 -1
  120. data/lib/active_record/model_schema.rb +52 -58
  121. data/lib/active_record/nested_attributes.rb +5 -5
  122. data/lib/active_record/no_touching.rb +1 -1
  123. data/lib/active_record/persistence.rb +46 -26
  124. data/lib/active_record/query_cache.rb +3 -3
  125. data/lib/active_record/querying.rb +10 -7
  126. data/lib/active_record/railtie.rb +18 -11
  127. data/lib/active_record/railties/databases.rake +50 -51
  128. data/lib/active_record/readonly_attributes.rb +0 -1
  129. data/lib/active_record/reflection.rb +273 -114
  130. data/lib/active_record/relation.rb +57 -25
  131. data/lib/active_record/relation/batches.rb +0 -2
  132. data/lib/active_record/relation/calculations.rb +41 -37
  133. data/lib/active_record/relation/finder_methods.rb +70 -47
  134. data/lib/active_record/relation/merger.rb +39 -29
  135. data/lib/active_record/relation/predicate_builder.rb +16 -8
  136. data/lib/active_record/relation/predicate_builder/array_handler.rb +32 -13
  137. data/lib/active_record/relation/predicate_builder/relation_handler.rb +1 -5
  138. data/lib/active_record/relation/query_methods.rb +114 -65
  139. data/lib/active_record/relation/spawn_methods.rb +3 -0
  140. data/lib/active_record/result.rb +18 -7
  141. data/lib/active_record/sanitization.rb +12 -2
  142. data/lib/active_record/schema.rb +0 -1
  143. data/lib/active_record/schema_dumper.rb +59 -28
  144. data/lib/active_record/schema_migration.rb +5 -4
  145. data/lib/active_record/scoping/default.rb +6 -4
  146. data/lib/active_record/scoping/named.rb +4 -0
  147. data/lib/active_record/serializers/xml_serializer.rb +3 -7
  148. data/lib/active_record/statement_cache.rb +95 -10
  149. data/lib/active_record/store.rb +5 -5
  150. data/lib/active_record/tasks/database_tasks.rb +61 -6
  151. data/lib/active_record/tasks/mysql_database_tasks.rb +32 -17
  152. data/lib/active_record/tasks/postgresql_database_tasks.rb +20 -9
  153. data/lib/active_record/timestamp.rb +9 -7
  154. data/lib/active_record/transactions.rb +53 -27
  155. data/lib/active_record/type.rb +23 -0
  156. data/lib/active_record/type/big_integer.rb +13 -0
  157. data/lib/active_record/type/binary.rb +50 -0
  158. data/lib/active_record/type/boolean.rb +31 -0
  159. data/lib/active_record/type/date.rb +50 -0
  160. data/lib/active_record/type/date_time.rb +54 -0
  161. data/lib/active_record/type/decimal.rb +64 -0
  162. data/lib/active_record/type/decimal_without_scale.rb +11 -0
  163. data/lib/active_record/type/decorator.rb +14 -0
  164. data/lib/active_record/type/float.rb +19 -0
  165. data/lib/active_record/type/hash_lookup_type_map.rb +23 -0
  166. data/lib/active_record/type/integer.rb +59 -0
  167. data/lib/active_record/type/mutable.rb +16 -0
  168. data/lib/active_record/type/numeric.rb +36 -0
  169. data/lib/active_record/type/serialized.rb +62 -0
  170. data/lib/active_record/type/string.rb +40 -0
  171. data/lib/active_record/type/text.rb +11 -0
  172. data/lib/active_record/type/time.rb +26 -0
  173. data/lib/active_record/type/time_value.rb +38 -0
  174. data/lib/active_record/type/type_map.rb +64 -0
  175. data/lib/active_record/type/unsigned_integer.rb +15 -0
  176. data/lib/active_record/type/value.rb +110 -0
  177. data/lib/active_record/validations.rb +25 -19
  178. data/lib/active_record/validations/associated.rb +5 -3
  179. data/lib/active_record/validations/presence.rb +5 -3
  180. data/lib/active_record/validations/uniqueness.rb +25 -29
  181. data/lib/rails/generators/active_record/migration/migration_generator.rb +8 -4
  182. data/lib/rails/generators/active_record/migration/templates/create_table_migration.rb +1 -1
  183. data/lib/rails/generators/active_record/model/templates/model.rb +1 -1
  184. metadata +66 -11
  185. data/lib/active_record/connection_adapters/postgresql/cast.rb +0 -168
@@ -0,0 +1,62 @@
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 inspect
31
+ Kernel.instance_method(:inspect).bind(self).call
32
+ end
33
+
34
+ def changed_in_place?(raw_old_value, value)
35
+ return false if value.nil?
36
+ raw_new_value = type_cast_for_database(value)
37
+ raw_old_value.nil? != raw_new_value.nil? ||
38
+ subtype.changed_in_place?(raw_old_value, raw_new_value)
39
+ end
40
+
41
+ def accessor
42
+ ActiveRecord::Store::IndifferentHashAccessor
43
+ end
44
+
45
+ def init_with(coder)
46
+ @coder = coder['coder']
47
+ super
48
+ end
49
+
50
+ def encode_with(coder)
51
+ coder['coder'] = @coder
52
+ super
53
+ end
54
+
55
+ private
56
+
57
+ def default_value?(value)
58
+ value == coder.load(nil)
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,40 @@
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
+ def text?
25
+ true
26
+ end
27
+
28
+ private
29
+
30
+ def cast_value(value)
31
+ case value
32
+ when true then "t"
33
+ when false then "f"
34
+ # String.new is slightly faster than dup
35
+ else ::String.new(value.to_s)
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,11 @@
1
+ require 'active_record/type/string'
2
+
3
+ module ActiveRecord
4
+ module Type
5
+ class Text < String # :nodoc:
6
+ def type
7
+ :text
8
+ end
9
+ end
10
+ end
11
+ 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,15 @@
1
+ module ActiveRecord
2
+ module Type
3
+ class UnsignedInteger < Integer # :nodoc:
4
+ private
5
+
6
+ def max_value
7
+ super * 2
8
+ end
9
+
10
+ def min_value
11
+ 0
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,110 @@
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 text? # :nodoc:
54
+ false
55
+ end
56
+
57
+ def number? # :nodoc:
58
+ false
59
+ end
60
+
61
+ def binary? # :nodoc:
62
+ false
63
+ end
64
+
65
+ def klass # :nodoc:
66
+ end
67
+
68
+ # Determines whether a value has changed for dirty checking. +old_value+
69
+ # and +new_value+ will always be type-cast. Types should not need to
70
+ # override this method.
71
+ def changed?(old_value, new_value, _new_value_before_type_cast)
72
+ old_value != new_value
73
+ end
74
+
75
+ # Determines whether the mutable value has been modified since it was
76
+ # read. Returns +false+ by default. This method should not be overridden
77
+ # directly. Types which return a mutable value should include
78
+ # +Type::Mutable+, which will define this method.
79
+ def changed_in_place?(*)
80
+ false
81
+ end
82
+
83
+ def ==(other)
84
+ self.class == other.class &&
85
+ precision == other.precision &&
86
+ scale == other.scale &&
87
+ limit == other.limit
88
+ end
89
+ alias eql? ==
90
+
91
+ def hash
92
+ [self.class, precision, scale, limit].hash
93
+ end
94
+
95
+ private
96
+
97
+ def type_cast(value)
98
+ cast_value(value) unless value.nil?
99
+ end
100
+
101
+ # Convenience method for types which do not need separate type casting
102
+ # behavior for user and database inputs. Called by
103
+ # `type_cast_from_database` and `type_cast_from_user` for all values
104
+ # except `nil`.
105
+ def cast_value(value) # :doc:
106
+ value
107
+ end
108
+ end
109
+ end
110
+ end
@@ -5,13 +5,14 @@ module ActiveRecord
5
5
  # +record+ method to retrieve the record which did not validate.
6
6
  #
7
7
  # begin
8
- # complex_operation_that_calls_save!_internally
8
+ # complex_operation_that_internally_calls_save!
9
9
  # rescue ActiveRecord::RecordInvalid => invalid
10
10
  # puts invalid.record.errors
11
11
  # end
12
12
  class RecordInvalid < ActiveRecordError
13
- attr_reader :record # :nodoc:
14
- def initialize(record) # :nodoc:
13
+ attr_reader :record
14
+
15
+ def initialize(record)
15
16
  @record = record
16
17
  errors = @record.errors.full_messages.join(", ")
17
18
  super(I18n.t(:"#{@record.class.i18n_scope}.errors.messages.record_invalid", :errors => errors, :default => :"errors.messages.record_invalid"))
@@ -29,21 +30,6 @@ module ActiveRecord
29
30
  extend ActiveSupport::Concern
30
31
  include ActiveModel::Validations
31
32
 
32
- module ClassMethods
33
- # Creates an object just like Base.create but calls <tt>save!</tt> instead of +save+
34
- # so an exception is raised if the record is invalid.
35
- def create!(attributes = nil, &block)
36
- if attributes.is_a?(Array)
37
- attributes.collect { |attr| create!(attr, &block) }
38
- else
39
- object = new(attributes)
40
- yield(object) if block_given?
41
- object.save!
42
- object
43
- end
44
- end
45
- end
46
-
47
33
  # The validation process on save can be skipped by passing <tt>validate: false</tt>.
48
34
  # The regular Base#save method is replaced with this when the validations
49
35
  # module is mixed in, which it is by default.
@@ -54,12 +40,14 @@ module ActiveRecord
54
40
  # Attempts to save the record just like Base#save but will raise a +RecordInvalid+
55
41
  # exception instead of returning +false+ if the record is not valid.
56
42
  def save!(options={})
57
- perform_validations(options) ? super : raise(RecordInvalid.new(self))
43
+ perform_validations(options) ? super : raise_record_invalid
58
44
  end
59
45
 
60
46
  # Runs all the validations within the specified context. Returns +true+ if
61
47
  # no errors are found, +false+ otherwise.
62
48
  #
49
+ # Aliased as validate.
50
+ #
63
51
  # If the argument is +false+ (default is +nil+), the context is set to <tt>:create</tt> if
64
52
  # <tt>new_record?</tt> is +true+, and to <tt>:update</tt> if it is not.
65
53
  #
@@ -71,8 +59,26 @@ module ActiveRecord
71
59
  errors.empty? && output
72
60
  end
73
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
+
74
76
  protected
75
77
 
78
+ def raise_record_invalid
79
+ raise(RecordInvalid.new(self))
80
+ end
81
+
76
82
  def perform_validations(options={}) # :nodoc:
77
83
  options[:validate] == false || valid?(options[:context])
78
84
  end