30
Jun 10

Handling Ajax Form Validation in Rails

Handling Ajax requests errors isn’t documented really well and there are many ways to accomplish this, some are simple and some are pretty darn complicated.

the easiest solution I found was rendering different partials depending on the status of the save or update methods, here is a snippet

  def update
    @bid = Bid.find(params[:id])

    respond_to do |format|
      if @bid.update_attributes(params[:bid])
        flash[:notice] = 'Bid was successfully updated.'
        format.js { render :partial => "update" }
      else
        format.js { render :partial => "update_fail"}
      end
    end
  end

It may not be the most elegant solution but is sure simple and does what it says it does.


29
Jun 10

The state of ISPs in Jordan.

This is one of the most frustrating issues in Jordan (that is, to a geek ofcourse), all the ISPs suck, they literally suck your life and money out of you in exchange of a crappy service.

I’ve been an ADSL user for 5 or 6 years, I don’t remember to be honest, here is my take on the ISPs that I have used:

  1. TeData: they used to provide a decent service for the bucks you paid them, it was the only ISP that WoW didn’t massively lag on, I switched because their connection would drop for hours at time, the authentication would not succeed and all they did was to blame JTC, and ofcourse JTC would blame them in return, a very ugly and vicious cycle.
  2. Sama Telecom: their price is the lowest in the market I believe, their package of 10G per month is quite low, I believe that is their only package whether you have 128kb or 2mb, they claim that from 8PM till 8AM you can download to your hearts content, this is simply a lie, my 2mbps connection gets capped at 128kb even at night, I called to ask them why am I capped even after 8PM, they said I’m on their blacklist, yup, they said I download too much so I had to be put on this list so I don’t disturb the service for other users.

Following are some other ISPs whom my friends and family members have used and shared their experiences:

  1. Orange: if there is one massive incompetent company then its hand-down Orange, it takes them at least 72-96 hours to resolve any issue, during this time you have no connection whatsoever, my friend decided to upgrade to their double plan over 2mbps, oh my he was sorry for that, it took them 3 weeks to upgrade his line, and then a massive 4 weeks just to upgrade his account, in the end it all failed miserably and they had to issue a new account for him , he got so pissed that he wanted to cancel but behold, he couldn’t get a single penny back, bearing in mind that he paid a full year in advanced, that is the only way you can get the double plan.
  2. Mada WiMAX: we use this one at the office, they claim 2.4mbps speed but the best we could get is 0.8mbps even though the tower is like 1 kilometer away from the office and signal strength is quite good, their price is not very high but I wouldn’t recommend their service because of the speed you will be getting.
  3. Jordan Cable Services: their connection speed is amazing, we got a 512 connection and we were delivered 512, their price is alright, no limitation whatsoever on the downloads, the only problem is that they only cover Gardens Street.

If you would like to share your experiences and comments please do.


10
Apr 10

متى تغضب؟

متى تغضب؟

د . عبد الغني بن أحمد التميمي

أعيرونا مدافعَكُمْ ليومٍ… لا مدامعَكُمْ

أعيرونا وظلُّوا في مواقعكُمْ

بني الإسلام! ما زالت مواجعَنا مواجعُكُمْ

مصارعَنا مصارعُكُمْ

إذا ما أغرق الطوفان شارعنا

سيغرق منه شارعُكُمْ

يشق صراخنا الآفاق من وجعٍ

فأين تُرى مسامعُكُمْ؟!


27
Mar 10

Migrating from Facebook Page to Wordpress.

Facebook has decided to close our group for political reasons, I’m not gonna discuss that here because its their site in the end and they can run it in whatever way they want, all we can do is become less dependent on facebook and migrate our stuff from there, from now on, I will think twice about posting any useful content on facebook and I will recommend everyone else to do just the same.

So we needed to pull out all the notes from the page we had, totalling 454 notes, there is no export functionality in facebook only an RSS feed that would give you the last 10 items.

So I found a post that shows how to use the API test tool to query the FB database, the relevant query is

SELECT note_id, title, created_time, content FROM note WHERE uid = 0000000000

and change the uid into the id of the facebook page you want.

I was able to get all the notes in XML format, now in order to get those into wordpress we need to import them, here is a post that shows a generic xml parser for wordpress, I had to change the script a bit to make is understand fb’s xml format.

here is the file xmltowp.php

Update: 20100402
Now we have migrated our photo albums from facebook aswell, its a bit more complicated but hey, it works :)
here are the queries for the FB API tool, first get your albums

SELECT name,aid FROM album WHERE owner = 00000000

next use the album ID to get the photos info for each album alone

SELECT pid, aid, src_big, src_big_height, src_big_width, link, caption, created, modified, object_id FROM photo WHERE aid= '00000000_00000'

