Rails automated testing of POROs in a app/lib directory - ruby-on-rails

I want to include some additional non-rails classes in my Rails App. I created a lib directory under app/ and a lib directory under test/ I am using minitest.
How do I make it so rails test also runs the tests under the new test/lib directory? How do I make it so my app includes app/lib in the autoload mechanism?

In the application.rb file add the following configuration which will load files from the app/lib folder
config.autoload_paths += "#{Rails.root}/app/lib/"

Related

How to load javascript file from rails plugin

I'm trying to create a plugin which is suppose to expose a javascript file, but I can't get it to load in my rails 5 application.
In my gem, I've tried adding my javascript file called my_gem.js to
vendor/assets/javascripts
app/assets/javascripts
lib/<gem-name>/assets/javascripts
But none of them work. I just get this error message when loading the file in my application.js
couldn't find file 'my_gem' with type 'application/javascript'
This is how I load my plugin/gem in to my rails project
gem "my-gem", path: "~/projects/my-gem"
We've created a gem which utilizes assets before.
You'll want to create an app/assets/javascripts/my_gem folder in your gem directory tree, where you'll put your my_gem.js file.
Most importantly, you need to add this file to the asset path (we use an engine):
#lib/my_gem.rb
module MyGem
class Gem < Rails::Engine
config.assets.precompile += %w(my_gem/my_gem.js)
end
end
This will either allow the file to be used standalone, or as part of your app:
#app/assets/javascripts/application.js
//= require my_gem/my_gem
You must tell Rails to compile my_gem.js. You can do this in an initializer, application.rb, or environment file.
Rails.application.config.assets.precompile += ["my_gem.js"]
I'd recommend keeping the source file at ~/projects/my-gem/app/assets/javascripts/my_gem.js

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/**/"]

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.

classes under lib folder are not detected in rails

In my project, I have written few classes under lib folder but rails is not detecting those classes in production environment. I get the uninitalized Constant error.
I use Apache in the production environment and rails script/server in the development environment.
Is anything wrong with RAILS_ROOT environment? Can anyone suggest how to overcome this problem?
I am not sure about Rails, but you achieve that in Ruby by this: (it will work in rails too, but rails must be having some elegant way)
require File.join(File.dirname(__FILE__), "lib",'your_module_name')
include your_module_name
Try this in config/application.rb (I assume you have rails3)
config.load_paths += %W( #{config.root}/lib )
Update: Rails - why would a model inside RAILS_ROOT/lib not be available in production mode?
Ensure that the name of your file matches the name of the class or module defined in it, accounting for any directories.
ie:
lib/my_new_class.rb
class MyNewClass
end
Or if you have a directory hierarchy:
lib/my_files/my_module.rb
module MyFiles
module MyModule
end
end

Load up the lib files when running Test::Unit tests in Rails?

I've added a few modules and dropped them in my /lib directory and I think the lib directory is loaded magically by Rails (unless I loaded the lib directory somewhere early in my project and forgot about it). However, when I run unit tests that require my additional modules, they are not loaded.
Should the lib directory be loaded automatically when running tests, or is there an elegant way to do so for testing? I had hoped that the rake scripts + Test::Unit would've loaded up my Rails environment exactly, but this doesn't seem to be the case. I'm left with doing adding something like this to test_helper.rb:
require File.expand_path(File.dirname(__FILE__) + "/../lib/foo")
I'm running my tests with the standard rake scripts like:
rake test
rake test:units
rake test:functionals
Your lib directory is not automatically loaded by rails. Loading occurs through ActiveSupport::Dependencies overriding const_missing. When you use a constant for the first time, if it is undefined, Rails attempts to find it in the lib directory (and other places in the load path). To accomplish this, it uses a naming scheme where something called SomeClass is expected to be in some_class.rb. Rails in test mode uses the same mechanism. Check your config/environments/test.rb and config/environments/development.rb to see if you do something funny with requires. In short, check your naming scheme.

Resources