Skip to content

Commit

Permalink
Adding Message and Mail integration rspec tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Roendal committed Mar 6, 2011
1 parent 282bb0b commit afa928f
Show file tree
Hide file tree
Showing 37 changed files with 92 additions and 869 deletions.
18 changes: 1 addition & 17 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,19 +1,3 @@
source "http://rubygems.org"

gem "rails", "3.0.0"
gem "capybara", ">= 0.3.9"
gem "webrat"
gem "sqlite3-ruby", :require => "sqlite3"

if RUBY_VERSION < '1.9'
gem "ruby-debug", ">= 0.10.3"
end


gem "rspec-rails", ">= 2.0.0.beta"

group :development do
gem "bundler", "~> 1.0.0"
gem "jeweler", "~> 1.5.0.pre3"
gem "rcov", ">= 0"
end
gemspec
139 changes: 0 additions & 139 deletions Gemfile.lock

This file was deleted.

3 changes: 3 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ Jeweler::Tasks.new do |gem|
# and development dependencies are only needed for development (ie running rake tasks, tests, etc)
# gem.add_runtime_dependency 'jabber4r', '> 0.1'
# gem.add_development_dependency 'rspec', '> 1.2.3'
gem.add_development_dependency('rspec-rails', '~> 2.4.1')
gem.add_development_dependency('factory_girl', '~> 1.3.2')
gem.add_development_dependency('forgery', '~> 0.3.6')
end
Jeweler::RubygemsDotOrgTasks.new

Expand Down
22 changes: 10 additions & 12 deletions app/models/mailboxer_conversation.rb
Original file line number Diff line number Diff line change
@@ -1,45 +1,43 @@
class MailboxerConversation < ActiveRecord::Base
attr_reader :originator, :original_message, :last_sender, :last_message, :users
has_many :mailboxer_messages
#has_many :mailboxer_mails
before_create :clean
#looks like shit but isnt too bad
#has_many :users, :through :messages, :source => :recipients, :uniq => true doesnt work due to recipients being a habtm association
has_many :recipients, :class_name => 'User', :finder_sql =>
has_many :recipients, :finder_sql =>
'SELECT users.* FROM mailboxer_conversations
INNER JOIN mailboxer_messages ON mailboxer_conversations.id = mailboxer_messages.mailboxer_conversation_id
INNER JOIN mailboxer__recipients ON mailboxer__recipients.message_id = mailboxer_messages.id
INNER JOIN users ON messages_recipients.recipient_id = users.id
WHERE conversations.id = #{self.id} GROUP BY users.id;'

#originator of the conversation.
def originator()
def originator
@orignator = self.original_message.sender if @originator.nil?
return @orignator
end

#first message of the conversation.
def original_message()
def original_message
@original_message = self.mailboxer_messages.find(:first, :order => 'created_at') if @original_message.nil?
return @original_message
end

#sender of the last message.
def last_sender()
def last_sender
@last_sender = self.last_message.sender if @last_sender.nil?
return @last_sender
end

#last message in the conversation.
def last_message()
def last_message
@last_message = self.mailboxer_messages.find(:first, :order => 'created_at DESC') if @last_message.nil?
return @last_message
end

#all users involved in the conversation.
def users()
if(@users.nil?)
@users = self.mailboxer_recipients.clone
def users
if @users.nil?
@users = self.recipients.clone
@users << self.originator unless @users.include?(self.originator)
end
return @users
Expand All @@ -49,8 +47,8 @@ def users()
#[empty method]
#
#this gets called before_create. Implement this if you wish to clean out illegal content such as scripts or anything that will break layout. This is left empty because what is considered illegal content varies.
def clean()
return if(subject.nil?)
def clean
return if subject.nil?
#strip all illegal content here. (scripts, shit that will break layout, etc.)
end
end
7 changes: 3 additions & 4 deletions app/models/mailboxer_mail.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
class MailboxerMail < ActiveRecord::Base
belongs_to :mailboxer_message
belongs_to :user
#belongs_to :mailboxer_conversation
belongs_to :receiver, :polymorphic => :true

#sets the read attribute of the mail message to true.
def mark_as_read()
def mark_as_read
update_attribute('read', true)
end

#sets the read attribute of the mail message to false.
def mark_as_unread()
def mark_as_unread
update_attribute('read', false)
end

