New Blog

Hi,

I have moved this blog over to here :

http://hamza.khan-cheema.com

All the old posts and soon the comments have been transfered there.

Please note this will be the last post on this blog, please check my my blog.

Hamza

Advertisements

Rails Autocomplete Tag List

I wanted to make an autocomplete text field on a edit form that listed all of the tags that are already entered in the system. It is quite simple, but I thought I would share it to save some time for someone. Of course we are using acts_as_taggable

The controller :

def auto_complete_for_tag_name
auto_complete_responder_for_tag_name params[:tag][:name]
end

def auto_complete_responder_for_tag_name(value)
@tag_list = Tag.find(:all,
:conditions => [ ‘LOWER(name) LIKE ?’,
‘%’ + value.downcase + ‘%’ ],
:order => ‘name ASC’,
:limit => 10)

render :partial => ‘tags’

end

Notice the partial

def edit
@content = Content.find(params[:id])
@tag = Tag.new
@tag.name = @content.tag_list
end

To retrieve the current tag list you need to make a new Tag object and add the tag list to it.

In my _tag.rhtml partial :

<ul class=”tags”>
<% for tag in @tag_list do -%>
<li class=”contact”><div class=”name”><%=h tag.name %> </div></li>
<% end -%>
</ul>

In My _form.rhtml

<p>Tags (Seperate with space)<br/>

<%= text_field_with_auto_complete :tag, :name,
{:size =>50, :skip_style => true},
{:indicator =>’searchIndicator’,
:tokens => [‘ ‘, ‘\n’]} %>
<%= image_tag(“/images/indicator2.gif”, :id => ‘searchIndicator’, :style => ‘display:none;’) %>
</p>

You will notice that I have added a search indicator, which you will need to have on your system.

Thats how easy it is 🙂

Hamza

Create Triple DES SecretKey in Java

For a project at work, I needed to integrate our website with another web application. The web application in question also had their own built in user management system. For seamless integration we wanted our users to only have to log in once.

As the web application was quite a large application, they already had a REST api that allowed you to supply them with a encrypted token in the url that authenticated you on their systems. You simply had to encrypt the user id (on their system) and the current time stamp.

As I had never done anything to do with encryption before in Java. I had to learn from scratch.

