Railsy Question about redefining ruby methods...

X

Xeno Campanoli

Please this is a Ruby question, not, at least as the main focus, a rails
one, as you'll see if you read all the way through.

Say, I've got a case in Rails where I want all my links to be prepended
with something like ~u1/rrsw/thingies so I can see
http://host/~u1/rrsw/thingies and http://host/~u2/rrsw/thingies instead
of http://host:9001/thingies and http://host:9002/thingies for a rails
tool that is used by some n projects. The rails part is apparently not
configurable, so I'm looking at redefining rails methods like link_to to
stick the path in there. This should be reasonable, but I'm stuck on a
ruby problem:

I want to redefine the link_to method so it's seen in its new guise in
all the places where it's presently used. It seems to make most sense
to put the new redefinition in the module ApplicationHelpers, but I
believe that may short circuit the access, as I'm not seeing the example
I put in which always puts out a traceable constant, resulting in the
expected changes in my test web pages. So, and here is the question:

Can anyone suggest how I should specify an appropriately available
method for the correct module inside another module so that it overrides
the original in the fashion I desire? I presume this is a simple
problem, as it's what people do all the time in OO right? I'm still a
little green on these patterns yet I think.

Thanks for any suggestions.

Sincerely, Xeno Campanoli

Droopy hacker on a Friday Evening...
 
P

Phillip Gawlowski

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Xeno Campanoli wrote:
| Please this is a Ruby question, not, at least as the main focus, a rails
| one, as you'll see if you read all the way through.
|
| Say, I've got a case in Rails where I want all my links to be prepended
| with something like ~u1/rrsw/thingies so I can see
| http://host/~u1/rrsw/thingies and http://host/~u2/rrsw/thingies instead
| of http://host:9001/thingies and http://host:9002/thingies for a rails
| tool that is used by some n projects. The rails part is apparently not
| configurable, so I'm looking at redefining rails methods like link_to to
| stick the path in there. This should be reasonable, but I'm stuck on a
| ruby problem:

You are aware that the port of the URL is not under Rails' nor Ruby's
control per se (it is the domain of the webserver)?

URLs are composed in (roughly) six parts: the
[protocol]://[host]:[port]/[user_directory]/[directory/that/app/uses/or/rewrites]/[document_requested.suffix]

Of those, only the directory and explicit document are under control by
any given web application.

Further, you can only have one server listen on any given port (though,
one server can listen on an unlimited number of ports).

What you want, can be achieved by using Apache + mod_rails.

Uou can get the ~u1/~u2 part, by having different users on a, for
example, UNIX based OS have their own web_root (or web_home, as it can
be called, by convention), which would be reachable as
host/~username/index.html (for example).

Details of implementing such a setup can be found in various ways on,
for example, <http://howtoforge.org>.


As far as overriding a Ruby method: You can usually achieve that by
reopening the class, adding an alias for the old method, and defining
your own implementation. Be careful, though, when you do.

Where to do that so Rails picks up a change to link_to, I do not know (I
suspect it is in ActiveSupport, though).

- --
Phillip Gawlowski
Twitter: twitter.com/cynicalryan
Blog: http://justarubyist.blogspot.com

~ - You know you've been hacking too long when...
...your complexion has turned pale from being constantly irradiated by the
monitor.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkg3sU8ACgkQbtAgaoJTgL/zGgCcD/x24SNTx8OFslWJ5fX5A5gq
x8UAniPPYbQl1pPj/ytTyFeoxS5Hp7N7
=Z9nu
-----END PGP SIGNATURE-----
 
X

Xeno Campanoli

Phillip said:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Xeno Campanoli wrote:
| Please this is a Ruby question, not, at least as the main focus, a rails
| one, as you'll see if you read all the way through.
|
| Say, I've got a case in Rails where I want all my links to be prepended
| with something like ~u1/rrsw/thingies so I can see
| http://host/~u1/rrsw/thingies and http://host/~u2/rrsw/thingies instead
| of http://host:9001/thingies and http://host:9002/thingies for a rails
| tool that is used by some n projects. The rails part is apparently not
| configurable, so I'm looking at redefining rails methods like link_to to
| stick the path in there. This should be reasonable, but I'm stuck on a
| ruby problem:


No no no no no no! I have all the other stuff working. I've got
UserDir and ProxyPass directives that get all the stuff set up initially
from my http://host:n to my http://host/~u1/rrsw like I want. All I
need now is to get all the URLs that are presently rendered in the
markup as /something to be ~u1/rrsw/something. That is all I need. I
have the rest working.

So, thank you for your feedback. It is interesting for other reasons,
but what I was asking about was the very thing you said you were unable
to answer; nothing more.

Sincerely, Xeno

xc
You are aware that the port of the URL is not under Rails' nor Ruby's
control per se (it is the domain of the webserver)?

URLs are composed in (roughly) six parts: the
[protocol]://[host]:[port]/[user_directory]/[directory/that/app/uses/or/rewrites]/[document_requested.suffix]


Of those, only the directory and explicit document are under control by
any given web application.

Further, you can only have one server listen on any given port (though,
one server can listen on an unlimited number of ports).

What you want, can be achieved by using Apache + mod_rails.

Uou can get the ~u1/~u2 part, by having different users on a, for
example, UNIX based OS have their own web_root (or web_home, as it can
be called, by convention), which would be reachable as
host/~username/index.html (for example).

Details of implementing such a setup can be found in various ways on,
for example, <http://howtoforge.org>.


As far as overriding a Ruby method: You can usually achieve that by
reopening the class, adding an alias for the old method, and defining
your own implementation. Be careful, though, when you do.

Where to do that so Rails picks up a change to link_to, I do not know (I
suspect it is in ActiveSupport, though).

- --
Phillip Gawlowski
Twitter: twitter.com/cynicalryan
Blog: http://justarubyist.blogspot.com

~ - You know you've been hacking too long when...
...your complexion has turned pale from being constantly irradiated by the
monitor.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkg3sU8ACgkQbtAgaoJTgL/zGgCcD/x24SNTx8OFslWJ5fX5A5gq
x8UAniPPYbQl1pPj/ytTyFeoxS5Hp7N7
=Z9nu
-----END PGP SIGNATURE-----
 
P

Phillip Gawlowski

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Xeno Campanoli wrote:

| No no no no no no! I have all the other stuff working. I've got
| UserDir and ProxyPass directives that get all the stuff set up initially
| from my http://host:n to my http://host/~u1/rrsw like I want. All I
| need now is to get all the URLs that are presently rendered in the
| markup as /something to be ~u1/rrsw/something. That is all I need. I
| have the rest working.

Why? Is the application not working? The webserver is smart enough to
construct the link properly, so that links should Just Work(tm).

And you don't really want absolute links, either, as this will break
once you move the application to another server.

Otherwise, look at Rails' routes.rb.

- --
Phillip Gawlowski
Twitter: twitter.com/cynicalryan
Blog: http://justarubyist.blogspot.com

Youth -- not a time of life but a state of mind... a predominance of
courage over timidity, of the appetite for adventure over the love of ease.
~ -- Robert Francis Kennedy (1925-1968)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkg4rxoACgkQbtAgaoJTgL/YBACgpy5rJGR6uwqjmVNp2254HsrH
2SQAoKccu01YWo+xnag0qY2GTeKqYKCm
=oIi1
-----END PGP SIGNATURE-----
 
D

David A. Black

Hi --

Please this is a Ruby question, not, at least as the main focus, a rails one,
as you'll see if you read all the way through.

Well, if my answer is correct, it's a very Rails-specific question :)
Say, I've got a case in Rails where I want all my links to be prepended with
something like ~u1/rrsw/thingies so I can see http://host/~u1/rrsw/thingies
and http://host/~u2/rrsw/thingies instead of http://host:9001/thingies and
http://host:9002/thingies for a rails tool that is used by some n projects.
The rails part is apparently not configurable, so I'm looking at redefining
rails methods like link_to to stick the path in there. This should be
reasonable, but I'm stuck on a ruby problem:

