Categories
Programming

Rake tasks

How can I search for available rake tasks?

rake -T | grep gem

(where “gem” is the thing you are searching for)

The above example will return all of the rake tasks relating to gems:

rake gems # List the gems that this rails application depends on
rake gems:build # Build any native extensions for unpacked gems
rake gems:install # Installs all required gems.
rake gems:refresh_specs # Regenerate gem specifications in correct format.
rake gems:unpack # Unpacks all required gems into vendor/gems.
rake gems:unpack:dependencies # Unpacks all required gems and their dependencies into vendor/gems.
rake rails:freeze:gems # Lock this application to the current gems (by unpacking them into vendor/rails)
rake rails:unfreeze # Unlock this application from freeze of gems or edge and return to a fluid use of system gems
Categories
Programming

Working with gems

Gems are installed by sources, or a repository which holds several gems. Generally all the gems you will need are either in gems.rubyforge.org or gems.github.com, so once you setup these two sources you don’t need to setup more sources until you need a gem from somewhere else.

How can I see the sources my gem is using?

gem sources

How can I add a source?

gem sources –add http://gems.github.com

How can I see the gems which are installed on my system?

gem list

What if I want to search for gems by name?

gem list -r | grep somegem

(where “somegem” is the name of the thing you’re looking for)

How can I see some information about my gem environment?

gem environment

How do I install all the gems needed for the app I’m working on?

rake gems:install
Categories
Programming

MySQL Dumping Cheat-Sheet

Make a quick dump of a mysql database:

mysql_dump db_name > backup-file.sql

You can then read that dump file back into the server like this

mysql db_name < backup-file.sql

or like this:

mysql -e “source /path/to/backup/backup-file.sql” db_name
Categories
Programming

List of Ruby Operands

operand what it does
<< append (in context of strings, arrays, IO); shift position of bits in Bignum and Fixnum
>> shift position of bits in Bignum and Fixnum
~ bitwise NOT (high-presendence)
& bitwise AND (medium-presendence)
| bitwise OR (medium-presendence)
^ bitwise XOR (medium-presendence)
** exponent
+ addition
subtraction
* multiplication
/ division
% modulo operator
Categories
Programming

Tips for Working with Rails

  1. Go to script/console to open ruby in interactive mode. script/console loads your environments (everything in environment.rb and all your plugin and gem initializers), so you basically have access to all your models right from interactive ruby mode (IRB).
  2. Remember use of the p (or its alias puts) command to print stuff to the output buffer. This is useful for testing in IRB mode, inside of a test, or other places where you have an output buffer. If you’re running mogrel, your output goes to the terminal. If you’re running Passenger with Apache, check your apache error_log for the output.
  3. Puts is not to be confused with logger.debug which will log the output of whatever you pass it to your log/development.log file. To see the log file as your are running, use this:
    tail -f log/development.log

    (The -f flag keeps the tail process running and will show you anything that is written to your log file as it is written.)

    You can see lots of info in the development log of course: all the actions that Rails is trying to render as well as all the SQL it is executing. Like I said above, use logger.debug to explicitly send stuff you want to see to this log.

    This only works in controllers:

    logger.debug “the value of myVar is #{myVar}”

    But this works everywhere:

    RAILS_DEFAULT_LOGGER.info
  4. Don’t forget your <em>explicit conversions</em>. These will work on most objects (where it makes sense) and will convert the variable to something that can be read easily:
    to_s  to string
    to_i  to integer
    to_f  to float
    to_a  to array
  5. Use the string concatenator (plus sign), like so:
    puts “myVariable is ” + myVariable.to_s
    logger.debug “the value of myVar is” + myVar
  6. Use inspect or dump to look the contents of a variable command as in
    myVar.inspect
    myVar.dump

    (You might want to try this):

    p myVar.inspect
  7. The inspect command often will output a long object (as in, lots of output). If you see \n markers (newline) in your output and it all runs together on one line, try “y” before the object to make it more readable. This also shows it in tree-like mode which helps you to see the objects within arrays and stuff. This works well in IRB:

    y myVar

  8. Keep in mind that you can do this stuff inside your tests too. (puts output will be printed by the test runner.) For functional testing, remember the “four Hashes of the apocalypse” – hashes which contain interesting information after your tests have run. Interesting enough to perform asserts against:

      * assigns Any objects that are stored as instance variables in actions for use in views.
      * cookies Any cookies that are set.
      * flash Any objects living in the flash.
      * session Any object living in session variables.

    Also, you can look at the response you’re getting with @response. Also keep in mind:

      * @controller The controller processing the request
      * @request The request
      * @response The response

    I like to use inspect on these, and surround them with big markers so that I can see them clearly when I’m running my test.

    puts “===== RESPONSE====== ”
    puts @response.inspect
    puts “==================== ”
