Colorizing console Ruby-script output

Mar 23
2007 18:28 (Ruby on Rails) · Русский (15,576 views)

Very often I have to implement console scripts (because of my laziness, for boring processes optimization). Many of them write some information to the output, show process status or display results of work. Anyway, it’s very wearisome action to read script output, and I want to highlight most important things: errors in red, successfully finished steps in green color, etc. And it is a case when ANSI escape sequences could help. They are supported by the most terminals, including VT100 (btw, Windows NT family console does not support it, but I will back to this issue later).

Read the rest of entry »

Using Sphinx search engine in Ruby on Rails

Nov 26
2006 10:55 (MySQL, RoR Plugins, Ruby on Rails) · Русский (25,666 views)

Almost all Web-applications needs data search logic and really often this logic should have full-text search capabilities. If you are using MySQL database, you can use its FULLTEXT search, but it’s not efficient when you have a large amout of data. In this case third party search engines used, and one of them (and I think, the most efficient) is Sphinx. In this article I’ll present my port of Sphinx client library for Ruby and show how to use it.

Read the rest of entry »

Ruby on Rails related cheat sheets

Nov 17
2006 07:32 (Ruby on Rails) · Русский (19,169 views)

There are couple of cheat sheets about Ruby on Rails and related technologies can be found in the web. I decided to collect all of them (almost all) in one post just to keep them in mind. All of them are in full color PDFs or PNGs.

Read the rest of entry »

In-place file upload with Ruby on Rails

Oct 28
2006 15:10 (Ruby on Rails) · Русский (63,549 views)

My friends often asked me how to upload file using AJAX, and usually they got answer “in no way”. Correct answer, but what if I need to upload file without full page reloading? And, of course, I want to use RJS in this case. Here I’ll explain what to do to get effect very similar to AJAX file upload (btw, Gmail uses this technique).

Read the rest of entry »

Flexible application configuration in Ruby on Rails

Oct 14
2006 06:27 (Ruby on Rails) · Русский (15,411 views)

In my current project in Ruby on Rails I need to store application specific configuration. There are several approaches I’ve found: AppConfig plugin, several methods described on HowtoAddYourOwnConfigInfo wiki page, but neither one looks “config-like”. Me with my friend, Alexey Kovyrin, discovered all of them and decided to use YAML-file. Ideal configuration, I think, file looks like following:

1
2
3
4
5
6
7
8
9
10
11
span class="re0"> common:
 support_email: admin@myhost.com
 root_url: myhost.com
 photos_max_number: 6

production:
 email_exceptions: true

development:
 root_url: localhost:3000
 photos_max_number: 10

In this example you can see three sections: common will be used as base configuration for all environments, production and development - environment specific options. Possible sections are production, development, and testing, or any other custom environment name. I’ve placed this file in config/config.yml and added following code to config/environment.rb:

1
2
3
4
5
6
7
require 'ostruct'
require 'yaml'

config = OpenStruct.new(YAML.load_file("#{RAILS_ROOT}/config/config.yml"))
env_config = config.send(RAILS_ENV)
config.common.update(env_config) unless env_config.nil?
::AppConfig = OpenStruct.new(config.common)

Now I’m able to use constructions like AppConfig.support_email and AppConfig.root_url. Looks like I’ve kept all my configs as DRY as possible :-)

 
Copyright © 2005 - 2008, Dmytro Shteflyuk