Blog

Jun
17

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

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.

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
29

Using Javascript Code for RJS Instead of IDs

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

UPDATE: JavascriptGenerator has a method name literal that will do the exact same thing, so the code below should do the trick:

 page.insert_html :bottom, page.literal("$$('p.welcome b').first()"), "Some item"

Bill Burhcam over at Meme Rocket made a post that *almost* solved a problem for me the other day. We’re currently doing some work that ends up creating html. Pretty standard stuff, but for a few reasons that aren’t really worth going into we can’t easily rely on our element ids being unique. The prototype functions ‘up’ and ‘down’ are perfect for this situation though.

So our problem basically boils down to this – RJS converts this code:

page.insert_html :bottom, "$$('p.welcome b').first()", "Some item"

into this javascript:

 new Insertion.Bottom("$$('p.welcome b').first()", "Some item"

Because the $$(’p…’) is in quotes, Insertion.Bottom just looks for an element with that id. Which isn’t what we really wanted.

After digging around in the ActionView source for a while I found JavascriptGenerator::GeneratorMethods and its javascript_object_for method. It turns out that if you pass in a ActiveSupport::JSON::Variable then insert_html will behave how we want. So for the example above, the code below will work as expected.

 js_literal = "$$('p.welcome b').first()" js_literal = ActiveSupport::JSON::Variable.new(js_literal) page.insert_html :bottom, js_literal, "Some item"

will output:

 new Insertion.Bottom($$('p.welcome b').first(), "Some item"

Thank you open source.