Rails for Designers – Using Haml/Sass for all projects

Posted by Eumir Gaspar on December 29, 2008 
Filed Under Ruby on Rails, Web Design, Web Development | Comments

I have recently acquired a newfound drive for designing sites(as opposed to my earlier desire to be a great design/backend mediator). As of now, due to some reent developments, I am the main designer for most of our projects, not to mention some of which are personal or freelance related. One pet peeve I had though was the diversity of my projects: meaning some were in rails(of course I was happy with fixing views and adding some javascript-fu), php, and some, just plain XHTML/CSS.

With that said, things can get pretty messy when multiple projects are at hand and I have to shift continually from regular XHTML/CSS coding and HAML/SASS. Though it is not a great hindrance, having to keep remembering which language I was using was kinda annoying. Add to the fact that I work twice(or thrice) as fast using HAML/SASS and the tiny nuisance becomes a glaring problem(for me that is).

At first, I made a semi-solution. A compromise of sorts: since I use the ever cool Elements CSS framework(actually I just use the general html structure and a modified reset.css which I have discussed with the author, Ben Henschel), my html is usually almost ready-made so my usual focus is just the css file. For projects that used Rails(I still didn’t have a solution for ones that didn’t) but not Haml, I just installed Haml on my part, and continued developing with Sass. Since Sass generates a .css file, that’s the one I usually push in repos(if the client can’t be bothered with installing Haml).

But that was just for Rails projects. The problem lies with normal design projects or ones that used PHP. Recently, I had another solution(I doubt this is the most efficient though): make a blank rails app and keep using Haml/Sass for myself and when done with coding the design, I just do a view source, copy-paste the whole thing in a blank html file, modify the paths for the css and javascripts, then just copy the images/stylesheets/javascripts folders in my public/ folder. Nifty!

It was a good thing for me, since now, I can just keep coding in Haml and Sass to my heart’s content. Thing is, I just realized I just can’t keep making blank rails apps for each project! So the plan was to separate the projects in self-titled folders and just use some routes.rb and controller magic suggested by my friend Greg:

In routes.rb:

map.connect 'projects/:id', :controller => 'projects', :action => 'index'

and in my single controller named projects_controller.rb:

def index
render :action => "#{params[:id]}/index"
end

With that, I can just go to say /projects/projectA and it will then render projectA. This also works as a portfolio and future-reference of sorts for me, which is excellent. Now I only need to think of a good way to organize the images and the stylesheets(especially the javascripts) so they won’t interfere with each other but basically, I’m just planning to put them in separate folders too(except for reset.css and jquery.js of course – every project needs that!)

Extra note:
Forgot to mention that I had to add 

config.frameworks -= [ :active_record, :active_resource, :action_mailer ]

since I wasn’t using any db.

Rack-based Session Stores on Rails

Posted by Christopher Rigor on December 22, 2008 
Filed Under Ruby on Rails, Web Development | Comments

Rails edge is now using Rack-based session stores. That means you can access the same session on any Rack applications. Read about it on the Rails blog.

Nothing changes though on how you use sessions on Rails. On your Rails app, you’ll have something like

session[:user] = some_user_id

Let’s see how we can access the same session on a Rack application. You can read about Rack here.

Rack handles session using Rack::Session::Cookie. Create a file config.ru

use Rack::Session::Cookie, :key => '_railssession_session', :domain => 'railssession.labs.admoolabs.com'
run Proc.new {|env| [200, {"Content-Type" => "text/html"}, "Rack session 'user' has a value of #{env['rack.session'][:user] || 'nil'}."]}

The key should be the same key that you used on config/environment.rb on your Rails app.

Check out the demo. Set session[:user] on the Rails app, then view the session on the Rack app.

Capistrano

Posted by Christopher Rigor on October 18, 2008 
Filed Under Web Development | Comments

Capistrano is used for “automating tasks via SSH on remote servers, like software installation, application deployment, configuration management, ad hoc server monitoring, and more.” It is written in ruby and is commonly used to deploy Rails applications. Capistrano can be used to deploy applications written on any language. It runs commands on remote servers via SSH so it’s not specific to any language.

I will show you how to use capistrano for deploying a Rails application. The goal is to deploy an application by running one command – “cap deploy”. This should update your code from Subversion, and restart your server.

I assume that you have a rails setup on your server. I use the nginx web server, mongrels and mongrel_cluster.

To install capistrano

gem install capistrano

To start using capistrano on your Rails application,

cd RAILS_ROOT
capify .

You should see two files – Capfile and config/deploy.rb. Capistrano looks for Capfile when you run any capistrano commands. The default Capfile loads config/deploy.rb. deploy.rb contains the settings for your application.

We will deploy the Rails app “Hello Labs”. This can be found on our repository. Let’s edit deploy.rb.

