Open Source Bridge
I submitted a proposal for Open Source Bridge, an ambitious, community-driven conference happening in Portland, Oregon from June 17th to the 19th. The deadline for submissions is today!
Comments Off on Open Source Bridge
Synthesis : Scott Becker
I submitted a proposal for Open Source Bridge, an ambitious, community-driven conference happening in Portland, Oregon from June 17th to the 19th. The deadline for submissions is today!
Comments Off on Open Source Bridge
Here are the slides for the presentation I gave the other week at the Portland JavaScript Admirers meeting: SproutCore – A Next Generation JavaScript Framework. It is an overview of some of the key features I think make SproutCore unique and very useful. There was also video recorded which should hopefully appear online soon. I plan to do another SproutCore focused talk with a walkthrough of a full backend-connected application.
Comments Off on SproutCore Slides
If you’re working on a project with a couple years of code under it’s belt, you may have moments of desire to completely throw the whole thing out and start anew. But, if you’re working for someone else, you know that your boss or client probably won’t like that. After all, he’s paying you to finish feature A! But, you lament, feature A builds on top of feature B that already exists but is written in a terribly unidiomatic, pre-TDD, pre-REST way, way before you or someone else learned the dark arts of coding mastery. Legacy code, argh!
So what should you do? What are your options? A) Ignore the problem and build the new feature on top of the existing badly written code. Watch things get even worse. B) Try to quickly fix the badly written code in-place so you can get on with it. Trigger cascading test failures, and palm your face. C) Start over and rewrite the entire project, get fired for being 6 months late on your estimate. None of these sound any good!
I think I may have found a solution that works for me, especially with Rails.
Start a fresh Rails project. Boom! It’s fresh and clean. Now you have room to work. The beauty of Rails is how quick it is to get started. Choose the aspect of the project you want to work on. Got some legacy code thats bugging you? TDD/BDD it from scratch, the Right Way. The idiomatic, Rails Way. Once you get it to the point where it’s working correctly and passing all tests, you can merge the new code into the main project, replacing the older, ugly, what-were-they-thinking legacy code that was getting in the way.
Now you can work in a clean environment and just focus on the problem at hand. This allows you to make progress quickly, and get rid of the old crap without having to completely start from scratch.
Comments Off on Starting Fresh, Without the Big Rewrite
A long overdue update of AssetPackager is finally here:
Get the latest at http://github.com/sbecker/asset_packager
Thanks to the many forkers for ideas and solutions.
Comments Off on AssetPackager update
PJ Hyett asked for it on Twitter, so I created it. Announcing:
The Github Theme for TextMate!
Hosted on, GitHub! (what else?) The basics are defined, but I’m sure it could be improved. In which case, you know what to do.
Asset Packager now has a tracker where you can submit tickets. You can find it here.
If you have a bug to report, and/or a patch for Asset Packager, this is the place.
Comments Off on AssetPackager Tracker
My company WeoGeo made it to the final round of the Amazon Web Services Startup Challenge. In the top 7 out of around 1000 entrants, not too shabby.
Check out the videos for the finalists, and vote for WeoGeo!
Comments Off on WeoGeo – AWS Startup Challenge Finalist
Just wanted to mention our Rails Rumble project, RubyBrigade.org. Jason Perry, James Seaman and I
worked through the weekend to build RubyBrigade.org – a geographically aware database of Ruby User groups.
Big thanks to James for the killer hand drawn illustrations and interface. Big thanks to Jason & Katie for letting us take over their house for the weekend.
If you like what you see, vote for us!
Comments Off on RubyBrigade.org – A Rails Rumble Success
In Scott Raymond’s excellent book Ajax on Rails I came across a cool (non-ajax related) pattern for insuring a request’s method is POST and showing a confirmation form if not.
This is really useful in situations such as confirmation links in emails, where a form can’t be displayed and javascript won’t work, yet we don’t want a destructive action occurring through a GET request.
Here’s an example:
def unsubscribe if request.post? @user = User.find(params[:id]) @user.update_attributes :subscribed => false redirect_to home_url else render :inline => %Q( <% form_tag do %> <%= submit_tag 'Confirm' %> <% end %> ) end end
One other piece of code you’ll need to get this to work, assumming you’re using RESTful routes, in config/routes.rb
map.resources :users, :member => {:subscribe => :any, :unsubscribe => :any}
This states that we have a couple extra actions in our controller, and we’re not going to mandate a specific HTTP method to get there. This way we can handle it within the action if the HTTP method is wrong.
Now if your user comes to your page from a straight GET request, he’ll get prompted to confirm the big destructive action he’s about to commit.
(In a real app we’d make this form look a bit nicer.)
Wouldn’t it be nice if we could re-use this pattern, without writing out the inline form code every time? Seems generic enough.
We could abstract that out, and also support any HTTP method we want. Lets follow Rails / RESTful conventions , and require PUT for updating a model. Using some handy ruby block syntax, we could write something like, say:
def unsubscribe confirm_unless :put do @user = User.find(params[:id]) @user.update_attribute :subscribed, false flash[:notice] = "User is now unsubscribed" redirect_to users_url end end
Much better. But how?! Keep reading.
To get the confirm_unless method for yourself, slap the following code into app/controllers/application.rb:
def confirm_unless(method) if request.method == method yield else render :inline => %Q( <% form_tag({}, :method => :#{method}) do %> <%= submit_tag "Confirm" %> <% end %> ) end end
We could take it one step further and make it a before_filter, but I’ll leave that for a possible future post.
Comments Off on Are you SURE?! (How to confirm HTTP methods in Rails)
A while back someone posted on rubyonrails-talk asking how to export to CSV from Rails. I posted a solution, and people seemed to dig it, so I’ll share it again here.
Get the FasterCSV gem. Why? It’s faster, and easier to use. Once you’ve got it, require it in environment.rb. Here’s an abbreviated version of my working controller method. Copy/paste/modify. And you’re done!
def export_to_csv
@users = User.find(:all)
csv_string = FasterCSV.generate do |csv|
# header row
csv << ["id", "first_name", "last_name"]
# data rows
@users.each do |user|
csv << [user.id, user.first_name, user.last_name]
end
end
# send it to the browsah
send_data csv_string,
:type => 'text/csv; charset=iso-8859-1; header=present',
:disposition => "attachment; filename=users.csv"
end
Now, if we wanted to get all clever about it, we could go further and serve both html and CSV data with only one action, using respond_to. This also requires that you add a RESTful route (map.resources :users) to your routes.rb file:
def index
@users = User.find(:all)
respond_to do |wants|
wants.html
wants.csv do
csv_string = FasterCSV.generate do |csv|
# header row
csv << ["id", "first_name", "last_name"]
# data rows
@users.each do |user|
csv << [user.id, user.first_name, user.last_name]
end
end
# send it to the browsah
send_data csv_string,
:type => 'text/csv; charset=iso-8859-1; header=present',
:disposition => "attachment; filename=users.csv"
end
end
end
Now if the user requests:
/users
she’ll get HTML. If she requests:
/users.csv
You get the point.
Comments Off on How to generate CSV files in Rails