Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

model_file_path should use Rails' autoloader, rather than assume a file location. #20

Closed
NullVoxPopuli opened this issue Feb 6, 2017 · 3 comments

Comments

@NullVoxPopuli
Copy link

Currently, I get the error 'No such file or directory @ rb_sysopen - path/app/models/underscored_model_name.rb'

This is because of https://github.com/palkan/logidze/blob/master/lib/generators/logidze/model/model_generator.rb#L91

I propose that instead, 'UnderscoredModelName'.constantize should be used, so the file path can be retrieved via:

[20] pry(main)> ap name = 'StepDefinitions::Base'; 
klass = name.constantize; 

(klass.methods - ActiveRecord::Base.methods).select{ |meth| 
  location_path = klass.method(meth).source_location[0]; 
  location_path.include?('/app/') && location_path.ends_with?("#{name.underscore}.rb") 
}.map{ |meth| 
  [
    meth, klass.method(meth).source_location
  ] 
}

=> [
  [
    :excluding_type,     
    [
      "/project_path/app/models/data/step_definitions/base.rb", 
      149
    ]
  ], 
  [
    :types,     
    [
      "/project_path/app/models/data/step_definitions/base.rb", 
      145
    ]
  ]
]
@palkan
Copy link
Owner

palkan commented Feb 6, 2017

Unfortunately, this method won't work if your model doesn't have any own methods.

I think, it's better to provide an option to set the file path explicitly, e.g.:

rails g logidze:model "StepDefinitions::Base" --path "app/models/data/step_definitions/base.rb"

@NullVoxPopuli
Copy link
Author

that's true. A path option would def solve the problem

@palkan
Copy link
Owner

palkan commented Feb 6, 2017

Done. Checkout version 0.4.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants