Permalink
Browse files

Removed routes related to user (rezm_user used instead)

Use Rails 2 new form_for syntax
Corrected bug in atom feed (inbox)
Force remove all layouts for atom, since using a layout in controller will break the atom feed.
updated readme
fixed users.yml (restful_authentication does not provide 'name' field for users
added fixme message
  • Loading branch information...
1 parent c98d772 commit 05041739ff60fc99d606f74876eb1127ac7d07a8 @gravis gravis committed Jun 27, 2008
View
90 README
@@ -1 +1,89 @@
-RESTful_Easy_Messages Plug-in
+RESTful_Easy_Messages Plug-in
+
+Mostrar / Ocultar Avisos
+RESTful_Easy_Messages
+
+ * 16th October , 2007 by Sam in Ruby on Rails
+ * 31 comments
+
+So three month ago I released my first Rails plug-in, Easy_Messages (EZM), and I was pleasantly surprised by the response, excited that people were actually using my code. Then I became paranoid as people were actually using my code! Since then I worked briefly on a project which was written with REST in mind and was forced to look into it. Up to that point I had been doing my best to not meet REST in the hallway as I was a little scared by him. I don’t know why? After watching the Peepcode screencast by Geoffrey Grosenbach, everything clicked and I realized that I could make the code for EZM much better. The end result is this plug-in. I hope you find it useful.
+
+== The code is hosted at GitHub.
+
+Here’s how to install the plug-in. (Rails 2.1 required for git plug-ins)
+
+./script/plugin install git://github.com/sschroed/restful_ezm.git
+
+== Here’s how to run the generator.
+
+For standard html views: ./script/generate messages erb
+For haml[1] views: ./script/generate messages haml
+
+[1] You will need to install the haml plug-in for the views to render properly.
+
+I’ve tried to decouple as much of the code as I could with this release. If you used Easy_Messages you’ll remember most of the code was stuck in the plug-in directory. With REZM the generator will put a controller, helper, model, tests, and a few other support files right into your project for easy access. To see the entire list view the FILELIST in plugins/restful_easy_messages. There is still a tiny bit of code in the plug-in though.
+
+If you are using Rick Olson’s RESTful_Authentication you can get REZM up and running with minimal setup as I pulled it from a project that uses it.
+
+First, update the user model.
+view plainprint?
+
+ 1. class User < ActiveRecord::Base
+ 2. restful_easy_messages
+ 3. # The rest of your class
+ 4. #
+ 5. #
+ 6. end
+
+class User < ActiveRecord::Base restful_easy_messages # The rest of your class # # end
+
+Then add the REZM routes.
+view plainprint?
+
+ 1. # Add these names routes to your project's config/routes.rb
+ 2.
+ 3. map.resources :users do |user|
+ 4. user.resources :messages,
+ 5. :collection => {:destroy_selected => :post,
+ 6. :inbox => :get,
+ 7. :outbox => :get,
+ 8. :trashbin => :get},
+ 9. :member => {:reply => :get}
+ 10. end
+
+# Add these names routes to your project's config/routes.rb map.resources :users do |user| user.resources :messages, :collection => {:destroy_selected => :post, :inbox => :get, :outbox => :get, :trashbin => :get}, :member => {:reply => :get} end
+
+Now run db:migrate and you should be good to go.
+
+But what if you didn’t use restful_authentication? Having to use Acts_As_Authenticated for EZM was the biggest complaint that I heard so I made REZM with hooks for you to switch out R_A if you want. Open lib\restful_easy_messages_controller_system.rb to do so. Just replace the current_user and login_required methods with calls to similar ones in your application.
+view plainprint?
+
+ 1. module RestfulEasyMessagesControllerSystem
+ 2. protected
+ 3.
+ 4. # This method provides an abstraction layer to the REZM controller for the
+ 5. # "current", logged-in user in case you are not using Restful_Authentication.
+ 6. def rezm_user
+ 7. # Provide your version of current_user here if not using Restful_authentication
+ 8. current_user
+ 9. end
+ 10.
+ 11. # This method provides an abstraction layer to the REZM controller for
+ 12. # requiring a user to be loggefd in if you are not using Restful_Authentication.
+ 13. def rezm_login_required
+ 14. # Provide your version of login_required here if not using Restful_authentication
+ 15. login_required
+ 16. end
+ 17.
+ 18. # Inclusion hook to make #rezm_user
+ 19. # available as ActionView helper methods.
+ 20. def self.included(base)
+ 21. base.send :helper_method, :rezm_user
+ 22. end
+ 23. end
+
+module RestfulEasyMessagesControllerSystem protected # This method provides an abstraction layer to the REZM controller for the # "current", logged-in user in case you are not using Restful_Authentication. def rezm_user # Provide your version of current_user here if not using Restful_authentication current_user end # This method provides an abstraction layer to the REZM controller for # requiring a user to be loggefd in if you are not using Restful_Authentication. def rezm_login_required # Provide your version of login_required here if not using Restful_authentication login_required end # Inclusion hook to make #rezm_user # available as ActionView helper methods. def self.included(base) base.send :helper_method, :rezm_user end end
+
+I believe that is it. Oh wait, there is also an Atom feed for the inbox!
+
+If you wish to try out REZM I’ve set up a sample app. You can message the user “sam” if you want to test writing a mesasge.
View
@@ -1,36 +1,34 @@
# Add these names routes to your project's config/routes.rb
-map.resources :users do |user|
- user.resources :messages,
- :collection => {:destroy_selected => :post,
- :inbox => :get,
- :outbox => :get,
- :trashbin => :get},
- :member => {:reply => :get}
-end
+map.resources :messages,
+ :collection => {:destroy_selected => :post,
+ :inbox => :get,
+ :outbox => :get,
+ :trashbin => :get},
+ :member => {:reply => :get}
# Here is a list of all the route created by Restful_EZM. Rake routes was used to generate this
- inbox_user_messages GET /users/:user_id/messages/inbox {:controller=>"messages", :action=>"inbox"}
- formatted_inbox_user_messages GET /users/:user_id/messages/inbox.:format {:controller=>"messages", :action=>"inbox"}
- outbox_user_messages GET /users/:user_id/messages/outbox {:controller=>"messages", :action=>"outbox"}
- formatted_outbox_user_messages GET /users/:user_id/messages/outbox.:format {:controller=>"messages", :action=>"outbox"}
- trashbin_user_messages GET /users/:user_id/messages/trashbin {:controller=>"messages", :action=>"trashbin"}
- formatted_trashbin_user_messages GET /users/:user_id/messages/trashbin.:format {:controller=>"messages", :action=>"trashbin"}
- destroy_selected_user_messages POST /users/:user_id/messages/destroy_selected {:controller=>"messages", :action=>"destroy_selected"}
-formatted_destroy_selected_user_messages POST /users/:user_id/messages/destroy_selected.:format {:controller=>"messages", :action=>"destroy_selected"}
- user_messages GET /users/:user_id/messages {:controller=>"messages", :action=>"index"}
- formatted_user_messages GET /users/:user_id/messages.:format {:controller=>"messages", :action=>"index"}
- POST /users/:user_id/messages {:controller=>"messages", :action=>"create"}
- POST /users/:user_id/messages.:format {:controller=>"messages", :action=>"create"}
- new_user_message GET /users/:user_id/messages/new {:controller=>"messages", :action=>"new"}
- formatted_new_user_message GET /users/:user_id/messages/new.:format {:controller=>"messages", :action=>"new"}
- edit_user_message GET /users/:user_id/messages/:id/edit {:controller=>"messages", :action=>"edit"}
- formatted_edit_user_message GET /users/:user_id/messages/:id/edit.:format {:controller=>"messages", :action=>"edit"}
- reply_user_message GET /users/:user_id/messages/:id/reply {:controller=>"messages", :action=>"reply"}
- formatted_reply_user_message GET /users/:user_id/messages/:id/reply.:format {:controller=>"messages", :action=>"reply"}
- user_message GET /users/:user_id/messages/:id {:controller=>"messages", :action=>"show"}
- formatted_user_message GET /users/:user_id/messages/:id.:format {:controller=>"messages", :action=>"show"}
- PUT /users/:user_id/messages/:id {:controller=>"messages", :action=>"update"}
- PUT /users/:user_id/messages/:id.:format {:controller=>"messages", :action=>"update"}
- DELETE /users/:user_id/messages/:id {:controller=>"messages", :action=>"destroy"}
- DELETE /users/:user_id/messages/:id.:format {:controller=>"messages", :action=>"destroy"}
+ inbox_messages GET /messages/inbox {:action=>"inbox", :controller=>"messages"}
+ formatted_inbox_messages GET /messages/inbox.:format {:action=>"inbox", :controller=>"messages"}
+ outbox_messages GET /messages/outbox {:action=>"outbox", :controller=>"messages"}
+ formatted_outbox_messages GET /messages/outbox.:format {:action=>"outbox", :controller=>"messages"}
+ trashbin_messages GET /messages/trashbin {:action=>"trashbin", :controller=>"messages"}
+ formatted_trashbin_messages GET /messages/trashbin.:format {:action=>"trashbin", :controller=>"messages"}
+ destroy_selected_messages POST /messages/destroy_selected {:action=>"destroy_selected", :controller=>"messages"}
+formatted_destroy_selected_messages POST /messages/destroy_selected.:format {:action=>"destroy_selected", :controller=>"messages"}
+ messages GET /messages {:action=>"index", :controller=>"messages"}
+ formatted_messages GET /messages.:format {:action=>"index", :controller=>"messages"}
+ POST /messages {:action=>"create", :controller=>"messages"}
+ POST /messages.:format {:action=>"create", :controller=>"messages"}
+ new_message GET /messages/new {:action=>"new", :controller=>"messages"}
+ formatted_new_message GET /messages/new.:format {:action=>"new", :controller=>"messages"}
+ edit_message GET /messages/:id/edit {:action=>"edit", :controller=>"messages"}
+ formatted_edit_message GET /messages/:id/edit.:format {:action=>"edit", :controller=>"messages"}
+ reply_message GET /messages/:id/reply {:action=>"reply", :controller=>"messages"}
+ formatted_reply_message GET /messages/:id/reply.:format {:action=>"reply", :controller=>"messages"}
+ message GET /messages/:id {:action=>"show", :controller=>"messages"}
+ formatted_message GET /messages/:id.:format {:action=>"show", :controller=>"messages"}
+ PUT /messages/:id {:action=>"update", :controller=>"messages"}
+ PUT /messages/:id.:format {:action=>"update", :controller=>"messages"}
+ DELETE /messages/:id {:action=>"destroy", :controller=>"messages"}
+ DELETE /messages/:id.:format {:action=>"destroy", :controller=>"messages"}
@@ -1,9 +1,9 @@
atom_feed do |feed|
- feed.title "#{@rezm_user.login.capitalize}'s Inbox"
+ feed.title "#{rezm_user.login.capitalize}'s Inbox"
feed.updated @messages.first.created_at
for message in @messages
- feed.entry(message, :url => user_message_url(rezm_user, message)) do |entry|
+ feed.entry(message, :url => message_url(message)) do |entry|
entry.title message.subject
entry.content message.body, :type => 'html'
@@ -2,7 +2,7 @@
<div><%= flash[:notice] %></div>
<div><%= rezm_menu -%></div>
<p></p>
-<% form_for :message, :url => destroy_selected_user_messages_path do |form| %>
+<% form_for :message, :url => destroy_selected_messages_path do |form| %>
<table border="1">
<tr>
<th></th>
@@ -7,7 +7,7 @@ class MessagesController < ApplicationController
# GET /messages
def index
- redirect_to inbox_user_messages_url
+ redirect_to inbox_messages_url
end
# GET /messages/1
@@ -37,7 +37,7 @@ def create
respond_to do |format|
if @message.save
flash[:notice] = 'Message was sent successfully.'
- format.html { redirect_to outbox_user_messages_path }
+ format.html { redirect_to outbox_messages_path }
else
format.html { render :action => "new" }
end
@@ -68,11 +68,10 @@ def destroy
def inbox
session[:mail_box] = "inbox"
@messages = rezm_user.inbox_messages
-
respond_to do |format|
format.html { render :action => "index" }
format.xml { render :xml => @messages.to_xml }
- format.atom { render :action => "index" }
+ format.atom { render :action => "index", :layout => false }
end
end
@@ -100,13 +99,14 @@ def trashbin
# GET /messages/1/reply
def reply
- @message = Message.find_by_id(params[:id])
+ replied_message = Message.find(params[:id])
+ @message = Message.new
respond_to do |format|
- if can_view(@message)
- @message.recipient = @message.sender_name
- @message.subject = "Re: " + @message.subject
- @message.body = "\n\n___________________________\n" + @message.sender_name + " wrote:\n\n" + @message.body
+ if can_view(replied_message)
+ @message.recipient = replied_message.sender_name
+ @message.subject = "Re: " + replied_message.subject
+ @message.body = "\n\n___________________________\n" + replied_message.sender_name + " wrote:\n\n" + replied_message.body
format.html { render :action => "new" }
else
headers["Status"] = "Forbidden"
@@ -126,7 +126,7 @@ def destroy_selected
end
format.html { redirect_to current_mailbox }
else
- format.html { redirect_to inbox_user_messages_path }
+ format.html { redirect_to inbox_messages_path }
end
end
end
@@ -142,13 +142,13 @@ def can_view(message)
def current_mailbox
case session[:mail_box]
when "inbox"
- inbox_user_messages_path
+ inbox_messages_path
when "outbox"
- outbox_user_messages_path
+ outbox_messages_path
when "trashbin"
- trashbin_user_messages_path
+ trashbin_messages_path
else
- inbox_user_messages_path
+ inbox_messages_path
end
end
@@ -18,67 +18,60 @@ def setup
def test_should_get_index
login_as("sam")
- get :index, :user_id => users(:sam)
+ get :index
assert_response :redirect
- assert_redirected_to inbox_user_messages_path
+ assert_redirected_to inbox_messages_path
end
def test_should_get_inbox
login_as("sam")
- get :inbox, :user_id => users(:sam)
+ get :inbox
assert_response :success
end
def test_should_get_outbox
login_as("sam")
- get :outbox, :user_id => users(:sam)
+ get :outbox
assert_response :success
end
def test_should_get_trashbin
login_as("sam")
- get :inbox, :user_id => users(:sam)
+ get :inbox
assert_response :success
end
def test_should_get_new
login_as("sam")
- get :new, :user_id => users(:sam)
+ get :new
assert_response :success
end
- def test_should_route_messages_of_user
- options = { :controller => 'messages',
- :action => 'inbox',
- :user_id => '3' }
- assert_routing('users/3/messages/inbox', options)
- end
-
def test_should_get_message
login_as("sam")
- get :show, :user_id => users(:sam), :id => messages(:abby_to_sam_1)
+ get :show, :id => messages(:abby_to_sam_1)
assert_response :success
end
def test_should_protect_message
login_as("sam")
- get :show, :user_id => users(:sam), :id => messages(:abby_to_catie_1)
+ get :show, :id => messages(:abby_to_catie_1)
assert_response :forbidden
end
def test_should_create_message
message_count = Message.count
login_as("sam")
- post :create, :user_id => users(:sam), :message => {:recipient => 'abby', :subject => "hi", :body => "Abby, how was school today?"}
+ post :create, :message => {:recipient => 'abby', :subject => "hi", :body => "Abby, how was school today?"}
assert_response :redirect
- assert_redirected_to outbox_user_messages_path
+ assert_redirected_to outbox_messages_path
assert_equal message_count + 1, Message.count
end
def test_should_delete_received_message
message_pre_delete_state = Message.find(messages(:abby_to_sam_1))
login_as("sam")
- delete :destroy, :user_id => users(:sam), :id => messages(:abby_to_sam_1)
+ delete :destroy, :id => messages(:abby_to_sam_1)
assert_response :redirect
message_post_delete_state = Message.find(messages(:abby_to_sam_1))
assert_not_equal message_pre_delete_state.receiver_deleted, message_post_delete_state.receiver_deleted
@@ -87,7 +80,7 @@ def test_should_delete_received_message
def test_should_delete_sent_message
message_pre_delete_state = Message.find(messages(:sam_to_abby_1))
login_as("sam")
- delete :destroy, :user_id => users(:sam), :id => messages(:sam_to_abby_1)
+ delete :destroy, :id => messages(:sam_to_abby_1)
assert_response :redirect
message_post_delete_state = Message.find(messages(:sam_to_abby_1))
assert_not_equal message_pre_delete_state.sender_deleted, message_post_delete_state.sender_deleted
@@ -96,21 +89,21 @@ def test_should_delete_sent_message
def test_should_not_delete_message
message_pre_delete_state = Message.find(messages(:abby_to_sam_1))
login_as("sam")
- delete :destroy, :user_id => users(:sam), :id => messages(:abby_to_catie_1)
+ delete :destroy, :id => messages(:abby_to_catie_1)
assert_response :forbidden
message_post_delete_state = Message.find(messages(:abby_to_sam_1))
assert_equal message_pre_delete_state.receiver_deleted, message_post_delete_state.receiver_deleted
end
def test_should_reply_to_message
login_as("sam")
- get :reply, :user_id => users(:sam), :id => messages(:abby_to_sam_1)
+ get :reply, :id => messages(:abby_to_sam_1)
assert_response :success
end
def test_should_not_reply_to_message
login_as("sam")
- get :reply, :user_id => users(:sam), :id => messages(:abby_to_catie_1)
+ get :reply, :id => messages(:abby_to_catie_1)
assert_response :forbidden
end
end
Oops, something went wrong.

0 comments on commit 0504173

Please sign in to comment.