Getting fast with rspec

guda | J November, 2009 | 8:03 pm

get the gem from here

http://github.com/timcharper/spork

 

How to fix the Missing error

http://chrisblunt.com/blog/2009/08/28/rails-configuring-rspec-for-spork/

 

Rails 2.3.2+, memcached and logger

guda | J August, 2009 | 1:43 pm

Finally our twitterized version of psspy.se is ready. But! When in production – read with many connection – the memcached get overloaded by not closed connections and on 1024 it says

“accept(): Too many open file” and memcached dies.

After 1 day investigating the problem  we have found that the following line in the initializers is causing the problem

RAILS_DEFAULT_LOGGER.level = Logger::DEBUG

We havent investigating deeper but when commenting this line everything works fine.

We have tested with Rails 2.3.2 and 2.3.3 with memcache-client-1.7.4

My Ubuntu workstation configuration

guda | J June, 2009 | 8:42 pm

XOrg configuration

  • https://wiki.ubuntu.com/X/Config/Resolution
  • cvt 1680 1050 75
  • xrandr –newmode …the output from cvt
    xrandr –newmode “1680×1050_75.00″ 187.00 1680 1800 1976 2272 1050 1053 1059 1099 -hsync +vsync
  • xrandr –addmode DVI-0 1680×1050_75.00

Lenovo Y510

You have to install the latest alsa.

Then replace the existing kernel modules with the just build one. Then run depmod -a as point kilbasar here

http://ubuntuforums.org/showthread.php?t=687663&page=2

sudo su
cd /lib/modules/2.6.22-14-generic/ubuntu/media/snd-hda-intel/
mv snd-hda-intel.ko snd-hda-intel.ko.bak
ln -s /lib/modules/2.6.22-14-generic/kernel/sound/pci/hda/snd-hda-intel.ko /lib/modules/2.6.22-14-generic/ubuntu/media/snd-hda-intel/snd-hda-intel.ko
cd /usr/src/alsa/alsa-driver-1.0.16rc2/modules
cp * /lib/modules/2.6.22-14-generic/kernel/sound/
depmod -a

 

Logitech Revolution MX

http://x4.6times7.org/dokuwiki/doku.php/devlog/blog/howto_logitech_mx_revolution_on_ubuntu

  • The mouse wheel control
    • Download from here http://goron.de/~froese/revoco/ or revoco-0.5.tar
    • make
    • sudo ./revoco manual=6
    • lets try to add it in ~/.xprofile next time
  • Here is my xorg.conf
Section "Monitor"
 Identifier "External DVI"
 # 1680x1050 74.89 Hz (CVT 1.76MA) hsync: 82.31 kHz; pclk: 187.00 MHz
 Modeline "1680x1050_75.00"  187.00  1680 1800 1976 2272  1050 1053 1059 1099 -hsync +vsync
 # 1680x1050 59.95 Hz (CVT 1.76MA) hsync: 65.29 kHz; pclk: 146.25 MHz
 Modeline "1680x1050_60.00"  146.25  1680 1784 1960 2240  1050 1053 1059 1089 -hsync +vsync
 # 1680x1050 69.88 Hz (CVT) hsync: 76.58 kHz; pclk: 174.00 MHz
 Modeline "1680x1050_70.00"  174.00  1680 1800 1976 2272  1050 1053 1059 1096 -hsync +vsync
 Option "PreferedMode"    "1280x1024_75.00"
EndSection

Section "Screen"
 Identifier    "Configured Screen Device"
 Device    "Configured Video Device"
 SubSection "Display"
 Depth    24
 Modes "1680x1050" "1024x768" "640x480"
 Virtual    2960 1050
 EndSubSection
EndSection

Section "Device"
 Identifier    "Configured Video Device"
 Option    "Monitor-DVI-0" "External DVI"
EndSection

My Development setup commands

I noticed simillar post here

sudo aptitude install mc vim subversion screen pidgin meld curl links2 vim-ruby vim-gnome vim-doc vim-scripts vim-ruby vim-perl \
subversion git-core bluefish kompozer\
libmysqlclient15off libmysqlclient15-dev mysql-common mysql-client \
libmysql-ruby1.8 irb ruby1.8 rdoc ri rake \
apt-utils build-essential ruby1.8-dev mysql-server memcached libxslt1-dev libpcre3-dev zlib1g-dev unzip gzip mytop openssl \
libopenssl-ruby libmagick9-dev imagemagick librmagick-ruby1.8 imagemagick librmagick-ruby-doc  libmagick9-dev ruby1.8-dev \
libdbd-sqlite3-ruby libsqlite3-ruby sqlite3 libsqlite3-dev

ln -s /usr/bin/ruby1.8 /usr/bin/ruby

mkdir ruby_install
cd ruby_install
wget http://rubyforge.org/frs/download.php/60718/rubygems-1.3.5.tgz
tar xzvf ruby*
cd rubygems-1.3.5
ruby setup.rb
cd ..
cd ..

ln -s /usr/bin/gem1.8 /usr/bin/gem

gem install rails mysql mongrel mongrel_cluster rails paperclip will_paginate andand \
json feed-normalizer hpricot mechanize ruby-debug uuid ruport ruport-util acts_as_reportable \
rufus-scheduler annotate openwferu-scheduler production_log_analyzer capistrano-ext libxml-ruby twitter capistrano wirble RedCloth
rspec-rails sqlite3-ruby \
--no-ri --no-rdoc

gem install rmagick -v 1.15.12
ruby -rrubygems -e "require 'RMagick'; puts Magick::Long_version;"

cat > /home/guda/.irbrc
require 'rubygems'
require 'wirble'
Wirble.init
Wirble.colorize
require 'logger'
if ENV.include?('RAILS_ENV') && !Object.const_defined?('RAILS_DEFAULT_LOGGER')
Object.const_set('RAILS_DEFAULT_LOGGER', Logger.new(STDOUT))
end

Rake mysql tasks

guda | J May, 2009 | 9:06 pm

I have found that I should be quicker if I put the following task in all my small projects.
Read the rest of this entry »

Mysql gem and the mkfm error

guda | J April, 2009 | 5:44 pm
The mysql gem was removed from the rails 2.3.2. and you need to install it via the gem install mysql command
But! There is a problem compiling it. seems that the mysql cant find the libmysql so..here are the options:
yum install openssl openssl-devel

Configure with path to mysql_config and install the mysql api via gem:

# cd /usr/lib/ruby/gems/1.8/gems/mysql-2.7
# ruby extconf.rb --with-mysql-config=/usr/bin/mysql_config
# make
# ruby ./test.rb [hostname [user [passwd [dbname [port [socket [flag]]]]]]]
# make install
# gem install mysql

Source: http://www.linuxweblog.com/ruby-on-rails-install

And after that you can read the README.html located in the mysql gem directory :)

Debian/Ubuntu

 sudo aptitude install libmysqlclient-dev libmysql-ruby  libmysql++-dev ruby-dev

ruby classes and instances override

guda | J February, 2009 | 3:43 pm

module InstanceMethods
def an_instance_method
“You called an_instance_method on #{self.class}”
end

end

module ClassMethods
def a_class_method
“You called a_class_method_from_module on #{self}”
end

def b_class_method
puts “b_class_metbod”
end

end

class MyClass
include InstanceMethods
extend ClassMethods

def MyClass.a_class_method
puts “Mine class Method”
end

def an_instance_method
puts “Mine instance”
end
end

my_class = MyClass.new
puts my_class.an_instance_method
puts MyClass.a_class_method
puts MyClass.b_class_method

seesaw gem configuration

guda | J November, 2008 | 5:20 am

I cant find good examples and help in Internet for seesaw gem. Maybe it is used only by very experirienced rails persons.

I will publish my configurations in case they are helpfull to some one.

guda@guda-desktop:~/system3/config$ cat seesaw.yml
---
restart_cmd: sudo kill -HUP `cat /usr/local/nginx/logs/nginx.pid`
config_symlink: http_cluster/cluster.conf
mongrel_config_path: /home/mongrel/system3/current/config/mongrel_cluster.yml
config_path: /home/mongrel/system3/current/config
config_files:
  all: cluster_all.conf
  1: cluster_1.conf
  2: cluster_2.conf
symlink_cmd: ln -sf

here is where my nginx conf link points

guda@guda-desktop:~/system3/config/http_cluster$ ls -l
total 16
-rw-r--r-- 1 guda guda 152 2008-11-07 17:17 cluster_1.conf
-rw-r--r-- 1 guda guda 152 2008-11-07 17:17 cluster_2.conf
-rw-r--r-- 1 guda guda 277 2008-11-07 17:17 cluster_all.conf
lrwxrwxrwx 1 guda guda  66 2008-11-08 01:30 cluster.conf -> /home/mongrel/system3/current/config/http_cluster/cluster_all.conf

and in deploy.rb I have this task rewritten.

set :seesaw_conf, "#{current_path}/config/seesaw.yml"
namespace :deploy do
  desc <<-DESC
  Restart the Mongrel processes on the app server using seesaw
  DESC
  task :restart, :roles => :app do
    run <<-CMD
      cd #{current_path} ; #{mongrel_rails} seesaw::bounce -C #{seesaw_conf}
    CMD
#    send(run_method, "cd #{current_path}; #{mongrel_rails} seesaw::bounce -C #{seesaw_conf}")
  end
end

I am not sure that this is the perfect way of configurating the seesaw. I think that I have some mistakes in the path configurations, and because of that I have to patch the capistrano restart task.

Installing notes for Rails on Debian/Ubunto

guda | J November, 2008 | 10:17 pm

Debian Etch with backports