Set the user and runner that will run the commands on your server.

set :user, "crigor"
set :runner, "crigor"

Set the application name

set :application, "hellolabs"

Set the scm and repository

set :scm, :subversion
set :repository, "http://labs.svn.admoolabs.com/hellolabs/trunk"

The default :scm is actually :subversion but I like to put it anyway.

Set the directory on the target server where you will deploy the application.

set :deploy_to, "/var/www/#{application}"

application is “hellolabs” which you set earlier. The deploy_to directory, which I’ll call DEPLOY_ROOT, is /var/www/hellolabs. You should create this directory on the target server. On Ubuntu, I set the group to www-data so mongrel has access to it.

Set the servers.

role :app, 'hello.labs.admoolabs.com'
role :web, 'hello.labs.admoolabs.com'
role :db, 'hello.labs.admoolabs.com', :primary => true

You can also use the IP address instead of the domain.

If nginx, mongrel, and mysql are all on the same server, put the same domain or IP address.

Now we will run our first capistrano task. Make sure /var/www/hellolabs exists on your server. On the RAILS_ROOT, run

cap deploy:setup

This will create the releases directory and shared directory under the hellolabs directory on the remote server. If you get a permission error, make sure the user you specified has access to /var/www/hellolabs.

Capistrano will ask for you server password. If you don’t want to enter your password every time you run a capistrano task, you can add

set :password, "mypassword"

Check if we have met the dependencies.

cap deploy:check

The output, if deploy:check is successful, looks like this.

Before we deploy the app, we define the deploy:restart task. By default capistrano runs /var/www/hellolabs/current/script/process/reaper. I use mongrel and mongrel_cluster so let’s add

namespace :deploy do
  desc "Restart the mongrels"
  task :restart do
    run "mongrel_rails cluster::restart -C #{shared_path}/config/mongrel_cluster.yml"
  end
end

Create the /var/www/hellolabs/shared/config directory and add mongrel_cluster.yml. We also need to add database.yml. I don’t commit database.yml in my repository since it contains the database password.

We have to symlink mongrel_cluster.yml and database.yml every time we deploy.

after "deploy:update_code", "deploy:symlink_configs"
namespace :deploy do
  desc "Symlink the config files."
  task :symlink_configs do
    run "ln -nfs #{shared_path}/config/database.yml #{latest_release}/config/database.yml"
    run "ln -nfs #{shared_path}/config/mongrel_cluster.yml #{latest_release}/config/mongrel_cluster.yml"
  end
end

We’re now ready to deploy our application.

cap deploy

If you see something similar to this, your app has been deployed.

The releases directory will contain directories with the timestamp as the name like 20081017125102 and 20081017132929. These directories will contain the rails application.

Below, you can see that current is a symlink to 20081017132929. /var/www/hellolabs/current is your RAILS_ROOT.

cap_directory_structure.jpg

Check the URL of your app. http://hello.labs.admoolabs.com

If you need a username and password to checkout your repostory, add

set :scm_username, "username"
set :scm_password, "password"

To get the list of capistrano tasks available, run cap -T.

For more information, check the capistrano website.

Here is the complete deploy.rb

set :user, "crigor"
set :runner, "crigor"

set :application, "hellolabs"
set :scm, :subversion
set :repository, "http://labs.svn.admoolabs.com/hellolabs/trunk"
set :deploy_to, "/var/www/#{application}"

role :app, 'hello.labs.admoolabs.com'
role :web, 'hello.labs.admoolabs.com'
role :db, 'hello.labs.admoolabs.com', :primary => true

after "deploy:update_code", "deploy:symlink_configs"
namespace :deploy do
  desc "Symlink the config files."
  task :symlink_configs do
    run "ln -nfs #{shared_path}/config/database.yml #{latest_release}/config/database.yml"
    run "ln -nfs #{shared_path}/config/mongrel_cluster.yml #{latest_release}/config/mongrel_cluster.yml"
  end

  desc "Restart the mongrels"
  task :restart do
    run "mongrel_rails cluster::restart -C #{shared_path}/config/mongrel_cluster.yml"
  end
end

Online privacy

Posted by Eumir Gaspar on September 15, 2008 
Filed Under Uncategorized | Comments

Recently, I have come across this news in Slashdot. I don’t fully understand the repercussions of that but from what I do think I understand, it just says that Google actually logs your IP and your online behavior, right? Like, “Hey, this guy using 123.456.789 .123 is surfing bestiality porn! LMAO!”.

Sure, it kinda violates your “privacy” but I don’t know. Google actually does it(log people’s online behavior, like surfed sites, search terms) because they want to know what kind of ads to show you, right? Sure, you use Firefox 3 and have adblock plus, but what about the others who don’t? What about the others who actually like getting ads as those ads help point them get to sites/buy items they usually wouldn’t have seen anyway?

