Permalink
Browse files

new `auto_frill` feature for controllers

  • Loading branch information...
1 parent 9502d9b commit ec44ad63283fe613438756eb8f0c3fe722fd6288 @moonmaster9000 committed Aug 25, 2012
View
20 lib/frill/rails.rb
@@ -1,5 +1,25 @@
+module Frill
+ module Auto
+ protected
+
+ def view_assigns
+ new_hash = {}
+
+ super.each do |key,value|
+ new_hash[key] = frill value
+ end
+
+ new_hash
+ end
+ end
+end
+
module ActionController
class Base
+ def self.auto_frill
+ self.send :include, Frill::Auto
+ end
+
helper_method :frill
private
View
23 readme.markdown
@@ -158,13 +158,36 @@ class ArticlesController < ApplicationController
@article = frill Article.find(params[:id])
respond_with @article
end
+end
```
And that's it. You don't have to update any of your views. Why? When you call the `frill` method inside your controller and pass it an object (or a collection of objects),
frill will attempt to extend the object with any applicable frills (i.e., frills that return `true` for the `frill?` method when passed the object and the request context).
That way, you can simple render your `created_at` attributes without any helpers, and they will automatically present themselves appropriately for their context (e.g., HTML v. JSON requests).
+Note that if prefer, you can configure your controllers to automatically frill all objects for presentation by calling the `auto_frill` method inside your `ApplicationController`, instead of manually having to opt them it via the `frill` method:
+
+```ruby
+class ApplicationController < ActionController::Base
+ auto_frill
+end
+```
+
+Now, you could remove the `frill` from your `ArticlesController`:
+
+```ruby
+class ArticlesController < ApplicationController
+ respond_to :json, :html
+
+ def show
+ @article = Article.find(params[:id])
+ respond_with @article
+ end
+end
+```
+
+Now, any instance variables you create in your controllers will be automatically frilled before handed off to your views.
### 'frill' decorates individual objects _and_ collections
View
9 test_frill_rails/app/controllers/auto_frill_controller.rb
@@ -0,0 +1,9 @@
+class AutoFrillController < ApplicationController
+ auto_frill
+ respond_to :html
+
+ def index
+ @model = Model.new
+ respond_with @model
+ end
+end
View
1 test_frill_rails/app/views/auto_frill/index.erb
@@ -0,0 +1 @@
+<%= raw @model.title %>
View
2 test_frill_rails/config/routes.rb
@@ -2,4 +2,6 @@
root to: "home#index"
match "associations" => "home#associations", as: "associations"
+
+ match "auto_frill" => "auto_frill#index", as: "auto_frill"
end
View
5 test_frill_rails/spec/integration/test_frill_integration_spec.rb
@@ -10,4 +10,9 @@
visit associations_path
page.should have_content "Decorated Title"
end
+
+ it "should let you opt in to automatic frilling by using the auto_frill method in a controller" do
+ visit auto_frill_path
+ page.should have_content "Decorated Title"
+ end
end

0 comments on commit ec44ad6

Please sign in to comment.