Refactor raises in requesting to validations in request, move to better controller structure

This commit is contained in:
Raphael 2010-11-16 16:13:10 -08:00
parent 6f72cd7184
commit 7876da8fb8
4 changed files with 54 additions and 52 deletions

View file

@ -32,39 +32,19 @@ class RequestsController < ApplicationController
end
def create
aspect = current_user.aspect_by_id(params[:aspect_id])
account = params[:destination_handle].strip
begin
finger = EMWebfinger.new(account)
finger.on_person{ |person|
if person.class == Person
rel_hash = {:person => person}
Rails.logger.debug("Sending request: #{rel_hash}")
begin
@request = current_user.send_contact_request_to(rel_hash[:person], aspect)
rescue Exception => e
Rails.logger.debug("error: #{e.message}")
flash[:error] = e.message
end
aspect = current_user.aspect_by_id(params[:request][:into])
account = params[:request][:to].strip
person = Person.by_account_identifier(account)
@request = Request.instantiate(:to => person,
:from => current_user.person,
:into => aspect)
if @request.save
current_user.dispatch_request(@request)
flash.now[:notice] = I18n.t('requests.create.sent')
redirect_to :back
else
#socket to tell people this failed?
end
}
rescue Exception => e
flash[:error] = e.message
end
if params[:getting_started]
redirect_to getting_started_path(:step=>params[:getting_started])
else
flash[:notice] = I18n.t('requests.create.tried', :account => account) unless flash[:error]
respond_with :location => aspects_manage_path
return
flash.now[:error] = @request.errors.full_messages.join(', ')
redirect_to :back
end
end
end

View file

@ -16,14 +16,19 @@ class Request
belongs_to :into, :class => Aspect
belongs_to :from, :class => Person
belongs_to :to, :class => Person
key :sent, Boolean, :default => false
validates_presence_of :from, :to
validate :not_already_connected, :if => :sent
validate :no_pending_request, :if => :sent
#before_validation :clean_link
def self.instantiate(opts = {})
self.new(:from => opts[:from],
:to => opts[:to],
:into => opts[:into])
:into => opts[:into],
:sent => true)
end
def reverse_for accepting_user
@ -68,4 +73,16 @@ class Request
self.from.diaspora_handle
end
private
def no_pending_request
if Request.first(:from_id => from_id, :to_id => to_id)
errors[:base] << 'You have already sent a request to that person'
end
end
def not_already_connected
if Contact.first(:user_id => self.from.owner_id, :person_id => self.to_id)
errors[:base] << 'You have already connected to this person!'
end
end
end

View file

@ -6,30 +6,27 @@ module Diaspora
module UserModules
module Connecting
def send_contact_request_to(desired_contact, aspect)
# should have different exception types for these?
raise "You cannot connect yourself" if desired_contact.nil?
raise "You have already sent a contact request to that person!" if self.pending_requests.detect {
|x| x.to == desired_contact }
raise "You are already connected to that person!" if contact_for desired_contact
request = Request.instantiate(
:to => desired_contact,
request = Request.instantiate(:to => desired_contact,
:from => self.person,
:into => aspect)
if request.save
if request.save!
dispatch_request request
end
request
end
def dispatch_request(request)
self.pending_requests << request
self.save
aspect.requests << request
aspect.save
push_to_people request, [desired_contact]
end
request
request.into.requests << request
request.into.save
push_to_people request, [request.to]
end
def accept_contact_request(request, aspect)
pending_request_ids.delete(request.id.to_id)
activate_contact(request.from, aspect)
request.reverse_for(self)
end

View file

@ -13,7 +13,7 @@ describe Request do
describe 'validations' do
before do
@request = Request.new(:from => user.person, :to => user2.person, :into => aspect)
@request = Request.instantiate(:from => user.person, :to => user2.person, :into => aspect)
end
it 'is valid' do
@request.should be_valid
@ -33,6 +33,14 @@ describe Request do
@request.into = nil
@request.should be_valid
end
it 'is not a duplicate of an existing pending request' do
request
@request.should_not be_valid
end
it 'is not to an existing friend' do
connect_users(user, aspect, user2, user2.aspects.create(:name => 'new aspect'))
@request.should_not be_valid
end
end
describe '#request_from_me' do