Oct 18, 2009

DataMapper - migration from 0.9.x to 0.10.x

Finally got some time to put here some notes for migrating DM from 0.9 to 0.10.

1. Errors. (That means you must fix)
  1.1 options[:default] must not be nil
property :customer_address_id, Integer #, :default => nil
  1.2 options :nullabe are unknown
property :deleted, Boolean, :field => 'deleteflag', :default => false #, :nullable => false
  1.3 +options[:order]+ entry "po.closed_date.desc" does not map to a property in PurchaseOrderLineItem
order_by = [DataMapper::Query::Direction.new(PurchaseOrder.properties[:closed_date], :desc)]
result = PurchaseOrderLineItem.first(:deleted => false, :cb3_id => cb3_id, 'po.deleted.eql' => false, 'po.completed.eql' => 'yes', :order => order_by)
  1.4 For composite keys
(e.g. when you do a migrate!)
property :user_id, Integer, :key => true, :nullable => false
property :role_id, Integer, :key => true, :nullable => false

2. Deprecated:
  2.1 Integer with explicit :serial option is deprecated, use Serial instead
property :id, Integer, :key => true, :serial => true, :field => 'unitsindex'
property :id, Serial
  2.2 +options[:class_name]+ is deprecated, use :model instead
belongs_to :customer_address, :class_name => "CustomerAddress", :child_key => [:customer_address_id, Integer]
  2.3 explicit use of 'in' operator is deprecated
belongs_to :order, :model => 'Order', :child_key => [:order_id, Integer], :order_type.in => ['O', 's', 'w'], you can use
:order_type => ['O', 'S'] instead.

3. Others
  model.new_record? now model.new?
  model.update_attributes now model.update

Sep 14, 2009

Merb - a bug for merb router

You can not include a number(0-9) in your controller's name if you use it with nested routes. e.g.

Here is the quick fix:

Aug 21, 2009

_why?

It really doesn't matter what his name is, what I know is I read his book, use his projects and admired him.

Be good - Why The Lucky Stiff.

Aug 17, 2009

Vim and CTaglist plugin

My ctaglist plugin stopped working after I migrated to my new MacBook Pro 13". After playing with it for a few minutes, I realized that my default ctags cmd is not correct. To have your ctaglist working, you have to use Exuberant Ctags. I am hoping this could save you some time for looking for the answer.

$> wget http://prdownloads.sourceforge.net/ctags/ctags-5.8.tar.gz
$> tar zxvf ctags-5.8.tar.gz
$> cd ctags-5.8
$> ./configure && make && sudo make install

You probably have to copy it over the old one if you not set the $PATH on you Mac( there is a ctags under /usr/bin)

$> sudo cp ctags /usr/bin/.

That's it, start you Vim, you are ready to go.

Aug 10, 2009

Render without calling the controller's action in Merb

I was working on a Merb project in the company, I found a common request is generate the report to view in the browser and also send the report as an attachment of an email in a cron job. Usually I have to do two parts of job, one is for the regular MVC parts to get the report in a browser. Then I have to generate the report and write it a file(html, text or pdf). Then send it out later. As you might see, there is a duplication here, I have to design the report twice. This really bothered me, why can't I just re-use the view templates which is used in the MVC. After poking around the merb-core source files, it turns out pretty easy to do that:



This is another reason I love Merb.

Aug 4, 2009

Enable/Disable bluetooth via cmd line

Since I got my new MacBook Pro 13", I found I constantly lost the connection with my bluetooth devices, like mouse or keyboard. It is so annoying that I have to reach my touch pad to disconnect the devices and re-connect them from the status bar or system preference. You'll know what I am talking about if you have the same problem as what I have. It is pretty pain.

Today, I found a command line tools for you to enable or disable the bluetooth, it is so handy. I just love it. Here is the link for blueutil.

Now, I can just issue the command: blueutil off and blueutil on, I am back on track again.

Jul 16, 2009

How to enable colour when running autotest

This will be just a quick reminder.

After migrating my user account from the MacBook Pro 15" to 13", my ZenTest autotest lost its color in its output. To re-enable it, just put following line in the .bashrc:

export AUTOTEST=true

Not sure why my old system can run without this line. Anyway, it is working now. Back on road...

Jul 8, 2009

Restore from time machine

Just got my new Mac Book Pro 13". It is REALLY pretty.

I was hoping the migration would be a breeze. But it turns out there is a catch.

