Archive for Uncategorized

Not Doing It

I am not not not not doing it! I am not getting an iphone. The lower price is very very tempting… but I am not doing it. A friend of mine signed up for an apple developer account like hours after it opened — I asked him yesterday if he was approved and he said ONLY 4 PEOPLE ARE! out of 200k … huh? what? where are all the HUNDREDS of apps that Apple thought would magically appear when they let the SDK loose on eager developers. Hello?

What is going on Apple? I love my mac… and would like to have a phone that would integrate with it ….

Meanwhile I haven’t done too much with Andriod development but I keep up reading blogs about it. I don’t feel like I have all the restrictions and “Steve Jobs says …”

I’m holding out for an android phone…and by the rate things are going with the iphone developers.. my app will be done before yours.

Comments

Potluck

I’ve been doing a variety of things lately:

Playing with Android
I got the “Hello Android” beta book from Pragmatic Programmers — I am trying to remember its beta but its sort of hard to follow. Be prepared to download the source code for the book and compare to what the book has to see what is missing, so you can get your thing to run. I am going to look at some other tutorials at the moment to get more familiar. Maybe the author assumes the reader knows more than me! Which is possible since I haven’t done much java programming.

Wordpress 2.5.1 and EngineYard

The DevChix.com blog is a wordpress blog and hosted at DreamHost. We got a free slice for DevChix from the kind folks at EngineYard. They were nice enough to set it all up for us with wordpress and giving us a deploy.rb file ready to go! Their support is pretty fast too and I was able to get some help from the irc chat as well. The new wordpress has some neat features and looks nicer. I want to install a captcha though for blog comments, even with Akesmet I get tired of sorting through them! I was able to get all our plugins installed and seems good.

Playing with App Engine
I worked on this last week while on an airplane. I wasn’t sure it would work offline — but it did. Even the database…err..yeah whatever it is. I was able to add data ..but where is that data?

Django your mango
Yes, been checking it by doing the bundled tutorials. I haven’t gotten back to it since my last post. Need to get back to it before I forget what I was doing. haha.

Git ‘er done
Finally, I am going to learn git and try it on a project with a friend. I am hoping it proves useful since I often code on the go with no internet access. I sometimes have to do a poor mans cvs and save things as myfile1.rb myfile2.rb to try out things and then able able to “go back”. Rock and roll.

Comments (2)

Hello Android

I’ve been hankering to try out programming for a device. I did a hello world app for my pocketpc with c++ years ago. Apparently it didn’t hold my interest cuz I never did much with it. I am not enthusiastic about the iphone and its developer requirements and restrictions (way to piss on the developers Apple). So I’ve been wanting to try out Android for awhile now.

I was excited to see a new beta book out by Pragmatic Programmers — on Android! hot dog! I bought it the same day and installed the SDK, Eclipse and the Eclipse plugins on my macbook. I installed an emulator too. It worked without a hitch — which I kinda expected there to be some goofy java error or some nonsense that would frustrate and discourage me. Nope — worked real easy.

The intro was real easy to understand and explained the different components. The book tells you about a soduko game that you will develop over the course of the book. I like that idea and I think its an excellent way to learn.

I do the Hello World application following the instructions in the book (yeah I know there are hello world tutorials on the google site). Whoo hoo! It worked! Excited I move on.

The next example I had some problems with. I think some code is missing from the book. I looked high and low and emailed the author and logged a issue at PragProg.com … no response yet, but I figured out if I just removed that part of the code then it worked. Just didn’t have a background color set. Oh well — I’ll be patient to see if someone gets back to me or its fixed in the next version of the book.

Moving on, I see code snippits that should be in certain files but not sure where in the file they go and I think sometime they are missing some import statements. I get errors and I poke around to figure it out ..and I give up for the moment … :(

Its a beta book.. so.. guess we’ll have to wait till the next version comes out..

Meanwhile I checked out some of the examples that Google has on their site.

The author got back to me and told me to check out the source code for the sample program….and he’d try to make it more clear about the background color.

I will keep plugging on!

Comments

Code Review: Rspec Tests

I am writing a rails application for my mom to enter her bakery orders for her business. The Order page has a form to search or add a new customer, so i can streamline the process as much as possible. In the Order create method, I either get a customer array or a customer ID.

Here's the order create method in the controller:

RUBY:
  1. def create
  2.    
  3.     new_customer = params[:order][:customer_id].blank? ? true : false
  4.    
  5.     if new_customer
  6.       @customer = Customer.create(params[:customer])
  7.       if @customer.valid?
  8.         @customer.orders.create(params[:order])       
  9.         flash[:notice] = 'New customer was added and order was created successfully'
  10.         redirect_to orders_url
  11.       else
  12.         flash[:notice] = 'Please fill in all required fields'
  13.         render :action => 'new'
  14.       end
  15.     else
  16.       @customer = Customer.find params[:order][:customer_id]
  17.       @customer.orders.create(params[:order])
  18.       flash[:notice] = 'Order was successfully created.'
  19.       redirect_to orders_url
  20.     end
  21.  
  22.  end

Ok? anybody see a better way to do that? It took me awhile to figure out the right branching. Now for my rspec tests. This can take two paths -- new customer or existing customer.

Test for New Customer:

RUBY:
  1. describe OrdersController, "handling POST /orders with a new customer" do
  2.  
  3.   before do
  4.     @order = mock_model(Order, :to_param => "1")
  5.    
  6.     Order.stub!(:create).and_return(@order)
  7.    
  8.     @params = {:customer_id => nil}
  9.    
  10.     @customer = mock_model(Customer, :to_param => "1")
  11.     @customer.stub!(:orders).and_return(Order)
  12.     Customer.stub!(:create).and_return(@customer)
  13.   end
  14.  
  15.   def post_with_successful_save
  16.     @customer.should_receive(:valid?).and_return(true)
  17.     post :create, :order => @params
  18.   end
  19.  
  20.   def post_with_failed_save
  21.     @customer.should_receive(:valid?).and_return(false)
  22.     post :create, :order => @params
  23.   end
  24.  
  25.   it "should create a new customer" do
  26.     Customer.should_receive(:create).with(anything()).and_return(@customer)   
  27.     post_with_successful_save
  28.   end
  29.  
  30.   it "should create a new order" do
  31.     Order.should_receive(:create).with(anything()).and_return(@order) 
  32.     post_with_successful_save
  33.   end
  34.  
  35.   it "should redirect to the new order on successful save" do
  36.     post_with_successful_save
  37.     response.should redirect_to(orders_url)
  38.   end
  39.  
  40.   it "should re-render 'new' on failed save" do
  41.     post_with_failed_save
  42.     response.should render_template('new')
  43.   end
  44. end

Hows that look? do I catch all the cases for the New Customer scenario?

Comments

Fun stuff - Oscar Picks !!

My friend David did a fun thing, Oscar Picks!

You can even get a widget and display on your blog:

Here are my picks:

Comments

RubyConf!

I finally made it to a ruby related conference! I almost went to the past two rails confs but something always came up (like -- new jobs!). Looking forward to meeting lots of people..

So far the session on Advanced Class Design is awesome... more updates to come

Comments

Beautiful Ruby: Learning the -isms

I did PHP for 6 years.... sometimes its hard not to do PHP in ruby...

Today i was testing a url to see if it contains a certain string:

RUBY:
  1. response = Net::HTTP.get_response(URI.parse("http://myawesomesite")
  2. if response.body.match("my awesome text") == nil  then
  3.       return false
  4. end

and remember to do:

RUBY:
  1. response = Net::HTTP.get_response(URI.parse("http://myawesomesite")
  2. return "false" if response.body.match("my awesome text").nil?

Another thing I was working on was I had to do a multiple if statement

I will simplify it for this example:

RUBY:
  1. color = red
  2.   if (color == "red") or (color =="green") or (color == "yellow") then
  3.     print "its valid)
  4. end

In PHP I might have actually made the values into an array, and then did in_array ... but in reading the code, you see below, if in the valid values, print something. I don't want to see array!

This is more elegant:

RUBY:
  1. color = red
  2. valid_values = ["red","green","yellow"]
  3. puts "its valid" if valid_values.include? color

This seems to only work for values you'd do == on, not ===

See... programming in ruby just makes me grin :)

Comments (3)

Weirdness Problem Solved!!!

When I posted at the Rails-Forum, I also emailed the internal DevChix list (sorry boys!) because I was totally at wits end about this problem. (got no response from Rails-Forum BTW) and at least I got some ideas...

Jen-Mei said to try this:

RUBY:
  1. Out of curiosity, when you run the Ruby console, what do you get when
  2. you type:
  3.  
  4. ActiveRecord::Base.connection.native_database_types[:string]
  5.  
  6. or just
  7.  
  8. ActiveRecord::Base.connection.native_database_types
  9.  
  10. ?
  11.  
  12. This is the array that ActiveRecord should be using to convert from
  13. the type you use in your migrations to database types.
  14.  
  15. I'm running MySQL 5. When I run it, I get:
  16.  
  17.                       >> ActiveRecord::Base.connection.native_database_types[:string]
  18.                        => {:name=>"varchar", :limit=>255}
  19.  
  20. Are you getting the same? The way it's running for you, it almost
  21. seems like it's returning nil or "string".

That worked as expected!! So that was not the problem...

Sandi M. had this idea:

RUBY:
  1. Looking in rails 1.2.3, I see that
  2.  
  3.     ActiveRecord::ConnectionAdapters::ColumnDefinition (1) has method
  4.       def sql_type
  5.         base.type_to_sql(type.to_sym, limit, precision, scale) rescue type
  6.       end
  7.  
  8. If this method raises an error, you'd get your original value back for the type.
  9.  
  10. It would be really interesting to go into this bit of code and change it to
  11.  
  12.       def sql_type
  13.         begin
  14.           base.type_to_sql(type.to_sym, limit, precision, scale)
  15.         rescue Exception => e
  16.           print e.backtrace.join("\n")   
  17.           raise "type_to_sql failed with #{e.class} #{e.message} for #{type}"
  18.         end
  19.       end
  20.  
  21. One would guess, looking at the type resolution code for various adaptors, that your install is missing, or didn't load, the adapter class which contains the type translation code for your database.  This backtrace should help locate the problem.

By this point, we had taken the conversation to instant messenger..

Hmm... so I saw it trying to work... I got an error saying argument error with expecting 4 params and only getting 2! Jen-Mei had me do a grep to see if type_to_sql is defined anywhere -- and AH HA.. there was a plugin -- mysql_bigint that was installed, and it had redefined the method and that was the culprit. This was a codebase that I inherited so I didn't even know what that plugin was used for or anything.

Here was the code in mysql-bigint plugin

RUBY:
  1. def type_to_sql(type, limit = nil) #:nodoc:
  2.           mysql_integer_types = %w{tinyint smallint mediumint intger bigint}
  3.         unless self.class.method_defined? :native_database_type
  4.           native = native_database_types[type]
  5.         else
  6.           native = native_database_type(type, limit)
  7.           limit = nil if mysql_integer_types.include? native[:name] # mysql doesn't use limit to indicate bytes of storage.
  8.                                           # Need to reassign native representation below.
  9.         end
  10.         limit ||= native[:limit]
  11.         column_type_sql = native[:name]
  12.         column_type_sql <<"(#{limit})" if limit
  13.         column_type_sql
  14.       end
  15.     end

Did a bit of research and found I had an old version installed:

http://www.northpub.com/articles/2006/10/29/new-version-of-mysql_bigint-rails-plugin

Quote from the site: "I received some feedback that edge Rails broke some things in the mysql_bigint plugin . I've checked in a new version that works correctly with edge rails. Thanks to Jamie Orchard-Hayes for a patch that fixed a few of these issues!"

So, I see there's a fix for it... but upon further investigation, the plugin is not use in the application at all! So I removed it for now.

Things I learned:

  • Check your plugins
  • Don't be afraid to dive into the rails code and print stuff to logs to see whats going on
  • Don't keep plugins laying around that are not in use
  • Probably a good idea to document what plugins are used and what for in the README
  • How to actually RUN the unit tests for rails (see the readme in the test dir), this way I knew for sure that my checkout of rails worked and it was something with my application code
  • DevChix ROCK!!

Comments

Weirdness and Coolness in Rails

Coolness

First the cool thing... and I think I have a good reason for this... I have a model that is not tied to any particular table, but is a summary table for about 8 tables. Rather than cluttering up my controller with a bunch of stuff, I am putting into a model (keeping REST in mind). I could have put it in the lib dir as a module.. but.. I thought i'd try it in the models dir, so i don't have to require it etc. And later I realized I need a bunch of fixtures for it, so I think its pretty handy as a model. Of course, I took out the base class of active record

app/model/summary.rb

RUBY:
  1. class Summary
  2.    attr_reader :date_from, :date_to
  3.    attr_writer :date_from, :date_to
  4.  
  5.   def has_dates?
  6.     (self.date_from && self.date_to) ? true : false
  7.   end
  8.  
  9.   def job_count
  10.     if self.has_dates?
  11.        Job.count(:conditions => ["completed_at BETWEEN ? and ?", self.date_from, self.date_to])
  12.     else
  13.       Job.count(:conditions => "completed_at IS NOT NULL")
  14.    end
  15.    
  16.    ...
  17. end

I might want to get the count of jobs for ALL time or a data range, so if there are dates set, it uses those. In addition to jobs, I need to do some 'not so easy" counting of other date and so i started changing my test fixtures to something like this:

test/fixtures/summary_report/jobs.yaml

RUBY:
  1. <% 1.upto(30) do |num| %>
  2. job_<%=num%>:
  3.   id: <%= num %>
  4.   created_at: <%=(Time.now - 1000).to_s(:db) %>
  5.   finished_at: <%= Time.now.to_s(:db) %>
  6.   name: One more number <%= num %> Awesome Job
  7. <% end %>

I have a bunch of other tests and I found one of them was not quite right, and my additional fixtures for the summary report messed it up. So I made a directory in test/fixtures/summary_report and then loaded the fixed in my summary test as such:

test/unit/summary_test.rb

RUBY:
  1. require File.dirname(__FILE__) + '/../test_helper'
  2.  
  3. class SummaryTest <Test::Unit::TestCase
  4.   fixtures 'summary_report/jobs',
  5.             'summary_report/people'
  6.  
  7.   def setup
  8.     @s = Summary.new
  9.   end
  10.  
  11.   def test_job_count_all
  12.     assert_equal 50, @s.job_count
  13.   end
  14.   ...

So I put fixtures for a particular set of tests in a directory in fixture. Is there a better way to do this?

Weirdness

I put it in a post I made on ruby-forum.com but here it is again:

Basically, when I try to use a rails version higher tha 1.1.6 in my vendor/rails directory my migrations don't have "varchar" they have "string" !! WTF! ... I even tried sqlite and same thing.

I've searched all over the web and mailing lists and can't find any info
on this problem.

I have a site built with Rails 1.1.6  I ran

rake rails:freeze:edge TAG=rel_1-2-3

I ran db:migrate (on a fresh database)

 rake db:migrate VERSION=1 --trace
(in /home/nola/projects/tardis/website/trunk)
** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:migrate
== AddContact: migrating
======================================================
-- create_table(:contacts)
rake aborted!
Mysql::Error: You have an error in your SQL syntax; check the manual
that corresponds to your MySQL server version for the right syntax to
use near 'string DEFAULT NULL, `city` string DEFAULT NULL)
ENGINE=InnoDB' at line 1: CREATE TABLE contacts (`id` int(11) DEFAULT
NULL auto_increment PRIMARY KEY DEFAULT NULL, `name` string DEFAULT
NULL, `city` string DEFAULT NULL) ENGINE=InnoDB
/home/nola/projects/tardis/website/trunk/config/../vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb:128:in
`log'
/home/nola/projects/tardis/website/trunk/config/../vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb:243:in
`execute'
/home/nola/projects/tardis/website/trunk/config/../vendor/plugins/mysql_bigint/lib/mysql_bigint.rb:32:in
`create_table'
/home/nola/projects/tardis/website/trunk/config/../vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb:353:in
`create_table'
/home/nola/projects/tardis/website/trunk/config/../vendor/rails/activerecord/lib/active_record/migration.rb:275:in
`method_missing'
/home/nola/projects/tardis/website/trunk/config/../vendor/rails/activerecord/lib/active_record/migration.rb:259:in
`say_with_time'
/usr/lib/ruby/1.8/benchmark.rb:293:in `measure'
/home/nola/projects/tardis/website/trunk/config/../vendor/rails/activerecord/lib/active_record/migration.rb:259:in
`say_with_time'
/home/nola/projects/tardis/website/trunk/config/../vendor/rails/activerecord/lib/active_record/migration.rb:273:in
`method_missing'
./db/migrate//001_add_contact.rb:3:in `real_up'
/home/nola/projects/tardis/website/trunk/config/../vendor/rails/activerecord/lib/active_record/migration.rb:212:in
`migrate'
/usr/lib/ruby/1.8/benchmark.rb:293:in `measure'
/home/nola/projects/tardis/website/trunk/config/../vendor/rails/activerecord/lib/active_record/migration.rb:212:in
`migrate'
/home/nola/projects/tardis/website/trunk/config/../vendor/rails/activerecord/lib/active_record/migration.rb:335:in
`migrate'
/home/nola/projects/tardis/website/trunk/config/../vendor/rails/activerecord/lib/active_record/migration.rb:330:in
`migrate'
/home/nola/projects/tardis/website/trunk/config/../vendor/rails/activerecord/lib/active_record/migration.rb:297:in
`up'
/home/nola/projects/tardis/website/trunk/config/../vendor/rails/activerecord/lib/active_record/migration.rb:288:in
`migrate'
/home/nola/projects/tardis/website/trunk/config/../vendor/rails/railties/lib/tasks/databases.rake:4
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:392:in `execute'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:392:in `execute'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:362:in `invoke'
/usr/lib/ruby/1.8/thread.rb:135:in `synchronize'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:355:in `invoke'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1739:in `top_level'
/usr/lib/ruby/gems/1.8/gems/rake- 0.7.3/lib/rake.rb:1739:in `top_level'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1761:in
`standard_exception_handling'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1733:in `top_level'
/usr/lib/ruby/gems/1.8/gems/rake- 0.7.3/lib/rake.rb:1711:in `run'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1761:in
`standard_exception_handling'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake.rb:1708:in `run'
/usr/lib/ruby/gems/1.8/gems/rake- 0.7.3/bin/rake:7
/usr/bin/rake:1

----------------------------

SO Basically, its trying to put in a sql create statement like this:

CREATE TABLE contacts (
 `id` int(11) DEFAULT NULL auto_increment PRIMARY KEY DEFAULT NULL,
 `name` string DEFAULT NULL,
 `city` string DEFAULT NULL
) ENGINE=InnoDB

string? when it should put in VARCHAR(255) .. weird huh?

Here's the migration:

class AddContact < ActiveRecord::Migration
  def self.up
    create_table :contacts do |t|
      t.column :name, :string
      t.column :city, :string
    end
  end

  def self.down
    drop_table :contacts
  end

Any ideas? I've also tried with rel_1-2-0 too... and get the same
result. I can create a table with ints or no fields just fine. When I
remove vendor/rails then its fine.

Comments (3)

Perl Survey

Hey ... a friend at oscon send me this survey PerlSurvey ... if you do perl, take it and let your voice be heard!

Comments (1)