The token had to be encrypted with Triple DES CBC scheme. They supply you with an access key. Here is where the problem lay. I could find quite a few examples that allowed you to use the inbuilt key generator in Java, but what about if you have your own key (our access key). This was a bit more difficult to find, so I thought I would blog a code snipit here. In the end it turned out to simple (it always is 🙂

The key needed to be Triple DES (DESede)

String keyString ="4d89g13j4j91j27c582ji69373y788r6"; // I made this key up by the way!


byte[] keyB = new byte[24]; // a Triple DES key is a byte[24] array


for (int i = 0; i < keyString.length() && i < keyB.length; i++) {
keyB[i] = (byte) keyString.charAt(i);
}

// Make the Key
SecretKey key = new SecretKeySpec(keyB, "DESede");

A good resource that I found was here :


Hamza

Atheros Wireless Setup – Ubuntu

As I stated earlier, I just bought a new laptop and installed Ubuntu 6.10 (Edgy Eft) on it. All was working apart from the wireless connection. The Laptop comes with a Atheros wireless card. Ubuntu also comes installed with madwifi drivers for the card. So in theory things should just work, right ? Well no. The version of the wireless card I have (I do not have the version to hand) does not work with the current ubuntu package of madWiFi drivers.

So this is a quick step by step tutorial on how I got things to work nicely :

  • Get the g++ compiler : sudo apt-get install build-essential
  • Download subversion : sudo apt-get install subversion
  • Create directory to store the drivers and navigate to it.
  • Download latest madwifi drivers using subversion : subversion svn checkout http://svn.madwifi.org/trunk madwifi
  • Get the current Kernel you are running : uname -r
  • Navigate to the correct lib directory : cd /lib/modules/$(uname -r) (use the output from the previous step to get the directory)
  • Delete the net lib files : sudo rm -rf net
  • Delete the madwifi files : sudo rm -rf madwifi
  • Delete this folder if it exists : sudo rm -rf madwifi-ng
  • Find the modules currently installed that you need to unload : lsmod | grep ath
  • From this output above issue a rmmod command for all the modules : sudo rmmod modulename
  • Go back to where you downloaded the new subversion drivers and run : sudo make and then sudo make install answer yes to remove the old module.
  • Load all of the modules you have just unloaded using modprobe. These should be:
    sudo modprobe ath_pci
    sudo modprobe ath_rate_sample
    sudo modprobe wlan
    sudo modprobe ath_hal
  • Check to see if modules have been loaded by typing dmesg and looking at the system log.
  • Open up network gui in Ubuntu and enable the wifi card and set the sessid

The card should now be configured. However if you want a dock applet that informs you how much strength your wireless card has, I would suggest netapplet.

I hope this has helped someone. This took me about 2-3 hours to get working!

Hamza

PC Nextday Zoostorm 4-5701 17″ Laptop – Wireless problem

So I bought a 17″ Zoostorm laptop from PCNextday . It got 5 stars out of 5 from Computer Shopper and it worked out about £200 cheaper the a 17″ Dell laptop.

All was well, I was aware that they did have a bad reputation for customer service, but I thought I would risk it as the saving was too much.

The laptop came within 3 days of me ordering it. This was a surprise as they said that it can take up to take 3 weeks.

The laptop was working well, I was impressed with the screen (even though computer shopper said it was dull). However I was having a problem with the built in wireless card (Atheros). It was getting a very low signal even though I was sitting next to the wireless router. I first thought this was my router, so I asked someone else to test it and they reported the same problems. I then went and upgraded the software to the latest driver. The windows drivers are difficult to find so here is a good resource. But this had no effect. So after tearing my hair out I could not figure it out I emailed the technical support at pcnextday. They took a long time to respond, but their response was worth the wait. They basically said to check weather the wireless card was installed correctly and gave me these pictures :

laptop screws

laptop_wireless

So I unscrewed the laptop and checked it. I found out that the card was in the correct place, however the antenna was connected to the wrong point in the card. So I changed that and tested it. Unfortunately this did not work. As there were two wires that were black, I thought maybe the person who installed it made a mistake and attached the wrong wire. So I added the other wire and it worked. Now I have an excellent signal 🙂

I hope this experience helps someone. I think pcnextday need to work on their quality control 🙂

I have installed Ubuntu 6.10 on the laptop. All of the things seem to work. I had to do some configuration to get the wireless card to work though, but that is another blog post.

Hamza

Dual Monitor Setup – Ubuntu ATI

I wanted to get my dual monitor setup working at work. I have the latest veresion of Ubuntu (6.06 LTS – the Dapper Drake).

aticonfig
This is a really easy way to get it working. I have a Dell Optiplex GX620 with a ATI graphics card. The card can support a dual monitor setup.

It turns out that ati have supplied a program (aticonfig) that inspects your current xorg.conf file and then changes it to allow for dual monitor setup.

Here is what you do in a terminal window :

sudo aticonfig –initial=dual-head –screen-layout=left

You may want to change the left to be right, dependendant on where your main monitor is (monitor 0).

As I have two monitors that support diffrent resolutions, the setup was using 1024×768 on both screens and I could not drag windows accross screens (I could drag the mouse 🙂

To address the dragging window issue, I did this after I executed the first command :

sudo aticonfig –dtop=horizontal, reverse –overlay-on=1

If you look at the help page for aticonfig, you will see that dtop can take different parameters. Because my second screen was to the left of monitor 0, I had horizontal, reverse But if it was to the right I would have used horizontal only.

To address the screen resolution do this (please see note)

sudo aticonfig –resolution=0,1600×1200,1280×1024,1024×768

This now sets my monitor 0 to be able to support 1600×1200 resolution. My smaller monitor (monitor 1) will use 1024×768, which is what I want. You can simply change where it says 0 to be 1 This will now change monitor 1.

Now simply restart gdm to see the effects. Or if you can’t do that, just restart the machine.
Note

Before you start changing the resolution, you need to know which is defined as monitor 0. As in my case monitor 0 supports a higher resolution then monitor 1. The easy way to find this out is to do the first two steps, start up and then the monitor with the menubar on top of it is monitor 0. If it is not the monitor you wanted, you can simply swap around the vga cables that go into the back of the video card
There is a way to do it by editing the xorg.conf, but this is supposed to be a simple way of doing it 🙂

I hope this helps someone!

Hamza

RailsConf, My thoughts

I have just got home from two very exciting days at the rubyconf conference in London. Here are a few observations and just a few things I have learnt.

Observations
I am not sure why, but I had a distinct impression that the majority of attendees would have been from Europe. However I seemed to come across people from the US or Canada. People who have come over from the states especially for the conference or people who are making it part of their vacation. Not that this is a bad thing. In fact it showed me that they were really committed to Rails and to Ruby. I have to say if my company was not paying for my ticket, I would never have dreamed of going to the conference. Maybe I need to get more involved 🙂

As this is my first conference, I was not sure what to expect. It was an entire new experience. I was pleasantly surprised by the organisation of the conference by skillsmatter and also by the excellent calibre of the speakers. As I am new to the ruby world, I did not know the speakers (apart from David H), this conference allowed me to put faces and personalities to these very influential people within the Rails / Ruby community.

I got to see the Rails core team. This was a real eye opener for me, as I had no idea who they were or what their thoughts were for the future of Rails (I know I should read more).

Then comes David H’s talk. I have to say that I was impressed with David’s ideas and the way he presented himself. I really like the RESTFUL stuff and ActiveResource and also the Simply Helper. For those of you that don’t know, this is a way of putting conventions into the view layer like he has just done for the controller using REST.

Things I learned

Overall I have to say that I did learn allot (especially about Ruby)

  • Rails 1.2 is to be released very soon and is going to depreciate a whole bunch of methods.
  • In Rails 2.0 these deprecated methods will be removed.
  • 1.2 is going to heavily push the REST stuff.
  • Routing has completely been rewritten as it was a bottleneck. This is to be released in 1.2
  • The goals of ActiveResource, It is not going to ship with Rails 1.2, maybe 2.0 ?
  • With all the restful work on the controllers, the view started to look dated, so they have developed SimplyHelper, which is a convention for views.
  • Rails speaks ‘C’ – This was a talk I attended. I think things like this are very important. If we have a very complex algorithm or we have some legacy C systems, we need to be able to call C code from Ruby. In the Python CMS world Zope does this for a lot of complex operations (as far as I remember, correct me if I am wrong). 4 different approaches were mentioned. I noted that SWIG seemed to be one of the better options (and it supports c++).
  • If you are using C to do a big CPU intensive task, or a task that will take a while to complete, then you need to use BackgroundDRB. This allows you to spawn requests in the background (surprise,surprise). So in a web app the client gets a response straight away, so he knows something is happening , a progress bar would be a good indicator in this situation.
  • I really enjoyed the talk about high performance. I always wondered if anyone had deployed Rails apps, which had to handle over a million hits per day. Well James Cox seemed to have allot of experience in this field, and he shared some tips with us. I will mention a few here.
    • Speed Perceived – This is to use Ajax to make the app perceive fast to the user. The user will not know.
    • Always use :select, :limit and :offset in your queries.
    • Try and use lazy loading (:include).
    • MEMCACHED – Basically always use this as much as possible, he really emphasised this point.
    • Avoid shared hosting (obvious).
    • Page Caching – BAD – Avoid this as it is a nightmare to clean up.
    • Fragment Caching – Is better then page caching, but their are scalability issues
    • 8 server Gem (server architecture) – 2 x Proxy / Web static servers, 4 x Application servers, 2x Database servers with database replication.
    • Session management – use memcache, not the database store
    • If you have one server use Apache (fcgi), but this does not scale well, so for multiple machines use mongrel.
    • Put Google Analytics in its own iframe, this will avoid your app waiting to show the page because it is retrieving the js from google.
  • Django
    • Really cool!
    • Has built in user and groups in the framework
    • Uses python code to define database schema. This allows them to add rich data models. So you can define an upload field or a url field, the system will automatically know what to do with this field. This allows them to auto generate the admin interface to such a high level and what streamlined is trying to replicate for Rails.
    • Reusable template views with hierarchy functionality.
    • Try and not use AJAX in the admin section, as you do not know what kind of PC your publishers are going to publish from (old, etc.)

I have a lot more to write. Which I should be doing over the weekend.

Thanks.

Please comment if you found this useful or not.
Hamza

RailsConf

I am really excited that I am going to RailsConf in London! Its going to be my first tech conference 🙂

Hopefully I can write some thoughts about it on this blog.

Hamza

Delicious with Ruby on Rails – JSON

I recently (well about a month ago!) read a very good post that informs you how to add your delicious links to your rails app.

I was using that method and all was well, until I got a request that the links should open in a new window. With the simple html access to my links, that was not an option you can pass. So I had to resort to using something more complicated; JSON seemed the easiest for me to read in and manipulate.

I would advise you to follow this tutorial and then come back here and see how the code can be changed to use JSON instead of the HTML api.

You will need to install the JSON ruby library first.

gem install json

If that does not work just download it from here and run the install script.

Here is the modified method (I have posted it in Pastie, all my future code samples will be in Pastie)

http://pastie.caboo.se/13083

Search problems

In my last post I said that I was using acts_as_ferret.  However I was finding it difficult to create an index for a certain section of the site.  I think this was due to me having null fields some of the database fields. I could nt change this.

When I was having problems I turned to acts_as_searchable. As the post states the search plugin uses HyperEstraier which is an opensource search engine.  It seems to be very good and above all (for me) the UTF support seems excellent. This is demenstroated by having a Japenese language search demo on their site.

I managed to get it to work 🙂 I will post how in the next couple of days.

My first thoughs about HyperEstraier is that it has some very good features, but most of the features I am not going to use in the near future.  Overall the set up is a bit more invloved then ferret, this is because you need to run a deamon.

More thoughts soon!

Hamza