Permalink
Browse files

hopefully already usable

  • Loading branch information...
0 parents commit c04834eb0c29cf79ba3427f225d5eca2d0b8f491 @xinuc committed May 15, 2009
@@ -0,0 +1,20 @@
+Copyright (c) 2009 Nugroho Herucahyono (xinuc@xinuc.org)
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,100 @@
+= HasMessages
+
+This plugin provides a nice and easy way to create simple internal messaging
+system in your application.
+
+== Usage
+
+These instructions will show you how to use the plugin.
+
+=== Generator
+
+Run the generator from your RAILS_ROOT:
+
+ ./script/generate has_messages
+
+This will create:
+
+ Model: RAILS_ROOT/app/models/message.rb
+ Migration: RAILS_ROOT/db/migrate/xxx_create_messages.rb
+
+And you will need to run a database migration from your RAILS_ROOT:
+
+ rake db:migrate
+
+=== Mixin
+
+add <tt>has_messages</tt> to your user-like model:
+
+ class User < ActiveRecord::Base
+ has_messages
+ ...
+ end
+
+or, just any activerecord models:
+
+ class Company < ActiveRecord::Base
+ has_messages
+ ...
+ end
+
+=== Read and send messages
+
+To get the messages:
+
+ user = User.find some_id
+ user.inbox # will return all messages received by the user
+ user.outbox # will return all messages sent by the user
+
+ user.has_new_messages? # return true if the user has unread messages
+ user.new_messages # return all unread messages
+
+and you can add options to the method, like:
+
+ user.inbox :limit => 20 # will return last 20 received messages
+
+to read the message:
+
+ message = user.read_message(message_id)
+ # receive a message_id (generally from the request's params) and return the message object
+ # this method will set the message as read
+
+ subject = message.subject
+ sender = message.sender
+ body = message.body # return the body of the message (string)
+ sent_at = message.sent_at
+
+to send a message:
+
+ me = User.find some_id
+ delilah = User.find some_other_id
+
+ subject = "Hi there Delilah"
+ body = "What's it like in New York city??"
+
+ me.send_message(delilah, subject, body) # send_message(receiver, subject, body)
+
+to delete the message
+
+ user.delete_message(message_id)
+ # receive a message_id and set the message as trashed,
+ # delete the record if the message has been trashed by both sender and receiver.
+ # don't use <tt>message.destroy</tt> directly, cause it will delete the message
+ # both from the receiver's inbox and sender's outbox. The users will be confused :-(
+
+== Installation
+
+Run the following command in your RAILS_ROOT:
+
+ ./script/plugin install git://github.com/xinuc/has_messages.git
+
+Or, simply get the tarball at:
+
+ http://github.com/xinuc/has_messages/tarball/master
+
+extract it to your vendor/plugins and rename it to 'has_messages'
+
+== Bugs, Patches or Feature requests
+
+If you find any bugs, submit your patches or request any features, drop me an email: xinuc@xinuc.org
+Or, just simply fork the project at github.com and send me a pull request.
@@ -0,0 +1,23 @@
+require 'rake'
+require 'rake/testtask'
+require 'rake/rdoctask'
+
+desc 'Default: run unit tests.'
+task :default => :test
+
+desc 'Test the has_messages plugin.'
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.libs << 'test'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = true
+end
+
+desc 'Generate documentation for the has_messages plugin.'
+Rake::RDocTask.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'HasMessages'
+ rdoc.options << '--line-numbers' << '--inline-source'
+ rdoc.rdoc_files.include('README')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
@@ -0,0 +1,8 @@
+author: Nugroho Herucahyono
+summary: add a nice and simple internal messaging system between users in your application.
+description: "Hmmph... yeah, like the little inbox in your facebook, maybe..."
+homepage: http://github.com/xinuc/has_messages
+plugin: git://github.com/xinuc/has_messages.git
+license: MIT
+version: 0.01
+rails_version: 2.0+
@@ -0,0 +1,9 @@
+Description:
+ Create model and migration
+
+Example:
+ ./script/generate has_messages
+
+ This will create:
+ app/models/message.rb
+ db/migrate/xxx_create_messages.rb
@@ -0,0 +1,31 @@
+class HasMessagesGenerator < Rails::Generator::Base
+ default_options :skip_migration => false
+
+ def manifest
+ record do |m|
+ m.class_collisions "message"
+
+ m.directory 'app/models'
+ m.directory 'spec/models'
+ m.directory 'db/migrate'
+
+ m.template 'message.rb', File.join('app/models', "message.rb")
+
+ unless options[:skip_migration]
+ m.migration_template 'migration.rb', 'db/migrate', :assigns => {
+ :migration_name => "CreateMessages"
+ }, :migration_file_name => "create_messages"
+ end
+ end
+ end
+
+
+ protected
+ def add_options!(opt)
+ opt.separator ''
+ opt.separator 'Options:'
+ opt.on("--skip-migration",
+ "Don't generate a migration file for this model") { |v| options[:skip_migration] = v }
+ end
+
+end
@@ -0,0 +1,55 @@
+class Message < ActiveRecord::Base
+ belongs_to :sender, :polymorphic => true
+ belongs_to :receiver, :polymorphic => true
+
+ validates_presence_of :receiver, :sender, :subject, :body
+
+ class << self
+
+ def find_received_messages(receiver, options = {})
+ with_scope :find => options do
+ find_all_by_receiver_id_and_receiver_type receiver.id, receiver.class.to_s,
+ :conditions => { :trashed_by_receiver => false }, :order => 'created_at DESC'
+ end
+ end
+
+ def find_unread_received_messages(receiver, options = {})
+ options[:conditions] ||= {}
+ options[:conditions].reverse_merge!(:read => false)
+ find_received_messages(receiver, options)
+ end
+
+ def find_sent_messages(sender, options ={})
+ with_scope :find => options do
+ find_all_by_sender_id_and_sender_type sender.id, sender.class.to_s,
+ :conditions => { :trashed_by_sender => false }, :order => 'created_at DESC'
+ end
+ end
+
+ def read_message(user, id)
+ returning message = find_by_id(id) do
+ if(message && user == message.receiver)
+ message.read = true
+ message.save
+ end
+ end
+ end
+
+ def trash_message(user, id)
+ message = find_by_id id
+ if message
+ if user == message.sender
+ message.trashed_by_sender = true
+ elsif user == message.receiver
+ message.trashed_by_receiver = true
+ end
+ (message.trashed_by_sender && message.trashed_by_receiver) ? message.destroy : message.save
+ end
+ end
+
+ end
+
+ def sent_at
+ created_at
+ end
+end
@@ -0,0 +1,30 @@
+class CreateMessages < ActiveRecord::Migration
+
+ def self.up
+ create_table :messages do |t|
+
+ t.string :subject
+ t.text :body
+
+ t.boolean :read, :default => false
+
+ t.string :sender_type
+ t.integer :sender_id
+
+ t.string :receiver_type
+ t.integer :receiver_id
+
+ t.boolean :trashed_by_sender, :default => false
+ t.boolean :trashed_by_receiver, :default => false
+
+ t.timestamps
+ end
+
+ add_index :messages, [:receiver_type, :receiver_id, :created_at]
+ end
+
+ def self.down
+ drop_table :messages
+ end
+
+end
@@ -0,0 +1,3 @@
+require File.dirname(__FILE__) + '/lib/has_messages'
+
+ActiveRecord::Base.send :include, Xinuc::HasMessages
@@ -0,0 +1 @@
+puts IO.read(File.join(File.dirname(__FILE__), 'README.rdoc'))
@@ -0,0 +1,49 @@
+module Xinuc
+ module HasMessages
+
+ def self.included( recipient )
+ recipient.extend( Xinuc::HasMessages::ModelClassMethods )
+ end
+
+ module ModelClassMethods
+
+ def has_messages
+ include Xinuc::HasMessages::ModelInstanceMethods
+ end
+
+ end
+
+ module ModelInstanceMethods
+
+ def inbox
+ Message.find_received_messages self
+ end
+
+ def new_messages
+ Message.find_unread_received_messages self
+ end
+
+ def has_new_messages?
+ self.new_messages.count > 0
+ end
+
+ def outbox
+ Message.find_sent_messages self
+ end
+
+ def read_message(message_id)
+ Message.read_message self, message_id
+ end
+
+ def send_message(receiver, subject, body)
+ Message.new(:receiver => receiver, :sender => self, :subject => subject,
+ :body => body).save!
+ end
+
+ def delete_message(message_id)
+ Message.trash_message self, message_id
+ end
+
+ end
+ end
+end
@@ -0,0 +1,4 @@
+# desc "Explaining what the task does"
+# task :has_messages do
+# # Task goes here
+# end
@@ -0,0 +1,8 @@
+require 'test_helper'
+
+class HasMessagesTest < ActiveSupport::TestCase
+ # Replace this with your real tests.
+ test "the truth" do
+ assert true
+ end
+end
@@ -0,0 +1,3 @@
+require 'rubygems'
+require 'active_support'
+require 'active_support/test_case'
@@ -0,0 +1 @@
+# Uninstall hook code here

0 comments on commit c04834e

Please sign in to comment.