Mostrar / Ocultar Avisos

Easy_Messages

A few weeks ago I was looking for Rails plug-in to provide simple messaging between users on a website. I came across Acts_As_Emailable by Matt Beedle. It was a good start but only gave me a model and some methods but I would still need to flesh out a controller and some views for my little private messaging project to be complete. The result of that work is this plug-in, Easy_Messages. I took Acts_As_Emailable as a starting point and added some controller methods, a few views, a helper, and some named routes.

Below are the installation instructions. This is my first Rails plug-in so let me know if you find something to refactor. I’m open to constructive criticism.

The code is hosted at RubyForge.

UPDATE I: I have created a new branch for 0.51, please use that one for the latest stable release

UPDATE II: I have removed the docs from branch 0.51 and placed them online here: http://easymessages.samuelschroeder.com. Now the generator should work like a charm

svn://rubyforge.org/var/svn/easymessages/branches/RB-0.51

The run the generator to create the model, migration, helper, and views.

script/generate easy_messages message account

Note: Easy_Messages assumes you have Acts_As_Authenticated installed with the defaults of a model named User and a controller named Account. And you must use the above line exactly. I have grand aspirations of making the generator fully dynamic someday but for now you’ll have to deal.

The generator should produce this output.

exists app/models/
exists app/helpers/
exists app/views/account
create app/models/message.rb
create app/helpers/easy_messages_helper.rb
create app/views/account/message_view.rhtml
create app/views/account/messages.rhtml
create app/views/account/send_message.rhtml
exists db/migrate
create db/migrate/###_create_easy_messages_messages.rb

Yes, I realize the redundant naming but once/if I get the dynamic generator going it would say ###_create_easy_messages_[MessageClassNames]s.

Now you have to do three manual changes to your code. First, add “easy_messages” to apps/model/user.rb.

Second, add “authenticated_commands” to apps/controllers/account_controller.rb.

Finally, you need to copy the named routes from vendor/plugins/easy_messages/config/easy_messages_named_routes.rb to config/routes.rb.

That should be it for the setup. So start your development server and point your brower to..

http://localhost:3000/account/inbox

There is also a complete RDoc listing in vendor/plugins/easy_messages/doc

Thanks to…

**Matt Beedle for writing and releasing Acts_As_Emailable.

** Rick Olson for writing and releasing the kick-ass Acts_As_Authenticated plug-in. It was been such a help when starting new projects.

Recommend Me

24 Responses

  1. Matt Beedle Says:

    This plugin looks excellent. It seems to include a lot more features than mine. I will be trying it out very soon.

  2. Sam Says:

    Thanks Matt. It is still basically acts_as_emailable but with some UI added.

  3. Marston A. Says:

    Looks great, will this work with restful_authentication? (based off AAA)

  4. Sam Says:

    Marston, I have not tried it with restful_authentication and I didn’t make Easy_Messages with REST in mind. That being said, E_M really only looks for the following from A_A_A.

    1. User model, because I was too lazy to make the generator more dynamic.
    2. Account controller for the named routes.
    3. The current_user method.

    You could give it a try and see if it works. Let me know if you do.

  5. railsgrunt Says:

    hi, thanks for sharing your plugin. ive been searching for something like this.

    thanks!

  6. Bryan Says:

    Looks like a great plugin - just finished building an private message client for a new app, and then i find this!

    Just a quick note that the generate command won’t run properly until you rename the folder “generators” in the doc > files folder.

    After that, it worked like a charm for me.

  7. Richard Says:

    Hi Sam,

    I would like to try out your plugin but I am not using A_A_A as I am using my own and I have already created a User, Account and Message models/controllers before seeing your plug-in. The message model I use has nothing to do with email. If I need to make alterations to use this plugin what recommendations or things I need to worry about to accomplish this? Thanks.

    -Rich

  8. Sam Says:

    @Bryan, thanks for the heads up. I’ve removed the docs from the 0.51 branch and placed them online here: http://easymessages.samuelschroeder.com

  9. Sam Says:

    @Rich, I think the list below _should_ cover it. You’ll make to manually move files or run the generator on a different project.

    Easy Part

    1. db/migrate/###_create_easy_messages_messages.rb: Change the table name “:messages” to
    2. app/models/message.rb: rename this file to and change the corresponding class name.
    3. Make sure there is a “login” field in your User model.
    4. I would keep the views in the Account controller.

    Fun Part: changes to vendor\plugins\easy_messages\lib

    1. Change :class_name => “Message” to :class_name => “
    2. Change all “:select” phrases to use your new table .
    3. Remover the before_filter in authenticated_commands
    4. Anywhere the class Message is used you’ll need to change that to
    5. You’ll need to have a current_user method just like the one in A_A_A as that is the security check to see if a user can view a message.

    Now I have not tested this and it’s my best guess as to what you’ll have to do. However, if you can, and haven’t done too much work, I would just move your message stuff to a new class and keep EZM intact otherwise you’ll have to do this again if I ever make a new release of it.

    Hope this helps.

  10. Kent Anderson Says:

    I tried the link “svn://rubyforge.org/var/svn/easymessages/branches/RB-0.51″ and it is giving me: “No such file or directory”

  11. Sam Says:

    @Kent, I just looked with my TortoiseSVN and it’s there. You have to do a SVN checkout to get the code.

  12. Vanq Says:

    I liked this plugin…
    I followed the steps mentioned…
    but when I rake…I get this error:

    rake aborted!
    uninitialized constant CreateEasyMessagesMessages

    …can u pls tell me what can be the issue?
    thanks!

  13. John Says:

    Found out why RAKE would abort with the Uninitialized Constant Error.

    What you have to do is open the migration EasyMessages creates and change it’s class name to:

    CreateEasyMessagesMessages

    that way when Rake runs the name of the migration class matches the name of the file it’s contained in, and thus your tables get built.

  14. Sandy Says:

    Hey, what do i need to fill out the account controller with if not using A_A_A. Do i need to fill out for each action eg. def send_message end etc so the routes work. Do we need to write our own controller methods? Or is this generated elsewhere. Just a little confused.

    Thanks

  15. Sam Says:

    Sandy, look up a few comments to the one I posted on July 30th. That should get you what you need.

  16. Matt Says:

    Hi Samuel,

    I followed your steps, and went to http://localhost:3000/account/inbox. I click on “Write” and I get the error:

    Unknown action
    No action responded to message

    I assume this is a problem to do with routing, but I’m not sure what. Here’s my routing info:

    map.connect ‘:controller/service.wsdl’, :action => ‘wsdl’

    # Install the default route as the lowest priority.
    map.connect ‘:controller/:action/:id.:format’
    map.connect ‘:controller/:action/:id’

    map.with_options :controller => ‘account’ do |m|
    m.compose ‘/account/message/send’, :action => ’send_message’
    m.delete ‘account/message/delete/:id’, :action => ‘delete_message’
    m.inbox ‘/account/inbox’, :action => ‘inbox’
    m.outbox ‘/account/outbox’, :action => ‘outbox’
    m.trashbin ‘/account/trashbin’, :action => ‘trash_bin’
    m.reply ‘/account/message/reply/:id’, :action => ‘reply_to_message’
    m.view ‘/account/message/view/:id’, :action => ‘view_message’
    end

    Any suggestions?

  17. Sam Says:

    First, you should move…

    # Install the default route as the lowest priority.
    map.connect ‘:controller/:action/:id.:format’
    map.connect ‘:controller/:action/:id’

    below the ezm routes. Try that first.

  18. samuelschroeder.com » Blog Archive » RESTful_Easy_Messages Says:

    […] 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 […]

  19. PJ Says:

    This is an amazing plugin!

    I’ve only been using rails for a couple of months and have never programmed before, but this was so simple to implement even for me…

    Thanks for providing good instructions.

    PJ.

  20. Johnb Says:

    Sam,

    Brilliant plug-in, you probably saved me a weeks work there!
    I am using no A-A-A and found that once sorted out routing priorities and weeded out all the references to current_user and updated the messages model to adapt the to/from references it probably took about 90minutes to get up and running. Now all I need to do is make it look pretty and integrate the controller behaviour with my user hub! Magic!

    Thanks also to Matt for the help yesterday! (I am sure he’ll keep an eye on progress)

    cheers guys - if you ever come over to the UK there’s a cold one waiting….

    ;¬)

    Johnb.

  21. Jason Says:

    Does this plugin work with rails 2.0 and above?

  22. Sam Says:

    @Jason, I believe so but I would try restful_ezm instead @ http://samuelschroeder.com/2007/10/16/restful_easy_messages It is newer, more Rails 2.0+ easy_messages.

  23. Vivek Says:

    Brilliant. Worked lie a charm.

    Thanx.

  24. Wolf Says:

    Hi,

    really nice work. But i’m wondering why nobody is doing some change on this? e.g. i want to have images instead of (unread)-text. how can i insert image_to tag to helper without converting tags?

    love your work

    wolf

Leave a Reply