now we have an XML file with all the photos info, lets get the URL and get all the files

$grep '<src_big>' album.xml > urls

in VIM do the following substitutes on the urls file

:%s/    <src_big>//g
:%s/<\/src_big>//g

now feed the urls file to wget through the -i option.
I have used the NextGEN gallery module for WordPress, using that I was able to import whole folders, the folders where wget saved all those photos. Now to get the captions of the original photos in WordPress aswell I have written a small ruby script that parses the albums.xml and generates an SQL file that you need to execute against your wordpress DB, here is the code, ugly but works

#!/usr/bin/ruby

require "rexml/document"
include REXML

file = File.new( "album.xml" )
doc = Document.new file
output = ""
doc.elements.each("*/photo") { |element|
  filename = ''
  caption = ''
  XPath.each( element, "src_big") { |src| filename = File.basename(src.text) }
  XPath.each( element, "caption") { |cap| caption = cap.text }
  output << "update wp_ngg_pictures set description = '#{caption}', alttext='#{caption}' where filename='#{filename}';\n"
}
outfile = File.new("update.sql", "w")
outfile << 'SET CHARACTER_SET_CLIENT=utf8;'
outfile << 'SET CHARACTER_SET_CONNECTION=utf8;'
outfile << 'SET CHARACTER_SET_RESULTS=utf8;'
outfile << output

hope this helps someone in need.


20
Mar 10

iWorld Cup 2010

512 logo

Obsessed with football? Afraid of missing out on anything off the upcoming WORLD CUP? This cool application will keep you updated with all the results and latest news. Get it now, and carry WORLD CUP 2010 within your pocket!

sc1

Here is what you get from this outstanding application:

  • Accurate minute by minute live update and coverage for all match details.
  • All games are ordered by date or by group so you can easily fit your schedule around your most anticipated games.
  • A unique and attractive design, switch easily between groups, matches, teams, venues and latest news.
  • Fifa.com direct RSS feed for all the latest news as it happens.
  • A count down till the first day of the tournament.
  • A detailed page for every team, contains team profile information and all team matches.
  • A detailed page for every match, containing the results, the venue, and all the match events.
  • A details page for every venue.
  • sc2

    We love football with a passion, we created this app for us and for you! A great deal of effort has been put in designing, developing and testing all the functionalities to make sure that this app is the best for football lovers and to let us and you enjoy the games to the max!

    sc5

    If you have any comments or suggestions please don’t hesitate in contacting us either on iTunes store or on our website, www.barmajeyat.com


16
Mar 10

Rails SEO/Freindly URLs

to get decent SEO URLs in your rails app, app the following snippet to the model in question

  #change the URL of the apps to be more SEO friendly
  def to_param
      "#{id}-#{parameterize}"
  end

that would generate URLs as the following

http://www.barmajeyat.com/apps/1-tajweed


06
Mar 10

Rails edit_polymorphic_path

This handy function comes in use when you don’t know that object type you want to edit, I used has_many_polymorphs to pull many objects related to my user object, here is the views code

<% @user.entities.each do |e| %>
<tr>
<td><%=h e.info.name  %>
<td><%= link_to 'Show', e %></td>
<td><%= link_to 'Edit', edit_polymorphic_path(e) %></td>
<td><%= link_to 'Destroy', e, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>

05
Mar 10

using Has_many_polymorphs

So I needed to establish a relation between one model and many other models, the User model can own many other models like Restaurants and Cafes, has_many_polymorphs comes to the rescue, check the plugin homepage for installation details.

Here are my models

class User < ActiveRecord::Base
  acts_as_authentic
  has_many_polymorphs :entities, :from => [:restaurants, :travel_agencies]
end

class EntitiesUser < ActiveRecord::Base
 belongs_to :user
 belongs_to :entity, :polymorphic => true
end

following is the migration for the EntitiesUser model

class CreateEntitiesUsers < ActiveRecord::Migration
 def self.up
   create_table :entities_users do |t|
     t.references :entity, :polymorphic => true
     t.references :user
     t.timestamps
   end
 end

 def self.down
   drop_table :entities_users
 end
end

And to make sure the links between the two models are being established I used the following code in the Entity model

  after_create :add_entity_user
  after_destroy :delete_entity_user

  protected
  def add_entity_user #add a relation between the entity and the currently logged in user
    user = UserSession.find.record
    eu = EntitiesUser.new( :entity_id => self.id, :entity_type => self.class.name, :user_id => user.id  )
    eu.save
  end
  def delete_entity_user #delete the relations for this entity
    eus = EntitiesUser.find(:all, :conditions => ["entity_id = ? and entity_type = ?", self.id, self.class.name])
    eus.each { |e| e.destroy }
  end

hope this helps.