Tech Tidbits - Ruby, Ruby On Rails, Merb, .Net, Javascript, jQuery, Ajax, CSS...and other random bits and pieces.

Friday, February 6, 2009

Rails & Testing - Resources

This is an in-progress set of testing resources aimed at the Ruby/Rails developer...

Rails Guides


http://guides.rubyonrails.org/testing_rails_applications.html

nullislove.com


http://www.nullislove.com/2007/11/14/testing-in-rails-part-1-unit-testing-in-ruby
http://www.nullislove.com/2007/11/17/testing-in-rails-part-2-unit-testing-ruby-classes/
http://www.nullislove.com/2007/11/20/testing-in-rails-part-3-unit-testing-ruby-classes-cont/
http://www.nullislove.com/2007/11/29/testing-in-rails-part-4-unit-testing-in-rails/
http://www.nullislove.com/2007/12/07/testing-in-rails-part-5-unit-testing-activerecord-models/
http://www.nullislove.com/2007/12/21/testing-in-rails-part-6-fixtures/
http://www.nullislove.com/2008/01/08/testing-in-rails-part-7-activerecord-relationships/
http://www.nullislove.com/2008/01/23/testing-in-rails-part-8-validations/


railstips.org


http://railstips.org/2008/12/12/when-you-dont-know-test
http://railstips.org/2009/1/6/test-or-die
http://railstips.org/2009/1/7/my-testing-theory
http://railstips.org/2009/1/8/test-or-die-validates-uniqueness-of

andrzejonsoftware.blogspot.com


http://andrzejonsoftware.blogspot.com/2007/05/15-tdd-steps-to-create-rails.html
http://andrzejonsoftware.blogspot.com/2007/05/and-some-more-tdd-steps-with-rails.html
http://andrzejonsoftware.blogspot.com/2008/01/what-do-i-gain-from-tdd-or-bdd.html

Behavior Driven Development (BDD)


http://behaviour-driven.org
http://www.jamievandyke.com/blog/2009/01/09/building-a-gem-using-bdd.html
http://www.oreillynet.com/pub/a/ruby/2007/08/09/behavior-driven-development-using-ruby-part-1.html
http://www.oreillynet.com/pub/a/ruby/2007/08/30/behavior-driven-development-using-ruby-part-2.html


Misc


http://giantrobots.thoughtbot.com/2008/11/7/a-critical-look-at-the-current-state-of-ruby-testing
http://www.cis.upenn.edu/~matuszek/cit597-2008/Lectures/40-unit-testing-in-rails.ppt
http://www.tutorialspoint.com/ruby-on-rails-2.1/rails-unit-testing.htm
http://technicalpickles.com/posts/test-or-die-validates-uniqueness-of-shoulda-and-factory-girl-edition

Blogs


http://blog.jayfields.com/

Books


http://www.amazon.com/Test-Driven-Development-Addison-Wesley-Signature/dp/0321146530/
http://www.railsprescriptions.com

Videos


http://peepcode.com/products/test-first-development

Tools


http://rspec.info
http://technicalpickles.com/posts/adding-cucumber-to-a-ruby-project
http://www.ultrasaurus.com/sarahblog/2008/12/rails-2-day-3-behavior-driven-development/ (BDD & Cucumber)

Tuesday, November 18, 2008

Nginx, Mongrel, Merb

Quick notes on installing and configuring nginx and running with existing mongrel and Merb app on my MacBookPro.

1) Download and install PCRE (if needed)
http://www.pcre.org/
ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/
ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-7.8.tar.gz

$ ./configure
$ ./make
$ ./make install

2) Download and install nginx:
http://nginx.net/
The latest stable version is nginx-0.6.32, the change log.
http://sysoev.ru/nginx/nginx-0.6.32.tar.gz

$ ./configure
$ ./make
$ ./make install

# Edit nginx.conf file (/usr/local/nginx/conf/nginx.conf)
# Note - based on Ezra's blog post (see notes below)

user nobody;
worker_processes 2;

error_log logs/error.log notice;
pid logs/nginx.pid;

events {
worker_connections 1024;
}

http {
include mime.types;
#include conf/mime.types;
default_type application/octet-stream;

# no sendfile on OSX uncomment
#this if your on linux or bsd
#sendfile on;
tcp_nopush on;

keepalive_timeout 65;
tcp_nodelay on;

upstream mongrel {
server 127.0.0.1:4000;
server 127.0.0.1:4001;
server 127.0.0.1:4002;
}

gzip on;
gzip_min_length 1100;
gzip_buffers 4 8k;
gzip_types text/plain;

server {
listen 80;
#server_name localhost;
root /Users/dsparlingimbp/git/dougsparlingdotcom/public;

access_log off;
rewrite_log on;

location ~ ^/$ {
if (-f /index.html){
rewrite (.*) /index.html last;
}
proxy_pass http://mongrel;
}

location / {
if (!-f $request_filename.html) {
proxy_pass http://mongrel;
}
rewrite (.*) $1.html last;
}

location ~ .html {
root /Users/dsparlingimbp/git/dougsparlingdotcom/public;
}

location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|mov)$ {
root /Users/dsparlingimbp/git/dougsparlingdotcom/public;
}

location / {
proxy_pass http://mongrel;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

}
}


4) Commands for starting and stopping nginx
start: sudo /usr/local/nginx/sbin/nginx
stop: sudo kill `cat /usr/local/nginx/logs/nginx.pid`

5) Start Mongrel cluster (I'm using 3 Mongrels):
$ merb -c 3
or
$ merb -p 4000 -c 3

6) Resources
http://brainspl.at/articles/2006/08/23/nginx-my-new-favorite-front-end-for-mongrel-cluster
http://articles.slicehost.com/2008/5/13/ubuntu-hardy-installing-nginx-from-source
http://articles.slicehost.com/2008/5/13/ubuntu-hardy-adding-an-nginx-init-script
http://articles.slicehost.com/2008/4/25/ubuntu-hardy-setup-page-1
http://articles.slicehost.com/2008/4/25/ubuntu-hardy-setup-page-2
http://forum.slicehost.com/comments.php?DiscussionID=2179
http://merb.rubyforge.org/files/README.html

Sunday, November 9, 2008

Ruby - Randomize arrays and strings


Randomize the elements of an array:


random_array = my_array.sort_by{rand}

Randomize a string (jumble a given word):

jumbled_word = word.split("").sort_by{rand}.join

Saturday, November 8, 2008

Updating to RubyGems 1.3.0 on Mac OS X Leopard 10.5.4

When attempting to stall final release of Merb 1.0, I got an error message that I needed RubyGems version >=1.3.0:

ERROR: Error installing merb:
merb-core requires RubyGems version >= 1.3.

So I did the usual "gem update --system" but got an error:

$ sudo gem update --system
Password:
Updating RubyGems
Updating rubygems-update
Successfully installed rubygems-update-1.3.0
ERROR: While executing gem ... (NameError)
undefined local variable or method `remote_gemspecs' for #
$ gem --version
1.2.0

I found that you have to run a second set of instructions:

$ sudo gem install rubygems-update
Successfully installed rubygems-update-1.3.0
1 gem installed
$ sudo update_rubygems
Installing RubyGems 1.3.0

More info here:
http://rails.wincent.com/wiki/Updating_to_RubyGems_1.3.0_on_Mac_OS_X_Leopard_10.5.4

Wednesday, August 27, 2008

Dynamic Details Row in GridView


<%@ Page Language="C#" %>
<%@ Import Namespace="System.Web.Configuration" %>

<script runat="server">

void GridView1_PreRender(object sender, EventArgs e)
{
if (GridView1.SelectedRow != null)
{
Table table = GridView1.SelectedRow.Parent as Table;

if (table != null)
CreateRow(table, GridView1.SelectedIndex);
}
}

void CreateRow(Table table, int index)
{
GridViewRow row = new GridViewRow(-1, -1, DataControlRowType.DataRow, DataControlRowState.Normal);
row.Cells.Add(CreateColumn());
table.Rows.AddAt(index + 2, row);
}

private TableCell CreateColumn()
{
TableCell cell = new TableCell();
cell.ColumnSpan = GridView1.Columns.Count;
cell.Width = Unit.Percentage(100);

DataSourceControl ds = CreateDataSourceControl();

cell.Controls.Add(ds);
cell.Controls.Add(CreateDetailsView(ds));
return cell;
}

private static DetailsView CreateDetailsView(DataSourceControl ds)
{
DetailsView dv = new DetailsView();
dv.AutoGenerateRows = true;
dv.DataSourceID = ds.ID;
return dv;
}

private DataSourceControl CreateDataSourceControl()

{
SqlDataSource ds = new SqlDataSource();
ds.ConnectionString = WebConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
ds.SelectCommand = "SELECT * FROM [Users] WHERE [UserID] = @UserID";
ds.ID = "SqlDataSource2";

Parameter cp = new Parameter("UserID", TypeCode.String, GridView1.SelectedValue.ToString());
ds.SelectParameters.Add(cp);
return ds;
}

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >

<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>

<body>
<form id="form1" runat="server">
<div>

 <asp:GridView ID="GridView1" Runat="server" DataSourceID="SqlDataSource1" DataKeyNames="UserID"
AutoGenerateColumns="False" OnPreRender="GridView1_PreRender" EnableViewState="False">
<Columns>
<asp:CommandField ShowSelectButton="True">
<asp:BoundField ReadOnly="True" HeaderText="UserID" DataField="UserID" SortExpression="UserID">
<asp:BoundField HeaderText="UserName" DataField="UserName" SortExpression="UserName"></asp:BoundField>
</Columns>
</asp:GridView>

<asp:SqlDataSource ID="SqlDataSource1" Runat="server" SelectCommand="SELECT [UserID], [UserName] FROM [Users]"
ConnectionString="<%$ ConnectionStrings:MyConnectionString %>">
</asp:SqlDataSource>

</div>
</form>

</body>
</html>

Saturday, August 23, 2008

Amazon S3 with Ruby aws-s3 gem

I stated using Amazon S3 to store and backup the music I've written and recorded. I'm using the Ruby aws-s3 gem. Note that I check for some basic audio file types so I don't have to manually type the bucket or mime-type.

aws-s3.rb
---------
#!/usr/bin/env ruby
require 'rubygems'
require 'aws/s3'

# File to upload
local_file = ARGV[0]

# bucket & mime-type
if local_file =~ /mp3$/
bucket = 'doug_mp3'
mime_type = 'audio/mpeg'
elsif local_file =~ /wav$/
bucket = 'doug_wav'
mime_type = 'audio/x-wav'
elsif local_file =~ /aif[c|f]?$/
bucket = 'doug_aiff'
mime_type = 'audio/x-aiff'
else
bucket = ARGV[1]
mime_type = ARGV[2] || "application/octet-stream"
end

if bucket.nil?
puts "need a bucket"
exit
end

puts "file: #{local_file}"
puts "bucket: #{bucket}"
puts "mime_type: #{mime_type}"

AWS::S3::Base.establish_connection!(
:access_key_id => 'REPLACE_ME',
:secret_access_key => 'REPLACE_ME'
)

base_name = File.basename(local_file)

puts "Uploading #{local_file} as '#{base_name}' to '#{bucket}'"

AWS::S3::S3Object.store(
base_name,
File.open(local_file),
bucket,
:content_type => mime_type
)

puts "Uploaded!"

I can call the script a couple ways:

# with defaults based on extension
$ ruby aws-s3.rb mysong.mp3

# or explicitly (file, bucket, mime-type)
$ ruby aws-s3.rb mysong.mp3 doug_mp3 'audio/mp3'

Add and remove a remote source from gem 'remote sources''

I was attempting to add a gem and ran into an error I hadn't seen before:

Macintosh:aws-s3 dsparlingimbp$ sudo gem i aws-s3
WARNING: RubyGems 1.2+ index not found for:
http://merbivore.com

RubyGems will revert to legacy indexes degrading performance.
Updating metadata for 63 gems from http://gems.rubyforge.org/
...............................................................
complete
ERROR: could not find gem aws-s3 locally or in a repository


So I tried to update gems:

Macintosh:aws-s3 dsparlingimbp$ sudo gem update --system
Updating RubyGems
WARNING: RubyGems 1.2+ index not found for:
http://merbivore.com

RubyGems will revert to legacy indexes degrading performance.
Updating metadata for 63 gems from http://gems.rubyforge.org/
...............................................................
complete
ERROR: While executing gem ... (Gem::RemoteFetcher::FetchError)
bad response Not Found 404 (http://merbivore.com/Marshal.4.8)

Next I looked the gems environment:

Macintosh:rubygems-1.2.0 dsparlingimbp$ gem env
RubyGems Environment:
- RUBYGEMS VERSION: 1.2.0
- RUBY VERSION: 1.8.6 (2007-03-13 patchlevel 0) [i686-darwin8.8.5]
- INSTALLATION DIRECTORY: /usr/local/lib/ruby/gems/1.8
- RUBY EXECUTABLE: /usr/local/bin/ruby
- EXECUTABLE DIRECTORY: /usr/local/bin
- RUBYGEMS PLATFORMS:
- ruby
- x86-darwin-8
- GEM PATHS:
- /usr/local/lib/ruby/gems/1.8
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :benchmark => false
- :backtrace => false
- :bulk_threshold => 1000
- :sources => ["http://gems.rubyforge.org", "http://merbivore.com"]
- REMOTE SOURCES:
- http://gems.rubyforge.org
- http://merbivore.com

Noticing the issues with Merb (which I recently installed), I removed http://merbivore.com from the list of remote sources:

sudo gem sources -r http://merbivore.com

I went ahead and added github while I was at it:

sudo gem sources -a http://gems.github.com

Checked my gems environment:

Macintosh:rubygems-1.2.0 dsparlingimbp$ gem env
RubyGems Environment:
- RUBYGEMS VERSION: 1.2.0
- RUBY VERSION: 1.8.6 (2007-03-13 patchlevel 0) [i686-darwin8.8.5]
- INSTALLATION DIRECTORY: /usr/local/lib/ruby/gems/1.8
- RUBY EXECUTABLE: /usr/local/bin/ruby
- EXECUTABLE DIRECTORY: /usr/local/bin
- RUBYGEMS PLATFORMS:
- ruby
- x86-darwin-8
- GEM PATHS:
- /usr/local/lib/ruby/gems/1.8
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :benchmark => false
- :backtrace => false
- :bulk_threshold => 1000
- :sources => ["http://gems.rubyforge.org", "http://gems.github.com"]
- REMOTE SOURCES:
- http://gems.rubyforge.org
- http://gems.github.com

Everything looked good, so I tried to install aws-s3 gem again:

Macintosh:rubygems-1.2.0 dsparlingimbp$ sudo gem install aws-s3
Successfully installed aws-s3-0.5.1
1 gem installed
Installing ri documentation for aws-s3-0.5.1...
Installing RDoc documentation for aws-s3-0.5.1...

Et VoilĂ ! C'est si bon...

About Me

My photo
Developer (Ruby on Rails, iOS), musician/composer, Buddhist, HSP, Vegan, Aspie.

Labels