All Your Records As CSV, fast and generic

Our admins are always data-greedy, so i provided a method that lets them get detailed data on any search result they like. Be careful to not let normal users access this (hide email addresses etc).

Installation

  • Provide a current_model method that returns which model the controller is working on.
  • Store the objects you want to render in @current_objects.
  • add this block to every index action response (in a DRY way…)

Usage

  • Visit /xxx/anything.csv to get all data in csv
format.csv do
  #collect data
  keys = current_model.new.attributes.keys.sort
  csv_string = FasterCSV.generate do |csv|
    csv << keys
    @current_objects.each do |record|
      csv << record.attributes.sort_by{|k,v|k}.map{|arr|arr[1]}
    end
  end

  #send data
  filename = current_model.to_s.downcase.gsub(/[^0-9a-z]/, "_") + ".csv"
  send_data(csv_string,
    :type => 'text/csv; charset=utf-8; header=present',
    :filename => filename
  )
end

Automatic Lossless Reduction Of All Your Websites Images

NOTE: If you are on windows you can leave now.

  • LOSSLESS size reduction (10-97% size reduction) in the cloud
  • optmizes all images(jpg+png) from a given folder

Install:
sudo gem install smusher
#if you do not have get ruby + rubygems first
See Install instructions

Usage:
smusher /my_app/public/images

smusher LOCAL_FOLDER or IMAGE

Output:
smushing /my_app/public/images/cart_small.png
3322 -> 537                              = 16%

smushing /my_app/public/images/pro_icon.png
3022 -> 260                              = 8%

smushing /my_app/public/images/message.png
2898 -> 138                              = 4%

smushing /my_app/public/images/logo.png
6799 -> 9677                             = 142%
reverted!

Protection
As you see images that get larger or empty or cannot be processed are reverted.

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