palkan/anyway_config

Issue when overriding configs with ActiveRecord class

ovinix opened this issue · 1 comments

What did you do?

class MyConfig < Anyway::Config
    attr_config some_class: ActiveRecordModel
end

What did you expect to happen?

> MyConfig.new.some_class.table_name
=> "active_record_models"

> MyConfig.new(some_class: OtherModel).some_class.table_name
=> "other_models"

What actually happened?

> MyConfig.new.some_class.table_name
=> "active_record_models"

> MyConfig.new(some_class: OtherModel).some_class.table_name
=> ""

Additional context

Looks like issue happens when dup method is called on ActiveRecord class before data was loaded to instance variables/etc.
https://github.com/rails/rails/blob/5f3ff60084ab5d5921ca3499814e4697f8350ee7/activerecord/lib/active_record/model_schema.rb#L205

E.g. in development environment:

> OtherModel.dup.table_name
=> ""

> OtherModel.table_name
=> "other_models"

> OtherModel.dup.table_name
=> "other_models"

Ruby Version:
2.6.5

Framework Version (Rails, whatever):
6.0.3

Anyway Config Version:
2.1.0

There is no issue with that on version 2.0.6

The change was introduced in 46d5f19.

We deep_dup all the configuration inputs (to make sure that the state is not shared). The older behavior (<2.1) wasn't quite correct and could lead to unexpected results (e.g., Rails.application.secrets could be modified during the config initialization).

I think, skipping duplication for classes/modules makes sense; let's do this.