Your friends at Viget present Extend, a Code & Technology Blog

ActiveAdmin Filters—Unlocking the Power of Ransack

For those who aren't familiar with ActiveAdmin, it provides a nice out-of-the-box admin interface for Rails projects that has a pretty solid DSL. If you've never heard of ActiveAdmin or used it before -- check out the documentation!

ActiveAdmin has some pretty great out-of-the-box filters on resource index pages, which lets you search on a field-by-field basis to scope the collection of records. On each index page, any field you have on a given model will get a corresponding filter based on the underlying column's data type or whether or not it represents an association.

default_filters

ActiveAdmin takes care of the DSL for defining filters as well as how they look on a page, but all the heavy lifting is done behind the scenes with the Ransack gem. If we want to do sweet, sweet things with filters, one must first learn how to harness the power of Ransack.


Visualization is for Sharing: Using React for Portable Data Visualization

Data visualization has become a more frequent element of our work at Viget. Be it simple pie charts or beautiful maps displaying jersey sales, visually representing data in a compelling way is a great device for storytelling and provides rich fuel for social sharing.

Yet making these illustrations fast, shareable, and accessible has its challenges. Although browsers are powerful, tightly coupling the rendering process to them greatly complicates the generation of images that can be shared or saved for presentations.


Why We Love Craft CMS

Because it’s awesome.

Mic Drop

Megan wrote a post last week discussing Craft and WordPress that sparked an internal discussion about the difficulty we’ve encountered selling less popular content management systems—basically, anything other than WordPress or Drupal—because so many clients have only heard of the big boys. Viget has dealt with this issue for quite a while since we have long been a proponent of ExpressionEngine. Having had extensive CMS experience, including experience building my own, Craft is the best CMS that I have ever used. Hopefully, this post will help explain to both developers and clients why I’ve come to this conclusion.


Exploring the `:inverse_of` Option on Rails Model Associations

Ever seen the :inverse_of option on an association and wondered what it did and why it was there?

class Criminal < ActiveRecord::Base
  belongs_to :prison, inverse_of: :criminals
end

class Prison < ActiveRecord::Base
  has_many :criminals, inverse_of: :prison
end

The first time I saw something like that, it seemed like an unnecessary distinction to make. Why is :inverse_of a thing?

Memory Optimization When Fetching Associated Records

It turns out that associated objects do not point to the same in-memory objects by default. To illustrate:

prison   = Prison.create(name: 'Bad House')
criminal = prison.criminals.create(name: 'Krazy 8')

# Without :inverse_of
criminal.prison == prison
# Prison Load (0.1ms)  SELECT "prisons".* FROM "prisons" WHERE "prisons"."id" = 2 LIMIT 1
# => true

# With :inverse_of
criminal.prison == prison
# => true

When we call criminal.prison without :inverse_of on both the :belongs_to and :has_many associations, it will hit the database. With :inverse_of, if we already have that prison record in memory then criminal.prison will point to the same prison.

It's worth clarifying that this only saves you a database look-up when going from Criminal to Prison (:belongs_to direction). Here's some console output going from the other direction:

prison = Prison.last
# Prison Load (0.1ms)  SELECT "prisons".* FROM "prisons" ORDER BY "prisons"."id" DESC LIMIT 1
# => #<Prison id: 3, name: "Broadmoor", created_at: "2014-10-10 20:26:38", updated_at: "2014-10-10 20:26:38">

criminal = prison.criminals.first
# Criminal Load (0.3ms)  SELECT "criminals".* FROM "criminals" WHERE "criminals"."prison_id" = 3 LIMIT 1
# => #<Criminal id: 3, name: "Charles Bronson", prison_id: 3, created_at: "2014-10-10 20:26:47", updated_at: "2014-10-10 20:26:47">

prison.criminals.first == criminal
# Criminal Load (0.2ms)  SELECT "criminals".* FROM "criminals" WHERE "criminals"."prison_id" = 3 LIMIT 1
# => true

A model's associations, as far as memory is concerned, are one-way bindings. The :inverse_of option basically gives us two-way memory bindings when one of the associations is a :belongs_to. A memory optimization isn't the only thing that :inverse_of gets you. Next, we'll take a look at two use cases involving the creation of associated records.


Craft vs. WordPress: The Good, the Bad, and the Ugly Data

Any web developer who has been working in the industry for more than a few days has probably heard of WordPress. Stay for a couple more months and there’s a good chance you’ve worked on a WordPress site — it’s a popular platform since it’s well-known, easy-to-use, and free.

You may have also heard of ExpressionEngine, Drupal, Joomla, and a few other CMS heavy-hitters. They all have their benefits and flaws, which is a topic for another time. I’d rather talk about the next up-and-comer and my new favorite, Craft.

Craft is a small CMS that was developed fairly recently by ExpressionEngine add-on developers Pixel & Tonic. Having worked with ExpressionEngine for a while, it’s obvious these guys really know the pain-points in any client-facing CMS. Everything they’ve built into Craft solves a problem I’ve had on almost every site I ever made with WordPress. If I had to make a CMS myself, it would probably resemble Craft pretty closely.

So, how does Craft stack up against the industry go-to?