Internetbureau Holder

Writing more with less

Roy van der Meij wo 30 jun 10

So you would like to use variables and inheritance with css? Now you can! With less.js.

So what is less?
With less you can do exactly what I stated above. Let’s check out some code.

@brand_color: #4D926F;

#header {
  color: @brand_color;
}

h2 {
  color: @brand_color;
}

How awesome is that :)
But what I like more is the inheritance part.

#header {
  color: red;
  a {
    font-weight: bold;
    text-decoration: none;
  }
}

Right now less is a gem and a plugin for rails, but they rewritten it entirely in javascript. So you can now use it without any use of a backend.
Just place this in your head tag.

<link rel="stylesheet/less" href="style.less" type="text/css" />
<script src="http://lesscss.googlecode.com/files/less-1.0.21.min.js"></script>

And have fun in your stylesheets/style.less file :-)

For more info check out these links:
lesscss.org
less.js on github
less tutorial

Gepost in hor |  1 reactie

Using rails3 safely without rvm

Wijnand Wiersma wo 30 jun 10

Currently installing rails3 will cause some breakage to older rails apps and you can’t easily generate a new rails 2.3 app.
One possible solution to this is rvm but I like to use my one and only REE installation instead.
So I decided to use some bundler magic to install rails only within the bundler bubble.

Currently I don’t have any rails3 gems installed:

yaya 22:29:~:  $ gem list | grep rails
autotest-rails (4.1.0)
rails (2.3.5, 2.3.4)
rspec-rails (2.0.0.beta.8, 1.2.9)
yaya 22:29:~:  $ which ruby
/opt/ruby/bin/ruby

So I create a directory (just to be clean) and add a Gemfile:

yaya 22:29:~:  $ mkdir to_bundle
yaya 22:30:~:  $ cd to_bundle
yaya 22:30:~/to_bundle:  $ vi Gemfile
yaya 22:30:~/to_bundle:  $ cat Gemfile 
source 'http://rubygems.org'
gem 'rails', '3.0.0.beta4'

Now I run bundle install to add rails3 to the bundler bubble:

yaya 22:30:~/to_bundle:  $ bundle install
Fetching source index from http://rubygems.org/
Using rake (0.8.7) from system gems 
Using abstract (1.0.0) from system gems 
Using activesupport (3.0.0.beta4) from system gems 
Using builder (2.1.2) from system gems 
Using i18n (0.4.1) from system gems 
Using activemodel (3.0.0.beta4) from system gems 
Installing erubis (2.6.6) from rubygems repository at http://rubygems.org/ 
Using rack (1.1.0) from system gems 
Installing rack-mount (0.6.6) from rubygems repository at http://rubygems.org/ 
Using rack-test (0.5.4) from system gems 
Using tzinfo (0.3.22) from system gems 
Using actionpack (3.0.0.beta4) from system gems 
Using mime-types (1.16) from system gems 
Using polyglot (0.3.1) from system gems 
Using treetop (1.4.8) from system gems 
Installing mail (2.2.5) from rubygems repository at http://rubygems.org/ 
Using actionmailer (3.0.0.beta4) from system gems 
Using arel (0.4.0) from system gems 
Using activerecord (3.0.0.beta4) from system gems 
Using activeresource (3.0.0.beta4) from system gems 
Using bundler (0.9.26) from system gems 
Installing thor (0.13.7) from rubygems repository at http://rubygems.org/ 
Using railties (3.0.0.beta4) from system gems 
Installing rails (3.0.0.beta4) from rubygems repository at http://rubygems.org/ 
Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed.

Cool, but the rails command in my $PATH is still the rails 2.3 version. Let’s ask bundler to run the rails3 beta to generate a new app:

yaya 22:32:~/to_bundle:  $ bundle exec rails new ../new_app
     create  
     create  README
     create  Rakefile
     create  config.ru
     create  .gitignore
     create  Gemfile
     create  app
     ...... lots more output

Now when I enter my new app directory I can use all the new rails commands, I just have to prepend it with bundle exec.
I am sure you will add some shell aliases to save some keystrokes.

yaya 22:33:~/new_app:  $ bundle exec rails c
Loading development environment (Rails 3.0.0.beta4)
irb(main):001:0> 

So, this way I can use rails 2.3 the way I did and I don’t have to manage multiple ruby installations with their own gem sets.

Gepost in hor |  0 reacties

and vs &&

Chiel Wester wo 30 jun 10

Last week we had a discussion about using the ‘and’ operator vs the ‘&&’ operator for equasions. The only reason to prefer ‘and’ above ‘&&’ would be that people say the code will be better readable and cleaner.

Although the code might be better readable, you have to be careful using the ‘and’ operator because it has a slightly different behaviour than the && operator. In this article you can see a very good example of this difference.

