Design Patterns in Ruby: Introduction

Hello everyone.With this post I would like to start a serie of articles concerning the description of the most common design patterns and how these can be applied with the Ruby language.In the world of software engineering you don't need to reinvent the wheel to face daily problems: these problems are often very common ...

Read more

Rails Best Practices 1: Fat Model – Skinny Controller

Maintain a skinny controller is one of the most important things to increase the readability and testability of the code. Let’s see some practices to move from the logic controller to the model.1. Named ScopeIn this first example we see how to use a named_scope to move the research methods in the model, simplifying the controller.Suppose you have a controller where in the index method we extract the list of sedan cars and that of the station wagon. The first implementation we ca...

Read more

Partial Validation in Rails

It happens sometimes that you have to implement creation or editing of a model splitted on more than one form. A classic case is a registration form divided into two steps or a guided wizard with several pages.This situation presents us with a problem: when and how to validate data?The first idea that may come to mind is simply to leave it all validations in the last page when you go to create or update the model. But this is also the least elegant solutions, both in terms of c...

Read more

How to model a custom search form in Rails

Often you need to create a search form to filter the rows in a table that corresponds to a specific model. "SearchLogic":http://github.com/binarylogic/searchlogic can be a valid solution but maybe you want to bet on a more customizable alternative. The solution I propose is to create a Search.rb c...

Read more

The top 20 plugins to create a Rails application

Rails is now closer to its official third release and all plugins and gems authors are working to ensure compatibility of their products with the new version of the framework.Among the many Rails plugins released by the community, I make a list of 20 most used (and useful) in our projects at DevInterface.Authentication Authlogic: this ...

Read more

Create zip files on the fly with ruby

In a Ruby on Rails application I'm developing, client asked me to add a feature that allows users to download a full picture gallery as a single zip file. Quite obviously, I decided to take advantage of rubyzip gem to create the compressed archive to download. But my idea was to create the zip on the fly, directly in memory, without fill folders of my application with many zip files that I need to ...

Read more

Add current_user to Cucumber step definitions

Cucumber provides a great way to apply BDD during your rails development.Sometimes you need to populate your testing database with background data and sometimes you need to access the1current_usermethod.Generally1current_useris a method defined in the1ApplicationControllerclass or in a user related module. So it’s not accessible to Cucumber’s scenarios: let’s add it a...

Read more

How to implement a viewing system in Rails

Hello everyone. Today I will show how to implement a viewing system for any model of your Rails application. Suppose we have a model News and we would like to keep track of how many times a single news has been displayed, in order to implement box like "the most 'seen'" etc.. Suppose we have a model News created in this way: myapp/db/migrate/001_create_news.rb: class CreateNews true t.d...

Read more

LINQ: serialize C# classes

The version 3.5 of Microsoft’s .NET Framework introudced the LINQ (Language Integrated Query).component.LINQ is a SQL-like language that allows to query different source of data, as objects, xml files and databases.What i’ll show today is how to generate an xml rappresentation of a C# class instance..Suppose to have a C# class Book so defined.123456public class Book { public St...

Read more