Skip to content
This repository has been archived by the owner on Sep 21, 2022. It is now read-only.

Commit

Permalink
FIXBUG: Invitation raise an exception if contact or other invitation …
Browse files Browse the repository at this point in the history
…exists

* Created custom exceptions.
* Refactored partials for flash messsages.
  • Loading branch information
jhbabon committed Dec 15, 2010
1 parent 875abb3 commit 490f6c4
Show file tree
Hide file tree
Showing 13 changed files with 96 additions and 17 deletions.
7 changes: 7 additions & 0 deletions app/controllers/invitations_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
class InvitationsController < ApplicationController
rescue_from Exceptions::Invitations::ContactExists do
redirect_to(contacts_path, :alert => I18n.t("views.invitations.flash.contact_exists"))
end
rescue_from Exceptions::Invitations::InvitationExists do
redirect_to(contacts_path, :alert => I18n.t("views.invitations.flash.invitation_exists"))
end

# GET /invitations
def index
@invitations = current_user.received_invitations.order("created_at DESC").paginate(:page => params[:page])
Expand Down
15 changes: 15 additions & 0 deletions app/models/invitation.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class Invitation < ActiveRecord::Base
before_create :is_necessary?

belongs_to :sender, :class_name => 'User'
belongs_to :receiver, :class_name => 'User'

Expand All @@ -12,4 +14,17 @@ def accept
Contact.create(:friend => sender,
:user => receiver)
end

protected

def is_necessary?
if Contact.reverse_proxy(User.find(self.receiver_id))
raise Exceptions::Invitations::ContactExists
end

if Invitation.where(:sender_id => self.sender_id, :receiver_id => self.receiver_id).first ||
Invitation.where(:sender_id => self.receiver_id, :receiver_id => self.sender_id).first
raise Exceptions::Invitations::InvitationExists
end
end
end
6 changes: 0 additions & 6 deletions app/views/layouts/_alert.html.erb

This file was deleted.

8 changes: 8 additions & 0 deletions app/views/layouts/_flash_message.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<% if flash[key] -%>
<div class="grid_6 prefix_3 suffix_3">
<div class="<%= key.to_s %>">
<%= flash[key] %>
</div>
</div>
<%= render 'shared/clear' %>
<% end -%>
6 changes: 0 additions & 6 deletions app/views/layouts/_notice.html.erb

This file was deleted.

4 changes: 2 additions & 2 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

<div class="layout">
<div class="container_12">
<%= render 'layouts/alert' %>
<%= render 'layouts/notice' %>
<%= render 'layouts/flash_message', :key => :alert %>
<%= render 'layouts/flash_message', :key => :notice %>
<%= yield :subnavigation %>
<%= yield %>
</div>
Expand Down
2 changes: 2 additions & 0 deletions config/locales/views/invitations/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ en:
flash:
sended: "The invitation was successfully sended"
error: "The invitation couldn't send"
contact_exists: "The invitation couldn't send: the contact already exists"
invitation_exists: "The invitation couldn't send: there is a pending invitation"
2 changes: 2 additions & 0 deletions config/locales/views/invitations/es.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ es:
flash:
sended: "La invitación se ha enviado con éxito"
error: "No se pudo enviar la invitación"
contact_exists: "No se pudo enviar la invitación: el contacto ya existe"
invitation_exists: "No se pudo enviar la invitación: ya hay una invitación pendiente"
2 changes: 1 addition & 1 deletion lib/debt_pursuit/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module DebtPursuit
module Version
MAYOR = "0"
MINOR = "9"
PATCH = "0"
PATCH = "1"

NUMBER = [MAYOR, MINOR, PATCH].join(".")

Expand Down
6 changes: 6 additions & 0 deletions lib/exceptions/invitations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module Exceptions
module Invitations
class ContactExists < StandardError; end
class InvitationExists < StandardError; end
end
end
2 changes: 0 additions & 2 deletions public/stylesheets/layout.css
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ h4 { font-size: 20px; }
.alert,
.notice {
text-align: center;
width: 95%;
max-width: 310px !important;
margin-bottom: 10px;
padding: 5px;
}
Expand Down
19 changes: 19 additions & 0 deletions test/functional/invitations_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,23 @@ class InvitationsControllerTest < ActionController::TestCase

assert_redirected_to invitations_path
end

test "should rescue from existence of contact" do
@invitation.accept
post :create, :invitation => Factory.build(:invitation,
:sender => @invitation.sender,
:receiver => @invitation.receiver).attributes

assert_redirected_to contacts_path
assert flash[:alert]
end

test "should rescue from existence of invitation" do
post :create, :invitation => Factory.build(:invitation,
:sender => @invitation.sender,
:receiver => @invitation.receiver).attributes

assert_redirected_to contacts_path
assert flash[:alert]
end
end
34 changes: 34 additions & 0 deletions test/unit/invitation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,38 @@ class InvitationTest < ActiveSupport::TestCase
assert_equal sender_contacts + 1, sender.contacts.count
assert_equal receiver_contacts + 1, receiver.contacts.count
end

test "should not save if the users are friends" do
sender = Factory.create(:user)
receiver = Factory.create(:user)
invitation = Factory.create(:invitation,
:sender => sender,
:receiver => receiver)
invitation.accept
invitation.destroy

invitation = Factory.build(:invitation,
:sender => sender,
:receiver => receiver)

assert_raise(Exceptions::Invitations::ContactExists) { invitation.save }
end

test "should not save if exists other invitation" do
sender = Factory.create(:user)
receiver = Factory.create(:user)
invitation = Factory.create(:invitation,
:sender => sender,
:receiver => receiver)

invitation = Factory.build(:invitation,
:sender => sender,
:receiver => receiver)
assert_raise(Exceptions::Invitations::InvitationExists) { invitation.save }

invitation = Factory.build(:invitation,
:sender => receiver,
:receiver => sender)
assert_raise(Exceptions::Invitations::InvitationExists) { invitation.save }
end
end

0 comments on commit 490f6c4

Please sign in to comment.