public
Description: Include stylesheets and scripts in a before_filter-esque fashion, and set up simple memoized methods for record fetches.
Homepage: http://www.mbleigh.com/plugins/needy-controllers-drying-stylesheets-scripts-and-fetching
Clone URL: git://github.com/mbleigh/needy-controllers.git
commit  879c6eaf944ac2db895ab13ac849c93f7d3c5321
tree    c3080db111913ee7f69d73502513eff4398d9bd5
parent  5078af0ee40e6f43e4b4a8cbb88d8736cfc4928e
name age message
file README Loading commit data...
file Rakefile
file init.rb
directory lib/
README
NeedyControllers
================

Needy Controllers is a way to DRY up commonly used tasks from both controllers and views 
to save time and effort. It consists of a single call with a number of options.

Description
-----------

With needy controllers, you can include stylesheets, javascripts, and memoized record 
helpers in your controllers and views with a much greater deal of specificity and 
automation than hand-coding.

Installation
------------

script/plugin install http://svn.intridea.com/svn/public/needy_controllers

Usage
=====

Styles and Scripts
------------------

To use Needy Controllers for styles and scripts, you simply call a "needs"  option
inside your controller like so:

class MyController < ApplicationController
  needs :styles => :standard
  needs :styles => :show, :only => :show
  needs :scripts => :behave, :except => :show

  def index
    # this action will have access to the 'standard.css' stylesheet
  end
  
  def show
    # this action will have access to the 'show.css' stylesheet
    # in addition to 'standard.css'
    # but will not have access to 'behave.js'
  end
end

Now that you have created your behavior and style chains, you need to include them 
in the view. Luckily, this is exceedingly easy! Just include :needs in your include 
and link tags like so:

<%= stylesheet_link_tag 'something', :needs %>
<%= javascript_include_tag 'prototype', :needs, 'effects' %>

Model Fetching
--------------

To use Needy Controllers for fetching records, you use it similarly, and it sets 
the instance variable:

class MyController < ApplicationController
  # here's a standard problem
  needs :record => :user, :from => :id, :as => :user
end

The :from and :as options in this example are the defaults (:id for :from and :as 
defaults to the name of the record). This will create a method ("user" in the example)
that will be accessible both from the controller and from the view as a helper. It
will find the record with a matching ID to the URL parameter associated with the
:from option. Therefore if you had nested resources you could call it as such:

needs :record => :user, :from => :user_id

Flushing and Removal of Needs
-----------------------------

If for some reason you want a controller not to inherit existing needs or you for some 
reason want to remove needs, there's a simple way to remove them:

class MyController < ApplicationController
  does_not_need :anything       # removes all styles and behaviors from the need chain
  does_not_need :any_styles
  does_not_need :any_scripts
  does_not_need :styles => "some_style"
  does_not_need :styles => ["some_style", "another_one"]  
  does_not_need :scripts => "some_script"
end

Resources
=========

Created by Michael Bleigh (michael@intridea.com) 
        of Intridea (http://www.intridea.com)

Comments, bugs, and patches go to http://trac.intridea.com/trac/public