Jun 29, 2009

Recent Update to Gedit Find in Files

I've updated the Find in Files plugin for Gedit again. It should be a tiny bit faster and also be able to search special characters. That means you can search for "<%= link_to" instead of just "%= link_to". (Characters are now properly escaped.) The conflict with the Find in Documents plugin should also work.

The original instructions for using the plugin were on my old site, but the gist is that you (may) need SnapOpen and need to enable the default File Browser plugin. Once you enable the File in Files plugin a 3rd tab will appear at the bottom of the screen. Typing in any search results there will display any matching files under your File Browser root with matching results. Basically a glorified grep -R


At some point I may get ambitious and add the ability to exclude log files, but for the moment I'd suggest only searching within a modest scope. i.e. searching within your Rails App folder? Ok! Searching within your Rails Root folder? Not such a great idea because of the potentially large log files and also all the plugins you may have. You can try it but you've been warned..

Again, I welcome any contributions or forks, so please have at it. Gedit plugins are written in Python or C and I don't program either particularly well.


Read on...

Validating form_tag in ActiveRecord

Activerecord valiations are seriously cool. If you just want to validate something in your form it's as easy as specifying the validation in your model and letting Rails take care of the rest. But what if you want to validate a form_tag? Generally, we use form_tag when there's no model associated with our form. Without the model we can't use Activerecord validations! Well, it turns out there's an elegant solution to the problem thanks to the Tableless Activerecord gem.


Now normally I'm pretty hesitant about adding a gem dependency to my application. These things tend to break between major rails upgrades and if the plugin developer isn't beholden to thousands of users it could be a while before you see a fix. In the end, if you're desperate, you could end up maintaining code you hadn't intended to.

The Tableless Activerecord gem is pretty straight forward though. Even though it hasn't been updated in a year, it seems to work fine with Rails 2.3.2. Usage is also pretty simple. To validate a contact form without a user model, just create a file called something like contact_messenger.rb and populate with the fields in your contact form and the validates you want:


class ContactMessage < ActiveRecord::Base

has_no_table

column :name, :string
column :email, :string
column :phone_number, :string
column :message, :string

validates_presence_of :name, :email, :message

end

In the above example, our contact form has fields for name, email, phone number, and message, but we're only requiring name, email, message. All other validations will work here as well so, for example, if you want to require the phone number be of a particular length you can do that as well.

Full instructions for using the gem/plugin can be found on Kenneth Kalmer's website.
Read on...

Jun 8, 2009

Quick Tip: Set up a Reverse SSH Tunnel

Often times I find myself using ssh to get to a server in order to get a file. The problem is that my local machine doesn't have its own publicly accessible IP address. So I end up ssh'ing into the remote server, creating the file I need (e.g. database backup) and then exiting only to run scp from my local machine afterwards. Plus, if after that I want to erase the file on the remote server I have to ssh in again and tidy up after myself. That's me running ssh x2 and scp x1 every time I want to accomplish what should be simple. There's got to be an easier way, right? You bet..


First thing you'll want to do is ssh into your server but this time we're going to pass a -R flag and a port number. That flag "Specifies that the given port on the remote (server) host is to be forwarded to the given host and port on the local side." Basically a reverse tunnel..

ssh -R 19999:localhost:22 youruser@yourserver.com


Once you're in, you can ssh back to localhost:
ssh localhost -p 19999


or copy a file down:
scp -P19999 YOUR_FILE youruser@localhost:/home/youruser/


And that is all there is to it! Credit due to HowTo Forge for pointing the way on this. They've got a bit more on the topic that you can read about here.

Read on...

Jun 6, 2009

Quick Tip: Search Email in Thinking Sphinx

If you use Sphinx as your search backend you may have noticed that it won't search for an entire email address. It's a bit perplexing at first since everything else works so well. After banging my head on the issue for a few hours, I got some direct help from Pat Allan, the creator of Thinking Sphinx. The problem is with the "@" symbol which is a reserved character in Sphinx. To get around that we need to modify Sphinx's allowable character set.

Now for better or for worse, you don't actually need to define any sort of config file by default to use Thinking Sphinx.. it's smart enough to pick sensible defaults. But in this case we'll have to go ahead and create one.


Every Ruby on Rails project comes with a config folder where many files you may need to modify are stored. In this directory create a file called sphinx.yml. It's a YAML file so be careful about spacing here. It's as sensitive as your database.yml file so if you accidentally alter the spacing things may not work.

For our example, let's use the UTF-8 character set. So in that sphinx.yml file, paste this content:

development:
port: 3312
charset_table: "0..9, a..z, _, @, A..Z->a..z, U+410..U+42F->U+430..U+44F, U+430..U+44F"
test:
port: 3313
charset_table: "0..9, a..z, _, @, A..Z->a..z, U+410..U+42F->U+430..U+44F, U+430..U+44F"
production:
port: 3312
charset_table: "0..9, a..z, _, @, A..Z->a..z, U+410..U+42F->U+430..U+44F, U+430..U+44F"


There are a number of other options you can put in your configuration file of course, but the charset_table one is going to be the one that helps you search email. In order to see the effect, you'll need to rebuild your index. If you've got a recent version of the Thinking Sphinx plugin:

rake thinking_sphinx:rebuild


Otherwise, you'll need to stop, index, and then start:
rake thinking_sphinx:stop
rake thinking_sphinx:index
rake thinking_sphinx:start


After that searching for emails should work!
Read on...

Jun 3, 2009

