Mostrar / Ocultar Avisos

Archive for ‘Ruby on Rails’

RESTful_Easy_Messages now on GitHub.

I just pushed the 0.7 svn tag of the RESTful_Easy_Messages plug-in to GitHub so I can play with the cool kids. Go see it here => http://github.com/sschroed/restful_ezm. This will be the authoritative source from here on out.

It is in need of an update for Rails 2.1 so any help is appreciated.

RSpec and Rails Partials

For some reason Test:Unit and I are not friends. I’m not really sure why, but I always drop to voice mail when I call. So, I moved and started working on project with Andy and Heather Tinkham (two local MPLS software testers) to learn RSpec. More to come on that at a later date. Stealth mode now. Move along, nothing to see…

It’s been fun and I’m enjoying BDD. Once I get the basics down I’ll get into RSpec stories too. Anyway, back to the point.

On my current project we are working on a new version of the most important feature of the site. Without it, everything else is crap. It’s the main driver for adding content and it needs to be rock solid, no, diamond solid. And being a newly minted RSpec user I wanted spec it all out, cover every angle, even add view specs. Here is where my noobness shines.

The new feature is in it’s own partial which is nested in another partial nested within a page. Pretty standard stuff. With the process for writing Rails views and partials in mind I set about trying to write nested view specs. That doesn’t work. AT ALL. The RSpec view docs talk about the expect_render and stub_render methods but I still couldn’t figure it out.

So I called google and still nothing really popped out until I saw an archived email chain where someone said just spec the partials. WTF? OK? So I added a new file to my specs: spec/views/my_view_folder/partial_name.html.erb_spec.rb, added the following, and waited for autotest to run. And you know what? That bastard ran. Maybe I’m just dense, or missed a paragraph somewhere, but an example of the right way to do partial specs would have been sweet. I hope this helps other unsuspecting noob.

Note: Just in case this genero code is confusing replace “my_view_folder” with the actual name of the view directory and “partial” with the actual partial name. And change ControllerHelper to be the name of the actual helper. You get that when you run the RSpec scaffold generator. I did that because I’m lazy and didn’t want to make all the spec files by hand.

UPDATE: I’ve updated the source to show how to do locals.

Resources I found useful

Live Blogging from the F1 Web Challenge

Well it’s 8:30 am and we are about to get started with 24 hours of hard core web development. I’ll be updating our progress throughout the day.

Team Ruby.mnTeam setup

8:55 am Opening comments
8:57 am Our non-profit is Little Brother: Friends of the Elderly
12:07 pm Lunch break with a Rock Band challenge

There are some hard core Rock Band players here. I hope our rookie team can do well, and by that I mean not place last. I think we’ll do well. Here’s the band…

Vocals: Robert Fischer
Guitar: Nate Kadlac
Drums: Andy Tinkhan
Bass: Kathleen O’Brien, from Little brothers

Drum Master AndyRobert Rocks OnThe Band

So far we’re in 2nd place…

6:58 pm Dinner time. Mmmm Chipotle.

Here is the official flickr stream for the event >> Photo me, baby!

Notice how Team Ruby.mn is properly featured?

Alicia and Nate have created a sweet site design that just blows away Little Brother’s current site. Justin and I are hacking away at the CMS. Lars is maxing out his credit cards with the online donations. Andy has written a book of RSpec tests. And Coung and Robert are building the volunteer sign-up. Everything is still parts is parts but everything is progressing nicely.

8:17 pm Little Brothers brought in a surprise for us. We got a clown. No one else got a clown. We rule.

Lars and MagicPink BunnyOur ClownMonkey Coder

12:02 am Half way there. I’m tired.

12:32 am Domino’s Pizza just delivered. Why did I think it was a good idea?

3:27 am Sierra Bravo just gave out some raffle prizes. I won “The Ultimate MAtix Collection” in HD-DVD. I don’t have an HD-DVD player yet and someone just told me Blue-Ray won the format war so I got that going for me. Which is nice.

5:12 am Can’t. Speak. In. Complete… Sentences…

7:05 am I just noticed the sun in coming up. I’ve been awake for 25 hours now. So tired…

9:35 am The site looks sweet. We’re just putting on the final touches and preparing for our demo.

9:44 am Time for a photo check-in.
Count Down

THIS IS NO PLACE FOR THE WEAK!

Sleeping Dude.

TEAM RUbY.mn hard at work.

Team Ruby.mn

10:51 am 1h 10 m left. Subversion commits are flying all over the place. Robert is also live blogging the end.

11:42 am Our final revision, number 161, was just checked in. Here are the stats for our project…

