Skip to content

Instantly share code, notes, and snippets.

@ryana
Created June 19, 2011 23:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryana/1034924 to your computer and use it in GitHub Desktop.
Save ryana/1034924 to your computer and use it in GitHub Desktop.
doing hayzoos' job :P
class Vid < AR::Base
after_save :encode
private
def encode
VidEncoder.go(self)
end
end
class VidEncoder
include Rails.application.routes.url_helpers
# default_url_options should be set by server setup or
# a setup block in your test system
class < self
def go(obj)
Zencoder.stuff(:source_url => url_for(obj))
end
end
end
@ryana
Copy link
Author

ryana commented Jun 19, 2011

untested, but this idea is the right way to do it. you will regret including those URL helpers in your models down the road. xoxo.

@heizusan
Copy link

Ok, this gets the helpers outside the model. But that's the easy part, and not the crux of the problem I was having. :P

The challenge I was tackling was this: url_for(obj) typically requires a :host option. Unless you specify a default host, somehow. That means that the model must know the host. This is how rails is designed. I reversed engineered to find out. If you don't provide :host => some_host, rails looks at obj.default_url_options - which does some magic method shit.

It's all fine, if you define a method for that - or the settings, for your model. But when I need the host to be based on the server/environment the code is running on, I need something in the application settings layer to define all that. Thus the ActiveRecord::Base extension. It's completely different from including the helpers.

Even if I do what you suggest (which I should, I will admit that), I still need the ActiveRecord extension that I implemented.

@ryana
Copy link
Author

ryana commented Jun 20, 2011

I tried making a comment on iPhone. It didn't work. What I said was: before you tell me what you still need wrt default_url_options, try out that code, because you're wrong.

@AdventureDuck
Copy link

Not only did what I still need not work. But the url_helpers don't either. :P

I have a route: video_zencoder.

In the model, with url_helpers included, url_for works. In the class I created, including the url_helpers, url_for does not work. Huh. Don't worry, this makes no sense to me, either.

Here's the shell I have:

class ZencodeCommunicator
include Rails.application.routes.url_helpers

def self.create(...)
...
url_for(...)
...
end
end

And I get this:

NoMethodError (undefined method `url_for' for ZencodeCommunicator:Class):

extend doesn't work, either. Already tried.

xoxo :P

@AdventureDuck
Copy link

Ok. After fixing your errors: (can't use a class - have to use an instance. So ZencodeCommunicator.new.create(...). Or, as I did, ZencodeCommunicator::ZC = ZencodeCommunicator.new in the library file that defines the class.)

I still get this: RuntimeError (Missing host to link to! Please provide :host parameter or set default_url_options[:host]):

THIS is what I was trying to solve with the activerecord extension.

xoxo :P

@AdventureDuck
Copy link

Last comment:

But, adding the default_url_options value to that class works. Still a problem that there's no way for default_url_options to be an application-wide setting. You have to set it individually for every context. It's not like these values are going to change for the various models (right now). But they do change, based on the environment.

@ryana
Copy link
Author

ryana commented Jun 20, 2011

The activerecord extension is wrong. default_url_options is defined on ZencodeCommunicator. you gotta set the host parameter just like the error says. you do this in your test system, config/initializers, or in a before_filter in application_controller.

sorry I can't spend more time teaching you this stuff. gotta do my own work :)

@AdventureDuck
Copy link

Last last comment: I didn't see your comment block in the code, before. I haven't found - anywhere online - a way to set default_url_options for the entire application. There must be a way. But nobody illustrates it. Not even the rails site, that I've found.

@AdventureDuck
Copy link

I know how to set it for an individual class - I've done that, successfully. But I should be able to set that somewhere for the entire application - so I don't have to have multiple lines like

config.action_mailer.default_url_options = ...
ZencoderCommunicator.default_url_options = ...

That's bullshit, when they're all the same setting. I need something like

config.default_url_options = ...

The others can override that value, if need be.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment