Module: Resourcelogic::Aliases

Included in:
ActionController::Base
Defined in:
lib/resourcelogic/aliases.rb

Overview

This module let’s you define various aliases for your controller. For example, lets say you have the following routes:

/account/addresses        => UsersController
/admin/users/5/addresses  => UsersController

Here is how your AddressesController would look:

class AddressesController < ResourceController
  belongs_to :user
end

The problem is that sometimes the parent object is called user, sometimes its called account. So the solution is to do:

class ResourceController < ApplicationController
  route_alias :account, :user
end

Now ResourceLogic knows that when it see account in the URL it will know the grab the User model.

Now I know an alternative could be to do somethig like:

belongs_to :user, :alias => :account

The above presents a problem. Take the following URL:

/productos/1/pictures/4/comments

In order for Resourcelogic to do its magic relative URLs, it needs to know what model “productos” should be using. Which should be the Product model, yet we can’t define that in the CommentsController because it’s 2 levels above, and we only specify the parent.

Now a lot of people say you should never nest more than 2 levels deep, and this is absolutely true 95% of the time. But what if I want to link back to the parent object from the comments controller and preserve it’s context? In order to do this I have to go 3 levels deep, because maybe context for the PicturesController is really important / required. The only way to preserve context is with the URL.

Sorry for rambling, this documentation is really more of an internal note for me and to hopefully clarify why I took this approach.

Defined Under Namespace

Modules: Config, InstanceMethods

Class Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



46
47
48
49
50
51
# File 'lib/resourcelogic/aliases.rb', line 46

def self.included(klass)
  klass.class_eval do
    extend Config
    include InstanceMethods
  end
end