Skip to content

Commit

Permalink
added ability to send/recieve messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff Dickey committed Dec 4, 2011
1 parent d683518 commit 0b1cf1a
Show file tree
Hide file tree
Showing 30 changed files with 726 additions and 39 deletions.
20 changes: 9 additions & 11 deletions Gemfile
@@ -1,17 +1,15 @@
source "http://rubygems.org"

# Declare your gem's dependencies in messaging.gemspec.
# Bundler will treat runtime dependencies like base dependencies, and
# development dependencies will be added by default to the :development group.
gemspec

# jquery-rails is used by the dummy application
gem 'haml'
gem 'devise'
gem 'simple_form'
gem 'mailboxer'
gem "jquery-rails"
gem 'coffee-rails'
gem 'sass-rails'

# Declare any dependencies that are still in development here instead of in
# your gemspec. These might include edge Rails or gems from your path or
# Git. Remember to move these dependencies to your gemspec before releasing
# your gem to rubygems.org.

# To use debugger
# gem 'ruby-debug19', :require => 'ruby-debug'
group :development, :test do
gem 'sqlite3'
end
38 changes: 38 additions & 0 deletions Gemfile.lock
Expand Up @@ -36,8 +36,25 @@ GEM
activesupport (3.1.3)
multi_json (~> 1.0)
arel (2.2.1)
bcrypt-ruby (3.0.1)
builder (3.0.0)
coffee-rails (3.1.1)
coffee-script (>= 2.2.0)
railties (~> 3.1.0)
coffee-script (2.2.0)
coffee-script-source
execjs
coffee-script-source (1.1.3)
devise (1.5.2)
bcrypt-ruby (~> 3.0)
orm_adapter (~> 0.0.3)
warden (~> 1.1)
erubis (2.7.0)
execjs (1.2.9)
multi_json (~> 1.0)
foreigner (1.1.1)
activerecord (>= 3.0.0)
haml (3.1.4)
hike (1.2.1)
i18n (0.6.0)
jquery-rails (1.0.19)
Expand All @@ -48,8 +65,12 @@ GEM
i18n (>= 0.4.0)
mime-types (~> 1.16)
treetop (~> 1.4.8)
mailboxer (0.5.4)
foreigner (>= 0.9.1)
rails (>= 3.1.0)
mime-types (1.17.2)
multi_json (1.0.4)
orm_adapter (0.0.5)
polyglot (0.3.3)
rack (1.3.5)
rack-cache (1.1)
Expand Down Expand Up @@ -78,6 +99,15 @@ GEM
rake (0.9.2.2)
rdoc (3.11)
json (~> 1.4)
sass (3.1.11)
sass-rails (3.1.5)
actionpack (~> 3.1.0)
railties (~> 3.1.0)
sass (~> 3.1.10)
tilt (~> 1.3.2)
simple_form (1.5.2)
actionpack (~> 3.0)
activemodel (~> 3.0)
sprockets (2.0.3)
hike (~> 1.2)
rack (~> 1.0)
Expand All @@ -89,11 +119,19 @@ GEM
polyglot
polyglot (>= 0.3.1)
tzinfo (0.3.31)
warden (1.1.0)
rack (>= 1.0)

PLATFORMS
ruby

DEPENDENCIES
coffee-rails
devise
haml
jquery-rails
mailboxer
messaging!
sass-rails
simple_form
sqlite3
1 change: 1 addition & 0 deletions app/assets/javascripts/messaging/application.js
Expand Up @@ -5,5 +5,6 @@
// the compiled file.
//
//= require jquery
//= require jquery-ui
//= require jquery_ujs
//= require_tree .
32 changes: 32 additions & 0 deletions app/assets/javascripts/messaging/messages.coffee
@@ -0,0 +1,32 @@
split = (val) ->
val.split /,\s*/

extractLast = (term) ->
split(term).pop()

$ ->

recipients = $('#message_recipients').data('autocomplete-source')
console.log recipients

# don't navigate away from the field on tab when selecting an item
$('#message_recipients').bind "keydown", (event) ->
if event.keyCode == $.ui.keyCode.TAB and $(@).data("autocomplete").menu.active then event.preventDefault()
$('#message_recipients').autocomplete
minLength: 0
source: (request, response) ->
# delegate back to autocomplete, but extract the last term
recipients = $('#message_recipients').data('autocomplete-source')
console.log recipients
response $.ui.autocomplete.filter(recipients, extractLast(request.term))
focus: -> return false # prevent last value inserted on focus
select: (event, ui) ->
terms = split(@value)
# remove the current input
terms.pop()
# add the selected item
terms.push ui.item.value
# add placeholder to get the comma-and-space at the end
terms.push ""
@value = terms.join ", "
return false
18 changes: 18 additions & 0 deletions app/assets/stylesheets/messaging/messages.sass
@@ -0,0 +1,18 @@
ul.ui-autocomplete
position: absolute
list-style: none
margin: 0
padding: 0
border: solid 1px #999
cursor: default
li
background-color: #FFF
border-top: solid 1px #DDD
margin: 0
padding: 0
a
color: #000
display: block
padding: 3px
a.ui-state-hover, a.ui-state-active
background-color: #FFFCB2
28 changes: 28 additions & 0 deletions app/controllers/messaging/messages_controller.rb
@@ -0,0 +1,28 @@
module Messaging
class MessagesController < ApplicationController
def index
@mailbox = current_user.mailbox
end

