Saturday, November 22, 2008

Mommy Look, new Plugin!

I've launched new simple plugin for Rails/ActiveRecord.

This one extends the belongs_to associations between models and allows you to assign related units by one (or several) fields for the model. Say you have got a message which belongs to a user, and you would like to assign the user to the message by his login name. That's our case. Use the plugin and everything will be fine! 8)

http://github.com/MadRabbit/assign_by/tree/master

Thursday, November 6, 2008

SearchableBy, the ActiveRecord plugin

I've created some simple plugin for Rails/ActiveRecord, which you can use for search methods definitions.

It is a simple stuff which just creates some named_scopes which you case use as various search methods.

Take a look at the README file at the repository http://github.com/MadRabbit/searchable_by there are some examples.

Friday, October 10, 2008

Writting own Form Helper for Rails

The plot of the story is following. Say I've got some simple models, like


Country
- name

City
- name
- country

Customer
- city

A city belongs to a country and a customer belongs to a city. And then on the customer form I would like to have a select box of cities with selection groups by country.

Well, that's not a problem, we can do that.


<%= select_tag "user[city_id]",
option_groups_from_collection_for_select(
Country.all, :cities, :name, :id, :name, @user.city_id
), :id => "user_city_id" %>

Not pretty but works. But then we add another unit

Office
- name
- city

Customer
- office

Now each customer belongs to an office, and we would like to write another drop-down list with groups on the customer's form.

We have already got some not-pretty stuff on the form. So, what you gonna do about that?

Obviously we should wrap this puppy up.
How?
Well, that's the question.

Certainly we can write yet another method in our helpers, and be happy, but I would like to be not just happy, but feel myself kinda sexy as well. So, I would like to write it in such a way that it was look like a standard form-element. Like that.


<%= f.grouped_collection_select :city_id, Country.all, :cities %>
<%= f.grouped_collection_select :office_id, Countries.all, :offices %>

How can we achieve such a beauty?
Simple.
You just need to know some simple things.

First of all, you should know about the existence of the class ActionView::Helpers::FormBuilder. This class is the class which instance you use in the forms building with rails. And that's the class we are going to extend.

The second thing you should know is the existence of the instance variables of the FormBuilder class, which you can use when you are building your own methods

@template - this is a reference to the current template processing object. Each helper method you call in your templates can be called as the variable method inside the FormBuilder class.
@object - a reference to the current model instance which the form covers.
@object_name - the name of the model variable you use to build your form.

The third thing you should know is how to extend classes and modules on fly. Say we create a special separated helper for our new form elements.


module FormHelper
def self.included(base)
ActionView::Helpers::FormBuilder.instance_eval do
include FormBuilderMethods
end
end

module FormBuilderMethods
def grouped_collection_select(method, collection, sub_name,
group_method="name", unit_id="id", unit_name="name")
@template.select_tag "#{@object_name}[#{method}]",
@template.option_groups_from_collection_for_select(
collection, sub_name, group_method, unit_id,
unit_name, @object.send(method)
), :id => "#{@object_name}_#{method}"
end
end
end

We define some module of ours, where new form-methods will be defined and then we include the module inside the FormBuilder class when our helper will get included to the system. And I've added some default values to the method, case my models all have uniform "name" method, so I didn't need to repeat that things over and over.

And voila, this is it. Enjoy!

Sunday, September 21, 2008

Nested Styles with Ruby and FrontCompiler

Nested styles is a feature which some people dream about for quite a long time. The idea is pretty simple and beautiful.

Say you have got some css like that.

div.article div.title {
font-weight: bold;
}
div.article div.title a.author {
padding-left: 12px;
background: url('user.png') no-repeat left;
}
div.article div.text {
border: 1px solid #EEE;
background: #FEF;
}

And now imagine that you could write it with nested constructions like that.

div.article {
div.title {
font-weight: bold;

a.author {
padding-left: 12px;
background: url('user.png') no-repeat left;
}
}
div.text {
border: 1px solid #EEE;
background: #FEF;
}
}

See the difference? How much more simple, clean and DRYish your css could be! Unfortunately current browsers don't support such things. At lest not yet.

So, does anybody want to sit down and wait for the future coming?

No, say 'no', you don't need tell such terrible things, because we have added the feature emulation in the FrontCompiler project. Which is a JavaScript/CSS/HTML processor written in Ruby and which can be used as a Rails plugin.

From now one, each time when you process a css file with FrontCompiler, it will automatically convert your nested styles in the way browsers can understand. So you can easily and safe use the nested styles advantages.

Have fun!

Monday, September 15, 2008

FrontCompiler Update

I have updated the JavaScript part of the the presented recently FrontCompiler. I've improved the compression algorithm, got rid of some old bugs, rewritten the code in a more clear way and made it work several times faster.

So now, it's better, faster, cooler, nicer, whatever.

And I have compressed the Prototype / ScriptAculo stuff, with the new compressor and shared them at here.

Try it, that's nice and free! 8)

Thursday, August 21, 2008

Javascript/CSS/HTML Compiler in Ruby

I have backed some pretty tasteful cookie in here!

Met FrontCompiler!

It's a collection of scripts which allows you to compact your JavasScript, CSS and HTML code to reduce the loading time, and it can be used with Rails/Merb/Whatever.

NOTE: in test of compacting the Prototype library the script beats famous YUI compiler on about 1 KByte.

Enjoy! 8)

Wednesday, August 6, 2008

The Mines Game in JavaScript

I have been playing a cool javascript hacker this weekend and as the result have implemented the well known game of mines in pure JavaScript/CSS/HTML. Take a look it's pretty nice.

JSMiner Game