Module: ModelLoader

Extended by:
ActiveSupport::Concern
Defined in:
lib/model_loader.rb,
lib/model_loader/railtie.rb,
lib/model_loader/version.rb

Overview

Abstracts a common (at least for me) pattern in Rails applications. When creating controllers that interact with models it is common to have methods called find_<model_name> for your models. This module defines those find_* methods for you.

When executing the find_* methods, if a matching record can not be found, the user is redirected to the 404 page. The find method first tries to find the object using the <model_name>_id parameter if it exists. If not, it tries to find the object using the id parameter.

For example, given a model named Post, find_post will first check to see if params[:post_id] exists. If it does, it will look up the model with the value of that parameter. If it doesn’t exist, params[:id] will be used insted.

The module is activated by calling the can_find_models method:

class ApplicationController < ActionController::Base
  can_find_models
end

Valid options are :only and :except:

class ApplicationController < ActionController
  can_find_models :only => 'Person'
end

Defined Under Namespace

Modules: ClassMethods Classes: Railtie

Constant Summary collapse

VERSION =
"0.0.3"