def new
@message = Message.new
end

def create
@message = Message.new params[:message]
unless @message.valid?
return render :new
end

puts @message.recipients.first.email
current_user.send_message(@message.recipients, @message.body, @message.subject)
flash[:notice] = "Message sent."

redirect_to root_path
end

def show
@conversation = Conversation.find_by_id(params[:id])
end
end
end
35 changes: 35 additions & 0 deletions app/models/messaging/message.rb
@@ -0,0 +1,35 @@
module Messaging
class Message
include ActiveModel::Validations
include ActiveModel::Conversion
extend ActiveModel::Naming

attr_accessor :recipients, :subject, :body

validates :recipients, presence: true
validates :subject, presence: true
validates :body, presence: true

def initialize(attributes = {})
attributes.each do |name, value|
send("#{name}=", value)
end
end

def persisted?
false
end


def recipients
@recipient_list
end

def recipients=(string='')
@recipient_list = []
string.split(',').each do |s|
@recipient_list << User.find_by_email!(s.strip) unless s.blank?
end
end
end
end
14 changes: 0 additions & 14 deletions app/views/layouts/messaging/application.html.erb

This file was deleted.

9 changes: 9 additions & 0 deletions app/views/layouts/messaging/application.html.haml
@@ -0,0 +1,9 @@
!!!
%html
%head
%title Messaging
= stylesheet_link_tag "messaging/application"
= javascript_include_tag "messaging/application"
= csrf_meta_tags
%body
= yield
7 changes: 7 additions & 0 deletions app/views/messaging/messages/index.html.haml
@@ -0,0 +1,7 @@
%h1 Messages
%h2 Inbox
%ul
- @mailbox.inbox.each do |m|
%li= link_to m.subject, message_path(m)
%nav
= link_to "Compose message", new_message_path
5 changes: 5 additions & 0 deletions app/views/messaging/messages/new.html.haml
@@ -0,0 +1,5 @@
= simple_form_for @message do |f|
= f.input :recipients, input_html: { data: { autocomplete_source: User.all.map(&:name) } }
= f.input :subject
= f.input :body, as: :text
= f.button :submit, 'Send message'
6 changes: 6 additions & 0 deletions app/views/messaging/messages/show.html.haml
@@ -0,0 +1,6 @@
%ul
- @conversation.messages.each do |message|
%li
%h2.subject= message.subject
.from= message.sender
.body= message.body
2 changes: 2 additions & 0 deletions config/routes.rb
@@ -1,2 +1,4 @@
Messaging::Engine.routes.draw do
resources :messages
root to: 'messages#index'
end
4 changes: 4 additions & 0 deletions test/dummy/app/controllers/home_controller.rb
@@ -0,0 +1,4 @@
class HomeController < ApplicationController
def index
end
end
23 changes: 23 additions & 0 deletions test/dummy/app/models/user.rb
@@ -0,0 +1,23 @@
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable

# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me

acts_as_messageable

def name
self.to_s
end

def mailboxer_email(message)
email
end

def to_s
email
end
end
8 changes: 8 additions & 0 deletions test/dummy/app/views/home/index.html.haml
@@ -0,0 +1,8 @@
%p Welcome the the Mailboxer Messenger Dummy app.
%nav
- if current_user
= link_to "Inbox", messaging_path
= link_to "Sign out", destroy_user_session_path, method: :delete
- else
= link_to "Register", new_user_registration_path
= link_to "Sign in", new_user_session_path
14 changes: 0 additions & 14 deletions test/dummy/app/views/layouts/application.html.erb

This file was deleted.

11 changes: 11 additions & 0 deletions test/dummy/app/views/layouts/application.html.haml
@@ -0,0 +1,11 @@
!!!
%html
%head
%title Mailboxer Messenger Dummy App
= stylesheet_link_tag "application"
= javascript_include_tag "application"
= csrf_meta_tags
%body
#alert= alert
#notice= notice
= yield
2 changes: 2 additions & 0 deletions test/dummy/config/environments/development.rb
Expand Up @@ -27,4 +27,6 @@

# Expands the lines which load the assets
config.assets.debug = true

config.action_mailer.default_url_options = { :host => 'localhost:3000' }
end

0 comments on commit 0b1cf1a

Please sign in to comment.