sskirby-activerecord 3.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG.md +6749 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +222 -0
- data/examples/associations.png +0 -0
- data/examples/performance.rb +177 -0
- data/examples/simple.rb +14 -0
- data/lib/active_record.rb +147 -0
- data/lib/active_record/aggregations.rb +255 -0
- data/lib/active_record/associations.rb +1604 -0
- data/lib/active_record/associations/alias_tracker.rb +79 -0
- data/lib/active_record/associations/association.rb +239 -0
- data/lib/active_record/associations/association_scope.rb +119 -0
- data/lib/active_record/associations/belongs_to_association.rb +79 -0
- data/lib/active_record/associations/belongs_to_polymorphic_association.rb +34 -0
- data/lib/active_record/associations/builder/association.rb +55 -0
- data/lib/active_record/associations/builder/belongs_to.rb +85 -0
- data/lib/active_record/associations/builder/collection_association.rb +75 -0
- data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +57 -0
- data/lib/active_record/associations/builder/has_many.rb +71 -0
- data/lib/active_record/associations/builder/has_one.rb +62 -0
- data/lib/active_record/associations/builder/singular_association.rb +32 -0
- data/lib/active_record/associations/collection_association.rb +574 -0
- data/lib/active_record/associations/collection_proxy.rb +132 -0
- data/lib/active_record/associations/has_and_belongs_to_many_association.rb +62 -0
- data/lib/active_record/associations/has_many_association.rb +108 -0
- data/lib/active_record/associations/has_many_through_association.rb +180 -0
- data/lib/active_record/associations/has_one_association.rb +73 -0
- data/lib/active_record/associations/has_one_through_association.rb +36 -0
- data/lib/active_record/associations/join_dependency.rb +214 -0
- data/lib/active_record/associations/join_dependency/join_association.rb +154 -0
- data/lib/active_record/associations/join_dependency/join_base.rb +24 -0
- data/lib/active_record/associations/join_dependency/join_part.rb +78 -0
- data/lib/active_record/associations/join_helper.rb +55 -0
- data/lib/active_record/associations/preloader.rb +177 -0
- data/lib/active_record/associations/preloader/association.rb +127 -0
- data/lib/active_record/associations/preloader/belongs_to.rb +17 -0
- data/lib/active_record/associations/preloader/collection_association.rb +24 -0
- data/lib/active_record/associations/preloader/has_and_belongs_to_many.rb +60 -0
- data/lib/active_record/associations/preloader/has_many.rb +17 -0
- data/lib/active_record/associations/preloader/has_many_through.rb +15 -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 +67 -0
- data/lib/active_record/associations/singular_association.rb +64 -0
- data/lib/active_record/associations/through_association.rb +83 -0
- data/lib/active_record/attribute_assignment.rb +221 -0
- data/lib/active_record/attribute_methods.rb +272 -0
- data/lib/active_record/attribute_methods/before_type_cast.rb +31 -0
- data/lib/active_record/attribute_methods/deprecated_underscore_read.rb +32 -0
- data/lib/active_record/attribute_methods/dirty.rb +101 -0
- data/lib/active_record/attribute_methods/primary_key.rb +114 -0
- data/lib/active_record/attribute_methods/query.rb +39 -0
- data/lib/active_record/attribute_methods/read.rb +135 -0
- data/lib/active_record/attribute_methods/serialization.rb +93 -0
- data/lib/active_record/attribute_methods/time_zone_conversion.rb +62 -0
- data/lib/active_record/attribute_methods/write.rb +69 -0
- data/lib/active_record/autosave_association.rb +422 -0
- data/lib/active_record/base.rb +716 -0
- data/lib/active_record/callbacks.rb +275 -0
- data/lib/active_record/coders/yaml_column.rb +41 -0
- data/lib/active_record/connection_adapters/abstract/connection_pool.rb +452 -0
- data/lib/active_record/connection_adapters/abstract/connection_specification.rb +188 -0
- data/lib/active_record/connection_adapters/abstract/database_limits.rb +58 -0
- data/lib/active_record/connection_adapters/abstract/database_statements.rb +388 -0
- data/lib/active_record/connection_adapters/abstract/query_cache.rb +82 -0
- data/lib/active_record/connection_adapters/abstract/quoting.rb +115 -0
- data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +492 -0
- data/lib/active_record/connection_adapters/abstract/schema_statements.rb +598 -0
- data/lib/active_record/connection_adapters/abstract_adapter.rb +296 -0
- data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +653 -0
- data/lib/active_record/connection_adapters/column.rb +270 -0
- data/lib/active_record/connection_adapters/mysql2_adapter.rb +288 -0
- data/lib/active_record/connection_adapters/mysql_adapter.rb +426 -0
- data/lib/active_record/connection_adapters/postgresql_adapter.rb +1261 -0
- data/lib/active_record/connection_adapters/schema_cache.rb +50 -0
- data/lib/active_record/connection_adapters/sqlite3_adapter.rb +55 -0
- data/lib/active_record/connection_adapters/sqlite_adapter.rb +577 -0
- data/lib/active_record/connection_adapters/statement_pool.rb +40 -0
- data/lib/active_record/counter_cache.rb +119 -0
- data/lib/active_record/dynamic_finder_match.rb +56 -0
- data/lib/active_record/dynamic_matchers.rb +79 -0
- data/lib/active_record/dynamic_scope_match.rb +23 -0
- data/lib/active_record/errors.rb +195 -0
- data/lib/active_record/explain.rb +85 -0
- data/lib/active_record/explain_subscriber.rb +21 -0
- data/lib/active_record/fixtures.rb +906 -0
- data/lib/active_record/fixtures/file.rb +65 -0
- data/lib/active_record/identity_map.rb +156 -0
- data/lib/active_record/inheritance.rb +167 -0
- data/lib/active_record/integration.rb +49 -0
- data/lib/active_record/locale/en.yml +40 -0
- data/lib/active_record/locking/optimistic.rb +183 -0
- data/lib/active_record/locking/pessimistic.rb +77 -0
- data/lib/active_record/log_subscriber.rb +68 -0
- data/lib/active_record/migration.rb +765 -0
- data/lib/active_record/migration/command_recorder.rb +105 -0
- data/lib/active_record/model_schema.rb +366 -0
- data/lib/active_record/nested_attributes.rb +469 -0
- data/lib/active_record/observer.rb +121 -0
- data/lib/active_record/persistence.rb +372 -0
- data/lib/active_record/query_cache.rb +74 -0
- data/lib/active_record/querying.rb +58 -0
- data/lib/active_record/railtie.rb +119 -0
- data/lib/active_record/railties/console_sandbox.rb +6 -0
- data/lib/active_record/railties/controller_runtime.rb +49 -0
- data/lib/active_record/railties/databases.rake +620 -0
- data/lib/active_record/railties/jdbcmysql_error.rb +16 -0
- data/lib/active_record/readonly_attributes.rb +26 -0
- data/lib/active_record/reflection.rb +534 -0
- data/lib/active_record/relation.rb +534 -0
- data/lib/active_record/relation/batches.rb +90 -0
- data/lib/active_record/relation/calculations.rb +354 -0
- data/lib/active_record/relation/delegation.rb +49 -0
- data/lib/active_record/relation/finder_methods.rb +398 -0
- data/lib/active_record/relation/predicate_builder.rb +58 -0
- data/lib/active_record/relation/query_methods.rb +417 -0
- data/lib/active_record/relation/spawn_methods.rb +148 -0
- data/lib/active_record/result.rb +34 -0
- data/lib/active_record/sanitization.rb +194 -0
- data/lib/active_record/schema.rb +58 -0
- data/lib/active_record/schema_dumper.rb +204 -0
- data/lib/active_record/scoping.rb +152 -0
- data/lib/active_record/scoping/default.rb +142 -0
- data/lib/active_record/scoping/named.rb +202 -0
- data/lib/active_record/serialization.rb +18 -0
- data/lib/active_record/serializers/xml_serializer.rb +202 -0
- data/lib/active_record/session_store.rb +358 -0
- data/lib/active_record/store.rb +50 -0
- data/lib/active_record/test_case.rb +73 -0
- data/lib/active_record/timestamp.rb +113 -0
- data/lib/active_record/transactions.rb +360 -0
- data/lib/active_record/translation.rb +22 -0
- data/lib/active_record/validations.rb +83 -0
- data/lib/active_record/validations/associated.rb +43 -0
- data/lib/active_record/validations/uniqueness.rb +180 -0
- data/lib/active_record/version.rb +10 -0
- data/lib/rails/generators/active_record.rb +25 -0
- data/lib/rails/generators/active_record/migration.rb +15 -0
- data/lib/rails/generators/active_record/migration/migration_generator.rb +25 -0
- data/lib/rails/generators/active_record/migration/templates/migration.rb +31 -0
- data/lib/rails/generators/active_record/model/model_generator.rb +43 -0
- data/lib/rails/generators/active_record/model/templates/migration.rb +15 -0
- data/lib/rails/generators/active_record/model/templates/model.rb +7 -0
- data/lib/rails/generators/active_record/model/templates/module.rb +7 -0
- data/lib/rails/generators/active_record/observer/observer_generator.rb +15 -0
- data/lib/rails/generators/active_record/observer/templates/observer.rb +4 -0
- data/lib/rails/generators/active_record/session_migration/session_migration_generator.rb +25 -0
- data/lib/rails/generators/active_record/session_migration/templates/migration.rb +12 -0
- metadata +242 -0
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
module ActiveRecord
|
|
2
|
+
module Translation
|
|
3
|
+
include ActiveModel::Translation
|
|
4
|
+
|
|
5
|
+
# Set the lookup ancestors for ActiveModel.
|
|
6
|
+
def lookup_ancestors #:nodoc:
|
|
7
|
+
klass = self
|
|
8
|
+
classes = [klass]
|
|
9
|
+
return classes if klass == ActiveRecord::Base
|
|
10
|
+
|
|
11
|
+
while klass != klass.base_class
|
|
12
|
+
classes << klass = klass.superclass
|
|
13
|
+
end
|
|
14
|
+
classes
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
# Set the i18n scope to overwrite ActiveModel.
|
|
18
|
+
def i18n_scope #:nodoc:
|
|
19
|
+
:activerecord
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
@@ -0,0 +1,83 @@
|
|
|
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_calls_save!_internally
|
|
9
|
+
# rescue ActiveRecord::RecordInvalid => invalid
|
|
10
|
+
# puts invalid.record.errors
|
|
11
|
+
# end
|
|
12
|
+
class RecordInvalid < ActiveRecordError
|
|
13
|
+
attr_reader :record
|
|
14
|
+
def initialize(record)
|
|
15
|
+
@record = record
|
|
16
|
+
errors = @record.errors.full_messages.join(", ")
|
|
17
|
+
super(I18n.t("activerecord.errors.messages.record_invalid", :errors => errors))
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# = Active Record Validations
|
|
22
|
+
#
|
|
23
|
+
# Active Record includes the majority of its validations from <tt>ActiveModel::Validations</tt>
|
|
24
|
+
# all of which accept the <tt>:on</tt> argument to define the context where the
|
|
25
|
+
# validations are active. Active Record will always supply either the context of
|
|
26
|
+
# <tt>:create</tt> or <tt>:update</tt> dependent on whether the model is a
|
|
27
|
+
# <tt>new_record?</tt>.
|
|
28
|
+
module Validations
|
|
29
|
+
extend ActiveSupport::Concern
|
|
30
|
+
include ActiveModel::Validations
|
|
31
|
+
|
|
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, options = {}, &block)
|
|
36
|
+
if attributes.is_a?(Array)
|
|
37
|
+
attributes.collect { |attr| create!(attr, options, &block) }
|
|
38
|
+
else
|
|
39
|
+
object = new(attributes, options)
|
|
40
|
+
yield(object) if block_given?
|
|
41
|
+
object.save!
|
|
42
|
+
object
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# The validation process on save can be skipped by passing <tt>:validate => false</tt>. The regular Base#save method is
|
|
48
|
+
# replaced with this when the validations module is mixed in, which it is by default.
|
|
49
|
+
def save(options={})
|
|
50
|
+
perform_validations(options) ? super : false
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# Attempts to save the record just like Base#save but will raise a +RecordInvalid+ exception instead of returning false
|
|
54
|
+
# if the record is not valid.
|
|
55
|
+
def save!(options={})
|
|
56
|
+
perform_validations(options) ? super : raise(RecordInvalid.new(self))
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# Runs all the validations within the specified context. Returns true if no errors are found,
|
|
60
|
+
# false otherwise.
|
|
61
|
+
#
|
|
62
|
+
# If the argument is false (default is +nil+), the context is set to <tt>:create</tt> if
|
|
63
|
+
# <tt>new_record?</tt> is true, and to <tt>:update</tt> if it is not.
|
|
64
|
+
#
|
|
65
|
+
# Validations with no <tt>:on</tt> option will run no matter the context. Validations with
|
|
66
|
+
# some <tt>:on</tt> option will only run in the specified context.
|
|
67
|
+
def valid?(context = nil)
|
|
68
|
+
context ||= (new_record? ? :create : :update)
|
|
69
|
+
output = super(context)
|
|
70
|
+
errors.empty? && output
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
protected
|
|
74
|
+
|
|
75
|
+
def perform_validations(options={})
|
|
76
|
+
perform_validation = options[:validate] != false
|
|
77
|
+
perform_validation ? valid?(options[:context]) : true
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
require "active_record/validations/associated"
|
|
83
|
+
require "active_record/validations/uniqueness"
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
module ActiveRecord
|
|
2
|
+
module Validations
|
|
3
|
+
class AssociatedValidator < ActiveModel::EachValidator
|
|
4
|
+
def validate_each(record, attribute, value)
|
|
5
|
+
if Array.wrap(value).reject {|r| r.marked_for_destruction? || r.valid?}.any?
|
|
6
|
+
record.errors.add(attribute, :invalid, options.merge(:value => value))
|
|
7
|
+
end
|
|
8
|
+
end
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
module ClassMethods
|
|
12
|
+
# Validates whether the associated object or objects are all valid themselves. Works with any kind of association.
|
|
13
|
+
#
|
|
14
|
+
# class Book < ActiveRecord::Base
|
|
15
|
+
# has_many :pages
|
|
16
|
+
# belongs_to :library
|
|
17
|
+
#
|
|
18
|
+
# validates_associated :pages, :library
|
|
19
|
+
# end
|
|
20
|
+
#
|
|
21
|
+
# WARNING: This validation must not be used on both ends of an association. Doing so will lead to a circular dependency and cause infinite recursion.
|
|
22
|
+
#
|
|
23
|
+
# NOTE: This validation will not fail if the association hasn't been assigned. If you want to
|
|
24
|
+
# ensure that the association is both present and guaranteed to be valid, you also need to
|
|
25
|
+
# use +validates_presence_of+.
|
|
26
|
+
#
|
|
27
|
+
# Configuration options:
|
|
28
|
+
# * <tt>:message</tt> - A custom error message (default is: "is invalid")
|
|
29
|
+
# * <tt>:on</tt> - Specifies when this validation is active. Runs in all
|
|
30
|
+
# validation contexts by default (+nil+), other options are <tt>:create</tt>
|
|
31
|
+
# and <tt>:update</tt>.
|
|
32
|
+
# * <tt>:if</tt> - Specifies a method, proc or string to call to determine if the validation should
|
|
33
|
+
# occur (e.g. <tt>:if => :allow_validation</tt>, or <tt>:if => Proc.new { |user| user.signup_step > 2 }</tt>). The
|
|
34
|
+
# method, proc or string should return or evaluate to a true or false value.
|
|
35
|
+
# * <tt>:unless</tt> - Specifies a method, proc or string to call to determine if the validation should
|
|
36
|
+
# not occur (e.g. <tt>:unless => :skip_validation</tt>, or <tt>:unless => Proc.new { |user| user.signup_step <= 2 }</tt>). The
|
|
37
|
+
# method, proc or string should return or evaluate to a true or false value.
|
|
38
|
+
def validates_associated(*attr_names)
|
|
39
|
+
validates_with AssociatedValidator, _merge_attributes(attr_names)
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
require 'active_support/core_ext/array/wrap'
|
|
2
|
+
|
|
3
|
+
module ActiveRecord
|
|
4
|
+
module Validations
|
|
5
|
+
class UniquenessValidator < ActiveModel::EachValidator
|
|
6
|
+
def initialize(options)
|
|
7
|
+
super(options.reverse_merge(:case_sensitive => true))
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
# Unfortunately, we have to tie Uniqueness validators to a class.
|
|
11
|
+
def setup(klass)
|
|
12
|
+
@klass = klass
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def validate_each(record, attribute, value)
|
|
16
|
+
finder_class = find_finder_class_for(record)
|
|
17
|
+
table = finder_class.arel_table
|
|
18
|
+
|
|
19
|
+
coder = record.class.serialized_attributes[attribute.to_s]
|
|
20
|
+
|
|
21
|
+
if value && coder
|
|
22
|
+
value = coder.dump value
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
relation = build_relation(finder_class, table, attribute, value)
|
|
26
|
+
relation = relation.and(table[finder_class.primary_key.to_sym].not_eq(record.send(:id))) if record.persisted?
|
|
27
|
+
|
|
28
|
+
Array.wrap(options[:scope]).each do |scope_item|
|
|
29
|
+
scope_value = record.send(scope_item)
|
|
30
|
+
relation = relation.and(table[scope_item].eq(scope_value))
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
if finder_class.unscoped.where(relation).exists?
|
|
34
|
+
record.errors.add(attribute, :taken, options.except(:case_sensitive, :scope).merge(:value => value))
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
protected
|
|
39
|
+
|
|
40
|
+
# The check for an existing value should be run from a class that
|
|
41
|
+
# isn't abstract. This means working down from the current class
|
|
42
|
+
# (self), to the first non-abstract class. Since classes don't know
|
|
43
|
+
# their subclasses, we have to build the hierarchy between self and
|
|
44
|
+
# the record's class.
|
|
45
|
+
def find_finder_class_for(record) #:nodoc:
|
|
46
|
+
class_hierarchy = [record.class]
|
|
47
|
+
|
|
48
|
+
while class_hierarchy.first != @klass
|
|
49
|
+
class_hierarchy.insert(0, class_hierarchy.first.superclass)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
class_hierarchy.detect { |klass| !klass.abstract_class? }
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def build_relation(klass, table, attribute, value) #:nodoc:
|
|
56
|
+
column = klass.columns_hash[attribute.to_s]
|
|
57
|
+
value = column.limit ? value.to_s.mb_chars[0, column.limit] : value.to_s if column.text?
|
|
58
|
+
|
|
59
|
+
if !options[:case_sensitive] && value && column.text?
|
|
60
|
+
# will use SQL LOWER function before comparison, unless it detects a case insensitive collation
|
|
61
|
+
relation = klass.connection.case_insensitive_comparison(table, attribute, column, value)
|
|
62
|
+
else
|
|
63
|
+
value = klass.connection.case_sensitive_modifier(value)
|
|
64
|
+
relation = table[attribute].eq(value)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
relation
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
module ClassMethods
|
|
72
|
+
# Validates whether the value of the specified attributes are unique across the system.
|
|
73
|
+
# Useful for making sure that only one user
|
|
74
|
+
# can be named "davidhh".
|
|
75
|
+
#
|
|
76
|
+
# class Person < ActiveRecord::Base
|
|
77
|
+
# validates_uniqueness_of :user_name
|
|
78
|
+
# end
|
|
79
|
+
#
|
|
80
|
+
# It can also validate whether the value of the specified attributes are unique based on a scope parameter:
|
|
81
|
+
#
|
|
82
|
+
# class Person < ActiveRecord::Base
|
|
83
|
+
# validates_uniqueness_of :user_name, :scope => :account_id
|
|
84
|
+
# end
|
|
85
|
+
#
|
|
86
|
+
# Or even multiple scope parameters. For example, making sure that a teacher can only be on the schedule once
|
|
87
|
+
# per semester for a particular class.
|
|
88
|
+
#
|
|
89
|
+
# class TeacherSchedule < ActiveRecord::Base
|
|
90
|
+
# validates_uniqueness_of :teacher_id, :scope => [:semester_id, :class_id]
|
|
91
|
+
# end
|
|
92
|
+
#
|
|
93
|
+
# When the record is created, a check is performed to make sure that no record exists in the database
|
|
94
|
+
# with the given value for the specified attribute (that maps to a column). When the record is updated,
|
|
95
|
+
# the same check is made but disregarding the record itself.
|
|
96
|
+
#
|
|
97
|
+
# Configuration options:
|
|
98
|
+
# * <tt>:message</tt> - Specifies a custom error message (default is: "has already been taken").
|
|
99
|
+
# * <tt>:scope</tt> - One or more columns by which to limit the scope of the uniqueness constraint.
|
|
100
|
+
# * <tt>:case_sensitive</tt> - Looks for an exact match. Ignored by non-text columns (+true+ by default).
|
|
101
|
+
# * <tt>:allow_nil</tt> - If set to true, skips this validation if the attribute is +nil+ (default is +false+).
|
|
102
|
+
# * <tt>:allow_blank</tt> - If set to true, skips this validation if the attribute is blank (default is +false+).
|
|
103
|
+
# * <tt>:if</tt> - Specifies a method, proc or string to call to determine if the validation should
|
|
104
|
+
# occur (e.g. <tt>:if => :allow_validation</tt>, or <tt>:if => Proc.new { |user| user.signup_step > 2 }</tt>).
|
|
105
|
+
# The method, proc or string should return or evaluate to a true or false value.
|
|
106
|
+
# * <tt>:unless</tt> - Specifies a method, proc or string to call to determine if the validation should
|
|
107
|
+
# not occur (e.g. <tt>:unless => :skip_validation</tt>, or
|
|
108
|
+
# <tt>:unless => Proc.new { |user| user.signup_step <= 2 }</tt>). The method, proc or string should
|
|
109
|
+
# return or evaluate to a true or false value.
|
|
110
|
+
#
|
|
111
|
+
# === Concurrency and integrity
|
|
112
|
+
#
|
|
113
|
+
# Using this validation method in conjunction with ActiveRecord::Base#save
|
|
114
|
+
# does not guarantee the absence of duplicate record insertions, because
|
|
115
|
+
# uniqueness checks on the application level are inherently prone to race
|
|
116
|
+
# conditions. For example, suppose that two users try to post a Comment at
|
|
117
|
+
# the same time, and a Comment's title must be unique. At the database-level,
|
|
118
|
+
# the actions performed by these users could be interleaved in the following manner:
|
|
119
|
+
#
|
|
120
|
+
# User 1 | User 2
|
|
121
|
+
# ------------------------------------+--------------------------------------
|
|
122
|
+
# # User 1 checks whether there's |
|
|
123
|
+
# # already a comment with the title |
|
|
124
|
+
# # 'My Post'. This is not the case. |
|
|
125
|
+
# SELECT * FROM comments |
|
|
126
|
+
# WHERE title = 'My Post' |
|
|
127
|
+
# |
|
|
128
|
+
# | # User 2 does the same thing and also
|
|
129
|
+
# | # infers that his title is unique.
|
|
130
|
+
# | SELECT * FROM comments
|
|
131
|
+
# | WHERE title = 'My Post'
|
|
132
|
+
# |
|
|
133
|
+
# # User 1 inserts his comment. |
|
|
134
|
+
# INSERT INTO comments |
|
|
135
|
+
# (title, content) VALUES |
|
|
136
|
+
# ('My Post', 'hi!') |
|
|
137
|
+
# |
|
|
138
|
+
# | # User 2 does the same thing.
|
|
139
|
+
# | INSERT INTO comments
|
|
140
|
+
# | (title, content) VALUES
|
|
141
|
+
# | ('My Post', 'hello!')
|
|
142
|
+
# |
|
|
143
|
+
# | # ^^^^^^
|
|
144
|
+
# | # Boom! We now have a duplicate
|
|
145
|
+
# | # title!
|
|
146
|
+
#
|
|
147
|
+
# This could even happen if you use transactions with the 'serializable'
|
|
148
|
+
# isolation level. The best way to work around this problem is to add a unique
|
|
149
|
+
# index to the database table using
|
|
150
|
+
# ActiveRecord::ConnectionAdapters::SchemaStatements#add_index. In the
|
|
151
|
+
# rare case that a race condition occurs, the database will guarantee
|
|
152
|
+
# the field's uniqueness.
|
|
153
|
+
#
|
|
154
|
+
# When the database catches such a duplicate insertion,
|
|
155
|
+
# ActiveRecord::Base#save will raise an ActiveRecord::StatementInvalid
|
|
156
|
+
# exception. You can either choose to let this error propagate (which
|
|
157
|
+
# will result in the default Rails exception page being shown), or you
|
|
158
|
+
# can catch it and restart the transaction (e.g. by telling the user
|
|
159
|
+
# that the title already exists, and asking him to re-enter the title).
|
|
160
|
+
# This technique is also known as optimistic concurrency control:
|
|
161
|
+
# http://en.wikipedia.org/wiki/Optimistic_concurrency_control
|
|
162
|
+
#
|
|
163
|
+
# The bundled ActiveRecord::ConnectionAdapters distinguish unique index
|
|
164
|
+
# constraint errors from other types of database errors by throwing an
|
|
165
|
+
# ActiveRecord::RecordNotUnique exception.
|
|
166
|
+
# For other adapters you will have to parse the (database-specific) exception
|
|
167
|
+
# message to detect such a case.
|
|
168
|
+
# The following bundled adapters throw the ActiveRecord::RecordNotUnique exception:
|
|
169
|
+
# * ActiveRecord::ConnectionAdapters::MysqlAdapter
|
|
170
|
+
# * ActiveRecord::ConnectionAdapters::Mysql2Adapter
|
|
171
|
+
# * ActiveRecord::ConnectionAdapters::SQLiteAdapter
|
|
172
|
+
# * ActiveRecord::ConnectionAdapters::SQLite3Adapter
|
|
173
|
+
# * ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
|
|
174
|
+
#
|
|
175
|
+
def validates_uniqueness_of(*attr_names)
|
|
176
|
+
validates_with UniquenessValidator, _merge_attributes(attr_names)
|
|
177
|
+
end
|
|
178
|
+
end
|
|
179
|
+
end
|
|
180
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
require 'rails/generators/named_base'
|
|
2
|
+
require 'rails/generators/migration'
|
|
3
|
+
require 'rails/generators/active_model'
|
|
4
|
+
require 'rails/generators/active_record/migration'
|
|
5
|
+
require 'active_record'
|
|
6
|
+
|
|
7
|
+
module ActiveRecord
|
|
8
|
+
module Generators
|
|
9
|
+
class Base < Rails::Generators::NamedBase #:nodoc:
|
|
10
|
+
include Rails::Generators::Migration
|
|
11
|
+
extend ActiveRecord::Generators::Migration
|
|
12
|
+
|
|
13
|
+
# Set the current directory as base for the inherited generators.
|
|
14
|
+
def self.base_root
|
|
15
|
+
File.dirname(__FILE__)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# Implement the required interface for Rails::Generators::Migration.
|
|
19
|
+
def self.next_migration_number(dirname) #:nodoc:
|
|
20
|
+
next_migration_number = current_migration_number(dirname) + 1
|
|
21
|
+
ActiveRecord::Migration.next_migration_number(next_migration_number)
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
module ActiveRecord
|
|
2
|
+
module Generators
|
|
3
|
+
module Migration
|
|
4
|
+
# Implement the required interface for Rails::Generators::Migration.
|
|
5
|
+
def next_migration_number(dirname) #:nodoc:
|
|
6
|
+
next_migration_number = current_migration_number(dirname) + 1
|
|
7
|
+
if ActiveRecord::Base.timestamped_migrations
|
|
8
|
+
[Time.now.utc.strftime("%Y%m%d%H%M%S"), "%.14d" % next_migration_number].max
|
|
9
|
+
else
|
|
10
|
+
"%.3d" % next_migration_number
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
require 'rails/generators/active_record'
|
|
2
|
+
|
|
3
|
+
module ActiveRecord
|
|
4
|
+
module Generators
|
|
5
|
+
class MigrationGenerator < Base
|
|
6
|
+
argument :attributes, :type => :array, :default => [], :banner => "field[:type][:index] field[:type][:index]"
|
|
7
|
+
|
|
8
|
+
def create_migration_file
|
|
9
|
+
set_local_assigns!
|
|
10
|
+
migration_template "migration.rb", "db/migrate/#{file_name}.rb"
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
protected
|
|
14
|
+
attr_reader :migration_action
|
|
15
|
+
|
|
16
|
+
def set_local_assigns!
|
|
17
|
+
if file_name =~ /^(add|remove)_.*_(?:to|from)_(.*)/
|
|
18
|
+
@migration_action = $1
|
|
19
|
+
@table_name = $2.pluralize
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
class <%= migration_class_name %> < ActiveRecord::Migration
|
|
2
|
+
<%- if migration_action == 'add' -%>
|
|
3
|
+
def change
|
|
4
|
+
<% attributes.each do |attribute| -%>
|
|
5
|
+
add_column :<%= table_name %>, :<%= attribute.name %>, :<%= attribute.type %><%= attribute.inject_options %>
|
|
6
|
+
<%- if attribute.has_index? -%>
|
|
7
|
+
add_index :<%= table_name %>, :<%= attribute.index_name %><%= attribute.inject_index_options %>
|
|
8
|
+
<%- end %>
|
|
9
|
+
<%- end -%>
|
|
10
|
+
end
|
|
11
|
+
<%- else -%>
|
|
12
|
+
def up
|
|
13
|
+
<% attributes.each do |attribute| -%>
|
|
14
|
+
<%- if migration_action -%>
|
|
15
|
+
<%= migration_action %>_column :<%= table_name %>, :<%= attribute.name %><% if migration_action == 'add' %>, :<%= attribute.type %><%= attribute.inject_options %><% end %>
|
|
16
|
+
<% if attribute.has_index? && migration_action == 'add' %>
|
|
17
|
+
add_index :<%= table_name %>, :<%= attribute.index_name %><%= attribute.inject_index_options %>
|
|
18
|
+
<% end -%>
|
|
19
|
+
<%- end -%>
|
|
20
|
+
<%- end -%>
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def down
|
|
24
|
+
<% attributes.reverse.each do |attribute| -%>
|
|
25
|
+
<%- if migration_action -%>
|
|
26
|
+
<%= migration_action == 'add' ? 'remove' : 'add' %>_column :<%= table_name %>, :<%= attribute.name %><% if migration_action == 'remove' %>, :<%= attribute.type %><%= attribute.inject_options %><% end %>
|
|
27
|
+
<%- end -%>
|
|
28
|
+
<%- end -%>
|
|
29
|
+
end
|
|
30
|
+
<%- end -%>
|
|
31
|
+
end
|