Giter Club home page Giter Club logo

simple_messenger's Introduction

Simple Messenger

Build Status

Add messaging functionality to active record models.

Requires ruby >= 2.0.0

Installation

Add this line to your application's Gemfile:

gem 'simple_messenger'

And then execute:

$ bundle

Or install it yourself as:

$ gem install simple_messenger

Run the generator to create the migration file and message model:

$ rails g messages

Migrate the database:

$ rake db:migrate

Usage

Message Model

The following model will be generated and placed in your app/models folder:

class Message
  include SimpleMessenger::MessageAdditions
end

You can add custom functionality to the Message model here.

Messenger Model

Add simple_messenger to the top of your activerecord model:

class User
  simple_messenger
end

The class is not restricted to User, it can be any class you add simple_messenger to.

Creating Messages

Send messages by passing in a sender, receiver and some content:

bob = User.create
alice = User.create
bob.create_message!(receiver: alice, content: 'Hello')
# => <Message id: 1 sender_id: 1 sender_type 'User' ... content: "Hello" viewed: false>

bob.messages.count
# => 1

bob.sent_messages.count
# => 1

bob.sent_messages_to(alice).count
# => 1

bob.sent_messages_to(jimmy).count
# => 0

bob.received_messages.count
# => 0

bob.new_messages.count
# => 0

bob.messages_with(alice)
# => <Message id: 1 ... >

alice.messages.count
# => 1

alice.sent_messages.count
# => 0

alice.received_messages.count
# => 1

alice.received_messages_from(bob).count
# => 1

alice.received_messages_from(jimmy).count
# => 0

alice.new_messages.count
# => 1

alice.messages_with(bob)
# => <Message id: 1 ... >

Messenger Helpers

The following constructors are available:

bob.build_message
bob.new_message
bob.create_message
bob.create_message!

Due to the design of the library you would have to type:

bob.sent_messages.build ...

As typing the bob.messages returns a specialized relation and is not created through a has_many relationship:

Message Helpers

Class Methods

To get all the member ids in a collection of messages use the following:

Message.uniq_member_ids_for(messages)

You can optionally omit one or more member from the collection:

Message.uniq_member_ids_for(messages, remove: current_user)

This could be useful for rendering a list of users which the current_user has messaged.

remove can be an id, array of ids, ActiveRecord object, collection of ActiveRecord objects, or anything that respond_to?(:id)

Instance Methods

Check to see if the message has been read:

msg = Message.create(sender: bob, receiver: alice, content: 'Hello')
# => <Message ... viewed: false>
msg.read?(bob) # true as bob is the sender
# => true
msg.read?(alice)
# => false

Find the opposite member in the message:

msg = Message.create(sender: bob, receiver: alice, content: 'Hello')
# => <Message ... >
msg.member_who_is_not(bob)
# => <User ... username: 'alice'>
msg.member_who_is_not(alice)
# => <User ... username: 'bob'>
msg.member_who_is_not(jimmy)
# => SimpleMessenger::NotInvolved error

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

simple_messenger's People

Contributors

kainage avatar vaot 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.