Code Monkey home page Code Monkey logo

has_mailbox's Introduction

has_mailbox

If you like to add messaging functionality between the users in your rails app, use this gem! this gem also provide standard mailbox/messagebox interface allowing each users have their own inbox, outbox and trash.

This rails engine compatible with Rails 3.x only.

Setup

add the gem to your Gemfile.

gem "has_mailbox"

run bundle command and generate the migration files, this will generate messages table and messagecopies table, after that don’t forget to run the migration.

rails g has_mailbox:migration
rake db:migrate

Then add has_mailbox method in your user model, for example :

class User < ActiveRecord::Base
   has_mailbox
   ...
end

After this step you will be able to send messages between the users across your application. Try to create two user object and send messages to one user with another, and list all messages from the entire mailbox.

@user1 = User.first  # => create first user
@user2 = User.last   # => return the second user

@user1.send_message("Hi Subject","Hi Body !!!",@user2)   # => send message to @user2
@user1.send_message?("Hi Subject","Hi Body !!!",@user2)  # => send message with true/false return
@user1.send_message("Hi Subject","Hi Body !!!",@user2,@user3)  # => send message with with multiple recipients

@user1.inbox   # => return all incoming messages for @user1
@user1.outbox  # => return outgoing messages for @user1
@user1.trash   # => return all messages that has been deleted

If you like to delete the message from user object, and empty the box try use this method.

@message = @user1.inbox.find(1) # => get message from inbox with id 1
@message.delete                 # => delete message from inbox will be moved to trash box.

@user1.trash                    # => return deleted messages

@message = @user.trash.find(1)  # => get the current deleted message

@message.undelete               # => will return the message to inbox
or
@message.delete                 # => will delete message permanently

@user.empty_mailbox(:inbox => true)  # => will delete all messages from inbox to trash.
@user.empty_mailbox(:outbox => true) # => will delete all outgoing messages permanently.
@user.empty_mailbox(:inbox => true)  # => will delete all messages from trash permanently.

Each user are able to mark their message as read/unread. Example :

@user1.inbox.find(1).mark_as_read    # update attribute message opened to true
@user1.inbox.find(1).mark_as_unread  # to false

Install Mailbox Views

This gem has been build using Devise authentication, but if you like using another authentication plugin, such as authlogic / restful-authentication or any other authentication plugins, it still suits to your app.

rails g has_mailbox:install

this generator will install to your application the views and stylesheet file in your public directory, notice the required argument “user_attribute_name”, you have to fill this with desired attribute name in your user model you wish to display in your generated views. e.g. : email, username, first_name, etc. the default will be set to “email”, cause Devise default user login using “email” attribute.

here’s the tricky part. you have to define the route for mailboxes in order to get controller will work with your views. Also you have to specifies the current user object from your authentication plugin and also the attribute name you wish to display.

with Devise authentication to get the default user object that is currently sign in is “current_user” and the display attribute using “email”. So in your config/routes.rb, please add mailboxes_for method. Example :

mailboxes_for :users    # => with this argument user_object_name
                        # => will set to "current_user" and
                        # => user_attribute_name will set to "email"

or if you using different plugins/gems, you can set with your own user_object_name and user_attribute_name

mailboxes_for :users, :user_object_name => "current_user_sign_in", :user_attribute_name => "username"

Your issues are needed

This gem is still under development mode, so if you are having some problems with has_mailbox, please submit an issues here.

github.com/fajrif/has_mailbox/issues

has_mailbox's People

Contributors

davidslvto avatar fajrif avatar jwoertink avatar seppsepp avatar tylerhoran avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

has_mailbox's Issues

Sending messages by ID

I have a problem with sending messages to the user.
After entering the name in the users and try to send I get the error "Could not find User with id = 0" But if I type a specific user id, the message is sent.

will_paginate dependency error

will_paginate is not loaded through the dependency on has_mailbox. Accessing the mailboxes route results in this error message:

undefined method `paginate' for []:ActiveRecord::Relation

Adding will_paginate to the host application Gemspec fixes this error. Not sure if this is a will_paginate error or not, however the README should be updated to include this step to avoid this.

"Back" in Chrome generates an error

When I write the letter and then I come back to page back that I see the following

$("#tabs-messages").html("\n<form accept-charset="UTF-8" action="/mailbox/update?locale=ru" data-remote="true" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓" /><input name="authenticity_token" type="hidden" value="l5TtQwhQY8/fRNRLJWWDQhZB86rSS/PP58xLSHFqED4=" /></div>\n <select id="option" name="option"><option value="Read">Read</option>\n<option value="Unread">Unread</option>\n<option value="Delete">Delete</option></select>\n <input class="submit" name="commit" type="submit" value="Apply to Selected" />\n <input id="mailbox" name="mailbox" type="hidden" value="inbox" />\n <table cellspacing="0" cellpadding="0" border="0" width="100%">\n \n \n <th width="5%" scope="col"><input type="checkbox" class="checkall" id="checkbox" name="checkbox"></th>\n <th width="20%" scope="col">From</th>\n <th width="55%" scope="col">Subject</th>\n <th width="20%" scope="col">Date</th>\n </tr>\n \n <tr class="">\n <td scope="col"><input id="messages" name="[messages][]" type="checkbox" value="7" /></td>\n <td scope="col">[email protected]</td>\n <td scope="col"><a href="/mailbox/show/inbox/7?locale=ru">zszeze</a></td>\n <td scope="col" title='Fri, Sep 23, 2011 at 06:44 AM'>06:44 AM</td>\n </tr>\n \n <td colspan="4"></td>\n </tr>\n </tbody> \n </table>\n</form>

\n <a href="/mailbox/new?locale=ru">Compose New Message</a>\n</p>\n");

does not work with rails 3.1

undefined method `paginate' for []:ActiveRecord::Relation

Started GET "/mailboxes" for 192.168.1.252 at Fri Oct 21 20:43:53 -0500 2011
Processing by MailboxesController#index as HTML
User Load (1.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
HasMailbox::Models::Message Load (0.8ms) SELECT "messages".* FROM "messages" WHERE "messages"."received_messageable_id" = 1 AND "messages"."received_messageable_type" = 'User' AND "messages"."deleted" = 'f' ORDER BY created_at desc
Completed 500 Internal Server Error in 35ms

NoMethodError (undefined method `paginate' for []:ActiveRecord::Relation):

Rendered /var/lib/gems/1.8/gems/actionpack-3.1.0/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.1ms)
Rendered /var/lib/gems/1.8/gems/actionpack-3.1.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (0.8ms)
Rendered /var/lib/gems/1.8/gems/actionpack-3.1.0/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (5.0ms)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.