Mostrar / Ocultar Avisos

RESTful_Easy_Messages

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.

Then add the REZM routes.

NOTE: Routes have been changed in the lastest release on GitHub. The User is no longer needed. This break backwards compatibility, unfortunately. Thanks to Gravis for the update.

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.

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.

***** Click to play with the REZM Sample App. *****

Thanks to…

**Geoffrey Grosenbach for the REST PeepCode

**Matt Beedle for writing and releasing Acts_As_Emailable which was my starting point with Easy_Message and now RESTful_easy_messages.

** Rick Olson for writing and releasing all of his plug-ins.

** Dr. Nic Williams for his multiple-openids-per-user-sample-app which I used as a starting point for the REZM sample app which I’ll put up soon.

** Ben Curtis for his OpenID sample app which Dr. Nic based his.

Lastly, please recommend me if you like RESTful_Easy_Messages.
Recommend Me

39 Responses

  1. Sam Says:

    Also please let me know if you use REZM in a production app and I’ll put up a link to it.

  2. Dr Nic Says:

    PeepCode is by Geoffrey Grosenbach @ http://nubyonrails.com/

  3. Sam Says:

    I knew I forgot something.

  4. Sam Says:

    I’ve uploaded the sample app for your viewing pleasure. Thanks to Dr. Nic and Ben Curtis for the head start.

  5. martin Says:

    can this be used with multiple recipients?

  6. Sam Says:

    Not right out of the box. I kept the functionality simple on purpose to let others add what they needed. However, it wouldn’t be too difficult to add.

  7. martin Says:

    I am kind of stuck on what the table structure would be I was thinking this:

    messages
    parent_id
    title
    body
    sender_id
    created_at

    recipients
    message_id
    recipient_id
    read_at

    What do you think?

  8. Sam Says:

    If you didn’t what to change the underlying schema you could just allow the form to accept multiple recipients and then have the controller to create multiple messages on send. The only downside would the sender’s outbox would fill-up, but then they would be able to see when each recipient read the message.

    That’s what I’d do, but I’m lazy. ;)

  9. Sam Says:

    Also, I write this with Rails 2.0 in mind so if you are having issues with the migration you’ll need to update to a 2.0 preview release. You can do this with the command…

    gem install rails –source http://gems.rubyonrails.org

    Note: there are two dashes in front of source

  10. PJ Says:

    Hi Sam

    When i go to:

    http://localhost:3001/users/1/messages

    I get the following error:

    undefined local variable or method `inbox_user_messages_url’ for #

    Please let me know what you think might be going wrong for me…

    Regards,

    PJ.

    What should i do?

    My routes

  11. Sam Says:

    @PJ: Did you add in the REZM routes?

  12. PJ Says:

    Hi Sam, this is what i have in my routes.rb file (i added the :name_prefix part, but that hasn’t made any difference)…

    map.resources :users, :member => { :enable => :put } do |user|
    user.resources :messages, :name_prefix => “user_”,
    :collection => {:destroy_selected => :post,
    :inbox => :get,
    :outbox => :get,
    :trashbin => :get},
    :member => {:reply => :get}
    end

    Many thanks…I had Easy_Messages working with acts_as_authenticated with no probs…great plug in, would love to get it working in REST mode!

    PJ.

  13. Sam Says:

    @PJ: Then I’m guessing you’re on an older version of Rails. What version are you running?

  14. PJ Says:

    I am on Version 1.2.3. I was hoping to get things working without upgrading…?

    Pj.

  15. Sam Says:

    I would suggest at least 1.2.4 for REZM.

  16. PJ Says:

    I updated to 1.2.5, and i also changed the names of the view files ie

    from - index.html.erb
    to - index.rhtml
    etc…

    Now it seems to be working….will play around with it a bit more and post back…thank you!

    PJ.

  17. Greg Says:

    The older version of this plugin was great. I just installed this version for a newer project and am having issues under Rails 2.0.1.

    First, I’m using restful_auth. and had to change current_user to @current_member (since my model is members instead of users). wouldn’t work without the @ like your current_user that was set. I had to go into all the habtm and change any user.* to member.*.

    I’m getting The error occurred while evaluating nil.inbox_messages when going to /messages still. All routes are in, I’m not getting any exceptions on @current_member so not sure what’s up.

  18. Sam Says:

    Greg, I wrote REZM against the Edge that was available around 1.2.5. I have not yet had a chance to verify it against 2.0 yet but I will soon and release any fixes.

  19. Greg Says:

    Ok Sam. I played around with it more this afternoon. Trashed my member models and reinstalled restful_auth with user models and still no go. Same thing happens. There’s something different in Rails 2.0/2.0.1 from 1.2.5 but I haven’t figured out what yet. If I do before you, I’ll let you know.

  20. gravis Says:

    I’m sorry, but really, I don’t understand the point of having a route like /users/[user_id]/messages/inbox where user_id can be replaced by any value with the same result.

    Am I missing something ??

  21. Sam Says:

    @Gravis - You’re right that it’s lame route. I was thinking of adding an admin area to review all of the messages. I’m working on getting REZM verified with Rail 2.X and I’ll remove the crap where I can.

  22. nerb Says:

    Howdy. Cool plugin, and i’m just about to try it… however, i’ve already used the messages controller, in my forum.

    If i use, say emessage, when i do the script/generate would everything be plug and play from there, do you think?

    I know inside your lib/name_of_plugin.rb file there is many mentions of message or messaging or messages, but will that mess up my symbols already called messages in my forum controller? i don’t think so, but thought i’d ask. thoughts?

  23. Sam Says:

    @Nerb, rightnow you can’t choose the name of the messages model. I’m working on the next version which will allow you to generate your own model name.

  24. nerb Says:

    thanks sam. I was thinking, since I’m in a bind, i’d just hack act_as_emailable, similar to how you’ve done, but leverage your hard work, but leaving the controller as email (or whatever they have)?

    The plugin looks great, i just wish i didn’t use the word messages already in my controller and models.

    Live and learn.

  25. Bill Says:

    Nice plugin. I had started working on the same thing and had it about 70% complete when a fired pointed this plugin out to me. Looks almost exactly like what I was doing already so it fit in nicely. Two things though, I have a login and an email for the user. You had the assumption that the email was the login. No big deal though. The other item was how would I move this into a parital? I was going to have a landing page for the user that had among other things, the view of their mailbox. When I moved the code into the partial though, I got undefined local variable or method `rezm_menu’. I’m new to rails so may have missed something easy. Have you tried to do something like this yet and if so could you give me a hint? Thanks again for the plugin.

  26. Sam Says:

    @Bill, the method ‘rezm_menu’ is in helpers/messages_helper.rb so you’ll need include that helper in the controller that calls your partial. Just put ‘include MessagessHelper’ at the top of your controller. Like so…

    class MyController < ApplicationController
    include MessagessHelper
    # The rest of your class
    #
    #
    end

  27. Gravis Says:

    Hi Sam,

    any news on the new routes ? It’s been a while, I hope you will continue to improve the plugin :)

    Cheers,
    Gravis

  28. samuelschroeder.com » Blog Archive » RESTful_Easy_Messages now on GitHub. Says:

    […] just pushed the 0.7 svn tag of the RESTful_Easy_Messages plug-in to GitHub so I can play with the cool kids. Go see it here => […]

  29. Asbjørn Morell Says:

    Helllo,

    I got the latest version of restful-authentication running with Ruby on rails 2.1. When I try to open a message I get this error: (/messages/inbox)

    NameError in MessagesController#index
    undefined local variable or method `login_required’ for #
    RAILS_ROOT: /home/ilikeyou/www

    Application Trace | Framework Trace | Full Trace
    lib/restful_easy_messages_controller_system.rb:15:in `rezm_login_required’
    /usr/local/bin/mongrel_rails:19:in `load’
    /usr/local/bin/mongrel_rails:19

    Had to rename restful-authentication to restful_authentication to get it working. Some bug about the naming. It is listed on the rest_auth git webpage.

    Any ideas?

  30. Sam Says:

    @Asbjørn Morell - the login_required method is part of restful_authentication. It iwould be in lib/authenticated_system.rb if you ran the RA generator. Have you added “include AuthenticatedSystem” in your Application controller? Otherwise, the the REZM controller won’t know about it.

  31. Asbjørn Morell Says:

    Thanks. That fixed the error. However I get a new error:

    Showing messages/index.html.erb where line #3 raised:

    inbox_user_messages_url failed to generate from {:controller=>”messages”, :action=>”inbox”} - you may have ambiguous routes, or you may need to supply additional parameters for this route. content_url has the following required parameters: [”users”, :user_id, “messages”, “inbox”] - are they all satisfied?

    I am logged in, but there is no messages yet. Routes has been added as you instructed in your documentation. Any ideas?

  32. Sam Says:

    @Gravis has updated the routes for this plugin. The new code in up on GitHub.

  33. hank Says:

    Hi,

    I’m having similar problems to ASBJØRN MORELL. I’m using Rails 2.1 with restful authentication. I get the following Name error when I navigate to 0.0.0.0:3000/users/1/messages

    undefined local variable or method `inbox_messages_url’ for #

    I used the new routes as posted on GitHub. Anyone else having similar problems?

  34. Gravis Says:

    @HANK & @Asbjørn Morell :

    If you’re using the last github version, you should not have routes like :

    /users/1/messages

    since you’re not supposed to read other user’s messages. This route has been replaced with /messages directly. Therefore inbox_user_messages_url is deprecated in favor of inbox_messages.

    I invite you to take a look at RESTFUL_EZM_ROUTES for new routes. Please let us know if there’s a glitch in the doc.

    Thanks
    Philippe

  35. HANK Says:

    Hi GRAVIS,

    Thanks for the help.

    I’m getting the same Name error, when I navigate to /messages. The Name error is “undefined local variable or method `inbox_messages_url’ for #”

    I have always used the routes as posted on github.

    Nevermind, I just figured out what’s wrong. There is a HUGE inconsistency with the docs. the routes in the README file are different from the routes in the EZM_ROUTES file.

    If you guys can clean up that inconsistency, that would be a great help. Great work BTW!

  36. Muthu Says:

    Awesome, I was able to get it up and running within an hour to our growing portal.

    Thanks a lot

    Muthu Arumugam
    CTO
    Amigle Inc

  37. Rajan Chandi Says:

    This looks amazing. We’ll try this out!

    Cheers
    Rajan

  38. Saurabh Says:

    awesome stuff loved it

  39. Paulette Says:

    Nice plugin. I’ve got a bit of a problem with the trashbin feature. I get an error as shown below when I click trashbin.

    SQLite3::SQLException: no such column: true: SELECT * FROM “messages” WHERE (”messages”.receiver_id = 1 AND (receiver_deleted = true and receiver_purged IS NULL)) ORDER BY created_at DESC

    I’ve tried reinstalling over and over but the same erroe pops up. Any idea why this is happening?

    Please I need help here.

Leave a Reply