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
100644 92 lines (67 sloc) 2.884 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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