Maybe there should be an option for turning off/on the tracking of your online behavior, but for me, I’m pretty sure it is harmless. It’s not like they’re watching your every move online. You are NOT that special. To them, you’re just a byte. Or a statistic. Or a number. Nobody. You’re an ip address at most. I’m pretty sure they’re not trying to be Orwell’s Big Brother by looking at everyone’s personal lives and trying to dictate everyone’s actions.

Maybe I’m wrong, but that’s how I understand it. Is there any other reason for them to get everyone’s surfing behavior?  Like almost all web services, I think it is important to “get to know” your users so that instead of giving them what they need when they need it, you can give them what they need/want before they even ask for it. Sure, they’ll know you surfed for car porn or door porn or even the fact that you googled “the number of married men/women uglier than me” or something, but that doesn’t mean they’ll try to find you. For what? To laugh at you? Tell the whole world that you’re one of a kind who searches google for married people uglier than you? Or maybe you purchased a sniper rifle to kill someone and they can trace you(Wow. That’s just genius on your part then)

I honestly don’t understand why people care about online privacy that much. It isn’t that much different from you asking the local librarian for books on “How to Impress a First Date” or “French Kissing 101″ or even “How to Wipe Your Ass Properly”. It isn’t much different from buying OTC potency pills. They wouldn’t actually care if you surf for weird stuff at night – they’ll just give you more ads about the weird stuff you like. And wouldn’t that make your web experience better?

These are just my thoughts and maybe I am missing the whole point here. And if so, feel free to steer me the right way. But right now, I still think that people are being overly paranoid on what Google might do to about their online behaviors. Am I just that naive? Can anyone enlighten me on this?

Scraping bot

Posted by Eumir Gaspar on July 8, 2008 
Filed Under Ruby on Rails, Web Development | Comments

I just recently found the joys of scraping. Yes, the “technique” is a bit underhanded, but it sure is fun. I guess as long as you don’t profit off of it, it’s ok(and it doesn’t affect the site you’re scraping). If you scraped a site just to present it in a different design(like scrape all the news off CNN and then make a site called BNN or something and then charged users with a smaller fee), then yeah, that’s just plain dirty.

I scrape for fun and that’s it. Anyways, there I was, scraping movie schedules off the only movie schedule site that I know of( clickthecity.com ) and suddenly, I had an idea. What if I just made an RSS feed for a movie house that I liked? It would be just so cool since clickthecity(or CTC) doesn’t have an RSS feed. So I started researching about makign RSS feed, and while doing so, Topher brought about the idea of making a bot. He showed me how jabberbot and from there I went on and played around with it.

To make the scraper, I used the Rubyful-soup plugin for Ruby on Rails(along with Mechanize). Using it was easy, and the main problem actually was how to traverse the atrocious table layout of clickthecity. Once I got over that hurdle though, getting the info I wanted was easy. And what did I need? I only just needed the date of the showtimes(so I’ll know if it was the latest update), the location of the cinemas(the mall the cinemas were located), the cinema name(or number), the movies being shown on each movie house,  and the show times of each. These were saved into a database I made(I had a bit of a trouble in putting them in, especially since there were movie houses that had two different movies being shown the same day)

Next was the bot itself. Like I said, I used jabberbot, which was also a handy plugin for Ruby on Rails. The syntax was easy, and this was the easiest part. I made a new google account just for the bot and now it is up. I still have yet to make it work on yahoo messenger(Topher tried it and it didn’t work, but I haven’t tried it for myself yet) so I’ll just have to settle with Gtalk.

The flow of the program is simple: scrape the data from clickthecity.com and then save it to the database(done using a rake task I made). I have yet to automate these tasks, along with the connection of the bot(I still have to manually start it using another rake task). Anyways, after manually callign the rake tasks for scraping the site and starting the bot, movieschedule-bot is ready to go.

So far, I only added a select few movie houses that me and my friends usually go to. You can actually add movieschedules (yes, google mail) to your gtalk/gmail contact list and start using it(don’t worry about down times. that just means I am currently updating/restarting it – try again after five minutes if it doesn’t work) Don’t try to spam it though as right now it is running from my local machine. I have yet to upload it to my free Heroku account and make it run the bot forever.

So there you have it. A scraper bot. There are a LOT of other uses for this, like a thesaurus bot, wikipedia bot, imdb bot or something but that’s for another discussion. As you can see, bots can be useful too! Right now, movieschedules is looking for more friends. Be kind to it!

To start using  movieschedules, just add it to your Google contact list then type “help” and send it. It will return you a list of commands that are currently available. Have fun!

Railsconf 2008

