The blog has moved to http://jessehouse.com/ ... Many google searches point here so I am leaving it operational, but there will be no new posts.

Wednesday, December 19, 2012

rails :remote => true jQuery events

I constantly find myself looking these up, saving this link for future reference

https://github.com/rails/jquery-ujs/wiki/ajax

contains all of the rails custom jQuery events for remote forms, such as "ajax:success", "ajax:before"; also includes the method signatures.


Sunday, December 16, 2012

Sending email with Rails Mailers, The Random bits...

See the rails guides and railscasts for getting started with Rails Mailers; 
this article includes some additional information on Rails Mailers not included in those articles.

RailsCasts: http://railscasts.com/episodes/206-action-mailer-in-rails-3
Rails Guides: "Action Mailer Basics" on the rails guides - The guide covers all of the common mailer tasks
  • setup and configuration
  • attachments
  • layouts

Random Bits
These examples were created using rails 3.2
Add importance headers

# importance flag on the email
headers['Importance'] = 'high'
headers['X-Priority'] = '1'
headers['X-MSMail-Priority'] = 'High'
NOTE: many email clients will ignore these headers

Add receipt header

# require email receipt
headers['Return-Receipt-To'] = 'confirm@example.com'
NOTE: many email clients will ignore these headers

Testing mailer headers with rspec

If you have rspec-rails gem installed with your rails application then running the generator will stub out most of your mailer tests, including a fixture file for the mailer view, which you may or may not want to remove.

mail.header['X-MSMail-Priority'].value.should == 'High'
NOTE: test against mail.header, not mail.headers

Use a different layout for some Mailer actions

our default layout is the customer facing one but we want to override it in some cases

class ExampleMailer < ActionMailer::Base
default from: "from@example.com"
# default layout for ExampleMailer
layout "customer_email"
# the order ready is sent to the customer and uses the default layout - customer_email
def order_ready
@greeting = "Hi"
mail to: "to@example.org"
end
# the ready to ship email is internal only - use the internal layout
def ready_to_ship
@greeting = "Hi Hi"
mail(to: "internal@example.org") do |format|
format.html { render :layout => "internal_email" }
format.text { render :layout => "internal_email" }
end
end
end



Saturday, November 3, 2012

Setting up a C.I. build (Continuous Integration) for a Ruby on Rails application


It has never been easier than with Semaphore - Hosted Continuous Integration


How easy is it

  • add semaphore ssh key to your github repo
  • then semaphore automatically determines your ruby version, database, etc...
  • you can tweak the build steps later, the defaults are pretty good

It will automatically detect new branches!!! That means zero setup when the new branch is added



It is not cheap, for personal side projects it might not make sense?
But well worth the money if you are doing paid development

Git Log Formatting

The git log command is great and has tons of good information, but the output doesn't give you that nice 'overview' of the commit history, too much detail on too many lines

I've been using the following in my .gitconfig file
l = log -20 --format='%h %ad %d %an: %s' --date=short
typing 'git l' renders the following output:



Last night I watched the Destroy All Software "PRETTY GIT LOGS" screencast which takes 'git l' to the next level

now 'git l' renders


   

Wednesday, October 31, 2012

Helper script to delete git tags locally and remote

Might be useful to others, delete a bunch of git tags - remote and locally


### matches all git tags that start with numeric value, update regex as needed
# commands to remove local tags
git tag -l | grep "^\d" | sed 's/^/tag -d /' > _remove_tags
# commands to remove remote tags
git tag -l | grep "^\d" | sed 's/^/push origin :refs\/tags\//' > _remove_tags_remote
# review contents of file
cat _remove_tags | while read line; do echo $line; done
# execute each line in file
cat _remove_tags | while read line; do git $line; done
# review contents of file
cat _remove_tags_remote | while read line; do echo $line; done
# execute each line in file
cat _remove_tags_remote | while read line; do git $line; done

Resources

Wednesday, September 5, 2012

414 Request-URI Too Large


How to configure nginx to accept larger querystring requests

Increase the 'large_client_header_buffers'

This is located in the 'http' section of the configuration

Example:



Saturday, August 25, 2012

Unofficial Twitter Bootstrap Documentation

Unofficial Twitter Bootstrap Documentation
http://unofficial-twitter-bootstrap-documentation.com/

The current project I am working on is running bootstrap 2.0.2 and after the release of 2.1.0 the online documentation for 2.0.x was gone.

I went ahead and compiled the archive docs from the bootstrap github repo into a single site - hopefully others find it useful.

Sunday, July 1, 2012

Rails - ActiveRecord created_by and updated_by gem


Often your application will want to track who created and updated your data.
On a previous rails 2 project I used a plugin called userstamp which worked well, but until recently it did not support rails 3+

I didn't find any other gems out there at the time, so I put together the clerk gem,
you can install it as a gem in your rails 3+ applications by adding gem 'clerk' to your Gemfile, see the README on github for details

NOTE: the current version relies on your database tables having columns named created_by_id and updated_by_id and the gem does not support any custom configuration.



Sunday, April 29, 2012

postgres: random useful things

Run sql statements from the command line, use the -c flag
psql your_db -c "update users set is_active = 'f';"


create a random value
select md5(random()::text)


crazy updates using regexp_matches
-- changes joe.smith@gmail.com -> joe.smith@qa.com
update users set email = replace(replace(regexp_matches(email, '.*@')::varchar, '{', ''), '}', '') || 'qa.com';
-- there must be a better way to get rid of the {} chars returned by regexp_matches


postgres: terminate all database connections

Using psql from the command line you can terminate all connections to a database

# replace your_db with the name of your database
psql postgres -c "select pg_terminate_backend(procpid) from pg_stat_activity where datname='your_db';"
Comes in handy when you want to do things like restore your staging or development database

psql postgres -c "select pg_terminate_backend(procpid) from pg_stat_activity where datname='your_db';"
dropdb your_db
createdb --template=template0 --encoding=unicode your_db

Friday, February 3, 2012

Git Workflow

This is the current git workflow we are using at work, your mileage may vary:

Always start with
# get the latest code
git checkout master
git pull --rebase
# ==============
Setup to work on new branch / feature
You are going to be doing one of two things before adding new changes:
# 1. Connect to an existing remote branch
# run the following to get the names of all local and remote branches
git branch -la
# creating a new branch from the remote branch (with tracking)
# and checking it out locally
# replace NEW_BRANCH with the remote name
# example: remotes/origin/xyz_branch - use xyz_branch in place of NEW_BRANCH
git checkout --track -b NEW_BRANCH origin/NEW_BRANCH
# 2. Create a new branch with remote tracking
# from master - run the following
# we are creating a new branch and checking it out locally
git checkout -b NEW_BRANCH
# from NEW_BRANCH - run the following
# this will create the remote branch
git push -u origin NEW_BRANCH
# ==============
Daily Tasks
# do lots of work with commits on NEW_BRANCH
...
# typical commands used, be careful not to add unwanted files
git status
git add --all
git add FILE_PATH_FOR_SPECIFIC_FILE
git commit -m "COMMIT_MESSAGE"
NOTE: make sure all files are committed before preforming pull or push
# get code changes from other developers, from the branch
git pull --rebase
# push your commits to github for other developers
git push
# you may receive rejected messages on some branches
# that is ok if they are from branches other than your branch
# ==============
Merge feature branch into master
# merge new code into master in 1 clean commit - squash all branch commits
git checkout NEW_BRANCH
git pull --rebase
git checkout master
git pull --rebase
git merge --squash NEW_BRANCH
git status
git commit -m "feature NEW_BRANCH"
# push changes to the master branch on github
git push
# delete topic branch locally and then the remote (if desired)
git branch -d NEW_BRANCH
git push origin :NEW_BRANCH
# ==============
Long lived local branches
# if a long lived branch, pull in code from master using rebase
git checkout master
git pull --rebase
# add these changes to branch with rebase so the history stays clean
# do NOT do this if using a shared remote branch
git checkout NEW_BRANCH
git rebase master
WARNING: do not squash commits or rebase branches that have been sent to a remote repository, this should not be an issue for local only branches.
# ==============
Hot-fixes
# you fix a bug on the master branch
# and now you want it integrated into the current dev branch
git checkout NEW_BRANCH
git cherry-pick -x master
# this will pull in the last commit from the master branch
# NOTE: the commit ID (SHA) is different, this is really a copy of the commit
# Since it will get squashed merged back into master later it will look like no change
# You can also use a commit ID in-place of 'master' or specify ranges of commits
# ==============
Other useful commands
# interactive rebase to squash back to master
git checkout NEW_BRANCH
git rebase -i master
# create a remote branch with tracking from current branch
git push -u origin REMOTE_BRANCH
# delete remote branch (named REMOTE_BRANCH in this example)
# note there is nothing on left side of the colon
# this means push nothing to the remote branch origin/REMOTE_BRANCH
git push origin :REMOTE_BRANCH
# create a tag; 1620 in this case, it could contain any non-space characters, i.e. 1620-fix1
git tag -a 1620 -m "TAG_MESSAGE"
git show 1620
# push tags to remote
git push --tags
# summary of the last few commits
git log -16 --format='%h %ad %d %an: %s' --date=short
view raw git-workflow.rb hosted with ❤ by GitHub



Resources