print out a controller's expected actions
Posted by jacqui maher on April 26, 2008 at 04:59 PM
Ever want to see a list of actions defined on your controller and what methods over http they expect?
try this in your console, where “users” is your controller’s name:
1 ActionController::Routing::Routes.routes.select do |route|
2 route.defaults[:controller] == "users"
3 end.uniq.each do |route|
4 puts "#{route.defaults[:action]} expects http method: #{route.conditions[:method]}"
5 end;nil
select!
Posted by jacqui maher on April 11, 2008 at 01:55 PM
I found myself having to reverse logic in some code I wrote recently due to the wants of the client doing a 180. I had been using reject! to remove elements in an array, and realized that I now wanted to use select! - only there is no select!
I started to write:
arr = arr.select{|a| some.method.here }
when it occured to me that was a little clunky. I wanted select in place.
Here’s my version of it:
1 class Array
2 def select!(&block)
3 selected = self.select(&block)
4 if selected.size > 0
5 replace(selected)
6 else
7 nil
8 end
9 end
10 end
Usage:
1 >> arr = ["Ren", "Easy-E", "Ice Cube", "DJ Yella", "Dr Dre"]
2 # => ["Ren", "Easy-E", "Ice Cube", "DJ Yella", "Dr Dre"]
3
4 >> arr.select!{|a| a =~ /^D/ }
5 # => ["DJ Yella", "Dr Dre"]
6
7 >> arr.select!{|a| a =~ /^J/ }
8 # => nil
9
10 >> arr
11 # => ["DJ Yella", "Dr Dre"]
mini-update
Posted by jacqui maher on January 30, 2008 at 11:12 AM
I have been really busy lately, and this blog has been awfully quiet. However! I am working on stuff.
Like what?
- testunits2rspec converter gem released by the end of the week.
- decision on wtf to do with my nelson mock creation automation framework, as there’s only so much one can do automagically in a dynamically-typed language
- hopefully will be releasing my fixtures2factory plugin (or maybe a gem) next week
- fixtures2mocks to follow
In the meantime, the awesome artist Shephard Fairey has made two posters recently that I like. I’m starting to consider voting for Obama, now that I finally have allowed myself to read what he stands for and compare it to what Clinton stands for. Mostly it’s their voting records that have me more in favor of Obama. Plus he’s a new face and I think that, more than anything, is what America needs to regain footing with the rest of the world. Because really, in the end, I don’t think any one politician that could get elected into the office of the president of the US can be that much different than another one.
So… come on people, at least, you american people reading this who are 18 and older:
… perhaps even vote for:
but no matter what you vote for, do vote.