I have an Array extension method I want to use in my Rails 3 project. Where should it live?
I have a app/classes where I originally put it (array_extensions.rb), and in my config/application.rb I load the path: config.autoload_paths += %W(#{Rails.root}/app/classes). However, when I drop to rails console the extension is not loaded.
Is there a pre-definded place I should put my extension methods for Rails 3? Or, a pre-definded way to add them? I know Rails has it's own extensions methods for Array. Should I add mine to active_support/core_ext/array/conversions.rb?
What's the best practice for Rails 3?
The better way is create your extension in lib/core_ext directory to understand easyly where is your core_ext.
After create an initializer to require this file.
All .rb files in config/initializers are required into the environment at startup; you should be putting extensions in there.
Related
I would like to have a new method for Date class in my Ruby 2.0 Rails 4 application. Adding a new like 'date_extensions.rb' in /lib used to work in another Ruby 1.9 Rails 3.2 app but not here. The extension is pretty simple now:
class Date
def week_day
self.wday == 0 ? 7 : self.wday
end
end
I do not like to put it in initializer as it keeps growing. Is there a good workaround?
At some point, Rails stopped adding the lib directory to the autoload paths. The other app had probably set a setting to autoload the lib directory and your new app doesn't have that (yet?). So that's why you're having to require the file directly via an initializer. For what it's worth: I think this is the better approach -- only load code when you need it. I usually add a config/initializers/application.rb and then add requires such as require "date_extensions" in there. Then the date_extensions.rb file goes in the lib folder as you suggest.
I'm working on a Rails 3 application (specifically 3.2.13) on ruby 1.9.3-p392.
In one of my controllers The "create" action can receive an image (as a ActionDispatch::Http::UploadedFile)
I'm trying to monkeypatch ActionDispatch::Http::UploadedFile by overriding its as_json method to return the tempfile path instead of the tempfile File object itself.
The reason why I'm doing this is because I have a database logger that serializes the log context (which includes the request params) by calling to_json on the context.
Problem is that calling to_json on said class produces a ton of binary data which fill up my log.
So I have created the following directory structure under "lib":
"action_dispatch/http/uploaded_file.rb"
Inside this file the class is named ActionDispatch::Http::UploadedFile
I've also added the lib folder to application.rb by:
config.autoload_paths += Dir["#{config.root}/lib"]
Problem is that rails doesn't load my monkey-patched class.
If I add require "action_dispatch/http/uploaded_file"to the top of my controller file everything works fine.
Why doesn't Rails autoloads my monkey-patched class?
The directory structure and class naming is according to the Rails conventions.
Move your code to config/initializers/your_filename.rb file. The code in these files is loaded during Rails Application boot process.
That's in general. But I'm not sure why you want to monkey-patch ActionDispatch::Http::UploadedFile class, because I haven't got much info about your app.
try
config.autoload_paths += ["#{config.root}/lib"]
Using Rails 3.2 and ruby 1.9.3p0
I am trying out the gem delayed_job. I have created a file lib/mailing_job.rb in which I have class MailingJob.
In a controller under app/controllers/requests_controller.rb I am calling
job = MailingJob.new(#request)
but this is returning the error
uninitialized constant RequestsController::MailingJob
I think it is because I need a proper way of referencing a class under a different folder structure.
Any idea how I can isntantiate class MailingJob from a different file (class) in a different folder?
Rails 3 does not include the lib folder within the load path so your application does not know how to find the class.
You can modify config/application.rb and add a line to instruct rails to also look in the lib folder like so
config.autoload_paths += %W(#{config.root}/lib)
In an initializer (e.g. config/initializers/delayed_job.rb), do this (doesnt matter where)
require 'mailing_job'
I want to parse a haml template to a variable using HAML::Engine inside a model.
Where in the rails app folder structure should I store my files (one for each template) and how can I call them as a string in the model?
Thanks
Using what Sameera stated, by configuring your app to load directories, I would probably put it in a new folder in the 'lib' directory.
You can then have your rails app to load everything in 'lib' by putting this in your application.rb file.
config.autoload_paths += Dir["#{config.root}/lib/**/"]
I'm not exactly sure that this is the answer to your question, But with rails you can always create extra folders (other than default folder structure given by rails),
But remember to load them when your app starts, in rails3 its in
'application.rb'
following line
config.autoload_paths += %W(#{config.root}/extras)
And IMO its not a bad thing to create an extra folder to make your code more organize.
HTH
cheers
sameera
I have some simple ruby classes that I want to use with rails (they are classes for things like points, lines, rectangles, etc.). How can I use them with a rails controller or an active record model?
Thanks!
The convention is to put them in your-app/lib/. You can have Rails automatically load them by configuring it to do so in config/application.rb. Edit the default to look something like this:
# Custom directories with classes and modules you want to be autoloadable.
config.autoload_paths += %W( #{Rails.root}/lib/shapes )
Where your class definition files are in your-app/lib/shapes.
Note: This is for Rails 3. Rails 2.x automatically loads files from lib.