Blog

Jun
17

Adding your own login method to Authlogic

Posted by Michael Cindric in development, gems, open source, ruby on rails, security | 12 Comments »

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

Jun
15

Cool colours and GitHub branch in Terminal

Posted by Michael Cindric in development, mac osx | No Comments »

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.

picture 4 Cool colours and GitHub branch in Terminal

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

Jun
10

Formatting Paypal dates

Posted by Michael Cindric in development, ruby on rails | No Comments »

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.

Jun
05

“Open in TextMate” from Leopard Finder

Posted by Michael Cindric in development, mac osx, open source, plugins | No Comments »

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.