Skip to content

Conversation

KL-7
Copy link
Contributor

@KL-7 KL-7 commented Jan 29, 2012

This PR is an attempt to fix #22.

I changed a bit the way model classes are located. After requiring model file we scan ObjectSpace for the class that has underscored name matching relative file path inside the models directory.

This approach eliminates necessity to guess model name by file name that allow to process models with any kind of non-standard capitalization in class and module names.

Though, it still requires that underscored file name matches underscored model class name.

If the file is located not at the top level of the models directory then we assume that model is either nested in the corresponding chain of modules or is not nested at all.

To be more precise it's possible to capture state of ObjectSpace before requiring model file and after the file is required check ObjectSpace for new classes. In that case it'd be easier to spot the model itself, but there might be problem if for some reason model class was already loaded before explicit requiring its file. So, I'm not sure whether it's a good idea or not.

I enabled one pending test case and added some new tests that all are passing now. I might have broken some edge cases. If so, let me know.

This approach eliminates necessity to guess model name by file name that allow to process models with any kind of non-standard capitalization in class and module names. Though, it still requires that underscored file name matches underscored model class name. If file is located not at the top level of models directory then we assume that model is either nested in the corresponding chain of modules or is not nested at all.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If i understand well, this will not work if there is no relation between the model name and the file name.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you mentioned this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. But in that case neither current implementation nor even Rails autoload mechanisms will work. Correct me if I'm wrong.

Instead of iterating over the list of the files, guessing corresponding classes' names and annotating these classes we can require all files from the list and then blindly annotate all descendants of ActiveRecord::Base that we can find, but I'm not sure whether there is any reason to do that.

Btw, do you have any models in your project that have their files named not as underscored names of the classes themselves?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, autoload mechanism will not work, but i always assumed that the autoload mechanism using Inflector was for convenience only, and should be possible to override by explicit require. I do not have any such models in my only rails project.

I would think about loading files one-by-one inside dynamically created modules, and building an association between file names and model classes like this, but i do not think i will go as far as trying to implement this myself :).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have doubts about that condition. I try to find appropriate class by the file name in the ObjectSpace that means class should be loaded first. Seems like it's impossible to rely on Rails auto-loading in that case and file should be always explicitly required.

@KL-7
Copy link
Contributor Author

KL-7 commented Feb 23, 2012

@ctran, any thoughts or comments on that?

@alexch alexch merged commit fcbec0e into ctran:master Jun 8, 2012
@ctran ctran added the released label Dec 17, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

4 participants