Open pfeiffer opened 6 years ago
Hey @pfeiffer,
I'n not entirely sure on what I could do to support this. Would you mind helping with the implementation?
Cheers
The problem that is breaking reloading is that mappings are not readded if the class is redefined: https://github.com/contentful/contentful_model/blob/b286afd939daae87fdeb1f320f43753dcaae3144/lib/contentful_model/base.rb#L123
If I override this method to return false, the class gets reloaded fine in development.
class TestModel < ContentfulModel::Base
# ....
def self.mapping?
false
end
end
This happens because the rails environment persists class variables like entry_mapping
, and that mapping contains a reference to the old version of the model.
I don't know if that helps.
Here is how this problem can be fixed
module MyApp
class Application < Rails::Application
config.to_prepare do
ContentfulModel.configuration.entry_mapping.each do |content_type_id, model|
ContentfulModel.configuration.entry_mapping[content_type_id] = Object.const_get(model.to_s)
end
end
end
end
It is still an expiemintal solution I am keeping an eye on it in my current project. And if it will work I will send a PR with a more generic fix
@dlitvakb Do you still need more information in order to understand how to improve this? This has been one of the most frustrating parts of using this library :(
At the moment changing a method in a class descending from ContentfulModel::Base requires full reload of Rails environment.
Example:
Changing the
#title
method:And then reloading: