CakePHP Migrations
Installation of CakePHP Migrations
This is a quick how to use CakePHP Migrations. First download the three packages Migrations Pear Core and MDB2. Put the migrations vendor plugin into the cake/vendor directory. Place the two Pear packages into your php include directory, and/or modify your php.ini to reflect the location of installation.
How To Use
In the cake/console directory there is a ./cake or cake.bat file (depending on operating system)
Generating a Migration
to generate a migration file for a User model simply write
cake.bat migrate g user
This will create a file in app/config/migrations/ named timestampuser e.g. 1226324252user.yml
# migration YAML file
UP:
create_table:
table_name:
name:
description: text
count: integer
is_active: boolean
DOWN:
droptable: tablename
Please Note: Make sure you remove the text "table_name" and replace it with the table name you generated the file for, e.g. "user"
Also ensure all fields have a type.
Processing Migrations
To get the database up to the latest migration file
cake.bat migrate all
firefox double page request
For the life of me I couldn't work out why every time I clicked on a new advert in my new classifieds section I'm developing it would count as two clicks in firefox.
After a bit of research I've found that if firefox finds an empty img src, i.e. img src="", then it reloads the page hoping it'll automatically appear.
I wonder why the moz devs decided this was a smart idea, is this part of the w3c? Anyway, to resolve, simply remove all of the empty src tags.
PHP5 with GD on leopard
Crazy! I have to say I miss apt-get! But ohh well. I assume you have ports installed..
Install GD2
sudo port install gd2
Now we want to install entropy php5
curl -O http://www2.entropy.ch/download/php5-5.2.5-6-beta.tar.gz
tar -xzf php5-*-beta.tar.gz
sudo mv php5 /usr/local/
sudo ln -sf /usr/local/php5/entropy-php.conf /etc/apache2/other/+entropy-php.conf
At this point go into the httpd.conf file and comment out the php5 LoadModule line.
sudo apachectl restart
you can then php -i to see we have GD, Imap, etc. etc. installed!
How To Get VirtualBox Working With Kernel 2.6.24.18
So I upgraded to the latest kernel and virtualisation broke. I've come to expect this after having to spend countless hours with vmware. Luckily virtual box is simple to fix and doesn't require hours in front of a pc.
sudo apt-get install virtualbox-ose-source
sudo m-a update
sudo m-a prepare
sudo m-a a-i virtualbox-ose
sudo /etc/init.d/vboxdrv restart
Once you've done that go through the menu bar and start virtualbox, and you'll see it no longer complains about the kernel number and just starts!
Could life be any simpler?
Mac Book Pro
I was tossing up for a while on getting an ultra portable for the train. I couldn't find anything I really liked, or supported a linux operating system. So I decided to get a mac. My mac book pro will land in around 14 days. Its an entry level mac book pro with 4gb of ram and the faster 200gb hdd.
It should be a monster! Hopefull its practical enough to use on a train.
Dvorak and the many typos
Does typing 200wpm tickle your fancy? Well being the geek I am it tickled mine. Nathan introduced me to the Dvorak keyboard layout. It's painstakingly slow to learn, as I'm learning while writing this blog post at about 5 words a minute! But if your a glutton for punishment then give it a shot!
RMagick on Ubuntu Hardy or Feisty
Having trouble installing rmagick? Well simply do a
sudo apt-get install imagemagick libmagick9-dev
This will install about 35 packages (yep its very lightweight), then all you need to do is
sudo gem install rmagick
Pidgin to fork development
It looks as if Pidgin will fork the pidgin code base. Why? Well because of the input box on the chat windows..
This latest release of Pidgin sees an input text box on chat windows (the place you type when talking to someone) that used to be height adjustable, now auto adjust! The community was outraged by this!
When the developers were asked to return it to an adjustable text box, they simply said no.. So a few members of the pidgin community have decided to fork the code base to allow for the adjustable text field..
This makes me ask the question, if the pidgin community wanted an adjustable text field why won't the pidgin devs just implement it? Is it pride holding them back? Or an underlying technical issue? Either way, it begs the question to be answered, why do we build software for? Is it for ourselves or for the community???
I know the answer, but does everyone else???
Firefox's Firebug vs Opera's DragonFly
Well I'm not really giving you a battle to the death, instead just my overall expectations vs realities.
For starts, way to go Opera! I love the Opera browser but tend to go with the clunky memory hog known as Firefox due to the plug ins (i call that the eclipse IDE effect)!
I was very happy when I heard of dragonfly and immediately downloaded it, only to find I was disappointed it wasn't easier to pull up! One of the nice things about firebug is the bug turns red when you fucked something up! Dragonfly is neatly tucked away in tools -> advanced -> developer tools.
This lead to a little bit of confusion, since it didn't dock at the bottom of each window you had to select the site profile to view it. But quickly overcame that. What I did like was how easy it was to select different files to get a rendered css/js source view. I also like the fact that its a nice layout and that it didn't feel clunky or confined.
What I didn't like was I couldn't view any HTML?? Was I missing something here? Anyone? Also I can't inspect.. Firebug has the great ability to "inspect" the HTML elements on the page. This saves a lot of time when trying to debug those nasty css issues!
Although this is alpha, so I'm sure it has a long way to go! Hopefully they read a lot of the blogs and spend a lot of the time researching other equivalent tools. The have the UI down, just gotta get some of the functionality working!
Please get this right Opera, I'm getting really sick of my browser locking up and chewing all of my ram!
Sound in Ubuntu Hardy Heron
If you playing music (mp3, etc) and you try to watch a flash movie, you'll find it won't work, a simple solution is to install flash support.. why aren't these things installed by default? Who knows..
sudo apt-get install libflashsupport
I believe in this version of ubuntu, we've all taken one step back. Lets hope we get moving forward again and not have to worry about these little things..
Firebug With Ubuntu Hardy Heron
First let me say, WTF Ubuntu! Why would you release a stable O/S with beta software? When I first formatted and went to Hardy first things first, I got the web browser set up the way I liked it, but was very unimpressed when most of my extensions wouldn't work! So how did I get them working?
First download firefox-2
sudo apt-get install firefox-2
Then open up firefox-2 profilemanager
note: you can't do this while any version of firefox is running.
firefox-2 -profilemanager
Here you need to create another profile called firefox2. Then close the profile manager.
Now to load the new profile
firefox-2 -P "firefox2"
You can easily add this to your icons, or quick launch buttons if needed. You can't run both firefox2 and firefox3 at the same time, firefox isn't that smart!
Office Space
what a great movie!
The end!
acts_as_taggable_on_steroids
One of the coolest things about rails is the development community behind it. Now I don't mean the core developers that are creating the rails code (they're cool also) but really its the guys who write the modules, plug-ins and gems.
For example, I stumbled across acts_as_taggable_on_steroids, and its so simple to use. All you need to do is download the plug-in, do a special generate migration, and then rake migrate and add acts_as_taggable in one of your models.. For full information on how to install and use acts_as_taggable_on_steroids check out Agile Web Developments Blog Post
Rails for PHP Developers
Pragmatic released a book a while ago called Rails for PHP Developers. Moving from PHP to ruby has been quite a challenge. Not because ruby or rails is difficult to pick up, but in my years of development I've mainly used c like languages, PHP Java, c, c++, etc. Ruby is not like any of these, it tends to take a whole different approach to development.Is this a bad thing? Well not when you finally get your head around how ruby does things.
This is where this book helped out a lot! I read the first few chapters on the train on the way to work and home, and it helped me understand the key differences between PHP and Ruby.. So If your making the move, its really worth your time. Hell, what is $40?? Maybe one hour of work or two if your a manager at McDonald's? This book has easily saved me countless hours of Google research and many many lines of bad code from being written!!
Ordered Hashes in ruby 1.8
So I came across a situation where I needed an ordered hash. After doing some research and talking to the #rubyonrails guys I've found I'm far from the only one who needs this, and in Ruby 1.9 there is a OrderedHash class. But until then I had to create my own, and with the help of the guys in #rubyonrails I finally got something working
# Usage:
# h = OrderedHash.new
# h[2] = 3
# h["x"] = "y"
# h.delete(3)
# h.each {|key| p key}
class OrderedHash < Hash
def initialize
@keys = []
super
end
def []=(key, value)
@keys << key unless member?(key)
super
end
def each
@keys.each {|key| yield key, self[key]}
end
def delete(key)
@keys.delete key
super
end
end
Yea its pretty simple, but I'm still pretty new at all of this, so I chalk this as another milestone! BTW my eagerly awaited books have arrived from pragmatic! Rails for PHP Developers and Programming Ruby! Let the fun begin!
paypal sandbox: how to verify a bank account and get the API credentials
After about 6 hours today of pulling my hair out and trying to verify my bank account on my sandbox, I finally stumbled across a post that helped me do this
For everyone else that is having a hard time getting the bank account to be verified, the secret is the BSB number. Do they tell you this anywhere? Well maybe, but do we look, hell no! Here are some step by step instructions on getting it working!
Prerequisites:
- Create a user account to access the Developer Central Sandbox.
- Log in to the Sandbox account
- Create a Test Account of type 'seller'.
- This will throw an error message on saving, but it will still create the account. Click the Test Accounts tab to see the account it created.
- This will throw an error message on saving, but it will still create the account. Click the Test Accounts tab to see the account it created.
Create Confirm the email address:
- Create business test account (done in prerequisite above)
- Log into PayPal Sandbox with test business account details created earlier.
- Go to Profile Tab
- Go to Email
- Choose the email address and click 'Confirm'
- Go back to the Sandbox Test Environment and click the Test Email tab
- Open the email with subject line 'Activate Your PayPal Account'
- From the email body, select the confirmation URL and paste into new browser window - it will load the PayPal site (in sandbox mode).
- Log in with business account.
- From the landing page, click the 'confirm email' link in the 'To Do List' box.
- Click the 'click here if a link doesn't appear in the email' link.
- Paste in the confirmation number from the email in step 8, and click confirm. A confirmation of success should be displayed.
- Click Continue.
Create Bank Accounts For Test Business Account:
- Click the Profile tab
- Click Bank Accounts link
- Create a fake bank account. Test Bank. Use BSB 242-200 for Australia. Any random integer for account number, so long as it's not already being used by another PayPal account. I ended up having to mash the keypad until I found an unused number.
- Confirm the bank account by clicking the 'confirm' link after the step above.
- Step 17 loads another page where you need to click 'Submit'.
- Step 18 loads another page where you need to click 'I Agree'.
- The account should now be verified.
- Click the Profile Tab again.
- Click API Setup
- Click Request API Credentials
- Select API Signature
- Click I Agree, Click Submit.
- Copy the details to a safe place for later reference.
Developer Central Sandbox
- Go back to the Developer central sandbox and login with your account details created in the prerequisite section of this document (i.e., not your test business account, but your actual Developer Central Sandbox account).
- The Test business account should now be verfied.
- Click API Credentials tab.
- The API credentials obtained in step 26 should appear.
ssl https with mongrel and apache2
I was trying to find a nice elegant way to get rails doing ssl via mongrel and apache2! I stumbled across this! I thought it would be handy for anyone searching for similar stuff
netbeans and rails
I was once all about gedit! It was very very lightweight, and with a few tweaks could be set up to be a very nice IDE, except for the fact its not an IDE, and the more I used it, the more I found limitations. So here I was asking about nice IDE's for ruby on rails on Ubuntu!
Luckily I found that the guys on #rubyonrails (on irc.freenode.org) have run into similar problems and they all recommend I tried out netbeans!
I was pretty sceptical after many attempts at eclipse and jedit, I've found that Java IDEs are about as fast as a runner at the special Olympics. But hell I'll give it a go! First I downloaded via apt-get, big mistake, the repository is way out of date, so I uninstalled and went to Netbeans Site where I see a nice "Ruby" version.
After downloading it, I'm really impressed with the coolness and speed! Its much much faster than eclipse and doesn't have those semi oval, semi ass about rounded tabs.. It has macros, snippets and all things nice! The only thing I'd like to see which is doesn't have is good variable completion!
Microsoft to invest in ruby on rails
A press release today from Microsoft detailed plans to invest heavily in the ruby language. A spokesperson today discussed the eventual ruby inclusion to the exclusive .Net framework. Ruby.Net will be 100% rails compatible allowing ruby developers to take full advantage of the brilliance that is .Net as well as the rails simplicity!
I know I'm not the only one excited by this move!
Happy April 1
routes vs to_param
I'm working on a website for a fella who draws comics. He wanted the url to be /comics/12-02-2008 (where 12-02-2008 was the date the comic was published). I tried for a long while, and finally got my routes to do :controller/:action/:published_on instead of the standard :id. I did this only for the comics controller, and all others had :controller/:action/:id.
I kicked myself when I found to_param!
class Comic < ActiveRecord::Base
def to_param
"#{id}-#{published_on}"
end
end
This allows me to convert the :id symbol to :published_on. Much nicer than the old crusty routes!
with_scope vs has_finder
When I first started out in my ror days, I was calling find in a not so elegant way. e.g.
find(:all, :conditions => { "published < '12-01-2008'" })
I then moved onto with_scope, and I was defining methods that would allow me to do things like this,
find_latest(:all)
The method in my model would look similar to this
def self.find_latest(*args)
with_scope(:find => {:conditions => ["published_on <= :today", {:today => Time.now.strftime('%Y-%m-%d')}]}) do
find(*args)
end
end
Just recently I've been pointed to a plugin that is now an extension of activerecord and comes with rails. has_finder allows a dev to do cool things like this.
has_finder :latest, :conditions => ["published_on <= NOW()"]
# usage
Comic.popular
Comic.popular.count
Comic.popular.find(:all, :conditions => ["foo = ?", true])
As you can see, there is a lot less code using hasfinder, and hasfinder seems to work with .count, where with_scope wouldn't!
To read more about this check out nicks blog
activeresource external url calls
Following up on yesterdays articles I thought I'd list the rails find command and corresponding url call.
in our model if we have set up self.site = "http://domain.com/rest/"
User.find(:all) -> http://domain.com/rest/users.xml
User.find(1) -> http://domain.com/rest/users/1.xml
User.find(100) -> http://domain.com/rest/users/100.xml
So if your doing an :all, the app will look for model.xml. If your doing any other call, it'll look for model/?.xml. I hope this helps when building external activeresource communicators.
Activeresource to a none rails site
Lately I've been getting into rails, what can I say its a nice clean language. I also run a forum, its a forum written in php. Now I want to build apps for my php (vbulletin) forum, but I didn't want to do cross site database calls. I wanted my plug ins to be as independent as possible from the main vbulletin app..
In another website I've been writing, there are three components that interconnect to each other using activeresource! Since it was so easy to implement I thought I'd give it a go cross programming language! Heres how I did it!
First in PHP create your restfull program!
I called mine rest.php :) I know very original!
Using a sexy XML library I created for php. From this simply class, I can easily create from a select from a database my xml!
<?php
$userid = $_GET['user'];
$result = $db->query_read("select userid, username, email from user where userid = '$userid' ");
$count = mysql_num_rows($result);
$xml = new XmlBuilder();
$xml->filename("user.xml");
for($i=0; $i<$count; $i++) {
$user = $db->fetch_array($result);
$xml->openTag("user");
foreach ($user as $tag => $value) {
$xml->addfield($tag, $value);
}
$xml->closeTag();
}
echo $xml->build();
?>
So far so good! So if we go to file.php?user=1 we should get the user with id 1.. You could add all sorts of cool things here. But this is just an example to get you guys going.
Now finally ensure you have mod_rewrite working in apache! now if your php file is called rest.php, you'll need this rewrite rule to make the magic happen
RewriteRule ^/rest/users/(.*) /rest.php?user=$1
In your rails app, now create a model named User, e.g. models/user.rb, and add
class User < ActiveResource::Base
self.site = "http://domain.com/rest/"
end
and don't forget your corresponding controller. This will now fetch all requests from our rest.php file. If you need any help, please drop me a line!
What Gedit Can Do That Textmate Can't
Ok I admit, maybe I was a little hasty with my comments regarding "gedit can do a few things textmate can't"!
Keith Rowell from RubyGreenBlue was nice enough to ask what! Giving it some thought I had the realisation that it wasn't textmate I was unhappy with, but instead the mac experience! Now no offence to anyone out there who uses a mac, but it felt like an early linux distro! Sure it has some nice features, like the apple+~, and the touchpad two finger scroll, but it just didn't feel like it had it where it counts.
No bash completion out of the box is crazy in this day and age, the terminal can't be styled, and what the hell is with the minimized windows won't be maximised on alt+tab! I understand it switches apps not windows, but maybe it should maximise? And where are the virtual desktops?
Now again, I don't want to start a mac vs linux debat, but instead I just want to say that linux feels right to me. Now Gedit falls into the same ball park, its nice and open source, its free, it has a nice range of plugins, and its fairly simple to use! Snippets are simple to set up, external commands are easily achieved. But best of all, its lightweight and tastes great!
So Keithy my good pal, how about we agree to disagree on this!!
BTW people, check out keiths photos, he has some nice shots!
restrict IE users from hitting your website
I can't remember where I got this from, but its something I wanted to use on here because quite frankly, I don't think I know one user who uses IE. And to those that do, the question has to be asked... why???
Anyway, here is a sexy script that you can put in your header to stop IE users from infecting your site with their nonsensical nonsense.
<script language="javascript">
<!--
if (navigator.appName == "Microsoft Internet Explorer") {
document.location = "http://<%= request.host_with_port %>/noie";
}
// -->
</script>
Too Simply? BTW: This is obviously being used with rails, change the document.location to a url, and you'll be hating on the blue e in no time
Nautilus GKSudo when you don't have permission
Don't you hate when you want to paste something into /var/lib/* and you can't because you need sudo.. What I proposed is gksudo to be called when a command you preform in nautilus needs sudo to complete successfully.
The user of course needs to be checked for "sudo" access, etc. etc. etc.
If you like the idea vote for it in the ubuntu brainstorm website.
ALT + ~ (tilde)
One thing I really found useful on the mac was ALT+~ (tilde). For those of you who haven't seen alt+tilde, it works a like this..
Lets say you have 4 terminals open, 3 firefox windows and 2 gedits windows. ALT+TAB will cycyle through each window of each application. ALT+TILDE will cycle through only the windows of the focused app..
So for example, I have one of my terminal windows focused, and I want to get to another terminal window I have open, I can ALT+TILDE and it'll only cycle through terminal windows! Brilliant
Coming back to ubuntu I really miss it! Ubuntu has just launched brainstorming, allowing the community to put forward ideas on enhancements! I have submitted my alt+tilde request through the website.. check it out
alt+tilde
Ruby on Rails in Gedit
There are a few tutorials out there to make gedit act like textmate.. I have to say, using textmate for nearly a month, I see things gedit can do that textmate can't! I guess I have linux in the blood..
Heres how to rubify your gedit!
First Install the plugins for gedit
sudo apt-get install gedit-plugins
Next, visit Insane Terminology as he has a good explanation on how to get syntax highlighting working in gedit! I'll summarize for the impatient.
If you have any installed mime packages in Override.xml remove them
rm ~/.local/share/mime/packages/Override.xml
Next we go to where the real mime packages/definitions are stored and download the mime type definitions courtesy Insane Terminology
cd /usr/share/mime/packages
sudo wget http://robzon.kapati.net/rails/rails.xml
sudo update-mime-database /usr/share/mime
Now to finish it up, we'll get syntax highlighting corrected for .rhtml (.html.erb) files.
cd /usr/share/gtksourceview-2.0/language-specs/
sudo wget http://robzon.kapati.net/rails/rhtml.lang
Theres a heap of plugins for gedit, search the net for them, they transform gedit from windows notepad to a real ide.
One thing I will mention, I have created shortcut on my top panel (taskbar, or whatever you want to call it). I've edited the gedit properties (i.e. right click --> properties) and changed
gedit %U
to
gedit %U --new-window
This allows multiple gedit windows
Installing Ruby On Rails (ror) in Ubuntu
Installing ruby is a cinch, rails on the other hand has all sorts of dramas! So lets get into it!
Lets get some packages
The following packages basically installs ruby, the ruby headers, some compliers and ruby docs.
sudo apt-get install ruby rdoc irb libyaml-ruby libzlib-ruby ri libopenssl-ruby build-essential ruby1.8-dev
Gems
Now gems via apt-get breaks, its in a bit of a mess, and who ever created the .deb should be shot out of a cannon, I might create my own one day.
Here we just want to wget (download) the gems package, untar it and install it.
wget http://rubyforge.org/frs/download.php/29548/rubygems-1.0.1.tgz
tar xzvf rubygems-1.0.1.tgz
cd rubygems-1.0.1
sudo ruby setup.rb
sudo ln -s /usr/bin/gem1.8 /usr/bin/gem
update gems
if this fails, try again.. rubygems is about as reliable as a Melbourne's weather.
sudo gem update --system
Install Rails
Here we install rails via gems
sudo gem install rails --include-dependencies
MysQL
Here we will install mysql and the ruby wrapper/connector
sudo apt-get install mysql-client mysql-admin mysql-query-browser libmysqlclient15-dev
sudo gem install mysql
SQLite3
If you prefer SQLite, then this is for you! If you plan to use mysql (like I have, then you can skip this section)
sudo apt-get install sqlite3 swig libsqlite3-ruby libsqlite3-dev
sudo gem install sqlite3-ruby
Install Some Gems
Mongrel is a very good webserver for rails
sudo gem install mongrel
And there you have it, a fully fledged production install of ruby on rails. Its about as easy as that.
Welcome to markgandolfo.com.au
Hey Guys,
Welcome to markgandolfo.com.au .
I run Ubuntu 7.10, and have been keeping up with the Ubuntu releases since breezy badger. I edit generally using gedit with a list of plugins that I'll cover at a later stage, plus I use vim for those quick edits. I develop in php, and ruby primarily and use firefox and opera to browse depending on what I'm doing at the time. (I'll cover my firefox extensions at a later stage also)
Along the way I would like to document for me (and anyone watching) how to do some of the tasks that I carry out once a while, such as setting up subversion and apache2. How to set up ruby on rails on ubuntu, or how to do a few cool things in php, rails or another application/language, etc.
