legendary_rob
legendary_rob

Reputation: 13012

Rails removing a specific Dir from your autoload_path

This question somehow is harder than it seems. Within our application we have a pretty dynamic structure within the app/. We can add folders to the models directory to group related models etc, we use this line here

Dir.glob("#{Rails.root}/app/models/*[^(.rb|.ignore)]").each {|dir| config.autoload_paths << dir }

to add any sub directories to the autoload_paths. Only last night i had to group some models together and it seemed that this wasn't loading the newly created folder, even after reseting the app etc. so i read up on Dir.glob and found an alternate method.

Dir.glob("#{Rails.root}/app/models/**/*/").each {|dir| config.autoload_paths << dir }

This seemed to do the trick just fine it finds any and all directories at least one level deep recursively.

Except our system needs to build some models it shares with other systems, so we have within the app/models/sms directory some models that share the same name as models already existing within our system. for example

app/models/account.rb
app/modles/sms/account.rb

There would be conflicts if app/modles/sms/account was autoloaded, so we had this to remove the sms path from the autoload_paths

config.autoload_paths -= %W(#{Rails.root}/app/models/sms)

which simply removed it from the loadpath and we could still access it through Sms::Account, which is semantic and reads well. but somehow its not working with the new way i am finding directories with Dir.glob?

I dropped a binding within my config/application.rb just to see what is populated in the loadauto_paths before and after the config.autoload_paths -= %W( #{Rails.root}/app/models/sms)

and its in not removing the path at all?

within ruby you can do ['a','b'] - ['a'] => ['b']

but this situation. (please excuse the large amount of code to follow)

if i call config.autoload_paths -= %W(#{Rails.root}/app/models/sms) within the rails console

pry(Toolkit::Application)> config.autoload_paths -= %W( #{Rails.root}/app/models/sms)
  => ["/Users/fortknokx/Work/toolkit-new_codes/toolkit2/app/models/access_to_finance/",
  "/Users/fortknokx/Work/toolkit-new_codes/toolkit2/app/models/addons/",
  "/Users/fortknokx/Work/toolkit-new_codes/toolkit2/app/models/beneficiaries/",
  "/Users/fortknokx/Work/toolkit-new_codes/toolkit2/app/models/bravo_managements/",
  "/Users/fortknokx/Work/toolkit-new_codes/toolkit2/app/models/bravo_skills_developments/",
  "/Users/fortknokx/Work/toolkit-new_codes/toolkit2/app/models/economic_developments/",
  "/Users/fortknokx/Work/toolkit-new_codes/toolkit2/app/models/employment_equities/",
  "/Users/fortknokx/Work/toolkit-new_codes/toolkit2/app/models/enterprise_and_supplier_developments/",
  "/Users/fortknokx/Work/toolkit-new_codes/toolkit2/app/models/enterprise_developments/",
  "/Users/fortknokx/Work/toolkit-new_codes/toolkit2/app/models/generals/",
  "/Users/fortknokx/Work/toolkit-new_codes/toolkit2/app/models/managements/",
  "/Users/fortknokx/Work/toolkit-new_codes/toolkit2/app/models/ownerships/",
  "/Users/fortknokx/Work/toolkit-new_codes/toolkit2/app/models/preferential_procurements/",
  "/Users/fortknokx/Work/toolkit-new_codes/toolkit2/app/models/scorecard_copier/",
  "/Users/fortknokx/Work/toolkit-new_codes/toolkit2/app/models/scorecard_copier/alpha_to_bravo_copier/",
  "/Users/fortknokx/Work/toolkit-new_codes/toolkit2/app/models/scorecards/",
  "/Users/fortknokx/Work/toolkit-new_codes/toolkit2/app/models/scorecards/alpha/",
  "/Users/fortknokx/Work/toolkit-new_codes/toolkit2/app/models/scorecards/bravo/",
  "/Users/fortknokx/Work/toolkit-new_codes/toolkit2/app/models/skills_developments/",
  "/Users/fortknokx/Work/toolkit-new_codes/toolkit2/app/models/sms/",
  "/Users/fortknokx/Work/toolkit-new_codes/toolkit2/app/models/socioeconomic_developments/",
  "/Users/fortknokx/Work/toolkit-new_codes/toolkit2/app/models/spend_items/",
  "/Users/fortknokx/Work/toolkit-new_codes/toolkit2/app/models/spend_periods/",
  "/Users/fortknokx/Work/toolkit-new_codes/toolkit2/app/models/validators/"]

This seems not to be? i am stuck pulling out what little hair i have left haha. it was working before when i used Regex to 'try' find all the directories. what am i doing wrong. its like i can either have some of the directories i want, or all the directories even those i dont want? haha

Upvotes: 1

Views: 1638

Answers (1)

coderhs
coderhs

Reputation: 4837

Dir.glob("#{Rails.root}/app/models/**[^sms]/*/").each {|dir| config.autoload_paths << dir }

This should load all the directories except the sms directory.

Upvotes: 2

Related Questions