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

Doesn't find models in subdirectories correctly #200

Closed
michael-moroku opened this issue Jul 24, 2014 · 13 comments · Fixed by #232
Closed

Doesn't find models in subdirectories correctly #200

michael-moroku opened this issue Jul 24, 2014 · 13 comments · Fixed by #232
Assignees
Labels
Milestone

Comments

@michael-moroku
Copy link

I have the following structure:

app/models/sub1/sub2/sub3

When annotating it correctly annotates models in sub1 classes but fails to annotate models in sub2 or sub3.

The error message says: Unable to annotate sub1/sub2/sub3/model.rb: cannot load such file -- path_to_app/app/models/sub2/sub3/model

@ctran
Copy link
Owner

ctran commented Jul 24, 2014

Can you give an example of your model class?
On Jul 23, 2014 7:22 PM, "michael-moroku" notifications@github.com wrote:

I have the following structure:

app/models/sub1/sub2/sub3

When annotating it correctly annotates models in sub1 classes but fails to
annotate models in sub2 or sub3.

The error message says: Unable to annotate sub1/sub2/sub3/model.rb: cannot
load such file -- path_to_app/app/models/sub2/sub3/model


Reply to this email directly or view it on GitHub
#200.

@michael-moroku
Copy link
Author

class FriendInvitationAcceptedEvent < ActiveRecord::Base
  acts_as :player_notification_event

  def self.create(player, notification)
    e = FriendInvitationAcceptedEvent.new
    e.player_id = player.id
    e.notification_id = notification.id
    e.save
  end

end

@ctran
Copy link
Owner

ctran commented Jul 24, 2014

Is this inclosed a module hierachy matching the directory structure?

@michael-moroku
Copy link
Author

That is correct

@ctran ctran self-assigned this Aug 5, 2014
@ctran ctran added this to the v2.6.6 milestone Aug 5, 2014
@ctran ctran added the bug label Aug 5, 2014
@ctran
Copy link
Owner

ctran commented Aug 5, 2014

I can't seem to reproduce this. Please specify the gem version and a complete model file.

@CyborgMaster
Copy link
Contributor

I have this same issue. The problem might be that in both of our cases the class hierarchy does not match the directory structure. In my case the error message looks like this:

Unable to annotate comm_engine/triggers/reminder_due_trigger.rb: cannot load such file -- /Users/jeremy/projects/allynova/app/models/triggers/reminder_due_trigger

You'll notice that the path on the left, has an extra folder (comm_engine). Out project has many models and we use sub directories to keep things organized. So there may be extra subfolders that don't match the class hierarchy. We allow this by adding the following to application.rb:

  # Allow models in subdirectories
  config.eager_load_paths += Dir[Rails.root.join('app', 'models', '**/')]

This seems to be a new bug, I didn't have this problem on version 2.6.3.

@pablox-cl
Copy link

I'm having this problem too:

Unable to annotate data_values/text/code_phrase.rb: cannot load such file -- /home/pablo/code/rails/simple-openehr/app/models/text/code_phrase

Which is weird, because I don't understand why it looks on app/models/text/code_phrase
PS.- Why is this issue closed?

@pablox-cl
Copy link

# /vendor/bundle/gems/annotate-2.6.5/lib/annotate/annotate_models.rb @ line 396 AnnotateModels.get_loaded_model:
[1] pry(AnnotateModels)> model_path
=> "data_values/text/code_phrase"
[2] pry(AnnotateModels)> ActiveSupport::Inflector.camelize(model_path)
=> "DataValues::Text::CodePhrase"
[3] pry(AnnotateModels)> ActiveSupport::Inflector.constantize(ActiveSupport::Inflector.camelize(model_path))
LoadError: Unable to autoload constant DataValues::Text::CodePhrase, expected /home/pablo/code/rails/simple-openehr/app/models/data_values/text/code_phrase.rb to define it
from /home/pablo/code/rails/simple-openehr/vendor/bundle/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:495:in `load_missing_constant'
# app/models/data_values/text/code_phrase.rb
class CodePhrase < ActiveRecord::Base
  belongs_to :CodePhraseable, polymorphic: true
end
# config/application.rb
config.autoload_paths += %W(#{config.root}/app/models/data_values/text)

Then after the rescue block:

    395:   rescue
 => 396:     binding.pry

[1] pry(AnnotateModels)> model_path
=> "text/code_phrase"

@ctran
Copy link
Owner

ctran commented Jan 22, 2015

Have you tried to specify the "--model-dir" option?

@ctran ctran reopened this Jan 22, 2015
@CyborgMaster
Copy link
Contributor

Further investigation seems to indicate that the problem only occurs when you have STI classes with the base class in a subdirectory.

From my previous post, my reminder_due_trigger.rb file inherits from trigger.rb, which might be why it's trying to load it from the app/models/triggers/ directory, but in my case triggers are stored in app/models/comm_engine/triggers/.

It seems to find models that aren't STI in subdirectories just fine. Ex: it annotates /app/models/sync/appointment.rb just fine.

@CyborgMaster
Copy link
Contributor

I'm trying to mess with the --model-dir option, but no luck so far.

@CyborgMaster
Copy link
Contributor

I think, I've figured it out, working on a pull request now.

@CyborgMaster
Copy link
Contributor

I've got a pull request open that fixes this bug. #232

@ctran ctran closed this as completed in #232 Mar 4, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants