Code Monkey home page Code Monkey logo

has_messages's Introduction

has_messages

has_messages demonstrates a reference implementation for sending messages between users.

Resources

API

Bugs

Development

Testing

Source

  • git://github.com/pluginaweek/has_messages.git

Mailing List

Description

Messaging between users is fairly common in web applications, especially those that support social networking. Messaging doesn’t necessarily need to be between users, but can also act as a way for the web application to send notices and other notifications to users.

Designing and building a framework that supports this can be complex and takes away from the business focus. This plugin can help ease that process by demonstrating a reference implementation of these features.

Usage

Installation

has_messages requires additional database tables to work. You can generate a migration for these tables like so:

script/generate has_messages

Then simply migrate your database:

rake db:migrate

Adding message support

class User < ActiveRecord::Base
  has_messages
end

This will build the following associations:

  • messages

  • unsent_messages

  • sent_messages

  • received_messages

If you have more specific needs, you can create the same associations manually that has_messages builds. See HasMessages::MacroMethods#has_messages for more information about the asssociations that are generated from this macro.

Creating new messages

message = user.messages.build
message.to user1, user2
message.subject = 'Hey!'
message.body = 'Does anyone want to go out tonight?'
message.deliver

Replying to messages

reply = message.reply_to_all
reply.body = "I'd love to go out!"
reply.deliver

Forwarding messages

forward = message.forward
forward.body = 'Interested?'
forward.deliver

Processing messages asynchronously

In addition to delivering messages immediately, you can also queue messages so that an external application processes and delivers them. This is especially useful for messages that need to be sent outside of the confines of the application.

To queue messages for external processing, you can use the queue event, rather than deliver. This will indicate to any external processes that the message is ready to be sent.

To process queued emails, you need an external cron job that checks and sends them like so:

Message.with_state('queued').each do |message|
  message.deliver
end

Testing

Before you can run any tests, the following gem must be installed:

To run against a specific version of Rails:

rake test RAILS_FRAMEWORK_ROOT=/path/to/rails

Dependencies

has_messages's People

Contributors

obrie avatar langalex avatar

Stargazers

Michał Połtyn avatar

Watchers

James Cloos avatar  avatar

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.