Posted by Christopher Rigor on May 12, 2008 
Filed Under Ruby on Rails | Comments

I will be at Railsconf 2008 at Portland. I still haven’t finalized the sessions I’m attending but I’m sure it will be great.


RailsConf 2008

Alphabar on Rails

Posted by Gerald Abrencillo on April 24, 2008 
Filed Under Bugs and Fixes, Ruby on Rails | Comments

I am working on this little project right now using Ruby on Rails and I ran into some trouble with the alphabar plugin. First, alphabar uses the with_scope method which, since Rails 2.0 I believe, has been protected, giving me some errors since I froze my Rails version. I was able to overcome this obstacle by using the send method instead. Just change the last part of the find method of the plugin from:

model.with_scope({:find => {:conditions => conditions}})
{model.find :all}

to:

model.send(:with_scope, {:find => {:conditions => conditions}})
{model.find :all}

After I finally got that to work the plugin was very useful, although the alphabar was limited to letters and blank which is useful if you don’t need numbers. To accomodate numbers just add this to the alphabar helper:

('0'..'9').to_a.each do |i|
slots << i
end

Theoretically it should work for any character but I haven’t tried it yet.

So there you have it. Hoepfully somebody having trouble with this plugin will find this post useful.

PNOI Judge

Posted by Christopher Rigor on March 13, 2008 
Filed Under Uncategorized | Comments

The first Philippine National Olympiad in Informatics (PNOI 2008) will be held on March 15, 2008 at Ateneo de Manila University, Loyola Heights, Quezon City.

PNOI is an individual programming contest for high school students. I’m a judge (and 4 others) at this contest.

Click here for the details.

Run from Capistrano

Posted by Christopher Rigor on March 8, 2008 
Filed Under Ruby on Rails | Comments

You can run shell commands from capistrano using the run method. For example, to create a symlink from the shared directory to the current directory, you’ll write

run "ln -nfs #{deploy_to}/shared/config/database.yml #{release_path}/config/database.yml"

run can also take a block like this

run "rsync /path/to/file host:/path/to/file" do |channel, stream, text|
  logger.info "[#{stream}] #{text}"
  output = case text
  when /\bpassword.*:/i
    "#{password}\n"
  when %r{\(yes/no\)}
    "yes\n"
  end
  channel.send_data(output) if output
end

When you run a command it might ask for your password. To handle this in capistrano, use send_data to send a response.

Check lib/capistrano/recipes/deploy/scm/subversion.rb on the capistrano gem to see how capistrano handles subversion commands.

The problem of capital

Posted by Bit Santos on February 8, 2008 
Filed Under Business, Startup Life | Comments

Every business startup encounters the same problem at some point: the problem of money or to be more precise, where to get it.

A startup by convention is defined as a business that aims to create and sell its own product/s, but such a business needs resources to just survive while everyone is still developing the product. These resources are what business and management types like to call “capital”. (Just a bit of sarcasm there, folks.)

Here in the Philippines, the general trend (from my own unscientific observations) for successful startups is that the founders are usually already rich to begin with or are backed by a rich family and thus don’t have to deal with the problem of capital as much as others. The government has done quite a lot to try and help SMEs and micro-enterprises such as provide tax breaks or even loans to those who want to enter particular industries. Unfortunately for us techies, the IT industry isn’t on the list.

Besides the lack of institutional support, we don’t have much of a VC culture to really talk about. Local business investors – as far as I can see – are not willing to invest in any high-risk ventures such as a software company despite the potentially high rewards. I read somewhere that the VCs in Silicon Valley have already understood how to handle this risk and I believe their approach makes sense and should be emulated if we are to even hope that we can start something similar here. Although the rate of Internet startup success is very low, even if just one of their investments hit it big, it usually hits big enough to more than make up for the ones that don’t make it.

How do we deal with it? By taking on client work. The problem here is the very basic concept that the more time you spend on something, the less time you have for something else. Time spent on clients is time not being spent on your own product. In an interview at Pinoy Web Startup, Luis of syndeo::media admitted that although their ultimate goal is to operate solely on their own products, they’re spending 95% of their time on client work just to get by. Although I’m sure that was just a rough estimate, if we were to calculate that in terms of a 40-hour work-week per programmer, that would work out to just 2 hours of work on their own products every week. (Of course we can probably safely assume that as in most startups, work-weeks aren’t exactly fixed at 40 hours every week.)

While I won’t question their approach (we here at Admoo Labs also seriously considered this approach), I must admit that it feels like a waste to be doing that when the real work towards your ultimate aim ultimately accounts for only 5% of your time but the truth of it is, there’s very little choice for a lot of us. We need the money.

Everything would be so much simpler if we had more funding sources in the region like Y-Combinator and its many clones

← Previous PageNext Page →