Created
March 22, 2012 17:41
-
-
Save jcasimir/2160696 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ApplicationController < ActionController::Base | |
extend AttributeViewable | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module AttributeViewer | |
def attr_viewable(*names) | |
names.each do |name| | |
attr_accessor name | |
helper_method name | |
private "#{name}=".to_sym | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<table> | |
<% messages.each do |message| %> | |
<tr> | |
<td><%= message.subject %></td> | |
<td><%= message.body %></td> | |
<td><%= link_to 'Show', message %></td> | |
<td><%= link_to 'Edit', edit_message_path(message) %></td> | |
<td><%= link_to 'Destroy', message, confirm: 'Are you sure?', method: :delete %></td> | |
</tr> | |
<% end %> | |
</table> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MessagesController < ApplicationController | |
attr_viewable :message, :messages | |
def index | |
self.messages = Message.all | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@myobie you could mix in a module on each request. So something like:
But I strongly advise against doing that. It breaks method caches which will slow method lookup on every request, and couples your code with the fact that the controller is a new instance on every request.
Usage of a context (or container, or presenter, or whatever you want to call it) object allows you to:
nil
to be used)