Blog

Jun
17

Adding your own login method to Authlogic

Posted by Michael Cindric in development, gems, open source, ruby on rails, security | 9 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

May
08

Application templates in Ruby on Rails

Posted by Michael Cindric in development, javascript, jquery, ruby on rails | 3 Comments »

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!"
Mar
18

Ruby on Rails 2.3 Searchable API Doc

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

A developer by the name of Vladimir Kolesnikov has put together a nice little searchable API doc for rails. Its been updated for rails 2.3.2 (Latest Version) and you can either download it or browse online. Currently its been tested with Safari 4, Firefox 3, Opera 9.5 Internet Explorer is currently unsupported but who uses IE anyways.

You can download it here

Mar
17

Rails 2.3 is out

Posted by Michael Cindric in development, mac osx, ruby on rails | 1 Comment »

Finally Rails 2.3 is out. Officially its tagged as 2.3.2 from memory. So what are you waiting for update already. Just run the following command in your terminal and your on your way. Sorry windows users find your own way

 sudo gem install rails

Check out the release notes here

Mar
11

Please accept the terms of service and privacy policy

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

If you need users the accept the terms of services and privcay policy or your rails application there is a simple way to handle this without a database field or javascript. It’s using the validation methods built into rails. Simply add the following code to your User model for example

validates_acceptance_of :terms_of_service

Then in your view add the following to your view and you will get a checkbox on the page that users will be required to check.

<%= check_box :user, :terms_of_service %>

This will then add this validation rule to your models validation rules and when you try to submit the form without checking the checkbox you get a nice little validation message letting you know hey you need to accept this.

Of course you can have your own validation message displayed like so

validates_acceptance_of   :terms_of_service, :message => ' and the privacy policy must be accepted'