Adding your own login method to Authlogic
So on a new project we are working on we have a need for a user to be able to login via either their “Login” or “Mobile” number. Now we are using the Authlogic gem which is a great gem and comes with all default methods for login etc and allows you to customise this very easily.
So firstly you need to add the following to your UserSession model. What this does is overwrite the default login method with the one we are defining below called “find_by_username_or_mobile”.
class UserSession < Authlogic::Session::Base find_by_login_method :find_by_username_or_mobile end
Then in your user model its as simply as creating the class method for login. Now of course the password is still apart of the login process but we only wanted to allow users to either login via their login or mobile so no need to change the password methods.
class User < ActiveRecord::Base
def self.find_by_username_or_mobile(login)
find_by_login(login) || find_by_mobile(login)
end
end
So give it a try and let us know how you go hope this helps
Cool colours and GitHub branch in Terminal
There are many posts out there about making terminal have nice colours and getting it to display your projects GitHub branch. So l thought l would post mine
Here is what my terminal looks like.

Yes l am using Ryan Bates railscasts as my sample project (Thanks Ryan). As you can see it has the username@machine and then if you are in a project running git is also has the branch.
All you need to do it add the following to either your bash_login or bash_profile depending on what you are using. So first you run
mate ~/.bash_login
I am using textmate to edit this file. Once there simple add the following
export CLICOLOR=1
export TERM=xterm-color
export LSCOLORS=gxfxcxdxbxegedabagacad # cyan directories
export PS1="\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;36m\]\w\[\033[00m\]\$ \`ruby -e \"print (%x{git branch 2> /dev/null}.grep(/^\*/).first || '').gsub(/^\* (.+)$/, '(\1) ')\"\`\[\033[37m\]$\[\033[00m\] "
Save the file and reload your bash like so and you are good to go enjoy.
. ~/.bash_login
Of course you can change the colour of the font but ill leave that to you
Formatting Paypal dates
We have been doing a some work on PayPal integration for one of our clients for their soon to be released application and l needed to reformat the date PayPal was sending me. Now l know that Active Merchant could help with this but it didn’t fit our application.
So any ways PayPal sends you a date like so “03:25:17 Apr 26, 2007 PDT” and l need it in a clean format so here is what l came up with, its a first run and l am sure l will change it when the time comes to refactor but for now it works.
def self.date_from_paypal(datetime_string)
datetime_string =~ /(d+):(d+):(d+) (.+) (d+), (d+)/
parseable_string = "#$6-#$4-#$5T#$1:#$2:#$3"
DateTime.parse(parseable_string)
end
%w(payment_date).each do |attrib|
module_eval %{
def #{attrib}=(datetime_string)
write_paypal_date :#{attrib}, datetime_string
end
}
end
private
def write_paypal_date(attribute, datetime_string)
write_attribute attribute, PaypalTransaction.date_from_paypal(datetime_string)
end
So what it does is when you go to set the “payment_date” field it runs the “write_paypal_date” method which formats the date for you a little cleaner. You can add other dates to this very simply by just adding them to the string array in the method above.
“Open in TextMate” from Leopard Finder
Henrik Nyh has written a great little plugin that allows you to open files in TextMate from finder. Now most developers are used to doing this via terminal but this is very helpful so check it out
So click here to view the Open in Textmate button.
Getting JQuery and Prototype to place nice
If you want to use JQuery in your rails application but still want prototype working as well there is a simple way to make sure they play nice and work side by side. All you have to do after you include your JQuery includes code is have the following
<script> var $j = jQuery.noConflict(); </script>
Now all you have to do to use jquery is use “$j” instead of just “$” and your good to go. To use just the standard prototype library is still just “$”. Simple isn’t it
Sentia rails application templates now on GitHub
We have just made a project on github to store our rails application templates that we talked about here. You can check it out here. Enjoy
Application templates in Ruby on Rails
Here is the latest application template we have put together. It removes all the usual items and adds the base gems that we use for all our applications. It also creates the files needed for deployment and runs “Capify” on the project, plus a few other things such as sass etc.
# Remove unnecessary Rails files run 'rm README' run 'rm public/index.html' run 'rm public/favicon.ico' run 'rm public/images/rails.png' run 'rm -f public/javascripts/*' # Download JQuery run "curl -s -L http://jqueryjs.googlecode.com/files/jquery-1.3.1.min.js > public/javascripts/jquery.js" run "curl -s -L http://jqueryjs.googlecode.com/svn/trunk/plugins/form/jquery.form.js > public/javascripts/jquery.form.js" #Configure required gems gem "haml", :version => "2.0.4" gem 'thoughtbot-shoulda', :lib => 'shoulda', :source => 'http://gems.github.com' gem "thoughtbot-factory_girl", :lib => "factory_girl", :source => "http://gems.github.com" #Create Sass directory run 'mkdir public/stylesheets/sass' #Capify and create production environment.rb run 'mkdir config/deploy' run 'touch config/deploy/production.rb' #Add UAT environment and settings file 'config/environments/uat.rb', <<-CODE # Settings specified here will take precedence over those in config/environment.rb # Code is not reloaded between requests. Server needs to be restarted. config.cache_classes = true # Log error messages when you accidentally call methods on nil. config.whiny_nils = true # Show full error reports and disable caching config.action_controller.consider_all_requests_local = false config.action_controller.perform_caching = true config.action_view.cache_template_loading = true # Don't care if the mailer can't send config.action_mailer.raise_delivery_errors = false CODE #Create gitignore file and setup base ignores run "touch tmp/.gitignore log/.gitignore vendor/.gitignore" file '.gitignore', <<-FILE .DS_Store log/*.log tmp/**/* db/*.sqlite3 public/stylesheets/*.css FILE # Set up git repository git :init git :add => '.' git :commit => "-a -m 'Initial commit'" # Success! puts "SUCCESS!"
Rails Junior Developer

Sentia is looking for a Junior Rails developer to join our team. You will be working out of our office on George Street in the heart of the Sydney CBD. You will have access to the latest technology and resources not to mention our foos ball table.
You will be working on some great projects with other senior developers where you will be given the chance to grow and develop your skills.
You will need to have the following skills and experience.
- A passion for development
- 6+ months experience in rails
- TDD experience
- Git and Svn
- Good HTML/CSS skills
- Javascript
- Good communication skills
- Keen to learn with a great attitude
Experience with agile methodologies and design skills a bonus.
So if you think you have what it takes email us at jobs@sentia.com.au>
No recruiters
We have moved
Finally we have the keys to the new office, if only telstra had got their act together we would have our phone connected as well. Ill post some pics once we are settled a little more
Open source flash charts like google analytics
We all want our charts to look like the ones in google analytics. Well know there is a way the “Open Flash Chart” project. The guys over at pull monkey have released a plugin for rails that helps you get in these great charts. Ill be testing this plugin very soon and ill put up my experience and a little walk through. In the mean time if you want to check out the project have a look here
Here is an example of what the charts look like