+----------------------+-------+-------+---------+---------+-----+-------+
| Name                 | Lines |   LOC | Classes | Methods | M/C | LOC/M |
+----------------------+-------+-------+---------+---------+-----+-------+
| Controllers          |   579 |   467 |      16 |      58 |   3 |     6 |
| Helpers              |    32 |    29 |       0 |       1 |   0 |    27 |
| Models               |   356 |   294 |      11 |      36 |   3 |     6 |
| Libraries            |   455 |   315 |       2 |      27 |  13 |     9 |
| Integration tests    |    46 |    40 |       1 |       2 |   2 |    18 |
| Functional tests     |   304 |   246 |      12 |      39 |   3 |     4 |
| Unit tests           |   242 |   201 |       7 |      31 |   4 |     4 |
+----------------------+-------+-------+---------+---------+-----+-------+
| Total                |  2014 |  1592 |      49 |     194 |   3 |     6 |
+----------------------+-------+-------+---------+---------+-----+-------+
  Code LOC: 1105     Test LOC: 487     Code to Test Ratio: 1:0.4

F1 Overnight Website Challenge

F1 Overnight Website ChallengeYesterday I found that my team was selected for the Sierra Bravo F1 Overnight Website Challenge. I’m pretty excited about it. The teams get to stay up all night coding like fiends while powered by free Red Bull, Peace Coffee, Buffalo Wild Wings, and Chipotle with an occasional break for a Guitar Hero contest. My team is one representing the Ruby Users of Minnesota (RUM) and we have some top quality developers. I hope I measure up.

One part nerd Olympics, one part community service project and one part race-against-the-clock — Sierra Bravo’s F1 Overnight Website Challenge presented by VISI will partner deserving Minnesota non-profits with teams of talented web developers for 24 hours of fun collaboration culminating in a fully operational website for each participating non-profit. - Sierra Bravo website

Here is the link to team Ruby.mn. I’m the one with the party hat.

RESTful_Easy_Messages

So three month ago I released my first Rails plug-in, Easy_Messages (EZM), and I was pleasantly surprised by the response, excited that people were actually using my code. Then I became paranoid as people were actually using my code! Since then I worked briefly on a project which was written with REST in mind and was forced to look into it. Up to that point I had been doing my best to not meet REST in the hallway as I was a little scared by him. I don’t know why? After watching the Peepcode screencast by Geoffrey Grosenbach, everything clicked and I realized that I could make the code for EZM much better. The end result is this plug-in. I hope you find it useful.

The code is hosted at GitHub.

Here’s how to install the plug-in. (Rails 2.1 required for git plug-ins)

./script/plugin install git://github.com/sschroed/restful_ezm.git

Here’s how to run the generator.

For standard html views: ./script/generate messages erb
For haml[1] views: ./script/generate messages haml

[1] You will need to install the haml plug-in for the views to render properly.

I’ve tried to decouple as much of the code as I could with this release. If you used Easy_Messages you’ll remember most of the code was stuck in the plug-in directory. With REZM the generator will put a controller, helper, model, tests, and a few other support files right into your project for easy access. To see the entire list view the FILELIST in plugins/restful_easy_messages. There is still a tiny bit of code in the plug-in though.

If you are using Rick Olson’s RESTful_Authentication you can get REZM up and running with minimal setup as I pulled it from a project that uses it.

First, update the user model.

Then add the REZM routes.

NOTE: Routes have been changed in the lastest release on GitHub. The User is no longer needed. This break backwards compatibility, unfortunately. Thanks to Gravis for the update.

Now run db:migrate and you should be good to go.

But what if you didn’t use restful_authentication? Having to use Acts_As_Authenticated for EZM was the biggest complaint that I heard so I made REZM with hooks for you to switch out R_A if you want. Open lib\restful_easy_messages_controller_system.rb to do so. Just replace the current_user and login_required methods with calls to similar ones in your application.

I believe that is it. Oh wait, there is also an Atom feed for the inbox!

If you wish to try out REZM I’ve set up a sample app. You can message the user “sam” if you want to test writing a mesasge.

***** Click to play with the REZM Sample App. *****

Thanks to…

**Geoffrey Grosenbach for the REST PeepCode

**Matt Beedle for writing and releasing Acts_As_Emailable which was my starting point with Easy_Message and now RESTful_easy_messages.

** Rick Olson for writing and releasing all of his plug-ins.

** Dr. Nic Williams for his multiple-openids-per-user-sample-app which I used as a starting point for the REZM sample app which I’ll put up soon.

** Ben Curtis for his OpenID sample app which Dr. Nic based his.

Lastly, please recommend me if you like RESTful_Easy_Messages.
Recommend Me

Installing ImageMagick and MiniMagick on Windows