Here is the little background. I have a Mac Book Pro 15" which was purchased in April 2008 ( 4GB, 2.6GHZ, 250GB ). I loved it, the only thing it missing is the multi-touch track pad, of course, it is not a uni-body. But I want the new 13", you know it would be nice to have if you have to carry it for some business trip.

First, I made a full backup of the 15" to the time machine. Then put in the installation DVD on 13", after reboot, I wipe the SSD drive and then from the menu bar selected Utilities > Restore System from Backup...


This brought up a dialog to let me select the Time Machine


Once you've selected your Time Machine volume, you can select which snapshot to restore from. I followed steps, and after 2 hours, it was done. Once it rebooted, every thing works fine, there is only one catch - no sound. You can hear the chim when starting the machine, but no sound devices found when you go to "system preferences". After googling for about another hour, tried several solutions. None of them worked for me. I guess that just is the driver's problem, but I have no idea which library to put onto my new machine. So I gave up searching, and re-do the installation. It turns out it is quite simple, the lesson I've learned is "Do not migrate a whole system, just the user account if you have different machines". Here is the steps:

Do are regular installation, once reboot, choose to migrate a user account from your time machine. After that, every thing works great!

A little thought about the better way to do it. The way that I did which migrated all the iWork08 to my new machine, although I can reinstall the iWork09. I was thinking maybe I can create a dummy user, login, do all the update and install all the new programs, then delete the dummy account, reboot, then migrate the user account. That would save me a lot of time. Maybe when snow leopard out...

Jun 24, 2009

DM hook before :save

I've been using DM's hooks a lot to do the customized validation. Today I was trying to set a property before save,

It won't work.

Finally, I figured it out, use attribute_set

Jun 2, 2009

What kind of parents we want to be

Just read a blog article this morning, it really made me thinking. As a new father, I am not doing anything like that now, but I think I have the tendency :-). Following is the shamelessly copy and paste from the blog:

Helicopter parents

A colloquial, early 21st-century term for a parent who pays extremely close attention to his or her child’s or children’s experiences and problems, particularly at educational institutions. These parents rush to prevent any harm or failure from befalling them and will not let them learn from their own mistakes, sometimes even contrary to the children’s wishes. They are so named because, like helicopters, they hover closely overhead, rarely out of reach, whether their children need them or not.

Blackhawk parents

…those who cross the line from a mere excess of zeal to unethical behavior, such as writing their children’s college admission essays.

Lawnmower parents

…[used] to describe mothers and fathers who attempt to smooth out and mow down all obstacles, to the extent that they may even attempt to interfere at their children’s workplaces, regarding salaries and promotions, after they have graduated from college and are supposedly living on their own.

I have a ten-month old baby girl, she is so lovable, me and my wife cherish her like a princess, sometimes we even think that we might be spoiling her. This article just in time, I will discuss with my wife, what parents we want to be?

May 27, 2009

How to use GMail’s SMTP Server with Merb

I switched our email service provider to Google last week. So I have to figure out how to send out emails with GMail. I started with the default Net::SMTP settings, but it failed miserable:

    ~ 530 5.7.0 Must issue a STARTTLS command first. 7sm1765038qwf.45

After playing a while and googled a bit, here is the solution:

1. In your dependencies.rb, add following line:
    dependency "tlsmail"
2. Install the gem
    bin/thor merb:gem:install
3. Modify your init.rb
   
4. Over write the mailer
   
    Note: the "Net::SMTP.enable_tls(OpenSSL::SSL::VERIFY_NONE)"
5. Sending email
   
    Note: use :text instead of :body

Now, you are good to go.


May 5, 2009

How to add a third-party framework in your iphone project

I was working on an iPhone project, which needs some other third-party frameworks which is developed in c++. Since I am new to XCode, it really took me a while to get it straight.

1. Hard copy the third-party's src code fold into your iPhone project.
2. In your iPhone project, create a new taget
3. Unfold your target, right click(or control click) on the new target, choose "add existing files", then find the src you just copied. Do NOT check "Copy items into destination group's folder", because you've already copied it.
4. Compile your new target, if every thing goes well you should be able to get the .a file.
5. Drag the .a file to your project target -> link binary with libraries folder.
6. This step is optional, it depends on what type the third party src is. If it is c++ src, you have to rename your file which make directly calls to the third-party from .m to .mm.

I am hoping this could save you sometime to get your iPhone project up and running.

Apr 30, 2009

