background preloader

Helpful ruby/rails patterns/articles

Facebook Twitter

Breadcrumbs on Rails with Gretel. You probably know that breadcrumbs are navigational elements that help users track their current location on a website.

Breadcrumbs on Rails with Gretel

Breadcrumbs can list either previously visited pages or parent pages of the current one (on hierarchical websites). You also probably know where this term comes, but I’ll remind you just to be sure. Wikipedia says that “Hansel and Gretel” is a well-known fairy tale of German origin recorded by Brothers Grimm and published in 1812. This tale tells about a young brother and sister who were left in the woods by their own father because the family had nothing to eat. Lassebunk/gretel. ActiveModel. Provides a way to track changes in your object in the same way as Active Record does.

ActiveModel

The requirements for implementing ActiveModel::Dirty are: include ActiveModel::Dirty in your object.Call define_attribute_methods passing each method you want to track.Call attr_name_will_change! Before each change to the tracked attribute.Call changes_applied after the changes are persisted.Call clear_changes_information when you want to reset the changes information.Call restore_attributes when you want to restore previous data. Domain Logic in Rails. What is Domain Logic?

Domain Logic in Rails

All software has a domain in which it attempts to solve problems. This can be equivalent to a real-world business or to a field of engineering or math. The domain logic is the representation of the business rules of the domain being modeled. How to persist hashes in Rails applications with PostgreSQL. Our recent blogpost about using UUID in Rails projects with PostgreSQL was appreciated so we decided to publish a tutorial about storing hashes in our favorite database.

How to persist hashes in Rails applications with PostgreSQL

Let’s see how it can be achieved. Sometimes we may need to not only store plain attributes like string, integer or boolean but also more complex objects. For now let’s talk about hashes. hstore. Handling Non-Persisted Data. Ajax Sortable Lists Rails 4. 22 Dec 14 jquery, ajax, sorting, plugins Learn how to easily achieve a drag and drop sortable lists functionality in your rails app that updates via ajax With me, is a simple to-do list application where users can create dummy to-do lists and displays them in card-like form just like in Trello.

Ajax Sortable Lists Rails 4

We want to enable users to sort the list by drag and drop so that they are displayed in the specific order the user sets them. You can take a sneak peek at the final result Getting Started. Managing ENV variables in Rails. 20 Oct 14 rails, ruby In this tutorial, I will guide you on an easy method on how to set up environment config variables in Rails.

Managing ENV variables in Rails

Often when developing Rails applications, you will find a need to setup a couple of environment variables to store secure information such as passwords, api keys, secrets, etc. This is done especially when your application's code is open source and you don't want people who access your code to see your passwords, secret keys etc. Setting this variables as environmental variables is safe as these values are stored at the operating system level that your app has access to.

There are many solutions out there like adding this keys in your .bashrc or .zrshrc, using gems such as Dotenv and Figaro. Private Inbox System in Rails with Mailboxer. 05 Apr 15 rails, messaging, inbox.

Private Inbox System in Rails with Mailboxer

Drag n drop

Ajax Sortable Lists Rails 4. Active Record scopes vs class methods. Here at Plataformatec we use Github Pull Requests a lot for code review and this usually yields tons of constructive comments and excellent discussions from time to time.

Active Record scopes vs class methods

One of the recent topics was about whether we should use scopes or class methods throughout the project to be consistent. It’s also not hard to find discussions about it all over the internet. The classic comment usually boils down to “there is no difference between them” or “it is a matter of taste”. The Detailed Guide on How Ajax Works with Ruby on Rails. This is a tutorial for ajax use with Rails conventions.

The Detailed Guide on How Ajax Works with Ruby on Rails

For illustrative purposes, we’ll build a single-page task list app. For reference and convenience, I have created 2 github repos: Static Task List: A textbook example on creating a basic and static task list. It also serves the purpose of demonstrating an app with excess navigation that can be significantly improved with ajax for an enhanced user experience. Rai-jax: This tutorial serves an illustrative example of the significant improvement a little ajax and some styling can provide to an otherwise dull and static app.

Sorting

Improving your Web App with Functional Object-Oriented Design. Object-oriented and functional programming, two households, both alike in dignity… except in this story, they’re not foes.

Improving your Web App with Functional Object-Oriented Design

Turns out that if you’re writing a web app, they’re perfect compliments, like barbecue and beer. A perfect meal has both. Using objects in a functional style makes a web app clean and testable, but it takes a few carefully considered design patterns, and they’re probably not the ones you’re used to. Server-Side Web To be clear, I’m talking about server-side web development. Enough with the domain models. Sandi Metz' Rules For Developers. Back in January, Sandi Metz introduced her rules for developers in a Ruby Rogues podcast episode episode.

Around the time Sandi’s rules were published, the team I am on was starting a new project. This post details the experience of that team applying Sandi’s rules to the new application. The rules Here are the rules: Classes can be no longer than one hundred lines of code.Methods can be no longer than five lines of code.Pass no more than four parameters into a method. Facade Pattern with POROs - Jumpstart Lab Curriculum. Models As projects grow in size the models tend to grow in complexity. Let’s look at a few strategies for managing the situation. Background The core issue is that ActiveRecord classes mix two roles: persistence and business logic. This is convenient, especially when first starting an application, but it violates the "Single Responsibility Principle. " From Wikipedia: In object-oriented programming, the single responsibility principle states that every object should have a single responsibility, and that responsibility should be entirely encapsulated by the class.

Facade Pattern Example – Blog – isotope. After reading a great article on Sandi Metz' rules for developers. I thought I'd do a generalized example on the Facade Pattern that was used to solve the "Only instantiate one object in the controller" rule. Structural Design Patterns - Issue 1.26. Practicing Ruby Issue 1.26 :: Published by Gregory Brown on February 28, 2011 In this two part series, I've been looking at the classical design patterns laid out by the Gang of Four and exploring their relevance to modern day Ruby programming. Rather than teaching the design patterns themselves, my goal is to give practical examples using real code so that you can consider what the strengths and weaknesses of these techniques are. In this issue, I'll be focusing on the structural design patterns, all of which have to do with the relationships between clusters of code. The seven patterns listed below are the ones we'll focus on in this article.