Numbers for Humans – Humanize for Numeric

number_with_precision(result.round(2)) — BE GONE!

Usage
1.humanize == “1”
1000000.humanize == “1.000.000”
1000.12345.humanize == “1.000,12”

Install

#config/initializers/numeric_humanize.rb
class Numeric
  def humanize(rounding=2,delimiter=',',separator='.')
    value = respond_to?(:round_with_precision) ? round(rounding) : self
    
    #see number with delimeter
    parts = value.to_s.split('.')
    parts[0].gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{delimiter}")
    parts.join separator
  end
end

Individual Items for Paypal Express with Active Merchant

As far as documentation goes , only a single “order name” is possible. But i want to show each order item to the customer, so he can double-check them.

Item listing

Item listing

payment_service_for ...see documentation... do |service|
  #enable item_name_1 attributes
  service.add_field 'cmd','_cart'
  service.add_field 'upload',1

  #add individual items
  @order.items.each do |item|
    num+=1
    [["item_name_#{num}",item.name],["amount_#{num}",item.price]].each do |name,value|
      service.add_field name,value
    end
  end
  ...add additional information...
end

Capistrano Recipe to Install or Update Passenger

Update:

  • the basic installation can be done via “passenger-install-apache2-module –auto”
  • a better script that does the whole job can be found in cap-recipes

Took me some minutes to figure this out, so here for your laziness-pleasure (for Ubuntu):

Usage
To install passenger or update to the newest release:
cap install_passenger

Install

#deploy.rb
  desc "Install Passenger"
  task :install_passenger do
    install_passenger_module
    config_passenger
  end

  desc "Install Passenger Module"
  task :install_passenger_module do
    sudo "gem install passenger --no-ri --no-rdoc"
    input = ''
    run "sudo passenger-install-apache2-module" do |ch,stream,out|
      next if out.chomp == input.chomp || out.chomp == ''
      print out
      ch.send_data(input = $stdin.gets) if out =~ /(Enter|ENTER)/
    end
  end

  desc "Configure Passenger"
  task :config_passenger do
    version = 'ERROR'#default
    #passenger (2.0.3, 1.0.5)
    run("gem list | grep passenger") do |ch, stream, data|
      version = data.sub(/passenger \(([^,]+).*/,"\\1").strip
    end

    puts "    passenger version #{version} configured"

    passenger_config =<<-EOF
      LoadModule passenger_module /usr/lib/ruby/gems/1.8/gems/passenger-#{version}/ext/apache2/mod_passenger.so
      PassengerRoot /usr/lib/ruby/gems/1.8/gems/passenger-#{version}
      PassengerRuby /usr/bin/ruby1.8
    EOF

    put passenger_config, "src/passenger"
    sudo "mv src/passenger /etc/apache2/conf.d/passenger"
  end

Source
Basic recipe

DRY Validation Testing and easy edge-case Records

I just pushed my latest changes and additions for the old asser_invalid_attributes to github.

  • create single valid record
  • edge-case records without fixtures
  • valid attributes to test a post or fill a form

README

Install

script/plugin install git://github.com/grosser/valid_attributes.git

Usage
  • set of valid attributes:
    valid_attributes User
  • a valid Record(new):
    valid User
  • a valid saved Record:
    create_valid User
  • an edge-case User:
    valid User, :name=>’oh noo it is too long’