Non-English resourceful routes
0 comments
|
2008-04-24
I previously wrote about resourceful routes improvement in edge that will provide URLs independent of resources path names in application. This is a great benefit for Non-English URLs.
But that didn’t solved ‘new’ and ‘edit’ parts of URL.
Finally this improvement and bugfix follow-up did just that:
map.resources :cars,
:as => 'kola',
:path_names => { :new => 'novo',
:edit => 'izmena' }
...so that:
new_car_path # => /kola/nova
edit_car_path(17) # => /kola/17/izmena
...will map nicely to appropriate controller actions.
To globally set new values use:
ActionController::Base.resources_path_names = {
:new => 'novo',
:edit => 'izmena' }
map.resources :x, :as => 'y'
0 comments
|
2008-03-26
Somehow I’ve missed this two months old improvement, that will mean a lot for non-english Rails apps.
From its creation, Resources had a convention without configuration for resource route names and URL. One determines another.
Finally, Rails edge (set to be in Rails 2.1) has this feature:
map.resources :comments, :as => 'komentari'
This will give you restful route names in English (preferred for code readability), while letting you customize resource name in URL (ie. /komentari/17).
UPDATE: This article has a follow-up.
Self or default if empty, DRY way
6 comments
|
2008-03-19
I really appreciate following idiom in Ruby:
person.phone || "N/A"
It’s DRY. No need for conditional structures, and repeating person.phone in condition and as return value.
But lets say you don’t control your data source, and there is an empty string instead of nil when data is not available (sounds familiar?). How would you deal with that and keep things simple and DRY?
Well, basicly all you need is a String method that will return nil if string is empty, or self if it’s not empty. String object doesn’t have that kind of method, at least not that specialized. Regexp to rescue. Lets try this:
>> ""[/.+/m]
=> nil
>> "ruby"[/.+/m]
=> "ruby"
Nice. So solution for the problem above is:
person.phone[/.+/m] || "N/A"
Throw andand in game to support both nil and empty value with no cost in complexity:
person.phone.andand[/.+/m] || "N/A"
andand
2 comments
|
2008-03-18
I really, really like andand . Try it and stop repeating yourself.
I mean, why would you want to write this:
entry.at('description') && entry.at('description').inner_text
when you can write this:
entry.at('description').andand.inner_text
Eleventh
5 comments
|
2008-02-11
Did you know that Ruby was Matz’ eleventh take on creating a programming language? Listen. So don’t despare next time you fail, you are right on track.
“I sat down and counted the other day and Ruby was eleventh language I’ve tried to write. Out of those 4 only had names, 3 of them didn’t work at all… the rest of them was just experiments. Ruby is the only one that really worked, so I guess that makes Ruby the first one, but it was one out of eleven attempts.” ~ Matz
The road to wisdom? -- Well, it's plain
and simple to express:
Err
and err
and err again
but less
and less
and less.
~Piet Hein
Nested Ternary operator
0 comments
|
2008-01-20
I was going through some of my old Java code and found this interesting piece of code. It’s basically alternative way of creating switch structure.
public Object getValueAt(int row, int col) {
Person person = data.get(row);
return
col==0? person.getFirstName()
: col==1? person.getLastName()
: col==2? person.getAge()
: col==3? person.getAddress()
: null;
}
Cons:
- unlike switch, there is no compile time error for duplicated cases
- this is not a standard way of dealing with different values of a variable so you could leave wondering the person that will read you code
- more control over branching logic since expressions are used (switch must use constants as case values)
- this is not a standard way of dealing with different values of a variable so you could be seen as hacker in the eyes of the person that will read you code ;)
Hello World
1 comment
|
2008-01-14
Well what title of first post did you expect from a programmer? :)

RSS
Contact