I have some logic in several of my rails models that I would like to split out into separate files.
Specifically, this is logic that is unique to these models and not something that would be shared between models. For that case I am aware of concerns/mixins and questions like this.
Since we are dealing with Ruby here it seems like the way to go is to have multiple class definitions. E.g:
# in app/models/user.rb
class User < ActiveRecord::Base
...
end
# in app/lib/integrations/ext/user.rb
class User
...
end
The problem I am facing here is now requiring the model extensions in the right place. Because of auto-loading, I am forced to explicitly require the model and the extension. My current best effort is to preload the User model and its extension inside of an initializer:
# in config/initializers/model_extensions.rb
require_dependency 'models/user'
require_dependency 'integrations/ext/user.rb'
But this creates issues with other gems (e.g. Devise not being loaded when the User model is loaded).
Is there a good way to do this or am I off keel here? Taking advantage of Ruby's open classes is a common idiom outside of Rails.