Skip to content

Instantly share code, notes, and snippets.

@lukemelia
Created August 20, 2009 17:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukemelia/171220 to your computer and use it in GitHub Desktop.
Save lukemelia/171220 to your computer and use it in GitHub Desktop.
diff --git a/lib/admin_assistant/request/index.rb b/lib/admin_assistant/request/index.rb
index de6b761..f0371e5 100644
--- a/lib/admin_assistant/request/index.rb
+++ b/lib/admin_assistant/request/index.rb
@@ -15,6 +15,9 @@ class AdminAssistant
@admin_assistant, @controller.params, controller_methods
)
@controller.instance_variable_set :@index, index
+ if @controller.respond_to?(:before_render_for_index)
+ @controller.send :before_render_for_index
+ end
render_template_file
end
diff --git a/test_rails_app/app/controllers/admin/blog_posts2_controller.rb b/test_rails_app/app/controllers/admin/blog_posts2_controller.rb
index 9936ae0..88842ce 100644
--- a/test_rails_app/app/controllers/admin/blog_posts2_controller.rb
+++ b/test_rails_app/app/controllers/admin/blog_posts2_controller.rb
@@ -81,6 +81,13 @@ class Admin::BlogPosts2Controller < ApplicationController
end
end
+ # This is run in the controller context, just before render is called
+ def before_render_for_index
+ if @index.records.any?
+ @var_set_by_before_render_for_index_hook = 'Confirmed that we have some records'
+ end
+ end
+
# By default, only show unpublished blog posts unless params[:all] is
# passed in
def conditions_for_index
diff --git a/test_rails_app/app/views/admin/blog_posts2/_after_index.html.erb b/test_rails_app/app/views/admin/blog_posts2/_after_index.html.erb
index 41a3e16..ad98888 100644
--- a/test_rails_app/app/views/admin/blog_posts2/_after_index.html.erb
+++ b/test_rails_app/app/views/admin/blog_posts2/_after_index.html.erb
@@ -1 +1 @@
-<div id="after_index">Custom HTML rendered after the index</div>
+<div id="after_index">Custom HTML rendered after the index</div>
\ No newline at end of file
diff --git a/test_rails_app/app/views/admin/blog_posts2/_before_index.html.erb b/test_rails_app/app/views/admin/blog_posts2/_before_index.html.erb
index 00ddb6b..0fbb54b 100644
--- a/test_rails_app/app/views/admin/blog_posts2/_before_index.html.erb
+++ b/test_rails_app/app/views/admin/blog_posts2/_before_index.html.erb
@@ -1 +1,2 @@
+<%= @var_set_by_before_render_for_index_hook %>
<div id="before_index">Custom HTML rendered before the index</div>
diff --git a/test_rails_app/spec/controllers/admin/blog_posts2_controller_spec.rb b/test_rails_app/spec/controllers/admin/blog_posts2_controller_spec.rb
index 470ff14..aef1205 100644
--- a/test_rails_app/spec/controllers/admin/blog_posts2_controller_spec.rb
+++ b/test_rails_app/spec/controllers/admin/blog_posts2_controller_spec.rb
@@ -285,6 +285,11 @@ describe Admin::BlogPosts2Controller do
without_tag("input[type=checkbox][name=?]", "search[body(blank)]")
end
end
+
+ it 'should output ivar set by index_before_render controller hook' do
+ assigns(:var_set_by_before_render_for_index_hook).should_not be_nil
+ response.body.should match(/Confirmed that we have some records/)
+ end
end
describe 'when there is one published post and one unpublished post' do
@@ -309,6 +314,7 @@ describe Admin::BlogPosts2Controller do
response.body.should_not match(/--published--/)
end
end
+
end
describe '#index when there is 1 blog post and 16 Users' do
diff --git a/website/api/index.markdown b/website/api/index.markdown
index 91d426c..1771b59 100644
--- a/website/api/index.markdown
+++ b/website/api/index.markdown
@@ -119,6 +119,25 @@ In a number of cases, you can get the same customization by passing a block to t
The trade-off here is that customizing this behavior through a block is probably a little neater---fewer methods scattered all over your controller---but isn't bound to the controller. So if you find yourself needing some convenience methods on, say, `ApplicationController`, you'll be better off using a protected controller method.
+#### before_render_for_index
+
+If defined on your controller, this hook is executed just before render takes place for the index action
+
+ class Admin::BlogPostsController < ApplicationController
+ layout 'admin'
+
+ admin_assistant_for BlogPost
+
+ protected
+
+ # Blog post authors should not be visible if they have requested anonymity
+ def before_render_for_index
+ @index.records.each do |record|
+ record.author = nil if record.author.requested_anonymity?
+ end
+ end
+ end
+
#### Helper methods and partials
Other behaviors can be set by creating certain methods on the helper. Usually these behaviors have to do with presentation.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment