Pages

Thursday, January 27, 2011

Quickly lock your screen



Quickly lock your screen
by Rob Griffiths, Macworld.com Jan 25, 2006 3:00 am

If you work with any kind of sensitive material—from trade secrets to love letters—you’ve probably wished for a way to block access to your Mac the minute you stand up. There are many ways to do this, from the obvious to the obscure. I’ll cover all the methods I know to accomplish this trick. For most of these methods to work, you need to require a password when your Mac wakes from sleep or screensaver mode. To do this, open System Preferences, go to the Security pane, and select the Require Password to Wake This Computer From Sleep or Screen Saver option. Now you’re ready.

A simple way to protect your files when you walk away from your computer is to hit Shift-Command-Option-Q to do a fast logout of your user. Your Mac will go back to the login screen. However, there’s a huge downside to this method—all of your currently-open documents will close, and any running applications will quit prior to the logout. Clearly there must be better alternatives, and there are.

You could also put the computer to sleep. Go to the Apple menu and select Sleep or, if you’re using a laptop, press the power button and choose Sleep from the pop-up dialog. (Note: This is an edit from the originally posted version, where I said to hold the power button down; if you do that long enough, you’ll turn the computer off.) Of course, it takes a bit of time to put a Mac to sleep and to wake it up. You may also have remote users connected to the machine, or some lengthy program running that you’d rather not interrupt. In those cases, this isn’t the ideal solution.

A relatively quick method of locking your Mac—while still leaving your programs running—is to activate the screen saver using a hot corner . To do this, open the Desktop & Screen Saver System Preferences panel, activate the Screen Saver tab, and click the Hot Corners button. Decide which corner of your screen you’d like to use, then click the corresponding pop-up menu and select Start Screen Saver. Now when it’s time to walk away, just fling your mouse into that corner of the screen, and you’ll trigger the screen saver.

If you have the corners of your screen devoted to Exposé or some other feature, here’s another option. It turns out that the screen saver is just an application, so you can put an alias to it in an easy-to-access location, such as your dock, or the Finder’s sidebar or toolbar. Just navigate to System -> Library -> Frameworks -> Screensaver.framework -> Versions -> A -> Resources, and then drag ScreenSaverEngine.app onto your dock, sidebar, or toolbar. Now when you want the screensaver to activate, just click the convenient icon. The dock will prove the easiest spot to reach, since it’s visible in all applications. If you have a launcher program such as Peter Maurer’s Butler or Objective Development’s LaunchBar, you could even create a keyboard shortcut that will open the program for you, no mousing around required.

Another method of locking your system is to show the login window, without actually logging out. You can do this by enabling fast user switching in the Accounts System Preferences panel. Click the Login Options button (you’ll probably have to enter your administrator password to do this), and then select the Enable Fast User Switching option. Once you have fast user switching enabled, you’ll see either an icon or a name in your menubar, depending on what option you chose on the Login Options screen. Click on your name or icon in the menubar and select Login Window from the drop-down menu. The login window will appear. When you return to your Mac, login as you usually do. All your applications will be just as your left them—even your iTunes music will start up again where it stopped playing, even if that means mid-song.

But what if you don’t want to always lock your screen when the screen saver activates or your computer wakes from sleep? In other words, you don’t want to set that option in the Security pane as you must for the methods I’ve described so far. (After all, it can be a pain to have to enter your password over and over again throughout the day.) Keychain Access holds the key. You can use this application (in your Applications/Utilities folder) to quickly activate your screen saver from the menubar and require a password to turn it off—even if the Security pane option isn’t enabled. Open Keychain Access and then go to Keychain Access: Preferences. Click on the General tab and select the Show Status in Menu Bar option. A small lock icon will appear in your menu bar. Close the Preferences window and quit Keychain Access. Now click the lock icon in your menubar and choose Lock Screen to start your screen saver. You can even define a keyboard shortcut for the Keychain Access Lock Screen menu. First make sure the Lock Screen icon is the leftmost of your Apple-provided menubar icons. Hold down the Command key and drag the lock icon to the left edge of your existing icons, then drop the icon. This step is necessary to make this trick work. Now open System Preferences, and click on the Keyboard & Mouse pane. Click the Keyboard Shortcuts tab, and then click the plus sign to add a shortcut. Leave the pop-up menu set to All Applications, enter Lock Screen for the Menu Title, and then define a keyboard shortcut to use:



Here I’ve defined Shift-Control-F8 as my lock screen shortcut; this may seem an odd combo, but here’s why—since this is a keyboard shortcut for a menubar icon, it only works when that area of the menubar is active (“has focus”). Luckily, one of OS X’s pre-defined keyboard shortcuts is Control-F8, which moves the focus to the status menu area of the menubar, where the lock icon appears. When you hit Control-F8, the leftmost menubar status item will activate; that’s why this trick only works with the lock icon in the leftmost position. And since Control-F8 is the status menu activation key, I assigned a very similar keystroke for the Lock Screen function. So to lock the screen, hit Control-F8, and then Shift-Control-F8. Presto, the screen saver will activate! Note that this may not work for everyone, depending on what other menu extras you have installed. You should always be able to hit Control-F8 and use the arrow keys, however, which is almost as easy and still mouse-less.

I’m sure I’ve missed at least a few additional methods of quickly locking your screen; if you have one, please share it in the comments.

Tuesday, January 25, 2011

A Rails Feature You Should be Using: with_scope

A Rails Feature You Should be Using: with_scope
Posted by ryan at 5:48 AM on Thursday, July 20, 2006
The with_scope method provided by ActiveRecord has been talked about before (see the resources section below), but I don’t feel like people recognize what a great utility it is. Maybe awareness will increase with more tools that make use of this feature, like the MeantimeFilter by Roman, or just more public conversation about it. Add the following to the latter category:

with_scope lets you bind a block of code operating on an active record model to a particular subset of that model’s collection. For instance, using the standard blog application example, if I have a controller method that performs a series of operations on a single user’s articles I would need to pass in the user id condition on every operation:


def create_avoid_dups

user_id = current_user.id
# Find all user's posts
user_posts = Post.find(:all, :conditions => ["user_id = ?", user_id])

# Do some logic looking for dups in user_posts
...

# then create new
@post = Post.create(:body => params[:body], :user_id => user_id)

end

Notice we had to pass in the user_id on both the find and create method. with_scope lets us extract that parameter so the core operations aren’t obscured by excessive parameters:


def create_avoid_dups

Post.with_scope(:find => {:conditions => "user_id = #{current_user.id}"},
:create => {:user_id => current_user.id}) do

# Find all user's posts
# No longer need user_id condition since we're in scope
user_posts = Post.find(:all)

# Do some logic looking for dups in user_posts
...

# then create new without specifying user_id
@post = Post.create(:body => params[:body])
@post.user_id #=> user_id

end
end

with_scope allowed us to specify conditions of the Post that would apply throughout the course of the block (conditions specified by operation, in this case :find and :create)

Contrived examples such as this one don’t do a great job of showcasing how useful this method is – but imagine never having to specify the user_id in any controller method because it’s been automatically scoped to that user for you. That’s exactly what the previously mentioned meantime filter does.

So don’t be shy. If you find yourself writing code that applies to a known subset of items, scope it with with_scope. All the cool kids are doing it.

JavaScript Testing with Cucumber and Capybara

(ref: http://openmonkey.com/articles/2010/04/javascript-testing-with-cucumber-capybara)

JavaScript Testing with Cucumber and Capybara

Tim Riley 2010.04.09

Capybara is a Ruby DSL for easily writing integration tests for Rack applications. It is an alternative to Webrat and can easily replace it as a backend for your Cucumber features. Its power and utility lies in that it comes bundled with several different browser simulators, equipping you with a flexible toolkit for testing all parts of your application, from the simplest page to the most complex, JavaScript-heavy page.


Cucumber Tutorial - Tagging

After a few months, scenarios and features in a project can become unwieldy. With hundreds of feature files, we might not want to run everything all the time. Even without that, it is often useful to be able to check just a subset of the features, for example skip running one or two very slow tests to get faster feedback.

Cucumber allows us to manage large groups of scenarios easier with tags. Tags are free-form text comments assigned to a scenario, that provide meta-data about the type, nature or group of that scenario. You can assign tags to a scenario by putting it before the scenario header, adding an ‘at’ sign (@) before the tag name.

For example:
 
 @slowtest 
 Scenario: end-to-end signup 
 ... 

You can execute only the scenarios with a particular tag using the --tags option, for example: 
cucumber --tags @fasttest 

Another option is to skip only the scenarios without a particular tag. For 
that, prefix the tag with a tilde (~). Here is an example: 
cucumber --tags ~@slowtest 

Wednesday, January 19, 2011

Rails installation

install specific version of rails

sudo gem install -v=2.2.2 rails --include-dependencies

Installing Ruby on Rails on Mac OS X

Installing Ruby on Rails on Mac OS X

Leopard (10.5)

Ruby (1.8.6), Ruby Gems (1.0.1) and Rails (1.2.3) ship with the latest version of OS X (10.5, Leopard).
You may want to upgrade Rails to take advantage of the latest and greatest improvements. The version of Rails that ships with OS X is rather old (in Rails development terms).
You should also make sure you have xCode tools installed from your OSX disk, since you will likely run into the need to compile applications during your run with Rails.
To accomplish this, you should first update Ruby Gems.
$ sudo gem install rubygems-update
$ sudo update_rubygems
Then a simple re-installation of Rails will get you to the latest release.
$ sudo gem update
$ sudo gem update --system
$ sudo gem install rails

Tiger (10.4) and Panther (10.3)

RubyOSX v1.2 is a one click installer that will install Ruby 1.8.6, RubyGems 0.9.4, Mongrel 1.0.1 and SQLite 3.4.0. for Tiger (10.4) and Panther (10.3). It however, will not install Rails. After installing RubyOSX you should first update Ruby Gems.
$ sudo gem install rubygems-update
$ sudo update_rubygems
Then a simple installation of Rails will get you to the latest release.
$ sudo gem install rails

Alternative Method for Leopard (10.5) and Tiger (10.4)

Bitnami RubyStack

Using Bitnami RubyStack with FiveRuns TuneUp BitNami RubyStack (1.4-beta-1) ships with the following:
  1. Ruby 1.8.6 and 1.9.0 developer branch
  2. RubyGems 1.2.0
  3. Rails 2.1.1
  4. ImageMagick 6.3.6
  5. Subversion 1.4.6
  6. SQLite 3.5.1
  7. MySQL 5.0.51a
  8. Apache 2.2.8
  9. Mongrel Web Server 1.1.5
  10. Capistrano 2.5
  11. PHP 5.2.6
  12. phpMyAdmin 2.11.2.2
  13. Git 1.5.5
  14. Nginx 0.6.30
Note: Bitnami RubyStack will not support Passenger, as it does not support Windows platform
 
If you select Developer version then you will get a choice of installing PhpMyAdmin. In developer version Apache isn't preconfigured 
If you select Production version then Bitnami will install Mongrel Cluster, with Apache preconfigured to act as a load balancer for Mongrel  
Enter MySql 'root' password, port for Apache webserver, OpenSSL and Mysql     
Bitnami will install 3 databases, Developer, Production and Test databases. Here you should add a user that will have rights to these databases.  
All Done.

Macports

You may wish to use MacPorts to install Rails if you are using Leopard (10.5), Tiger (10.4) or Panther (10.3).
First you should Download the latest version of Xcode Tools if you have the currently shipping release of Mac OS X. Without Xcode Macports will not work
Download and install the appropriate version of Macports from MacPorts install page. MacPorts can be installed with a .dmg image using a familiar step-by-step installer. This will take quite some time to install, so be patient, it downloads the entire ports repository during “Finishing Installation” phase.

Command Line option

Open the Terminal application and type the following command, followed by the return key. It will most likely prompt you to enter your password.
$ sudo port install rb-rails
or type in the complete path like so
$ sudo /opt/local/bin/port install rb-rails
This will install the required rails dependencies like Ruby, RubyGems, Rake and SQLite. This will also take a very long time, depending on your computer speed, since everything is compiled from source.
That's it! Now you're ready to create new Rails applications.
If you want you can also install Mongrel, Capistrano, Mysql and Rspec using the following command.
$ sudo port install rb-capistrano rb-mongrel_cluster rb-mysql rb-rspec-rails

GUI option

Install Porticus, which is a gui for Macports Package Manager. Search for rails in the search bar in the top right corner 
Select rb-rails and click the install icon on the top right corner. It will install all the required dependencies. All done, rails is now installed. If you want you can also install rb-mongrel_cluster. Although its recommended to use Passenger with apache.

( http://wiki.rubyonrails.org/getting-started/installation/mac)