I have the following folder structure:
app
├── assets
├── controllers
├── helpers
├── mailers
├── market_adapters
│ └── german.rb
│...
And the file market_adapters/german.rb is:
module MarketAdapters #(I've also tried naming it singular)
class German
end
end
When running tests I get the error:
/gems/activesupport-5.0.0/lib/active_support/dependencies.rb:512:in
`load_missing_constant': Unable to autoload constant German,
expected .../app/market_adapters/german.rb to define it (LoadError)
Adding the market_adapters folder to the autoload_paths seems to have no effect config.autoload_paths << "#{Rails.root}/app/market_adapters"
If I move the market_adapters to the lib folder, everything works. But still would like to have it under app, any ideas?
By the way, I'm using Rails 5.
All the subfolders in app are automatically auto-loaded and added to the load path. Therefore, the folder market_adapters is added to the load path, and the file called german.rb is expected to define the German class.
If you really want to use market_adapters as namespace and keep the file in app, you need to store it in the folder app/market_adapters/market_adapters/german.rb.
The right place, however, is in lib.
Related
I have the following directory structure:
app
├── consumers
│ └── consumers
│ ├── foo.rb
...
I expect the foo consumer to be automatically included in the rails autoload_paths but when I look at the config in the spec:
MyApp::Application.config.autoload_paths
=> [#<Pathname:/home/myapp/lib>]
Any idea what's going on?
Explicitly adding the directory solves the issue:
config.autoload_paths << Rails.root.join('app/consumers')
Since this app/consumers isn't a default load path, it won't be set unless you explicitly specify it in application.rb. Are you sure config.autoload_paths << Rails.root.join('app/consumers') is present in application.rb?
You can access a list of all the autoload_paths (both explicitly added in config/application.rb and the ones added by the Rails themselves by default), by calling ActiveSupport::Dependencies.autoload_paths.
MyApp::Application.config.autoload_paths will only contain a set of paths that have been defined in config/application.rb. These paths actually get prepended into a the default set of paths defined by rails during the initialization.
I have created a module in the lib directory (it seems to be the place where you put it). It has one method that is useful for many of my model classes.
I want to unit test it. However when I try to put a test in the model folder in test it fails:
Minitest::UnexpectedError: NoMethodError: undefined method `type' for nil:NilClass
So it is probably wrong. Where should the test be, and what should it inherit etc to work?
Your tests almost always just follow the same folder structure. So if you have:
app
├── controllers
├── models
Your tests directory would be:
spec
├── controllers
│ └── some_controller_spec.rb
├── models
│ ├── some_model_spec.rb
├── rails_helper.rb
└── spec_helper.rb
In your instance, if you have a lib folder, you'd simply add a lib folder to your spec/test directory.
I want to add the JS from Google Analytics embed API into my rails application and only for Rails Admin (not needed anywhere else).
We have overwritten the Rails Admin Dashboard and now we are struggling with the custom JS.
I found here: https://groups.google.com/forum/#!topic/rails_admin/KgUl3gF5kTg that it needs to be placed in app/assets/javascripts/rails_admin/custom/ui.js.
So I've placed all the .js files and the .js.map files in the directory:
.
└── custom
├── active-users.js
├── chart.js
├── datepicker.js
├── moment.js
├── platform.js
├── platform.js.map
├── polymer.js
├── polymer.js.map
├── promise.js
├── ui.js
└── viewpicker.js
And I've added //= require_tree . in the ui.js file but in rails_admin I still receive:
Uncaught ReferenceError: Polymer is not defined
This means that the .JS file isn't loaded.
Thanks to this link for finding the above link: Rails Admin: add javascript library to custom action
Edit 1: Using Rails Admin in a Rails 3 env.
Edit 2:
So for testing reasons I removed all the custom JS/HTML etc.
I've set this in the ui.js:
//=require_tree .
I've set this in leecher.js:
$(document).ready(function() { document.MY_SIMPLE_VARIABLE = 2; } );
When I restart the server I go to rails admin, log out, restart the server again, log in. Go to console in chrome and type:
document.MY_SIMPLE_VARIABLE
And I receive the following:
For testing reasons I've added an:
alert("hello world");
But still no trigger.
The location of the ui.js and leecher.js is:
app/assets/javascripts/rails_admin/custom/
And this my current Gemfile:
http://pastie.org/private/zrrzjpbx5jq7enpeknsa
Edit 3:
If I put the JS code into the ui.js it shows an alert! So they are problems with the includes (//= require tree .)
Edit 4:
All the JS files I'm using: https://gist.github.com/YOUConsulting/243397c31ea28d217367
Simple example that doesn't work:
https://gist.github.com/YOUConsulting/571e785d6b6c1ed06d6b
Simple example that does work:
https://gist.github.com/YOUConsulting/52de78aa043239aae707
I've rewritten my answer to describe the complete solution in case other people need this in the future.
app/assets/javascripts/rails_admin/custom/ui.js works like a manifest file in the Rails asset pipeline, thus it supports these directives.
In order to include all .js files under custom, add
//= require_tree .
at the top of the file.
If that does not work right out of the box, running
rake tmp:clear
will solve the problem ;)
Cheers!
After talking more to Cec on this topic. He found that a simple rake tmp:clear, was the correct answer. See answer above or below.
the jquery.flipcountdown.css file is:
.xdsoft_flipcountdown.xdsoft_size_lg >.xdsoft_digit{
width:53px;
height:76px;
background-image:url(./img/digit-lg.png);
}
the directory structure is
+flipcountdown-master
+ img
- digit-lg.png
- jquery.flipcountdown.css
the question is: how to use asset-pipeline in this case.
1: put the css under the assets/stylesheets, and put the img folder under the assets/images folder ? CAN IT WORK ?
2: put the css file and the img folder all to the assets/stylesheets folder, i am pretty sure it can work, but the asset-pipeline can gzip it?
i'm not good at js/css, and i spent much time to read the Asset-Pipeline doc and video on Youtube, but i still don't know how to use it in right way.
thx in advance
There are a few things you can do here:
If you want to utilize the organization structure provided by the asset pipeline plugin, place the .css file in assets/stylesheets directory and the image in the assets/images directory and change any references of ./img/digit-lg.png to digit-lg.png (you could also specify /assets/digit-lg.png or /assets/images/digit-lg.png and get the same result). Here's what the directory structure should look like:
├── assets
│ ├── images
│ │ ├── digit-lg.png
│ ├── javascripts
│ └── stylesheets
│ └── jquery.flipcountdown.css
The grails asset pipeline ignores the top level images, javascripts, and stylesheets folders when resolving assets, as they are purely for organization purposes.
Or, since this is a third-party stylesheet, it may be best to keep the .css file unmodified, and keep things self contained. You can just add the flipcountdown-master directory to either the assets/ directory or assets/stylesheets/, keep the css background-image url set to ./img/digit-lg.png and everything should work fine. Here's the structure in that case:
├── assets
│ ├── images
│ ├── javascripts
│ └── stylesheets
│ └── flipcountdown-master
│ └── img
│ └── digit-lg.png
│ └── jquery.flipcountdown.css
The asset pipeline is very flexible to however you feel most comfortable organizing your assets, there isn't only one way to do things. Any file that the pipeline picks up will be eligible for gzip compression.
Here is the sample app.
Particularly look at application.js and application.css where the external libraries are included.
Just clone the app from github, run-app, and hit the index page. You should see a countdown clock in the index page.
I'm working on a Rails 3 app, and I've got a hierarchy of classes in my lib folder, e.g.:
lib
├── assets
├── tasks
│ └── import.rake
└── importer
├── base.rb
└── source
├── facebook.rb
├── google.rb
└── twitter.rb
I've updated config/application.rb to include this line:
config.autoload_paths += %W(#{config.root}/lib)
Then inside of Importer::Base, I have an instance method that attempts to load all classes in the Provider module, e.g.:
Importer::Source.constants.each do |class_name|
Importer::Source.const_get(class_name).process
end
The three classes in lib/importer/base have a class hierarchy similar to:
module Importer
module Source
class Facebook
# ...
end
end
end
When I call this method, Importer::Source.constants ends up returning an empty array. The classes appear to be lazy-loaded properly if I reference them by name directly, but they are not accessible in the constants call. How can I fix this?
Using #apneadiving's suggestion, I was able to fix this by adding this line to the beginning of my base.rb file:
Dir[Rails.root.join('lib/importer/source/**/*.rb')].each(&method(:require))