ruby objects, get outta my face. (conversationscontroller new)

This commit is contained in:
danielgrippi 2011-03-30 12:45:22 -07:00
parent 5c7bd6298a
commit d3c0f1b3d2
3 changed files with 25 additions and 15 deletions

View file

@ -58,7 +58,10 @@ class ConversationsController < ApplicationController
end
def new
@all_contacts_and_ids = current_user.contacts.map { |c| {:value => c.id, :name => c.person.name} }
@all_contacts_and_ids = Contact.connection.execute(current_user.contacts.joins(:person => :profile).select("contacts.id, profiles.first_name, profiles.last_name, profiles.diaspora_handle").to_sql).map do |r|
{:value => r[0], :name => Person.name_from_attrs(r[1], r[2], r[3])}
end
@contact = current_user.contacts.find(params[:contact_id]) if params[:contact_id]
render :layout => false
end

View file

@ -84,11 +84,12 @@ class Person < ActiveRecord::Base
end
def name(opts = {})
@name ||= if profile.nil? || profile.first_name.nil? || profile.first_name.blank?
self.diaspora_handle
else
"#{profile.first_name.to_s} #{profile.last_name.to_s}"
end
@name ||= Person.name_from_attrs(self.profile.first_name, self.profile.last_name, self.diaspora_handle)
end
def self.name_from_attrs(first_name, last_name, diaspora_handle)
first_name.blank? ? diaspora_handle : "#{first_name.to_s} #{last_name.to_s}"
end
def first_name

View file

@ -64,27 +64,33 @@ describe Person do
end
end
context '#name' do
let!(:user) { Factory(:user) }
let!(:person) { user.person }
let!(:profile) { person.profile }
context '.name_from_attrs' do
before do
@person = alice.person
@profile = @person.profile
end
context 'with first name' do
it 'should return their name for name' do
person.name.should match /#{profile.first_name}|#{profile.last_name}/
Person.name_from_attrs(@profile.first_name, @profile.last_name, @profile.diaspora_handle).should match /#{@profile.first_name}|#{@profile.last_name}/
end
end
context 'without first name' do
it 'should display their diaspora handle' do
person.profile.first_name = nil
person.profile.last_name = nil
person.save!
person.name.should == person.diaspora_handle
Person.name_from_attrs(nil, nil, @profile.diaspora_handle).should == @profile.diaspora_handle
end
end
end
context '#name' do
it 'calls Person.name_from_attrs' do
profile = alice.person.profile
Person.should_receive(:name_from_attrs).with(profile.first_name, profile.last_name, profile.person.diaspora_handle)
alice.name
end
end
describe 'xml' do
before do
@xml = @person.to_xml.to_s