iPhone - provision and cert

Just a quick note if you have the same issues like I had.

After you finished debugging in your simulator, it is time for you to deploy on your device. You must have a developer ID and cert before you can do that. More details please read the official document. But even you followed every steps in the guide, you might be encounting some errors. I solved that by manually open the Info.plist with vim, then modify key "CFBundleIdentifier" to what is in your provision file. Save it, launch XCode. Then you should be good.

Another issue I had was if you get someone's Xcode project, and the owner happened deployed it to his/her devices, you will failed to deploy it to your device. To fix this, you have to do it in a terminal, open the project_home/project.xcodeproj/project.pbxproj. Search for "PROVISIONING_PROFILE...", then replace it with your profile identifier in the Organizer.

Apr 24, 2009

Developing Apps on iPhone

In the past two weeks I was extremely busy - I am diving into iPhone program development! I have a shopping list. I even bought a brand new iPhone 3G (Without the AT&T contract) from Apple local store.

So, let's go programing, baby!

Apr 10, 2009

Get all controllers and actions in Merb

When I was implementing our home-grown authorization system, I have to know what controllers are available, for the given controller, what actions are available. This shouldn't difficult using reflection within Ruby. But after reading the source code of Merb, I found it is dead simple to get what you want:

- Application.subclasses_list
- YourController.callable_actions

Here is the code in my Role modle:

Apr 4, 2009

Daycare

April 1 was the first day of my baby girl in a daycare center. Since that daycare center is supposed to be the best in town, I've been on the waiting list for really a long time, even before she was born. I was expecting some difficulties, both physical and emotional, but I've never thought it was that hard.

The beginning in the morning wasn't that pleasure. For the first time I know my baby girl is that loud, she cried really hard and not letting her mom go. We sneaked into the private room, which has a one-way window on the wall, so we can watch her, even can listen to her with the help of headphone. No teacher can calm her down. So the mom had to get into the classroom...

Then the next day, she seems to be OK in the morning, but just before the end of the day, the teacher called, my baby girl caught a fever - 101.3F. Then in the next two days we have to fight with the fever. Today, I even had to bring her to ER...

She is totally fine now. As the first time parents, my wife and I really enjoy this whole new experience. We've learned a lot, and we are continuing the process, it is like perusing another Ph. D again, only the experience is much better.

Here is what we learned from the daycare:

1. Your kid will like there, the daycare is like a Toys R Us opened in a small school.
2. Your kid will grow strong, the daycare is like a germ camp.

Mar 28, 2009

Constanize the merb controller

I am implementing an authorization system now. There are two sub-system:
  1. Role based control system
  2. Rule based authorization system

The role sub-system is relatively easy to implement. I am going to talk about the rule based authorization system only here.

According to the requirement, the authorization system is very detail orientated. What does that mean? I can give you a very simple example here:
   For role 1, he can view e1, but can view, edit, delete e2
   For role 2, he can view, edit, but not delete e1, can view e3
   For role 3, he can view, edit and delete, archive e1, no any permissions on e2 and e3

In our system, we have 50+ entities, and maybe 20 roles. You can imagine how many possibilities you can have here. Thank god, I've designed the system mostly in the RESTful way. So the next problem is how can I get all the controllers and callable actions. There are two situations I have to address first:
  1. My controllers has name space
      Purchasing::PurchaseOrder
  2. My controllers not inherited from the Application directly, they might have their own hierarchy.

It turns out pretty simple in Merb and Ruby way.(This is another reason I like it over Java's reflection). The key is to Dynamically instaniating a class by name. Here is the code.

Mar 21, 2009

DataMapper - some tiny issues within 0.9.9

I am working on a home-grown authorization system. It is very customized for our special requirement. I have to hack DataMapper to get the job done. The more I dig in DM, the more I love it. I can't imagine how much code I have to write to do the same thing if I do it with Java. As a relative new ORM framework, it sure has some corners that might not work well for you, following are some thing I found out in one day:

1. auto_migrate! vs. auto_upgrade!


I also noticed that there is a plugin dm-constraints, I haven't check it out yet, but it seems like the same, here is the code from the plugin:


I filed an issue report on lighthouseapp.com.

UPDATE(Feb-21-2009): Just got the note from Dan Kubb( one of the core DM developers):
"The Collection#destroy! method is actually supposed to bypass all hooks. The way you're doing it by looping over the records and calling each Resource#destroy method is the proper approach to execute each Resources' hooks.
In dm-core 0.10.0 there will be a Collection#destroy method that wraps this up and essentially does the same thing."

This makes a lot of sense. Like always, I really appreciate the attitude of the developers from DM, that is one one the reason I am using DM.

Feb 7, 2009

Smart Sleep - Mac OS X

I can't remember since when Apple decided that when you sleep your laptop, it will write all the data in memory to the hard drive, then go to hibernate. It is a good thing if you are on road, and want to change the battery. Once you power it on, every thing just like before you swap the battery. But for me, I've had 4 apple laptops, I've never done this for a single time. Especially for my latest MacBook Pro, I have to wait it to dump 4GB memory on to hard drive every time. And most of the time, I found it is even faster if you just power off it. I would be more happier if it can go to sleep right away when I close the lid.

Now, you can do that - SmartSleep PrefPane. I've been looking for something like this for a while. Why I love it, you know, now, I can get home 5 mins earlier every day. :-)

Feb 1, 2009

Bigger system fonts on Mac

Maybe I am too old[I am not :)], or I am working with too many 'elder' people in office, I was frequently asked "How can I get bigger fonts on my screen?". Well, I am not talking about "CMD +". It is too easy, most of the situation, they want every thing if possible.

First, I found "Quartz Debug".

I must to say it is very interesting. You should try it out if you've not yet. You will get the bigger fonts as you want, menu, labels... But it may have some problem when rendering the mouse.(Here, on an iMac). All that said, I am supposing you know what you are doing...

Then I tried "TinkerTool".

It is a very nice app. Not only the fonts, it can do other stuff for you too. e.g, I am putting the dock on the left-bottom now. :-) I would like to recommend TinkerTool for changing the fonts.

Jan 25, 2009

PDF generation with Prawn in Merb

In my company, the customer service representatives asked me to generate a PDF file format of the price letter for our customers, because those are relative sensitive files, which was used to be in M$ word format.

I started using HTMLDOC at first, which is fine if you've already had the HTML version. Really, I like it. It is pretty simple to set up and running. You do not need to care about too much about the layout and format of the file, because it is relatively easy to do that in a html.erb file. But there is a catch on production servers, if you have, like me, several different types of environment, Red Hat, Ubuntu, OS X, you really have to be care of the PATH to the binary htmldoc command. Otherwise the Phusion Passenger will throw out very strange error message.

After I came back from Ruby Conf 2008(if you were there, you problaly knew Gregory Brown, he held a hack session with Prawn.), so I decide to give Prawn a try.

Here are the steps( of cause within Merb):

1. Add mime type in your config file

2. In the view file, the form for the PDF request:

Please notice the :format => 'pdf'
3. In controller, provides the pdf format:


From here, you have two ways to send the PDF file:
In the controller/action

Or, in a pdf.erb file

Now, you should be able to get a PDF file.

UPDATE(Feb-23-2009):
Someone asked what if you using 'GET' instead of post?
Well, it is pretty simple, you can just add the format like:
resource(:ordering, @price_letter, :print, :format => :pdf)
I am hoping this is helpful.

Jan 17, 2009

jQuery: unobtrusive javascript form submit with Ajax in Merb.

I was working on a project this week. There are several forms on one page, I want to submit any form without leaving the page. At first, this requirement really bothered me for a few minutes, after reading some documents from jQuery and Merb, it turns out pretty easy to implement:

1. View - file which have the forms you want to submit
In your form.html.erb file, you can create them like the normal forms.

2. Javascript(jQuery)
Here is the javascript to do the unobtrusive form submit.

3. Controller
In your controller's action, you have to specify provides :js

4. View - file which does the render
This is the good part I like Merb, you can mix Ruby code and javascript together to operate the DOM.


It is pretty easy and handy.

Jan 10, 2009

Create PDF file from image files

Recently, I had to create a PDF file from a lot of image files. Usually, I have to open the image in Preview, then save it to a pdf file. Once I get all the pdf files, combine them together to create one file. It is OK if you are only dealing with a few, but I am facing thousands of them. Finally, I find the Automator( Come with OS X). It is super helpful. Although this is the first time I am working with it, I love it.
Step 1: Select finder items


Step 2: Convert and combine


Step 3: Save as a finder plug in

Jan 4, 2009

Elaine is 6 month now!

Here is the video(HD) I edited during the new year holiday.
I'm 6-month now! from Elaine Zuo on Vimeo."