Quick Tip: Nginx and cap deploy:web:disable

If you've followed my instructions for installing Ruby on Rails in Ubuntu you may have felt ambitious and also gotten Capistrano working too. The only issue is that, by default, nginx doesn't work with Capistrano's web:disable task. In order to get that working, you'll need to get your system folder setup, add a snippet of code below your server block and then restart nginx.




1. First let's add that snipped of code to nginx. If you installed nginx via phusion, then your configuration file is in /opt/nginx/conf/nginx.conf. Use any editor to add these lines within the server{} block:

if (-f $document_root/system/maintenance.html) {
rewrite ^(.*)$ /system/maintenance.html break;
}


2. Now that this is done, give nginx a restart:

sudo /etc/init.d/nginx stop
sudo /etc/init.d/nginx start


You can now try disabling the application using the capistrano task:

cap deploy:web:disable


3. If you get an error, that says:

Net::SFTP::StatusException (Net::SFTP::StatusException open /var/www/my_app/shared/system/maintenance.html (2, "no such file"))


Then you'll need to create a system folder. So log into your remote server and make sure that the you have a system folder with the right permissions setup.

mkdir -p /var/www/my_app/shared/system


Try it again:

cap deploy:web:disable


If it worked this time then you're done! Just don't forget to enable the server again using:

cap deploy:web:enable


Otherwise, be sure to check out the error message (most like a permission issue).

Read on...

Sum and index a related column in Thinking Sphinx

It's fairly straight forward to get indexing working using the excellent Thinking Sphinx plugin. But sometimes you may want to do something a little more complicated. For example, if you have a Model foo with a has_many relationship to model Bar and you want to search on Bar how do you set it up? Or, what if you want to find out how many Bars Foo has? Enough! Let's work on an example.

Let's start and try to index a User model. That user has many bank accounts each of which has a balance:

User 1
--> Account 1 ($5)
--> Account 2 ($100)
--> Account 3 ($3)

User 2
--> Account 4 ($5)
--> Account 5 ($75)

In our example, we want to search for users by the amount of cash they have in their accounts. To do that we need to configure sphinx to first tell it that a User has many Accounts with a bank balance:


class Brigade < ActiveRecord::Base
has_many :accounts

#sphinx index
define index do
has accounts(:bank_balance), :as => :bank_balance
indexes "SUM(bank_balance)", :as => :total_bank_balance, :sortable => true
end
end


With that done, you can now do a search for users with a bank balance of $5 and it will return the 2 users with that balance. But, you can also do a search for users with a total bank balance of $80 and it will return only user #2.

Read on...

May 31, 2009

10 little features: Android Cupcake

We've been hearing about Cupcake for a while and so the feature set is probably no mystery. I got tired of waiting for T-mobile to push the update down and so after a few false starts I decided to update myself (instructions here. After playing around, I found a few things which I thought I'd share in case you hadn't gotten the update yet. Here are a few of the minor things you've got to look forward to when it does finally come over the air.



1. You can now mute threads in Gmail. This feature is even more handy when you're on the road than when in front of your desktop. Nothing worse than sitting in the middle of a thread you care nothing about when travelling. I wonder how Google Wave will handle this..

2. Most applications will auto-rotate now. Auto-rotating had been disabled initially (rumored at Apple's request) but it's now been enabled. Almost all the apps, from games to the browser take advantage of this. If you're an app developer be sure to see what your app looks like when in landscape mode...

3. There's a software keyboard with autocomplete options and you get the ability to enable haptic vibrate response. T-9 on Android is still not possible due to patent issues, but luckily they've found another way to get the job done. As you type a small toolbar appears with a number of guesses on what you're typing. It's pretty effective. In fact my typing speed on the software keyboard quickly matched how fast I peck using the HTC G1 hardware keyboard.

4. If you hard press the power button it will let you both silence and enable airplane mode on the phone. Not much more needs to be said about this. It's nice to have and allowed me to uninstall the ToggleAir application.

5. When you press the any button other than menu and your phone is locked, it will show the wallpaper background. This helps identify your phone even when locked. Especially useful if you work in an office with multiple Android phone owners..

6. When you are on the phone, instead of having the screen black out and the dialpad disappear, it now stays on but makes you double tap to access the dialpad. Since the G1 doesn't have a hardware proximity sensor like the iPhone, it has no way of knowing when you hold it up to your ear and when you take it down to push a button (Apple really thought of everything didn't they!). This new behavior tries to do as best as possible given the lack of sensor.

7. The Email IMAP client now really deletes email from the IMAP server. If you spent a lot of time on the G1 without using the Google account as your mail email provider, you probably noticed right away that you couldn't really delete emails. Thankfully, this is now fixed.

8. Camera improvements: Speed to launch the camera has improved as well as the speed of taking a picture. It's almost usable now.. just missing a flash. Hopefully future Android phones figure this out. Plus, you can now just hold down the camera button to launch it. No more going through the app menu and clicking the camera icon.. As covered elsewhere, there is also a camcorder now.

9. Live folders and widgets: We've got calendar widgets and a live folder that will launch various things like 'contacts with phone numbers'. It's a small thing but it does add to the usefulness of the phone.

10. There are now animations when moving between screens and lots of minor polishing has been done. You can unlock even more here if you install the "Spare Parts" application from the market.

Overall Cupcake is an excellent update. I'm really looking forward to Donut though. I especially want the ability to have the phone turn itself on if you have the alarm set. Google: think of how much energy you would save the world if you enabled this!

Read on...