aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord/lib')
-rw-r--r--activerecord/lib/active_record/associations/builder/association.rb10
-rw-r--r--activerecord/lib/active_record/associations/builder/belongs_to.rb10
-rw-r--r--activerecord/lib/active_record/associations/builder/has_many.rb11
-rw-r--r--activerecord/lib/active_record/associations/builder/has_one.rb13
-rw-r--r--activerecord/lib/active_record/railtie.rb36
5 files changed, 44 insertions, 36 deletions
diff --git a/activerecord/lib/active_record/associations/builder/association.rb b/activerecord/lib/active_record/associations/builder/association.rb
index f45ab1aff4..c3f32b5ed9 100644
--- a/activerecord/lib/active_record/associations/builder/association.rb
+++ b/activerecord/lib/active_record/associations/builder/association.rb
@@ -77,6 +77,16 @@ module ActiveRecord::Associations::Builder
end
end
+ def check_valid_dependent!(dependent, valid_options)
+ unless valid_options.include?(dependent)
+ valid_options_message = valid_options.map(&:inspect).to_sentence(
+ words_connector: ', ', two_words_connector: ' or ', last_word_connector: ' or ')
+
+ raise ArgumentError, "The :dependent option expects either " \
+ "#{valid_options_message} (#{dependent.inspect})"
+ end
+ end
+
def dependent_restrict_raises?
ActiveRecord::Base.dependent_restrict_raises == true
end
diff --git a/activerecord/lib/active_record/associations/builder/belongs_to.rb b/activerecord/lib/active_record/associations/builder/belongs_to.rb
index 4bef996297..405dfc97b7 100644
--- a/activerecord/lib/active_record/associations/builder/belongs_to.rb
+++ b/activerecord/lib/active_record/associations/builder/belongs_to.rb
@@ -72,16 +72,14 @@ module ActiveRecord::Associations::Builder
end
def configure_dependency
- if options[:dependent]
- unless options[:dependent].in?([:destroy, :delete])
- raise ArgumentError, "The :dependent option expects either :destroy or :delete (#{options[:dependent].inspect})"
- end
+ if dependent = options[:dependent]
+ check_valid_dependent! dependent, [:destroy, :delete]
- method_name = "belongs_to_dependent_#{options[:dependent]}_for_#{name}"
+ method_name = "belongs_to_dependent_#{dependent}_for_#{name}"
model.send(:class_eval, <<-eoruby, __FILE__, __LINE__ + 1)
def #{method_name}
association = #{name}
- association.#{options[:dependent]} if association
+ association.#{dependent} if association
end
eoruby
model.after_destroy method_name
diff --git a/activerecord/lib/active_record/associations/builder/has_many.rb b/activerecord/lib/active_record/associations/builder/has_many.rb
index 81df1fb135..d0bdfa7d9c 100644
--- a/activerecord/lib/active_record/associations/builder/has_many.rb
+++ b/activerecord/lib/active_record/associations/builder/has_many.rb
@@ -19,14 +19,11 @@ module ActiveRecord::Associations::Builder
private
def configure_dependency
- if options[:dependent]
- unless options[:dependent].in?([:destroy, :delete_all, :nullify, :restrict])
- raise ArgumentError, "The :dependent option expects either :destroy, :delete_all, " \
- ":nullify or :restrict (#{options[:dependent].inspect})"
- end
+ if dependent = options[:dependent]
+ check_valid_dependent! dependent, [:destroy, :delete_all, :nullify, :restrict]
+ dependent_restrict_deprecation_warning if dependent == :restrict
- dependent_restrict_deprecation_warning if options[:dependent] == :restrict
- send("define_#{options[:dependent]}_dependency_method")
+ send("define_#{dependent}_dependency_method")
model.before_destroy dependency_method_name
end
end
diff --git a/activerecord/lib/active_record/associations/builder/has_one.rb b/activerecord/lib/active_record/associations/builder/has_one.rb
index cdb45e8e58..cd6d9c871b 100644
--- a/activerecord/lib/active_record/associations/builder/has_one.rb
+++ b/activerecord/lib/active_record/associations/builder/has_one.rb
@@ -25,14 +25,11 @@ module ActiveRecord::Associations::Builder
private
def configure_dependency
- if options[:dependent]
- unless options[:dependent].in?([:destroy, :delete, :nullify, :restrict])
- raise ArgumentError, "The :dependent option expects either :destroy, :delete, " \
- ":nullify or :restrict (#{options[:dependent].inspect})"
- end
-
- dependent_restrict_deprecation_warning if options[:dependent] == :restrict
- send("define_#{options[:dependent]}_dependency_method")
+ if dependent = options[:dependent]
+ check_valid_dependent! dependent, [:destroy, :delete, :nullify, :restrict]
+ dependent_restrict_deprecation_warning if dependent == :restrict
+
+ send("define_#{dependent}_dependency_method")
model.before_destroy dependency_method_name
end
end
diff --git a/activerecord/lib/active_record/railtie.rb b/activerecord/lib/active_record/railtie.rb
index 9432a70c41..672d9a4246 100644
--- a/activerecord/lib/active_record/railtie.rb
+++ b/activerecord/lib/active_record/railtie.rb
@@ -29,6 +29,8 @@ module ActiveRecord
'ActiveRecord::RecordNotSaved' => :unprocessable_entity
)
+ config.active_record.use_schema_cache_dump = true
+
rake_tasks do
require "active_record/base"
load "active_record/railties/databases.rake"
@@ -66,6 +68,25 @@ module ActiveRecord
end
end
+ initializer "active_record.check_schema_cache_dump" do |app|
+ if config.active_record.delete(:use_schema_cache_dump)
+ config.after_initialize do |app|
+ ActiveSupport.on_load(:active_record) do
+ filename = File.join(app.config.paths["db"].first, "schema_cache.dump")
+
+ if File.file?(filename)
+ cache = Marshal.load File.binread filename
+ if cache.version == ActiveRecord::Migrator.current_version
+ ActiveRecord::Model.connection.schema_cache = cache
+ else
+ warn "schema_cache.dump is expired. Current version is #{ActiveRecord::Migrator.current_version}, but cache version is #{cache.version}."
+ end
+ end
+ end
+ end
+ end
+ end
+
initializer "active_record.set_configs" do |app|
ActiveSupport.on_load(:active_record) do
app.config.active_record.each do |k,v|
@@ -117,21 +138,6 @@ module ActiveRecord
end
end
- ActiveSupport.on_load(:active_record) do
- if app.config.use_schema_cache_dump
- filename = File.join(app.config.paths["db"].first, "schema_cache.dump")
-
- if File.file?(filename)
- cache = Marshal.load File.binread filename
- if cache.version == ActiveRecord::Migrator.current_version
- ActiveRecord::Model.connection.schema_cache = cache
- else
- warn "schema_cache.dump is expired. Current version is #{ActiveRecord::Migrator.current_version}, but cache version is #{cache.version}."
- end
- end
- end
- end
-
end
end
end