So, when using the and operator, make sure you put brackets around the equasion to prevent strange behaviour! If you don’t want to use the brackets, just use && this will save you from a lot of trouble

Gepost in hor |  0 reacties

PDFKit

Dax Huiberts di 29 jun 10

Another new kid in town. This time it is PDFKit, a new tool for generating PDF documents in your Rails application.

Before this we had two main choices. Either use PrinceXML with the princely gem to convert your HTML documents + CSS stylesheets to PDF while paying a hefty server license of $3,800. Or use Prawn and build your PDF documents using nothing but Ruby code. I once thought Prawn was actually a nice tool, but I was amazed by the inordinate amount of code needed to display a simple table.

Here comes PDFKit, a new tool to create your PDF documents using HTML + CSS again. It’s build on the Webkit HTML engine, so you’ll get fine rendering and you don’t have to pay an excessive server license.

Go check out this Railscast on PDFKit.

Gepost in hor |  0 reacties

nifty rails 3.0 methods II

Daniel Willemse zo 27 jun 10

@Wijnand,
I have to agree with you, in that class methods you describe can do the same the scopes can. The only reason I can think of, is the one you would reject.

Now for the new methods I made a post on about a week ago.
I thought to myself, what would happen if i wrote a class method “order” and tried to use it on the class.
Here’s what I tried:

 
Class Invoice < ActiveRecord::Base
  def self.order
   return "new class method"
  end
end

Now, when I tried this in the console
with

>> Invoice.order

I got the expected “new class method” string returned.

With

>> Invoice.order.where("id = ?", 1)

I got
NoMethodError: undefined method `where’ for new class method

Which was not unexpected.

However, when I tried Invoice.where(“id = ?”, 1).order
It returned the active record relation as if ignoring my own class method.

Keep this in mind when (for whatever reason you may have) overriding
certain class methods like order or where. Should you want to use your order method over the pre-defined rails one, you need to put that method first in line on your model like
Model.own_order_method.where(“arguments”)

Gepost in hor |  0 reacties

Named scopes, still needed ?

Wijnand Wiersma vr 25 jun 10

I am wondering what the use cases are for named scope in the Rails3 release.
Just creating a class method which returns an ActiveRelation object gives more clear code and allows for more customisations.

The most obvious use case are old rails 2.3 lambda scopes.
See this example:

named_scope :for, lambda {|author| {:conditions => ["customer_id IS NULL OR customer_id = ?", author.customer_id]} }

You can call this like

Post.for(author) # eg anything that responds to customer

This looks like a method call, so I was happy to be able to do this in Rails3:

def self.for(testmaker)
 where(:customer_id => testmaker.customer_id)
end

Now it’s not just like calling a method, it actually is a method!

For me this feels far more natural and thanks to Arel magic it just works in the same way too.
You can still chain it as if it was an instance method:

Post.limit(10).for(author)

So if we had to use the hackish lambda scopes regularly (the real need is questionable off course) and now we are better served with a class method: wouldn’t it be far more natural to skip named scopes and just create intelligent class methods?

Rails2:

named_scope :published, :conditions => {:published => true}

Rails3:
scope :published, where(:published => true)

The Rails3 way I like it:
def self.published
 where(:published => true)
end

So unless anybody can give me a compelling reason to use named scopes (lines of code arguments will be rejected) I will proceed on this path.
I think Rails apps should become more Ruby like instead, too much magic hurts my brains.

And remember, everytime you write a lambda scope, god kills an unicorn.

Gepost in hor |  2 reacties

Phusion Passenger 2.2.15 just released

Paul Engel do 24 jun 10

Although earlier this month the guys of Phusion teased us with performance improvement stats of Passenger 3, they have just blogged about their Phusion Passenger 2.2.15 release.

It contains the following:

- Apache: Fixed incorrect temp dir cleanup by passenger-status
- Apache: Fixed some upload handling problems
- Nginx: Fixed compilation issues with Nginx >= 0.7.66
- Nginx: Changed its default Nginx version from 0.7.65 to 0.7.67
- Bundler: Fixed some problems

For further details, please check out their blog post in which upgrade instructions to 2.2.15 are also provided.

Gepost in hor |  0 reacties

Smooth menu's for real browsers

Johan Vermeulen wo 23 jun 10

With the features which are introduced in de CSS3 standard, it is possible to create rounded border corners. This can be a real time saver when slicing a website.

In this example I created a simple menu with rounded corners and a background image for the on hover and active events.

Result in browsers which support HTML5/CSS3:

Result in older browsers:

Download the sprite

The HTML:

<ul>
  <li class="first"><a href="weblog.html">Weblog</a></li>
  <li><a href="weenies.html">Weenies</a></li>
  <li><a href="jobs.html">Jobs</a></li>
  <li class="last"><a href="zandbak.html">Zandbak</a></li>
</ul>

The CSS

Tweaks:

If you like you can add webkit and mozilla specific CSS to create the same results in Firefox 3 or webkit 1 enabled browsers:

-moz-border-radius: 10px;
-webkit-border-radius: 10px;

Gepost in hor |  0 reacties

Custom Subdomains in Rails 3

Stephan Kaag wo 23 jun 10

Rails 3 supports subdomains out of the box, which is great. But did you know that the constraints in the Router can actually match patterns too? This means instead of hardcoding each subdomain into your routes you can allow your customers to decide their own subdomains.

I created a ‘lib/sub_domain.rb’ with the following code:

class AdminSubdomain
  def self.matches?(request)
    subdomain = request.subdomain
    ['admin', 'backend', 'adminpanel'].include? subdomain
  end
end

In my routes.rb file I can now wrap all routes I want under a custom subdomain

# config/routes.rb
TestApp::Application.routes.draw do |map|
  constraints(AdminSubdomain) do
    get "admin/index"
  end

  root :to => "home#index"
end

The admin panel is now reachable at all three subdomains.

Gepost in hor |  0 reacties

nifty rails 3.0 methods

Daniel Willemse di 22 jun 10

So with the Rails 3.0 beta release as of late, I decided to try and play around with a new rails app. I’m pretty new to Ruby and Rails, so trying a new version of Rails (where they also changed routing, one of the things I personally have most problems with) was pretty challenging for me.

For those that haven’t yet checked out the new and cool stuff that’s available with rails 3, here’s some new code versus the 2.x version.
I had a fresh app in rails 2.3.5, which I tried to rebuild in rails 3.0.
In the old app for instance, there was code like:

Invoice.find(:all, :conditions => ["number > ?", 5])
or
Invoice.find(:all, :order => "number DESC")

While in the new and improved rails,
you can write it much easier like this:

Invoice.where("number > ?", 5) and
Invoice.order("number DESC")

The cool part about this, is that you can chain these new methods like this:

Invoice.order("number DESC").where("number > ?" 5)

This technique also works for named scopes. With rails 3.0, you can easily make a named scope as follows:

scope :above_1, where("number > 1")
scope :below_5, where("number < 5")
scope :above_1_and_below_5, above_1.below_5

(notice how it’s no longer called named_scope!!)
These scopes don’t make any sense, but you get the picture.

There are more of these new methods and as I play more with rails 3.0 I’ll try and update

So besides making it easier for you to chain together find options, why would you use these above the find methods
in rails 2.x?
Well I found my answer in a railscast where it is explained that the find option has been revised.
http://railscasts.com/episodes/202-active-record-queries-in-rails-3
In the video, it is shown that for instance, Model.scope (Article.recent in the video) returns an active record relation instead of performing the query. And that the query is performed when you try and get the actual data.

I have not yet been able to reproduce the same queries as in the railscast so I’ve not figured out how it works.
When I do however, I’ll try and share my findings and post an update.

Daniel Willemse

(the code I use, which should show me an active record relation in the console)

class Invoice < ActiveRecord::Base
  scope :above_1, where("number > 1")
  scope :below_5, where("number < 5")
  scope :above_1_and_below_5, above_1.below_5
end

>> i = Invoice.above_2_and_below_5.all

returns:

=>#<Invoice id: 4, …. ….. ….. , updated_at: "2010-06-22 19:50:29">]

Where I expect it to return something along the lines of
#<ActiveRecord::NamedScope::Scope

Gepost in hor |  1 reactie

Welcome to Holland On Rails

This weblog is the official Ruby techblog from the guys at Holder, a Ruby development company. Holder is also the company behind the RubyAndRails Europe Conference in Amsterdam.

Recente Jobs


Bekijk alle jobs »»

Gereedschapskist

Onmisbare tools voor
iedere developer!
Ruby On Rails
Framework voor de web 2.0 developer. Eindelijk vooruitgang!
TextMate
Editor for true pro's
Typ, tab, top :-)
Nee, niet voor Win.
Made On A Mac
En nou is het over met die saaie grijze Windows bak van je!

Auteurs op deze site

Chris Obdam

'Less is more' evangelist, past dit ook dagelijks toe op zijn tandenborstel.

Chiel Wester

Snelheidswonder op Ruby wielen. Leuk om mee te pair-programmen ;-) Recommend Me

Stephan Kaag

Het eerste Rails coreteam- member uit Nederland? Rails evangelist van het eerste uur.

Paul Engel

Én Rails programmeren én interfaces designen? Je zou hem superman kunnen noemen..

Dax Huiberts

Official Zip-Programmer, skinny code is helemaal zijn ding. Haalt meer code weg dan dat er bij komt.

Freek Monteban

Het nieuwste telg uit het Holland on Rails nest! Hij doet niets anders meer!

Johan Vermeulen

De stylesheet-koning uit de kop van Noord-Holland!