Categories
Programming

Passenger with Rails

Passenger is a great thing because it makes Rails apps able to be deployed on Apache.

The installation is pretty painless, but there are a few gotchas you may want to know about.

Start here: http://www.modrails.com/install.html

sudo gem install passenger

The installation will prompt you to add this to your httpd.conf file: (note the version number of passenger might be different)

LoadModule passenger_module /Library/Ruby/Gems/1.8/gems/passenger-2.1.3/ext/apache2/mod_passenger.so
PassengerRoot /Library/Ruby/Gems/1.8/gems/passenger-2.1.3
PassengerRuby /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby

If you are NOT using the Passenger preference pane, follow these instructions to add a virtual host to your Apache configuration file, and set its DocumentRoot to /somewhere/public. If you ARE using the passenger preference pane, you don’t need to do this.


RailsEnv development
ServerName mysite.local
DocumentRoot /Users/jason/DEVELOPMENT/mysite/public

Options FollowSymLinks
Order allow,deny
Allow from all

======================================================
SOME COMMON PROBLEMS
======================================================
SYMPTOM:
When first installing passenger (sudo gem install passenger), I get this error:
Building native extentions. This could take a while…
ERROR: Error installing passenger:
ERROR: Failed to build gem native extensions

FIX: Type ‘make’ at the command prompt. If you get the error “make: command not found,” then you don’t have ‘make’ (gcc compiler) installed, so your system can’t build anything. Install XCode Tools to solve this.

======================================================
SYMPTOM: Site loads but it does not go to your rails app correctly.

FIX: Check to make sure apache config file, or the file you included in apache config, is correct. Try putting your site at a different port like this:

Listen 8000

Remember to restart apache each time you make a change with:
sudo apachectl restart

======================================================
SYMTPOM: Browser says “500 Internal Server Error”

FIX: Check the Apache error log (Applications > Utilities > Console, click “Show Log List” if the list of logs does not appear, go to /var/log/apache2/error_log )

======================================================
SYMPTOM: *** Passenger ERROR:Could not spawn the sever the server… Permission denied

FIX: If you are running a custom ruby executable (see custom ruby executable with passenger), you may not have set your custo ruby executable file to have execute permissions.

======================================================
SYMPTOM: Routing error … no route for /YourApp

FIX: If you see this in your browser (where /YourApp is the name of your app), it is probably because you did not have this in your Rails config/environment.rb file:

Rails::Initializer.run do |config|

config.action_controller.relative_url_root = “/YourApp”
end
======================================================

SYMPTOM: Ruby on rails version ‘2.3.1’ not found
(or some other version)

FIX: Upgrade rails (or install the needed Rails version, more than one can run at once):
sudo gem install rails –version 2.3.1

======================================================

SYMPTOM: Apache does not respond in the browser.

FIX: Make sure that apache is started ok, try this to see if there are any errors in your httpd.conf file:
sudo apachectl configtest

======================================================

Categories
Programming

Show all hidden files in the Finder windows

Type this in terminal:

defaults write com.apple.Finder AppleShowAllFiles TRUE
KillAll Finder

To hide the hidden files again:

defaults write com.apple.Finder AppleShowAllFiles FALSE
KillAll Finder
Categories
Programming

SVN cheat sheet

Setup the working copy of the repository
svn checkout

First navigate to the root of your working copy.

svn status

This will list any files you’ve added, deleted or modified.

svn add
for any files you are adding to the repository

svn status
to make sure all files are added (no ?s next to name)

svn commit -m ‘
The changes will commit to the repository.

svn update
will update your working copy with changes others have made