Rails 3: how to load files in /lib? - ruby-on-rails-3

I'm new to rails and making some sort of noob mistake: I frequently need to count the number of lines in a file, so I'm trying to monkey patch class File like this:
class File
def self.line_count( filename )
%x{wc -l #{filename}}.split.first.to_i
end
end
I saved this to /lib/file_util.rb. I thought that this was supposed to be auto-required, so that I could just use it, but that doesn't work:
$ rails console
>> File.line_count('Gemfile')
NoMethodError: undefined method `line_count' for File:Class
...
So I try to require it manually, no joy:
>> require '<myproj>/lib/file_util.rb' # same result with require 'file_util.rb'
=>nil
But it works if I require it within IRB:
$ irb
>> require '<myproj>/lib/file_util.rb'
=> true
>> File.line_count('Gemfile')
=> 22
I also tried to to add the require to config/application.rb:
...
Bundler.require(:default, Rails.env) if defined?(Bundler)
require 'file_util.rb'
module <myproj>
...
and I get:
$ rails console
<myproj>/config/application.rb:9:in `require': no such file to load -- file_util.rb (LoadError)
What am I doing wrong?

Ok, I seem to have mostly figured it out. Rails doesn't automatically require everything under /lib. It only auto loads when you try to use a new class name that matches a file name in lib. So if I define line_count in class FileUtil instead of File it automatically finds and loads 'file_util.rb'. But patching File and naming the patch file 'file.rb' doesn't work, since the File class is already defined, so Rails doesn't go looking for a definition.
My other problem was that I was trying to do the require too soon in the startup sequence, before Rails had a chance to enhance require to look in its directories. When I added "require 'file_util'" to config/environments/development.rb it works fine.
But this doesn't explain why I can't manually require the file from within rails console.

Monkeypatching classes can be done more easily by adding a file in config/initializers. All of those files are automatically loaded by Rails at startup.
You can call your initializer file anything you want. Try config/initializers/file.rb.

Related

Gem with models

I created a Gem with models (actually, extracted it from the main project) to share amongst the projects we have in our platform.
We have dozens of models, so instead of requiring them one by one, I wrote the following code:
Gem.find_files("my_gem/models/*.rb").each { |path| require path }
I access one of the projects that has my_gem in the gem file and running rails c I get the following output:
/Users/myuser/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/dynamic_matchers.rb:50:in `method_missing': undefined method `has_attached_file' for #<Class:0x007fad4b93ccb8> (NoMethodError)
One of my models is using the gem paperclip, what is weird is:
If I remove the line declared above to load all the models
automatically, rails c runs fine
If I try to include onlye the model that uses paperclip require
"my_gem/models/paperclip_model" I receive the same error
So then I change my gem to not load any model, and when I try to reference any model from rails console, it says the class is not loaded, but then I run Gem.find_files("my_gem/models/*.rb").each { |path| require path } or require "my_gem/models/paperclip_model" 'they work perfectly and I am able to work with the model.
Has any of you seen the same issue?
Seems that changing require for autoload solved the problem
I changed Gem.find_files("my_gem/models/*.rb").each { |path| require path }
for
Gem.find_files("my_gem/models/*.rb").each do |f|
filename = File.basename(f, '.*')
class_name_symbol = filename.classify.to_sym
autoload class_name_symbol, "my_gem/models/#{filename}"
end
and now it is working.
It sounds like one of the models in your gem depends on Paperclip, but you don't explicitly set it as a dependency. So what's happening is that if your models get loaded before paperclip gets loaded, you'll see the UndefinedMethod error for has_attached_file.
If you use your models in a Rails application which has paperclip as a dependency, and you load those models after the console (or server) has spun up, Paperclip will be present, so you won't see this error.
The solution is to explicitly add paperclip as a dependency in your gemspec, something like:
s.add_dependency('paperclip')
Assuming that this gem will always be used in the context of a Rails application, this should work. If not, you might also need to add the following line to the top of your models that use paperclip:
require "paperclip"

Rails Production Single JS File Isn't Precompiled?

So I have a file jquery.tmpl.min.js sitting under app/assets/javascripts/ and for whatever reason it's not being found in my production server. After runnings rake assets:precompile it completes without any errors whatsoever. All my other javascript assets get compiled properly and sent to the browser. I don't have any issues on my development server finding this JS file.
I have the following lines in my production.rb file:
config.serve_static_assets = true
config.assets.compile = true
config.assets.precompile += %w( *.js *.css )
Error message:
ActionController::RoutingError (No route matches [GET] "/assets/jquery.tmpl.min.js"):
Edit
According to this issue: https://github.com/rails/rails/issues/3596
Using the javascript_include_tag with something like 'jquery.ba-url.min' wont append the .js extension. Originally I had that but have since changed it to include the .js extension. Still no dice however.
Edit 2
I tried adding //= require jquery.tmpl.min.js to my application.js but now when I attempt to precompile my assets it says it can't find the file.
Edit 3
Tried adding //= require_tree and still it isn't found. This is driving me nuts!
Ok, I found the answer. It turns out Edit 2 was the fix that I was looking for. Problem was that I made the edit on development and commited to my production server using github. However I forgot to add the renamed file to the commit so all my commit ended up doing was deleting the file on the production server.

How do I get the root directory of a gem in the Rspec tests for my Rails project?

I've got a gem, we'll call it ToastMitten, which I'm including in one of my Rails apps. I'm writing some tests for ToastMitten in which I need to load a file, and I want to specify a path from the root of the gem.
I just tried using Rails.root.to_s, but that gives me something like /Users/me/projects/toastmitten/spec/dummy. I would have expected that path to end at toastmitten/.
What am I doing wrong?
Rails.root.parent.to_s
If it always gives back your dummy Rails app, just move up to the parent.
It looks like you are using a Rails engine (generated with enginex, hence the dummy app in your spec folder). If you need to require a file in your test using an absolute path, you can use the following:
file = File.expand_path(File.join(File.dirname(__FILE__), 'path', 'to', 'file.ext'))
root = File.expand_path(File.join(File.dirname(__FILE__), '..', '..'))
Can you be more specific on where you are trying to require this file?
Also, The root example may require more ..'s.

Multiple public folders, single rails installation

I have a rails application I would like to use for multiple sites, each with different designs.
I would like to change the rails installation /public directory to something else (dynamically eventually). However, I have run into a problem (bug?) changing directories...
In my application.rb file I change the paths.public path to something other than "public" (let's say "site_one"). Here is the code:
puts paths.public.paths
paths.public = "site_one"
puts paths.public.paths
The two "puts" commands are for debugging. Now run "rails s" and you will see:
/home/macklin/app/public
/home/macklin/app/site_one
This verifies the path is changed correctly. However, shortly afterward, rails throws the following error (let me know if you need the full trace):
Exiting
/usr/lib/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/paths.rb:16:in `method_missing': undefined method `javascripts' for #<Rails::Paths::Path:0x7f422bd76f58> (NoMethodError) from /usr/lib/ruby/gems/1.8/gems/actionpack-3.0.3/lib/action_controller/railtie.rb:47
My guess is it cannot find the javascripts directory even though it is clearly sitting in the "site_one" folder.
Does anyone know why I am getting this?
I know this question is pretty old, but I think I found an answer for this in Rails 4.2.
You just simply have to put this line in your config/application.rb:
middleware.use ::ActionDispatch::Static, "#{Rails.root}/another_public_folder_name", index: 'index', headers: config.static_cache_control
This makes all files in /another_public_folder_name to be served by Rails.
This is the way Rails use to setup the standard /public folder. I found it checking the sources:
https://github.com/rails/rails/blob/52ce6ece8c8f74064bb64e0a0b1ddd83092718e1/railties/lib/rails/application/default_middleware_stack.rb#L24
Duh. Just add 2 more rules for stylesheets and javascripts (I guess they get wiped when you change the parent path)
paths.public.stylesheets = "site_one/stylesheets"
paths.public.javascripts = "site_one/javascripts"

Execute migration files dynamically

How can we execute a mignation file dynamically from the source code. Means we want to execute a migration file from an action of a controller. How can we do so?
The main issue was that we do not know the names of migration files. I do it with the following code
ActiveRecord::Migrator.migrate("vendor/plugins/#{self.id.to_s}/lib/db/migrate/", nil)
Package::Rake.call('db:schema:dump')
And the Rake class have the following method
def call(task, options={})
options[:rails_env] = Rails.env
args = options.map { |n,v| "#{n.to_s.upcase}='#{v}"}
system "rake #{task} #{args.join(' ')} --trace >> #{Rails.root}/log/rake.log &"
end
Hope this will help some body with similar problems.
This is assuming the migration is static and in your db/migrate directory when the app server starts:
You could add the migrations directory to your autoload path in config/application.rb, and then require the migration file to be run inside your controller (or in a config initializer):
application.rb
config.autoload_paths += %W(#{Rails.root}/db/migrate)
your_controller.rb
require '20101209102033_some_migration_file'
#....
SomeMigrationFile.up
I would be interested to know what the use case is here. Seems pretty wild!