Uninitialized content when trying to include ActionController::UrlWriter in model - ruby-on-rails

I'm using Rails 3 beta 4 and trying to include ActionController::UrlWriter in my model, which is the correct way to go about it as far as i can tell, but i get "Uninitialized Constant ActionController::UrlWriter".
Any idea why that would be? Did it move in rails 3?

First I agree with zed. This shouldn't be done in a model. Your models should be unaware of any http url.
I do the same in a resque job. Here's what I do :
include ActionDispatch::Routing::UrlFor
include ActionController::PolymorphicRoutes
include Rails.application.routes.url_helpers
default_url_options[:host] = 'example.com'
Then you can call any usual url generator.
url_for(object)
page_url(object)
It'll create the link on the host defined as default_url_options[:host]

Answers to Can Rails Routing Helpers (i.e. mymodel_path(model)) be Used in Models? are pretty good
or see
http://api.rubyonrails.org/classes/ActionDispatch/Routing/UrlFor.html
http://siddharth-ravichandran.com/2010/11/26/including-url-helpers-in-models/
Basically, you can do something like this in a model:
def url
Rails.application.routes.url_helpers.download_attachment_path(self)
end
It is worth considering whether this is the right layer, though. In my case it's for file attachments and I want to do attachment.url instead of writing out a helper a lot.

Related

Extend a Model from a Rails Engine (not replace it)

I have a Rails app that uses a gem called ActsAsTaggableOnSteroids, which is a Rails Engine. Specifically, I'm using PavelNartov's fork of the gem. But nevermind that.
I need to add specific functionality to the Tag model, which is supplied by the engine.
But, according to my understanding of Rails engines and the magical loading functionality in Rails, if I put a file called "tag.rb" in my models directory, then it will completely replace the one from the Engine.
Ideally, I would be able to do something like:
class Tag < ActsAsTaggable::Tag
# my stuff
end
...but alas, that doesn't work because the model supplied by the engine is not namespaced.
So, I came up with this nightmare, which I put in app/models/tag.rb:
path = ActsAsTaggable::Engine.config.eager_load_paths.grep(/models/).first
require File.join(path, 'tag')
Tag.class_eval { include TagConcern }
But there has to be a better way! I feel like I'm missing something. I'd prefer not to add this strangeness to my app if possible.
Just require the file by looking up the path of the gem's model:
require File.join(Gem::Specification.find_by_name("bborn-acts_as_taggable_on_steroids").gem_dir, 'app/models/tag')
Tag.class_eval do
# ...
end

Losing namespace information in a rails namespaced model

When you create a namespaced model with rails scaffolding, you get two files. For example, this scaffold:
rails generate model Staff::Location name:string address:string
Generates these files:
/app/models/staff.rb
module Staff
def self.table_name_prefix
"staff_"
end
...
/app/models/staff/location.rb
class Staff::Location < ActiveRecord::Base
...
I am running into problems when in development mode where rails unloads the Staff module and never reloads it. This causes several annoying bugs such as Location not able to access it's table due to the missing table_name_prefix. The problem seems to crop up when I don't access the models directly, such as through a polymorphic relationship.
I can't seem to get the module loaded on a consistent basis. Is this the best practice way to do namespaced models? If it is, what am I missing?
Although I wasn't able to reproduce the problem in Rails 3.2.2, I've run into something like this before. The generic way to hack around this problem in development mode is through an ActionDispatch callback. Add this to config/environments/development.rb:
MyApp::Application.configure do
ActionDispatch::Callbacks.before do
load Rails.root.join('app', 'models', 'staff.rb')
end
end
Anything you do in that block will be executed before each request, so make sure you're only doing it in development mode.† Otherwise, you're going to suffer a performance hit in production.
I logged a message inside the staff.rb file and within the Staff module itself, and both messages appeared in the log for each request.
† I tried using the to_prepare callback, since that seems to be the documented way to execute code before each request only when cache_classes is false. But that only seemed to execute after restarting the application. There's at least one other open Stack Overflow question regarding this, although he's using a slightly different syntax than I used. If you can get to_prepare to work, I'd suggest that instead of before.
About a year later, I have finally found the answer to this question. This answer is specifically for rails 3.1. I am not sure if it is a problem in rails 3.2.
The problem occurs when setting up a model. If scaffolding is used, no helper file is generated. This would normally be in /app/helpers/staff/location_helper.rb. There are two ways to setup this file:
module Staff::LocationHelper
...
end
module Staff
module LocationHelper
...
end
end
In rails 3.1, specifically for helpers, you must use the first solution. You do not have to use it for other modules that use a namespace in other parts of the rails project. In fact, some structures in ruby require the second solution.
If you use the second solution when declaring a helper, in certain cases the Staff module in the helper file will override the module in /app/models/staff.rb. It will silently replace it with the empty Staff module in the file. This does not happen 100% of the time because helpers are not always loaded.