Add this line in sources.list to get rails > 1.8.5 in rails etch

deb http://www.backports.org/debian etch-backports main contrib non-free
# deb ftp://ftp.nz.debian.org/backports etch-backports main contrib non-free
apt-get install debian-backports-keyring
aptitude update

# some ruby stuff
aptitude install -t etch-backports ruby1.8-dev libmysqlclient15off libmysqlclient15-dev mysql-common mysql-client libmysql-ruby1.8

# some system utils/libs
aptitude install apt-utils build-essential mysql-server mysql-common mysql-client memcached libxslt1-dev libpcre3-dev zlib1g-dev unzip gzip mytop  openssl

aptitude install -t etch-backports libopenssl-ruby

# gems
gem install rails mongrel mongrel_cluster -y

check this step-by-step

Image magick linux howto – http://rmagick.rubyforge.org/install2-linux.html

aptitude install libmagick9-dev  imagemagick librmagick-ruby1.8 imagemagick librmagick-ruby-doc  libmagick9-dev ruby1.8-dev


# for version 1 of imagemagick we use this rmagick, else try it without the -v
gem install rmagick -v 1.15.12 

# lets test it
> irb
require 'rubygems'
require 'RMagick'
Magick::CenterGravity
puts Magick::Long_version

# or with 

ruby -rrubygems -e "require 'RMagick'; puts Magick::Long_version;" 

I love this tutorial on rubyinside

Chunks for the String method.

guda | J September, 2008 | 10:59 am

If you want to generate some sort of chunks from a large strings you can use the following method.
You give as a param an array of arrays, each array is pointing with % the start position of the chunk and the last position.


class String

def String.random_alphanumeric(size=16)
(1..size).collect { (i = Kernel.rand(62); i += ((i < 10) ? 48 : ((i < 36) ? 55 : 61 ))).chr }.join
end

def chunkanize(parts = [[0,4], [48, 52], [95, 100]])
return [self] if self.size <= 1

# working with words
terms_array = self.split(/ /)
join_type = " "

# working with letters
if terms_array.size <= 100
terms_array = self.split(//)
join_type = ""
end

one_percent = terms_array.size.to_f / 100

parts.collect do |part|
start = part[0]
stop = part[1]
terms_array[(start * one_percent)..(stop * one_percent)].join(join_type)
end
end

end

Rails caching notes

guda | J August, 2008 | 9:31 pm

General Notes

Very nice tutorial for rails 2.1 caching.

API references: Fragments, Sweepers, the Store

Post how to move the page cache in a folder with some server examples

Observing Controllers with Sweepers

Here is described how you can use the Sweepers to observe controller actions. I totaly agree that the documenation is very bad on the sweepers and it is like a rule book of MTG than usefull rdoc.

Here is what I have found on the naming of the callbacks.

Checking the source code in sweeping.rb shows that the callbacks that are used for the controller are constructed in this way:

controller_callback_method_name = "#{timing}_#{controller.controller_name.underscore}"
action_callback_method_name = "#{controller_callback_method_name}_#{controller.action_name}"

This means that the method which will be called in the sweeper will be named

  • after/before_user – for the controller UserController
  • after/before_user_login – for the action login in the controller UserController

Here is an example.

class Dagens::UserController < Dagens::BaseController
  cache_sweeper DagensAgentSweeper, :only => [:do_login, :do_delete_account, :do_logout]
  def do_login
  end
  ....
end

class DagensAgentSweeper < ActionController::Caching::Sweeper
  observe DagensAgent

  def after_user_do_login
    puts "after do login \n \n \n \n \n \n \n "
    expire_cache_for(@logged_user)
  end
end

Skipping callbacks from an observer

I know that it is not good idea, but some times there is no time/way to do skip it. I have found a plugin and this post here. I have choose to use the second one in my project. Here is the example

class DagensAgentSweeper < ActionController::Caching::Sweeper
  observe DagensAgent

  def self.skip_callback(callback, &block)
    method = instance_method(callback)
    remove_method(callback) if respond_to?(callback)
    define_method(callback) { true }
    result = yield
    remove_method(callback)
    define_method(callback, method)
    result
  end
........
end

And later in the controller….model…

      DagensAgentSweeper.skip_callback(:after_update) do
        @agent.shown_times += 1
        @agent.save
      end

timed_fragment_cache

Some patches on the timed_fragment_cache plugin.

Note how Jolyon Says suggest to expire the cache when you have time zone : when_fragment_expired ‘tags_home_page’, Time.now + 10.minutes do

I have notice that timed_fragment_cache (or the patch) have a problem with expiring the cache from the controller. In the documenation it is stated that it is ok to expire the cache only in the controller with

when_fragment_expired  "_last_update",  Time.now + 1.minutes  do

and there is no need to expire it in the view, but you MUST expire it in the view.

cache "_last_update" do

must be

cache "_last_update", Time.now + 1.minutes  do