rss

Compile & install Nginx web server

Category : Ruby on Rails

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
 # See:
# - http://en.wikipedia.org/wiki/Nginx
# - http://wiki.codemongers.com/NginxGettingStarted
# - http://wiki.codemongers.com/NginxInstallOptions
# - http://wiki.codemongers.com/NginxCommandLine
# - http://wiki.codemongers.com/NginxConfiguration
 
export PATH="/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin"
 
mkdir -p ~/Desktop/Nginx
cd ~/Desktop/Nginx
 
curl -L -O http://sysoev.ru/nginx/nginx-0.7.2.tar.gz
tar -xzf nginx-0.7.2.tar.gz
 
curl -L -O http://downloads.sourceforge.net/pcre/pcre-7.7.tar.gz
tar -xzf pcre-7.7.tar.gz
 
cd ~/Desktop/Nginx/nginx-0.7.2
./configure --help
./configure --prefix=/usr/local/nginx --sbin-path=/usr/local/sbin --with-debug --with-http_ssl_module --with-pcre=../pcre-7.7
make
sudo make install
 
which nginx
otool -L /usr/local/sbin/nginx
open /usr/local/nginx
open -e /usr/local/nginx/conf/nginx.conf
 
nginx -v
nginx -V
sudo nginx # start server
sudo nginx -t
 
open http://localhost:80
open http://localhost:80/50x.html
 
sudo nano /usr/local/nginx/conf/nginx.conf # editing ... server { listen 8080; ...
 
# reload configuration
sudo kill -HUP `cat /usr/local/nginx/logs/nginx.pid`
 
# stop server
sudo kill -15 $(ps -auxxx | egrep "[n]ginx.*master" | awk '{ print $2 }') 2>/dev/null
Share

Create a Hash from two Array objects

Category : Ruby on Rails

Requires Ruby 1.8.7 or higher.

1
2
3
4
5
6
keys = [1,2,[3,4]]
[1, 2, [3, 4]]
values = ['a','b',['c','d']]
["a", "b", ["c", "d"]]
Hash[ * keys.zip(values).flatten(1) ]
{1=>"a", [3, 4] => ["c", "d"], 2 => "b"}
Share

File Upload Helpers

Category : Ruby on Rails

Handy little helpers to check if a file was provided to a controller, and to save it.

1
2
3
4
5
6
7
8
9
10
11
 def file_provided?
   [StringIO, Tempfile].include?(@file.class) and @file.size.nonzero?
 end 
 
 def save_file
   if @file.is_a? Tempfile
     FileUtils.cp(@file.path, path)
   elsif @file.is_a? StringIO
     File.open(path, "wb") { |disk_file| disk_file << @file.read }
   end
 end
Share

Renaming your .rhtml to .erb on EdgeRails

Category : Ruby on Rails

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
## just slap this in your rake file or in lib/tasks
 
namespace 'views' do
  desc 'Renames all your rhtml views to erb'
  task 'rename' do
    Dir.glob('app/views/**/*.rhtml').each do |file|
      puts `svn mv #{file} #{file.gsub(/\.rhtml$/, '.erb')}`
    end
  end
 end
 
 ## or if you have rhtml and rxml you could probably use the following (untested)
 
 namespace 'views' do
   desc 'Renames all your rhtml views to erb'
   task 'rename' do
     Dir.glob('app/views/**/*.rhtml').each do |file|
       puts `svn mv #{file} #{file.gsub(/\.(rhtml|rxml)$/, '.erb')}`
     end
   end
 end
Share

Automatically tagging releases with capistrano

Category : Ruby on Rails

Even though Capistrano “tags” each release by creating a new folder on the production server(s), it might be interesting to have a historical perspective in your repository anyway. This makes it easier to know exactly what went up for a release. I would like to share the following Capistrano recipe for your pleasure: config/deploy.rb

1
2
3
4
5
6
7
8
 require ‘uri‘
 task :after_deploy do
   source = repository
   dest = URI.parse(repository).merge(“../releases/#{File.basename(release_path)}“)
   cmd = “svn copy –revision=#{revision} –quiet –message \”Auto tagging release #{release_path}\” #{source} #{dest}“
   puts cmd
   `#{cmd}`
 end

First, we start by requiring uri, because Subversion does not like relative URLs. Next, we find the location into which to tag the release, and finally, we just do it. Simple, effective. Enjoy !

Share

Exporting development data to fixture files for your test (Ruby)

Category : Ruby on Rails

For your unit & functional tests you have to provide input data to your test files and you do that through fixtures. The script below extracts the data from the development data base and puts it to the fixture file for each table.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
namespace :db do
  desc 'Create YAML test fixtures from data in an existing database.
  Defaults to development database. Set RAILS_ENV to override.'
 
  task :extract_fixtures => :environment do
    sql = "SELECT * FROM %s"
    skip_tables = ["schema_info", "sessions"]
    ActiveRecord::Base.establish_connection
    tables = ENV['FIXTURES'] ? ENV['FIXTURES'].split(/,/) : ActiveRecord::Base.connection.tables - skip_tables
    tables.each do |table_name|
      i = "000"
      File.open("#{RAILS_ROOT}/test/fixtures/#{table_name}.yml", 'w') do |file|
        data = ActiveRecord::Base.connection.select_all(sql % table_name)
        file.write data.inject({}) { |hash, record|
          hash["#{table_name}_#{i.succ!}"] = record
          hash
        }.to_yaml
      end
    end
  end
end
Share

shame.rake

Category : Ruby on Rails

If you’ve ever been working on a Rails application and found yourself [stupidly] slipping in your testing, We have just the tool for you. Using rake and rubyosa, you can now automatically post your Code to Test Ratio as your iChat status message—meaning you can now use shame and self-humiliation to keep you motivated. After you’ve installed rubyosa, add shame.rake to your lib/tasks directory. The file contains:

1
2
3
4
5
6
7
8
9
10
11
12
require 'rbosa'
require 'code_statistics'
 
task :shame do
  stats = CodeStatistics.new(*STATS_DIRECTORIES)
  code  = stats.send :calculate_code
  tests = stats.send :calculate_tests
  ichat = OSA.app('ichat')
  msg = "Code To Test Ratio: 1:#{sprintf("%.1f", tests.to_f/code)}"
  ichat.status_message = msg
  $stderr.puts %|iChat status set to: #{msg.inspect}|
end
Share

Simple method to find age of person

Category : Ruby on Rails

1
2
3
def age
   ((Time.now - birthday) / (60*60*24)/365.2422).to_i
end
Share

Rails Image Cropping

4

Category : Ruby on Rails

I was looking for an UI for image cropping utility for my project and came across this, it was using php as its backend but i changed it to use rails as its backend. The UI is simple and can be incorporated with different backend solutions.

Here is the code.

Share

Rails Cheatsheets

1

Category : cheatsheet, Ruby on Rails

Here are the rails cheatsheet’s which will help you to get a good overview of how things work in RAILS.

Full Ruby on Rails cheatsheet

Rails Assertions

RJS Cheatsheet

REST Cheatsheet

Rails Refrence

Rails Migration

Rails File Cheatsheet

Form Helpers

Asserts in Rails

Active Record cheatsheet

rubyonrails-cheatsheet.pdf

Share

Amit Yadav is Stephen Fry proof thanks to caching by WP Super Cache