diff options
-rw-r--r-- | activerecord/lib/active_record/railtie.rb | 8 | ||||
-rw-r--r-- | railties/test/application/configuration_test.rb | 47 |
2 files changed, 55 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/railtie.rb b/activerecord/lib/active_record/railtie.rb index 812fecbf32..81ad9ef3a2 100644 --- a/activerecord/lib/active_record/railtie.rb +++ b/activerecord/lib/active_record/railtie.rb @@ -137,6 +137,14 @@ end_error end end + initializer "active_record.define_attribute_methods" do |app| + config.after_initialize do + ActiveSupport.on_load(:active_record) do + descendants.each(&:define_attribute_methods) if app.config.eager_load + end + end + end + initializer "active_record.warn_on_records_fetched_greater_than" do if config.active_record.warn_on_records_fetched_greater_than ActiveSupport.on_load(:active_record) do diff --git a/railties/test/application/configuration_test.rb b/railties/test/application/configuration_test.rb index 8c10413525..9b01d42b1e 100644 --- a/railties/test/application/configuration_test.rb +++ b/railties/test/application/configuration_test.rb @@ -310,6 +310,53 @@ module ApplicationTests assert_equal %w(noop_email).to_set, PostsMailer.instance_variable_get(:@action_methods) end + test "does not eager load attribute methods in development" do + app_file "app/models/post.rb", <<-RUBY + class Post < ActiveRecord::Base + end + RUBY + + app_file "config/initializers/active_record.rb", <<-RUBY + ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:") + ActiveRecord::Migration.verbose = false + ActiveRecord::Schema.define(version: 1) do + create_table :posts do |t| + t.string :title + end + end + RUBY + + app "development" + + assert_not_includes Post.instance_methods, :title + end + + test "eager loads attribute methods in production" do + app_file "app/models/post.rb", <<-RUBY + class Post < ActiveRecord::Base + end + RUBY + + app_file "config/initializers/active_record.rb", <<-RUBY + ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:") + ActiveRecord::Migration.verbose = false + ActiveRecord::Schema.define(version: 1) do + create_table :posts do |t| + t.string :title + end + end + RUBY + + add_to_config <<-RUBY + config.eager_load = true + config.cache_classes = true + RUBY + + app "production" + + assert_includes Post.instance_methods, :title + end + test "initialize an eager loaded, cache classes app" do add_to_config <<-RUBY config.eager_load = true |