Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Dr Nic Williams http://drnicwilliams.com

« Newer Snippets
Older Snippets »
Showing 11-15 of 15 total

Reset user password based on another users

When you first create an app you might not have time *cough* to add special user password reset features. Especially if you don't have many users.

Here's a quick script to set the password of a user to another user (say a manually created user 'tester' with a known password).

namespace :user do
  desc 'Reset user (USER=username) password to that of username "tester" (or FROM=username env)'
  task :reset => :environment do
    reset_username = ENV['USER']
    unless ENV['USER']
      puts 'Require "USER=username" for user to be reset'
      next
    end
    reset_user     = User.find_by_username reset_username
    unless reset_user
      puts "Cannot find user: #{reset_username}"
      next
    end
    from_username  = ENV['FROM'] || 'tester'
    from_user      = User.find_by_username from_username
    unless from_user
      puts "Cannot find user: #{from_username}"
      next
    end
    
    reset_user.crypted_password = from_user.crypted_password
    reset_user.salt             = from_user.salt
    reset_user  .save
    puts 'User password reset'
  end
end

Turn numbers into strings with thousands' separators

Turn numbers into strings with thousands' separators (from zenspider)

class Numeric
  def commify(dec='.', sep=',')
    num = to_s.sub(/\./, dec)
    dec = Regexp.escape dec
    num.reverse.gsub(/(\d\d\d)(?=\d)(?!\d*#{dec})/, "\\1#{sep}").reverse
  end
end


So, you get:

1233232423424.23423.commify # => "1,233,232,423,424.23"

Capistrano logs

To get the last n lines from a remote log file, use this task

Usage: cap log [-s lines=100] [-s rails_env=production]
So, "cap log" returns 100 lines from production.

desc "Returns last lines of log file. Usage: cap log [-s lines=100] [-s rails_env=production]"
task :log do
  lines     = configuration.variables[:lines] || 100
  rails_env = configuration.variables[:rails_env] || 'production'
  run "tail -n #{lines} #{app_dir}/log/#{rails_env}.log" do |ch, stream, out|
    puts out
  end
end

Migrate rhtml views into erb views within subversion repository

From Brandon Keepers:

To migrate rhtml views into erb views within subversion repository:

Dir.glob('app/views/**/*.rhtml').each do |file|
  puts `svn mv #{file} #{file.gsub(/\.rhtml$/, '.erb')}`
end

Create classes at runtime

Dr Nic's Magic Models adds automatic ActiveRecord generation if you haven't defined a model, and for all ActiveRecords it can generate associations and validations on-the-fly based on the database schema (the table and column definitions).

It does this with some interesting Ruby meta-programming.

For example, if you want to create a new class at runtime, and assign it a superclass, try the following:

  def create_class(class_name, superclass, &block)
    klass = Class.new superclass, &block
    Object.const_set class_name, klass
  end


With this code, you can create a class as follows:

create_class('Person', ActiveRecord::Base) do
  set_table_name :people

  def fullname
    "#{firstname} #{lastname}" # assuming the people table has firstname,lastname columns
  end
end

« Newer Snippets
Older Snippets »
Showing 11-15 of 15 total