07 April 2012

Built a simple site with Node.js

I had a simple site to build for a friend (a local business called It's A Gift) so I decided this would be a good time to get going with Node.js. Initially I wanted to see if I could do this on Windows, but with my earlier issues with it, I decided to just stick with Ubuntu. More and more I'm preferring working in Ubuntu.

Number 1 goal here was to do coffeescript end to end. Got that working fairly easily. My setup looks like this: 

- HTML Markup: Coffeekup: https://github.com/mauricemach/coffeekup
- MVC: Express:  http://expressjs.com/

I wouldn't say it was easy exactly, there were some minor bumps along the way. But I was pretty impressed with just how easy it was considering how little I knew going into it. Node just wants to get out of your way so you can get your site going. 

Another thing that I like about this setup is all of the markup and code looks fairly similar: 


div '.top.child', ->
  img '.logo', src: "images/logo.png", alt: "It's a Gift logo"
div '.bottom.child', ->
  h3 'Opening April 2012'
  p style: 'margin-bottom: 40px;'


light_green = #B8CD88
dark_green = #64893D
beige = #F4BF7F
orange = #F79323
sea_green = #73A4A2
sans_font = 'Yanone Kaffeesatz', sans-serif
  font: 12px Helvetica, Arial, sans-serif;
  background: url('/images/paper_texture.png')
  margin-top 60px

So everything is nice and similar. Love that. 

15 March 2012

Oy vey, coffeescript and node.js on Windows

I'm building a new site today. Just a small one, so this seemed like a good place to get started with node.js. Well, I found this really excellent blog post about it: 


Nifty. So I get started and install coffee via npm. Now, just because I despise CMD (sooo 1990) I do this in PowerShell. OK, so I do the npm: 
PS>npm install -g coffee-script
npm http GET https://registry.npmjs.org/coffee-script
npm http 304 https://registry.npmjs.org/coffee-script
C:\Users\Justin\AppData\Roaming\npm\cake -> C:\Users\Justin\AppData\Roaming\npm\node_modules\coffee-script\bin\cake
C:\Users\Justin\AppData\Roaming\npm\coffee -> C:\Users\Justin\AppData\Roaming\npm\node_modules\coffee-script\bin\coffee
coffee-script@1.2.0 C:\Users\Justin\AppData\Roaming\npm\node_modules\coffee-script

Then I type 'coffee':

The term 'coffee' is not recognized as the name of a cmdlet, function, script file, 
OK, that's a path problem. So I use this rather tortured bit of syntax:
[Environment]::SetEnvironmentVariable( "Path", $env:Path + ";C:\Users\xxxx\AppData\Roaming\npm\node_modules\coffee-script;", [System.EnvironmentVariableTarget]::Machine )
Then restart powershell. Now when I type 'coffee' into Powershell I get this:

Say what?? Oy vey. Kinda gave up here.

Then I try it in Linux (in the VM):
~/app/log> sudo apt-get install coffeescript
[sudo] password for user: 
Reading package lists... Done
Building dependency tree       
ldconfig deferred processing now taking place
~/app/log> coffee
coffee> ~/app/log> 
~/app/log>  coffee -bpe "alert i for i in [0..10]"
var i;
for (i = 0; i <= 10; i++) {

Aaand, we're done. Every time this sort of thing happens, it's another nail in the coffin for me developing in Windows.

19 January 2012

Storing values outside your stub with Rspec

I'm working on some code today that saves records to a database. For my rspec tests I want to redirect the "save" to a stub. And I want to take the data that would have been saved and stow it somewhere else. Well, I had plenty of problems with this so I'm here to share the real solution.

The first issue that I ran into: return values on stubs must match the type of the return value in your live code. Duh. But I'm coming from the static typed world, so this one slipped by me. I had a method that was returning the number of new records created (0 or 1). Simple. But the stub was just pushing the input into an array:

  saved_rows = []
    proc.stub(:save) do |arg0| 
      puts 'redirecting save'
      saved_rows << arg0

Which meant that the return value (that was going to 'real' code) was a big array. No bueno. Ok, fixing that was a huge aha moment. Just add a '0' on the line after  @saved_rows << arg0. Much cursing about this one -- the object worked find from irb but failed when I did the test. 

Next up: storing the row in an array outside of the stub. I couldn't find any samples of this, so it was trial and error. In the code above, it certainly looks like it'd work. But it doesn't. I assume saved_rows is getting created in two places. The solution: use an @instance variable: 

    @saved_rows = []
    proc.stub(: save ) do |arg0|
      puts " redirecting save "
      @saved_rows << arg0

Bada-bing. Now I have a save method that is storing the new data in a place where I can get to it (and test it). 

Related Rspec doc:


25 November 2011

sometimes hard conventions suck (rendering partials in RoR)

I spent an hour today on this one:
partials are named with a leading underscore to distinguish them from regular views, even though they are referred to without the underscore
So I had some code that said:

  render :partial => "short_list", :collection => @items

which was in an ajax call. I was doing it with 'render :inline' but partials seemed like a much better idea. Well, that wasn't such a great idea. I kept getting ActionView::MissingTemplate errors. Tried about 15 things, read three or four blog posts, several SO questions. Then I noticed one that had an underscore in front of the file name. Odd. So I go read the docs and sure enough.

Even if you specifically say: 
  render :partial => "item/short_list", :collection => @items 
it will not find your partial because it doesn't start with an underscore. Even though there is only one view in that directory.

That, that is stupid. Conventions are fine when you want to just sorta let the app wire things up. But when you specifically say "do it this way" the convention should not get in your way. 

The odd thing about Ruby is that it is much more of an RTFM language than C# -- because there's no compiler to tell you "hey, you did that wrong" and the autocomplete is usually just not there. 

15 November 2011

More Rubymine Impressions

OK here's another couple of things I love about this IDE. For the sake of typing, RM means RubyMine.

  • Local History: you get a diff-able view of all the saves that you have made to a file. If you're using a DVCS that's 3 different views of the changes that you've made to a file. Wild. I love this because I've often thought of adding a "commit on every successful build" sort of thing to Visual Studio so that I have a step by step recreation of what I did with a file. That way if you think 'huh, that was working 20 minutes ago' you'd be able to backtrack. With RubyMine, no need. 
  • Parentheses: Seems like such a small thing, but it makes my life easier. When I type '(' into RM it gives me a closing paren for free. But if I miss that for whatever reason and then type the closing paren anyway, RM figures it out and just overwrites the closing paren that I typed. I'm waiting for this to trip me up, but it hasn't happened yet so so far it's awesome. 
  • Find is fast fast fast: So far I've got about 200 files in my app. Not many really. But when I do a Shift Ctrl F and search for a string in the files it is nearly instantaneous. It is possible that that's because the files are on an SSD. Still it seems faster than Visual Studio. 

09 November 2011

ROR + RubyMine impressions (from a .NET Dev)

Playing with RubyMine and ROR today. I'm actually hoping to build something real with this in the next few days. Well, a few days might be optimistic. I'm a .NET dev and I remember people saying that MVC was really just Microsoft-ROR. Didn't know much about it at the time but I'm starting to see it.

Here are some impressions of RubyMine and ROR, in no particular order.
  • What a deal: I got RubyMine for $35. A steal. Part of the 'Winter is coming' sale. Someone's been watching too much Game of Thrones eh? 
  • Autocompletey magic: Love the auto complete in RubyMine so far. I've never seen Visual Studio pick up on `<` as well as RubyMine does. Gives me every option that I could ever want to put next to a less than. 
  • Ruby is: I don't know yet. Still deciding. 
  • VCS: RubyMine's source control integration is effortless. Makes me wonder why MS hasn't made a decent plug in for SVN by now. If a $35 product can pull it off but a $5k+ product is 'missing' that, well what's the deal? 
  • Console: wait so I can interact with my model directly from the command line? http://guides.rubyonrails.org/getting_started.html Section 6.6 (no anchor there?!). OK that's pretty cool. 
  • Wizard-ish things: `rails generate model ...` makes Rails stronger in code generation than Visual Studio? Weird. I expected a lot of 'type this text into a new text file'. Instead there's a generator that takes care of that for me. The generator also builds functional tests and unit tests? Testing as the norm instead of a 'like-ta-have'. Huh. 
  • Associations are built into the routing (nested resource). Interesting. 
  • ASP.NET MVC = C# on Rails? Yeah I can see it. Wonder what web language used <%= %> first. 
  • Speed speed speed: this one keeps coming back to me as I read the getting started guide. Something that would take me half an hour in C# MVC would take maybe 4 mins in ROR. Most of that time is in the database. And I'm not including the migration time for a database that is in a new environment (e.g Beta) in the .NET world -- what version is the db in now and what version should it be migrated to? In C# MVC: don't know and don't know (usually). In ROR: known and known. Versioning and migration are built in over here in the ROR world. This I like. 
More later.

12 June 2011

Thinking of an ideal logging framework

First, let me say that logging is important. It's one of the Martian Principles:  #13 Log Everything. Disk space is so cheap anymore that having weeks worth of detailed logs shouldn't be any big deal, provided you aren't taking a noticeable performance hit.

Today I'm working with the Enterprise Library Logging framework and finding it lacking. It certainly does all-that-and-a-bag-of-chips. But it's got some issues that have really tedious workarounds:

  1. Changing logging configuration on the fly without restarting your web app isn't easy. You have to add some custom code to make Enterprise Library look for a config file that isn't web.config. Small workaround, but every app needs it. I hear that EL 5.0 Optional Update 1 has a fix for this. Note the version number there: EL is up to version 5 and just now it's thinking of a standalone config.
  2. Changing the EL.config as part of your deployment is wow a lot of work. I found this post by Oleg Sych that explains how to do it. It isn't trivial. Getting the rest of my team to agree to that amount of work to get Prod/Testing/Debug configuration of EL.config working? Hah, forget it. It requires hand-editing of the csproj file. Not good. 
Hand editing config files on a live and running site seems like a bad idea to me. One wrong save and BANG site is offline or not working correctly -- silently broken. And if what you silently broke is your logging? Ouch. I'd prefer to have Production locked down to the point that this just isn't an option unless it's an emergency.

I did look at ELMAH. It looks good, but it breaks the MP#13 because it's only logging the errors.

So, the ideal laundry list for a logging solution:

  • Simple simple simple to add to an existing app. 
  • Configure the logging level on the fly without restarting the app. 
  • Write to a local (in the app directory) no-footprint database. 
  • Multiple handling of certain classes of entries (errors get an email, verbose entries don't)
  • Easy to configure for production deploy via a script or .config transform (preferably second option)
We had a similar setup at the last job (but it wrote to the Windows event log, yech). I don't really want to roll my own here. We were using log4net, but I recall a lot of configuration pain around it. Not sure why, but the guy who was working on it was bright enough that I don't think he was the issue. Plus, I think the on-the-fly config was something we had to do on our own. 

Will update if I can find anything that satisfies all these conditions.