Expand Down
40 changes: 19 additions & 21 deletions app/models/mailboxer_mailbox.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ class MailboxerMailbox
#this is used to filter mail by mailbox type, use the [] method rather than setting this directly.
attr_accessor :type
#the user/owner of this mailbox, set when initialized.
attr_reader :user
attr_reader :messageable
#creates a new Mailbox instance with the given user and optional type.
def initialize(user, type = :all)
@user = user
@type = type
def initialize(recipient, box = :all)
@messageable = recipient
@type = box
end
#sets the mailbox type to the symbol corresponding to the given val.
def type=(val)
Expand All @@ -33,7 +33,7 @@ def type=(val)
# phil.mailbox[:inbox].mail_count(:unread)
#
def mail_count(filter = :all, options = {})
default_options = {:conditions => ["user_id = ?", @user.id]}
default_options = {:conditions => ["user_id = ?", @messageable.id]}
add_mailbox_condition!(default_options, @type)
add_conditions!(default_options, "read = ?", filter == :read) unless filter == :all
return count_mail(default_options, options)
Expand Down Expand Up @@ -62,7 +62,7 @@ def mail_count(filter = :all, options = {})
# phil.mailbox.mail(:conversation => Conversation.find(23), :conditions => 'mail.read = false')
#
def mail(options = {})
default_options = {:conditions => ["user_id = ?", @user.id]}
default_options = {:conditions => ["receiver_id = ? AND receiver_type=?", @messageable.id, @messageable.type]}
add_mailbox_condition!(default_options, @type)
return get_mail(default_options, options)
end
Expand All @@ -81,7 +81,7 @@ def mail(options = {})
# phil.mailbox[:inbox].unread_mail
#
def unread_mail(options = {})
default_options = {:conditions => ["read = ? AND user_id = ?", false, @user.id]}
default_options = {:conditions => ["read = ? AND user_id = ?", false, @messageable.id]}
add_mailbox_condition!(default_options, @type)
return get_mail(default_options, options)
end
Expand All @@ -100,7 +100,7 @@ def unread_mail(options = {})
# phil.mailbox[:inbox].read_mail
#
def read_mail(options = {})
default_options = {:conditions => ["read = ? AND user_id = ?", true, @user.id]}
default_options = {:conditions => ["read = ? AND user_id = ?", true, @messageable.id]}
add_mailbox_condition!(default_options, @type)
return get_mail(default_options, options)
end
Expand Down Expand Up @@ -137,10 +137,10 @@ def latest_mail(options = {})
def add(msg)
attributes = {:mailboxer_message => msg}
attributes[:mailbox_type] = @type.to_s unless @type == :all
attributes[:read] = msg.sender.id == @user.id
attributes[:user_id] = @user.id ##AÑADIDO POR MI
attributes[:read] = msg.sender.id == @messageable.id
mail_msg = MailboxerMail.new(attributes)
@user.mailboxer_mails << mail_msg
mail_msg.receiver = @messageable
@messageable.mailboxer_mails << mail_msg
return mail_msg
end
#marks all the mail messages matched by the options and type as read.
Expand All @@ -158,7 +158,7 @@ def add(msg)
# phil.mailbox[:inbox].mark_as_read()
#
def mark_as_read(options = {})
default_options = {:conditions => ["user_id = ?", @user.id]}
default_options = {:conditions => ["user_id = ?", @messageable.id]}
add_mailbox_condition!(default_options, @type)
return update_mail("read = true", default_options, options)
end
Expand All @@ -177,7 +177,7 @@ def mark_as_read(options = {})
# phil.mailbox[:inbox].mark_as_unread()
#
def mark_as_unread(options = {})
default_options = {:conditions => ["mailbox_type != ? AND user_id = ?",@user.mailbox_types[:sent].to_s, @user.id]}
default_options = {:conditions => ["mailbox_type != ? AND user_id = ?",@messageable.mailbox_types[:sent].to_s, @messageable.id]}
add_mailbox_condition!(default_options, @type)
return update_mail("read = false", default_options, options)
end
Expand All @@ -189,14 +189,14 @@ def mark_as_unread(options = {})
#
def move_to(mailbox, options = {})
mailbox = mailbox.to_sym
trash = mailbox == @user.mailbox_types[:deleted].to_sym
default_options = {:conditions => ["user_id = ?", @user.id]}
trash = mailbox == @messageable.mailbox_types[:deleted].to_sym
default_options = {:conditions => ["user_id = ?", @messageable.id]}
add_mailbox_condition!(default_options, @type)
if(!trash)
#conditional update because sentmail is always sentmail - I believe case if the most widely supported conditional, mysql also has an if which would work as well but i think mysql is the only one to support it
return update_mail("trashed = 'f', mailbox_type =
CASE mailbox_type
WHEN '#{@user.mailbox_types[:sent].to_s}' THEN mailbox_type
WHEN '#{@messageable.mailbox_types[:sent].to_s}' THEN mailbox_type
ELSE '#{mailbox.to_s}'
END", default_options, options)
end
Expand All @@ -208,7 +208,7 @@ def move_to(mailbox, options = {})
#options:: see mail for acceptable options.
#
def delete(options = {})
default_options = {:conditions => ["user_id = ?", @user.id]}
default_options = {:conditions => ["user_id = ?", @messageable.id]}
add_mailbox_condition!(default_options, @type)
return delete_mail(default_options, options)
end
Expand All @@ -222,7 +222,7 @@ def <<(msg)
#options:: see mail for acceptable options.
#
def empty_trash(options = {})
default_options = {:conditions => ["user_id = ? AND trashed = ?", @user.id, true]}
default_options = {:conditions => ["user_id = ? AND trashed = ?", @messageable.id, true]}
add_mailbox_condition!(default_options, @type)
return delete_mail(default_options, options)
end
Expand Down Expand Up @@ -252,12 +252,10 @@ def [](mailbox_type)
private
def get_mail(default_options, options)
build_options(default_options, options) unless options.empty?
#return @user.mailboxer_mails.find(:all, default_options)
return MailboxerMail.find(:all, default_options)
end
def update_mail(updates, default_options, options)
build_options(default_options, options) unless options.empty?
#return @user.mailboxer_mails.update_all(updates, default_options[:conditions])
return MailboxerMail.update_all(updates, default_options[:conditions])
end
def delete_mail(default_options, options)
Expand Down Expand Up @@ -287,7 +285,7 @@ def only_latest(mail)
end
def add_mailbox_condition!(options, mailbox_type)
return if mailbox_type == :all
return add_conditions!(options, "mailbox_type = ? AND trashed = ?", mailbox_type.to_s, false) unless mailbox_type == @user.mailbox_types[:deleted]
return add_conditions!(options, "mailbox_type = ? AND trashed = ?", mailbox_type.to_s, false) unless mailbox_type == @messageable.mailbox_types[:deleted]
return add_conditions!(options, "trashed = ?", true)
end
def add_conversation_condition!(options, conversation)
Expand Down
Loading

0 comments on commit afa928f

Please sign in to comment.