I want to redefine the link_to method so it's seen in its new guise in all
the places where it's presently used. It seems to make most sense to put the
new redefinition in the module ApplicationHelpers, but I believe that may
short circuit the access, as I'm not seeing the example I put in which always
puts out a traceable constant, resulting in the expected changes in my test
web pages. So, and here is the question:

Can anyone suggest how I should specify an appropriately available method for
the correct module inside another module so that it overrides the original in
the fashion I desire? I presume this is a simple problem, as it's what
people do all the time in OO right? I'm still a little green on these
patterns yet I think.

Have a look at relative_url_root, which I believe (though I haven't
used it in a while) does what you want.


David

--
Rails training from David A. Black and Ruby Power and Light:
INTRO TO RAILS June 9-12 Berlin
ADVANCING WITH RAILS June 16-19 Berlin
INTRO TO RAILS June 23-26 London (Skills Matter)
See http://www.rubypal.com for details and updates!
 
X

Xeno Campanoli

Phillip said:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Xeno Campanoli wrote:

| No no no no no no! I have all the other stuff working. I've got
| UserDir and ProxyPass directives that get all the stuff set up initially
| from my http://host:n to my http://host/~u1/rrsw like I want. All I
| need now is to get all the URLs that are presently rendered in the
| markup as /something to be ~u1/rrsw/something. That is all I need. I
| have the rest working.

Why? Is the application not working? The webserver is smart enough to
construct the link properly, so that links should Just Work(tm).

And you don't really want absolute links, either, as this will break
once you move the application to another server.

They aren't absolute at all. What I get rendered by rails link_to, for
instance, is things like <a href='/something'>Something</a>. What I
need is still a relative link, which is <a
href='~u1/rrsw/something'>Something</a> for u1, and <a
href='~u2/rrsw/something'>Something</a> for u2. I originally expected
there would be a simple rendering configuration for this in rails, and
spent much of yesterday and part of Thursday asking on the #rubyonrails
IRC. Not only did those folks know of no such configuration, but they
didn't seem to even understand the OO need for encapsulation, nor the
logical reason, nor the traditional Unix Webserver usage of ~user,
nowadays configured with the UserDir Apache directive. Anyway, those
are mostly Rails oriented issues and problems, which are interesting,
but not completely appropriate here. What is appropriate to address
here is the whole idea of redefinition of a Ruby method like link_to
which is embedded in some arbitrary community/tree of modules and/or
classes. I've never done much of that, and I know it's part of the real
power that experienced OO users acquire in such environments.

Now ideally it would be nice if some Rails supergeek came out of the
woodwork and said there in fact was a configuration to change the
rendering, but it looks like there's not. You wouldn't want to change
the URLs that Rails renders with an external rewrite in Apache if you
can help it because that breaks encapsulation, which is a standard and a
good ethic in OO programming any reasonable engineer would recognize,
and given the ability to simply redefine link_to, url_for, or whatever
other methods I use can achieve what I want, and not only that perhaps
help lead to a future Rails configurability on such things in a
reasonably elegant matter, presuming that it in fact doesn't yet exist,
seems to me to make that the proper direction for me at this point.

Thanks for asking.
 
X

Xeno Campanoli

David said:
Hi --



Well, if my answer is correct, it's a very Rails-specific question :)

Thank you David. I'll look at this right now, and try to have an answer
by tomorrow afternoon, as my evening is about to draw me away.'

Sincerely, Xeno
 
X

Xeno Campanoli

David said:
I'm cutting this short and just saying that David Black's suggestion
works regarding:
Have a look at relative_url_root, which I believe (though I haven't
used it in a while) does what you want.

Since this is a ruby group, and the suggestion is rails, I am going to
top off my discussion directly to my two correspondencts.

Thank you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top