Skip to main content

Posts

Rails-like Unit Testing in MS MVC

While following the Professional ASP.NET MVC 1.0 sample chapter's section about Unit Testing, I decided to use NUnit because I do not have Visual Studio Professional. Because there is no wizard to create a Unit Test project in Visual Developer Express, I downloaded a NUnit MVC testing template and use it as my base project to complete the tutorial. The following are some different approaches I had to make in order to follow the tutorial: Instead of using [TestMethod] I used [Test], which what NUnit uses to mark test methods. I used the NUnit's GUI to perform the unit tests instead of using Visual Studio's since the Express version does not come with support for testing. The tutorial in the chapter shows you how to create a fake repository so that your unit tests do not use the database. Because I come from a Rails background where you actually have (or supposed to have) a separated database for testing, I decided to just copy the database and use the new copy for testing...

DrySQL for the Microsoft MVC .NET framework

The sample Chapter 1 from Professional ASP.NET MVC 1.0 ( http://aspnetmvcbook.s3.amazonaws.com/aspnetmvc-nerdinner_v1.pdf ) is a great tutorial that guides you through the features of Microsoft's MVC framework . If you have used Rails before, when you read the tutorial you keep telling to yourself "wow, this is like rails but compiled and with excellent Intellisense and a nice and fast database backend". That's right, SQL Server is tightly coupled to the Linq to SQL framework that you can use as the ORM for the MS MVC (please note that the use of Linq to SQL is optional; and you could use other ORM frameworks). Just because is tightly coupled does not mean that is a bad idea; it actually integrates really well with the visual tools from Visual Studio. Here is a screen shot showing a basic overview of the database ORM mapping tool in MS Vistual Web Developer Express (which is free, like all the technologies described here, including the SQL server 2008 Express editio...

Method for communication between drivers on the road

Have you ever wanted to warn somebody in front of you that there is a cop behind or that one of their tires is flat? Sometimes I see car that are smoking from behind and I want to warn the people inside the car; but how do I do that? do I just honk like crazy until they hear me? I would like to propose simple method for driver-to-driver communication using the high lights of your car: One dot (.) means flash your lights for a short time, about a second. A dash (-) means flashing them for about 4 seconds. ... : use it to alert the car in front of you that a high priority vehicle like a police car or an ambulance is behind trying to move around the traffic. -.- : use it to alert the car in front of you that something is wrong with their car (like a flat tire) and they should stop to check it. --. : use it to alert the car in front that one of the doors is open. I want also to mention that learning Morse code is good idea so that can you can communicate with your eye lids i...

How to solve Named Pipes error 40 when connecting to local MS SQL Server instances

I was getting the following error when trying to add my local MS SQL server 2008 Express database to the data connections in Visual Web Developer 2008 Express : Named Pipes Provider, error: 40 - Could not open a connection to SQL Server The problem was that I was using just (local) in the server name field. To solve the problem, I had to add the instance name (sqlexpress) like this: (local)\sqlexpress When I entered that into the server name field, everything worked perfectly.

How to make a very inexpensive, dynamic website

Some of my customers in my freelance jobs require me to create for them a an easy-to-maintain website with some dynamic content. Normally I would help them set up a CMS like Joomla or Drupal, but some of the customers have a cheap, shared, web hosted accounts which do not run those CMS at optimal speed. For those customers, I usually implement the following to give them a website running at fast speed: Most of their website is statically generated by a custom offline CMS that I created. The offline CMS is an easy to use rails app that runs on my computer and uses webgen to generate clean HTML and then uploads the generated HTML to the shared account. That way is easy for me or for my customers to make changes to the website. For storing data from the visitors and other small dynamic services, I use the free Google App Engine . By using Google App Engine, my customers have access to Google's vast resources without paying a penny. Furthermore, I know for sure that those custome...

The system cannot find the path specified when executing jruby

If you get "The system cannot find the path specified" when trying to run jruby in Windows, you need to add JRUBY_HOME and JAVA_HOME to your environment variables. JRUBY_HOME and JAVA_HOME both need to point to the root folder of jruby and Java JDK, e.g.: C:\jruby-1.1.6\ and C:\Program Files\Java\jdk1.6.0_11. You need to also add %JAVA_HOME%\bin and %JRUBY_HOME% \bin to your PATH variable. You can manage your environment variables by doing the following under Windows XP: Right click on My Computer Click Properties Click the Advance tab Click on Environment Variables

Rails migration for acts_as_authenticated plugin

The following shows a typical Rails migration for creating the users, roles and role_users table for the acts_as_authenticated plugin. Note that an initial admin user with a default password is added: class CreateRolesAndUsers true do |t| t.column :login, :string t.column :email, :string t.column :crypted_password, :string, :limit => 40 t.column :salt, :string, :limit => 40 t.column :created_at, :datetime t.column :updated_at, :datetime t.column :remember_token, :string t.column :remember_token_expires_at, :datetime end create_table :roles, :force => true do |t| t.column :name, :string, :limit => 40 t.column :authorizable_type, :string, :limit => 30 #t.column :authorizable_id, :integer t.column :created_at, :datetime t.column :updated_at, :datetime en...

Systems Management: A time saver

Systems Management in Rails is a rails application for keeping track of computer inventory and also allows the user to execute remote ssh commands, wake on LAN, vnc and remote desktop on them. I developed it to help distribute software to all the machines at my work in a easy way. It uses the OpenAudit database (populated of course by the OpenAudit scripts) with some added changes such a new column in the systems table for type of password and a new table to keep the ssh commands history. Software distribution is done this way: User specify which file to send to which machines and the command to be run when the upload is successful The file is uploaded using sftp The command is executed using SSH. Systems Management also allows the user to access a computer via VNC or RDP by just clicking on a link. You can download it from http://rubyforge.org/projects/managesystems/

Script to generate an RSS feed from RenWeb's announcements

The following ruby (jruby) and sql code will get the Announcements from RenWeb and create an RSS feed file from them. The scripts needs the JDBC ActiveRecord gem. If you have any questions on how to run this script, please write a comment below. Both files are released under the MIT license . main.rb: require 'rss/maker' require 'cgi' require 'rubygems' gem 'activerecord-jdbc-adapter' require 'jdbc_adapter' class AnnouncementsDistrict :jdbc, :username => "YOUR ODBC USERNAME", :password => "YOUR ODBC PASSWORD", :driver => "com.microsoft.sqlserver.jdbc.SQLServerDriver", :url => "jdbc:sqlserver://THE URL TO CONNECT;DatabaseName=YOUR DATABASE NAME") set_table_name("AnnouncementsDistrict") #Gets an sql query from a file def self.get_sql_query_from_file(filename, params_hash_for_sql_query = nil) fi = File.open(filename) sql_query = fi.read if pa...

How to create a yahoo datatable in rails

To create a Yahoo Ajax Datatable (with local datasource) from a rails view, I had to do the following. First I had to add this helper method to the Application controller: module ApplicationHelper #Outputs the javascript code necesary to create a yahoo datable # from a plain datasource # column_array: and array of 2-item arrays in the form: # ["name_wihout_spaces", "Text to show in the column header"] # data_rows: an array or rows. Each row is an array that has the values in the # order specified in the column_array. def to_yahoo_array_data_source(column_array, data_rows, custom_div_id = "myContainer", custom_col_var_name = "data_cols", custom_datasource_var_name = "data_source") column_items = [] column_array.each do |column| column_items Next I added the required javascript libraries to the application layout: <html> <head> <title>Email De...

Strange behavior with the up,left,space bar key combo

I was checking if the up and left arrow and the space bar was pressed in my game code to allow the player to move and turn and shoot at the same time. Turns out that in GameMaker 7, if the user holds up+left+space bar keys at the same time, either the up or the left arrow key event will be ignored. I had to change the shoot key to the key to go around this weird behavior.

Math game for Areas and Volumes

I just uploaded an educational game to YoYoGames called Math Areas and Volumes . The game was made using GameMaker and the original purpose for it was to help my eight grade student to improve their skills of finding the area and volume of objects. If you have ever taught children about areas and volumes, you probably will notice that some of them confused the two concepts. The game uses plain graphics to help students distinguish the difference between the area and volume concepts. A 2D square with little squares filling it, represent a square unit. A 3D cube with little cubes on it represents the cubic units. You would need to play it to completely get the idea of what I was trying to do to help the students. Something else worth noting is that I used some of the excellent graphics that came with my Multimedia Fusion. I already asked in the forum, and the official response was that I could use the graphics as long as distributed the game only as an “.exe”. Thank you Clickteam!

Warbler uses gem files from cache folder instead of gems folder

I was having the following error when running warbler war on my rails application folder: Cannot load gem at [/home/juanito/jruby-1.1.4/lib/ruby/gems/1.8/cache/activerecord-jdbc-adapter-0.8.2.gem] in /home/juanito/bulletonrails/svn_copy/bullet_gui /home/juanito/jruby-1.1.4/lib/ruby/site_ruby/1.8/rubygems/format.rb:41:in `from_file_by_path' I looked around the gems/1.8/cache folder and sure enough, the gem file for activerecord-jdbc-adapter was missing. I had deleted that file and other gem files from that folder a while ago because I thought I would not need them and they was taking space. Well, it turns out that Warbler uses the gem files from the rubygem's cache folder to unpack the gems into the WEB-INF folder. As a consequence, by basically using the original gem files, it bypasses any modification you make to your local gems. So if your newly deployed jruby WAR application is missing custom behavior that you added to your local gems, you may want to convert the gem...

Make your rails app launch the web browser when starting

In jruby, if you ever want to start the browser pointing to your application's base index page after rails finishes initialzing your application, you can take advantage of java.awt.Desktop::desktop.browse. java.awt.Desktop::desktop.browse will open the default OS's browser and point it to an URL you pass to it (tested on Windows, failed in Ubuntu+Firefox, don't know if it works on MacOSX or Solaris). Add these lines at the end of environment.rb to launch the browser: include Java java.awt.Desktop::desktop.browse( java.net.URI.new('http://localhost:3000'))

How to make Net::SSH work with Jruby on Windows

Net::SFTP works like a charm in Jruby on Linux. However, when I tested my application in Windows, I ran into a fail "to load -- dl/import ssh" error and then into "IOError (The system cannot find the path specified)". To work around the errors, I had to make some changes to the Net::SSH code to make it work. There are 2 files you need to change gems\net-ssh-2.0.4\lib\net\ssh\known_hosts.rb and gems\net-ssh-2.0.4\lib\net\ssh\authentication\pageant.rb. For known_hosts.rb change the add(host, key) method located at the end of file to read like this: # Tries to append an entry to the current source file for the given host # and key. If it is unable to (because the file is not writable, for # instance), an exception will be raised. def add(host, key) begin #jpt File.open(source, "a") do |file| blob = [Net::SSH::Buffer.from(:key, key).to_s].pack("m*").gsub(/\s/, "") file.puts "#{host} #...

Java-jruby program to turn on computers remotely

Using wakeonlan.rb from Kevin R. Bullock, I was able to creat a small jruby swing-gui program for turning on remote computers using Wake On LAN. The program displays in an array buttons corresponding to the computers listed in a text file. When you click the button, the program sends a WOL packet to the computer to turn it on. The code is the following (note that you need wakeonlan.rb in the same folder): # Copyright (c) 2008 Juan Pablo Tarquino # # Permission is hereby granted, free of charge, to any person # obtaining a copy of this software and associated documentation # files (the "Software"), to deal in the Software without # restriction, including without limitation the rights to use, # copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the # Software is furnished to do so, subject to the following # conditions: # # The above copyright notice and this permission notice shall be # included in al...

New Avery 5160 template for Jasper Reports

I had to create an Avery 5160 (address labels 3x10) template for jasper for work. If you would like to use that production-ready template you can download it from here . The template has been used in a production environment, however, as always, make sure it works for you before printing too many labels :).

Ruby script to get a batch of MARC records

At work we bought a lot of new books for our new library. Our libraries use aprogram called LibraryPro which can import MARC records to add books to the catalog. To save ourselves some time, I look on the web for a script to get MARC records from the Internet and found this excellent script from William Denton. Because I needed to get batches of records and modify the records gotten to follow our custom dewey decimal number (number/category/author), I modified his script to accomplish that and this is how it looks now: #!/usr/local/bin/ruby -w # Script to get a batch of MARC records # #original code taken from http://www.miskatonic.org/library/zmarc.html # original programmer # William Denton wtd@pobox.com # April 2007 # modifications made by Juan Pablo Tarquino http://jptarqu.blogspot.com # Released under the MIT License. # Copyright (c) 2007 William Denton # Copyright (c) 2008 Juan Pablo Tarquino # # Permission is hereby granted, free of charge, to any person # obtaining a cop...