'RuntimeError: route set not finalized' when calling Rails.application.routes.generate() or Rails.application.routes.recognize_path()

I use the Rails.application.routes.generate() and Rails.application.routes.recognize_path() in my application to disassemble and generate URLs using the Rails routing table. I never had a problem with this using the Rails 2.3.x series but since Rails 3 (specifically 3.1.3) I've been getting the following error when invoking these commands
RuntimeError: route set not finalized
Two questions :
Why? Am I getting this - my application is up and running and handling other requests. Why are the routes not finalized by now?
To solve this error I call Rails.application.routes.finalize! before invoking either of these methods as it seems to work. Are there any implications to doing this?
In Rails 3, you don't use this technic. You need include Rails.application.routes.url_helpers after you can use your named_route
class User < ActiveRecord::Base
include Rails.application.routes.url_helpers
def my_own_url
user_url(self)
end
end

Use Rails path helpers from inside an plain old ruby class? (Resque job)

I need to make some API calls from inside a Resque job (which is just a plain old Ruby class) and those API calls need to include URLs to resources on my site.
Is it "correct" to just add this to my class definition?
include Rails.application.routes.url_helpers
Or is there an "expected" way to do this?
It seems that is the way to do it.
http://snipplr.com/view/37063/to-access-url-helpers-urlfor-etc-from-rails-console-rails-3/
linked to from here http://apidock.com/rails/ActionController/UrlWriter
In your controller:
Rails 1.x:
include Rails.application.routes.url_helpers
Rails 2.x:
include ActionController::UrlWriter

How to organize models in Sinatra?

I have a tirable orgnizing my Models in a Sinatra project.
Let's say I have 2 models: Post and Comment, nn Post model, I have to call Comment model. And now I have <class:Post>': uninitialized constant Comment (NameError).
I know its an issue in ordering the requiring for the models, but what about if I have lots of models? What's the Rails way in requiring models, etc.?
UPDATE
I use this code to auto_load my models in Sinatra/Rack/Grape applications. This code should be at the top of your code ie in the boot file.
models = File.join(File.dirname(__FILE__), 'app', 'models') # path to your models
$LOAD_PATH << File.expand_path(models)
# Constent Missing for requiring models files
def Object.const_missing(const)
require const.to_s.underscore
klass = const_get(const)
return klass if klass
end
You should put all of your models in a folder, such as lib in your application, then you can add this to the top of your Sinatra app file:
$: << File.dirname(__FILE__) + "/lib" # Assuming app.rb is at the same level as lib
require 'post'
require 'comment'
You should organise your code so that you do not call other models until all model declarations are loaded.
The Rails way is based on a very nice Ruby feature: const_missing. You could write your const_missing method or looking around the web for a solution with const_missing and sinatra.
no prob when I tried this
a Comment if it is in a method of Post shouldn't be actually evaluated
there must be some circumstance triggering the NameError
don't call Post in the body of the class declaration
load all the model files per the first commenter's suggestion
shouldnt be having the same reference troubles as Java per se
in a dynamic lang like Ruby

Resources