rails auto-include libraries issue - ruby-on-rails

I want Rails to auto-include all libraries in my lib directory.
This my current configuration in config/application.rb:
config.autoload_paths += %W(#{config.root}/lib)
However, Rails will only include some of the libraries not all.
Is it maybe because library has 2 uppercases in the module name such as ModuleName?
If not, what else could it be?

Try this
config.autoload_paths += Dir["#{config.root}/lib/**/"]

Related

Rails auto loading files from wrong location

I see lots of
LoadError: Unable to autoload constant SomeModule::MyJob expected /app/lib/some_module/my_job.rb to define it.
errors in my Rails 5.2.0 application. The part I don't understand is, why it would look inside app/lib/, since it's defined to be loaded like this in the application.rb:
config.autoload_paths << Rails.root.join('lib')
So I would expect it to load from /lib (where the module in question is located). So why is it looking inside the /app directory and how can I change it?
Change
config.autoload_paths << Rails.root.join('lib')
to
config.autoload_paths += Dir["#{Rails.root}/lib/*"]
it should work.

How to use lib in rails migrations?

I was wondering if there is a way to easily access methods/classes from the lib folder in migrations? I found a way to require them with
require File.expand_path("../../lib/my_library")
I don't really like this and I was wondering if there is a more common way to include those libraries required by the migration?
rake db:migrate load all rails environment, so you can just add lib in autoload in application.rb file like this and it should work.
config.autoload_paths += Dir["#{config.root}/lib/**/"]

Cannot load such file located in lib directory

I have been trying to set up my work environment on a new computer (ruby 1.9.3.p0 and rails 3.2.6) for the last two days and I keep getting the following error when I try to run rails server or rails console:
application.rb:7:in `require': cannot load such file -- acts_as_loggable/acts_as_loggable (LoadError)
This is what my application.rb looks like:
require File.expand_path('../boot', __FILE__)
require 'acts_as_loggable/acts_as_loggable'
require 'acts_as_abusable/acts_as_abusable'
require 'acts_as_luba/acts_as_luba'
module MyProgram
class Application < Rails::Application
config.active_record.schema_format = :ruby
# Custom directories with classes and modules you want to be autoloadable.
config.autoload_paths += %W(#{config.root}/lib/)
config.autoload_paths += %W(#{config.root}/app/models/game_mechanics)
config.autoload_paths += Dir["#{config.root}/app/admin/"]
config.autoload_paths += Dir["#{config.root}/app/models/"]
config.active_record.pluralize_table_names = true
config.action_view.sanitized_allowed_attributes = ['data-link']
My acts_as_loggable.rb file is located in lib/acts_as_loggable. All my other files load okay, except for the 3 that I left in my application.rb code sample.
If I hardcode the path/directory (see below), my app works.
require './lib/acts_as_loggable/acts_as_loggable'
require './lib/acts_as_abusable/acts_as_abusable'
require './lib/acts_as_luba/acts_as_luba'
Does anyone have any clue what is happening and how I can get my app to work without the hardcoded paths?
Thanks.
Your require is before config.autoload_path.

ActiveAdmin assets precompile error

ActiveAdmin is giving me an
Undefined mixin 'global-reset'.
error when it try to run
rake assets:precompile
ActiveAdmin is 0.3.4.
I have ActiveAdmin and an assets group in my Gemfile with sass, coffee-rails and uglifier.
I've just stumbled on this. The problem I had turned out to be in the config.assets.precompile directive in my production.rb file. I had a regular expression there, which was matching some assets from the activeadmin gem, that should not be matched for precompilation. Changing the option to the following worked for me:
# Needed for the ActiveAdmin's manifest assets.
config.assets.precompile += ['active_admin.css', 'active_admin.js']
The problematic code block I had was this:
# This one effectively turns every js/css file, which starts with
# a letter or a number, into an includeable asset manifest (similar to
# what application.js and application.css already are).
# You may want to omit this line for your application.
config.assets.precompile += [/^[a-z0-9]\w+\.(css|js)$/]
It was matching assets from the activeadmin gem and declaring them as standalone manifests and when the asset pipeline tried to complie them, this error was produced.
For more details on how the config.assets.precompile directive works in Rails, check out this Gist.
The problem is indeed, as #dimitar points out, the line with the catch all because the asset pipeline is trying to compile partials and since they aren't written to be compiled on their own, dependency issues appear.
Depending on your app, you might need that catch all, specially if you have many JS, CoffeScript and SCSS/SASS files in several child folders. In that situation you might encounter that rails complains because something isn't compiled for production when the catch all is removed.
The solution is to have a catch all that excludes the SASS partials, _filename.css.[scss|sass] and that would solve it (worked for me!). I also included some other tips from other activeadmin suggestions including exactly some ActiveAdmin dependencies to be compiled. Here's my code:
# Include all JS files, also those in subdolfer or javascripts assets folder
# includes for exmaple applicant.js. JS isn't the problem so the catch all works.
config.assets.precompile += %w(*.js)
# Replace %w( *.css *.js *.css.scss) with complex regexp avoiding SCSS partials compilation
config.assets.precompile += [/^[^_]\w+\.(css|css.scss)$/]
#Adding active_admin JS and CSS to the precompilation list
config.assets.precompile += %w( active_admin.css active_admin.js active_admin/print.css )
In your CSS file, you most likely have:
#include 'global-reset';
However, you are trying to import your global reset, so you should change that to:
#import 'global-reset';
Hope this helps!

Rails Webrick: Configure what directories are watched for changes

At the moment I'm having to restart the server if I change files in the lib/ and app/helpers/ directories. Is there a way to configure either Rails or Webrick such that it hotswaps the files in those directories as well?
In your config/application.rb:
config.autoload_paths += %W(#{config.root}/lib #{config.root}/app/helpers)
There should be a commented line which explains what autoload_paths is for.

Resources