I know this has been documented in other places so this is mostly for my own sanity. I want to use the attachment_fu plug-in by Rick Olson for my rails app to upload user avatars. I also want to resize the images and create a series of smaller thumbnails for use throughout the site.

Steps to install ImageMagick

  1. Download the Windows binary of ImageMagick. I used ImageMagick-6.3.5-4-Q16-windows-dll.exe
  2. Run the ImageMagick installer (The file you just downloaded) and accept the defaults.
  3. Open a command prompt to verify the install was successful
  4. Type >convert logo: logo.miff
  5. Type >imdisplay logo.miff
  6. If all went well with the install a little picture of blue wizard should appear.

Steps to install MiniMagick

  1. Install the gem from the command prompt with the command gem install mini_magick. You may be asked to install a few required dependencies. Say yes.
  2. Wait for the download and install to complete.

You should be good now to install attachment_fu, but that is beyond the scope of this article. Use the links below for more information

References:

Easy_Messages

A few weeks ago I was looking for Rails plug-in to provide simple messaging between users on a website. I came across Acts_As_Emailable by Matt Beedle. It was a good start but only gave me a model and some methods but I would still need to flesh out a controller and some views for my little private messaging project to be complete. The result of that work is this plug-in, Easy_Messages. I took Acts_As_Emailable as a starting point and added some controller methods, a few views, a helper, and some named routes.

Below are the installation instructions. This is my first Rails plug-in so let me know if you find something to refactor. I’m open to constructive criticism.

The code is hosted at RubyForge.

UPDATE I: I have created a new branch for 0.51, please use that one for the latest stable release

UPDATE II: I have removed the docs from branch 0.51 and placed them online here: http://easymessages.samuelschroeder.com. Now the generator should work like a charm

svn://rubyforge.org/var/svn/easymessages/branches/RB-0.51

The run the generator to create the model, migration, helper, and views.

script/generate easy_messages message account

Note: Easy_Messages assumes you have Acts_As_Authenticated installed with the defaults of a model named User and a controller named Account. And you must use the above line exactly. I have grand aspirations of making the generator fully dynamic someday but for now you’ll have to deal.

The generator should produce this output.

exists app/models/
exists app/helpers/
exists app/views/account
create app/models/message.rb
create app/helpers/easy_messages_helper.rb
create app/views/account/message_view.rhtml
create app/views/account/messages.rhtml
create app/views/account/send_message.rhtml
exists db/migrate
create db/migrate/###_create_easy_messages_messages.rb

Yes, I realize the redundant naming but once/if I get the dynamic generator going it would say ###_create_easy_messages_[MessageClassNames]s.

Now you have to do three manual changes to your code. First, add “easy_messages” to apps/model/user.rb.

Second, add “authenticated_commands” to apps/controllers/account_controller.rb.

Finally, you need to copy the named routes from vendor/plugins/easy_messages/config/easy_messages_named_routes.rb to config/routes.rb.

That should be it for the setup. So start your development server and point your brower to..

http://localhost:3000/account/inbox

There is also a complete RDoc listing in vendor/plugins/easy_messages/doc

Thanks to…

**Matt Beedle for writing and releasing Acts_As_Emailable.

** Rick Olson for writing and releasing the kick-ass Acts_As_Authenticated plug-in. It was been such a help when starting new projects.

Recommend Me

Trying to Install Ruby Gems On Windows Though a Proxy Server

As with most larger businesses we have a corporate firewall which can lead to small annoyances, such as not being able to connect to rubyforge.org to download new gems. I recently had a new hard drive installed at work so, of course, I had to spend too much time reinstalling all of my development tools. Visual Studio 2005 takes way too long, btw.

Yesterday, I spent a good hour trying to get Rails with the standard gem install rails –include-dependencies. No luck due to our firewall. So I checked the ruby gems doc to find the correct install parameters. Still no luck. No matter which combination I tried to tell gem to look remotely through the proxy server it still wouldn’t go. So I asked the Google to help out and after reading through blog entries and message boards I stumbled upon one that talked about setting environment variables in linux. Well, I’m a windows guy so that didn’t help much but it gave me the idea to try and set a Windows environment variable. What the hell, right?

So, I added a User Variable for my account called http_proxy with the value of http://<proxyserveraddress>:8080, rebooted, ran plan old vanillia gem install rails –include-dependencies, and magically, it worked. See below the fold for details.

Read the rest of this entry »

Rails Rake Task for Resetting Your Database

The code below was given to me by Jonathan Viney when we worked on a Rails project together last summer and I’ve found it very useful. It is a rake task that will drop your developement dB and restore it based on your migrations and test fixtures. And as a good Rails developer I’m assuming you are using both.

Ugh.  I really need to find the bug killing my formatting in the SyntaxHighlighter.