diff --git a/app/controllers/people_controller.rb b/app/controllers/people_controller.rb index 1a8f68cb9..5877cff59 100644 --- a/app/controllers/people_controller.rb +++ b/app/controllers/people_controller.rb @@ -111,7 +111,7 @@ class PeopleController < ApplicationController respond_to do |format| format.all { respond_with @person, :locals => {:post_type => :all} } format.json { - render :json => @person.to_json + render :json => @person.to_json(:includes => params[:includes]) } end end diff --git a/app/models/person.rb b/app/models/person.rb index e67a5071f..68d18a011 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -219,15 +219,17 @@ class Person < ActiveRecord::Base self.posts.where(:type => "Photo").exists? end - def as_json(opts={}) + def as_json( opts = {} ) + opts ||= {} json = { :id => self.id, :name => self.name, :avatar => self.profile.image_url(:thumb_medium), :handle => self.diaspora_handle, :url => "/people/#{self.id}", - :hashtags => self.profile.tags.map{|t| "##{t.name}"} } + json.merge!(:tags => self.profile.tags.map{|t| "##{t.name}"}) if opts[:includes] == "tags" + json end protected diff --git a/public/javascripts/widgets/hovercard.js b/public/javascripts/widgets/hovercard.js index 02a0f9e6a..4b7774c88 100644 --- a/public/javascripts/widgets/hovercard.js +++ b/public/javascripts/widgets/hovercard.js @@ -7,7 +7,7 @@ this.start = function() { self.personCache = new this.Cache(); self.dropdownCache = new this.Cache(); - + var card = $("#hovercard"); self.hoverCard = { tip: $("#hovercard_container"), @@ -52,7 +52,7 @@ self.hoverCard.tip.hide(); self.hoverCard.tip.prependTo(self.target.parent()); - self.personCache.get(self.target.attr("href") + ".json", function(person) { + self.personCache.get(self.target.attr("href") + ".json?includes=tags", function(person) { self.populateHovercard(person); }); }; @@ -70,7 +70,7 @@ self.hoverCard.dropdown.attr("data-person-id", person.id); self.hoverCard.hashtags.html(""); - $.each(person.hashtags, function(index, hashtag) { + $.each(person.tags, function(index, hashtag) { self.hoverCard.hashtags.append( $("", { href: "/tags/" + hashtag.substring(1) diff --git a/spec/controllers/people_controller_spec.rb b/spec/controllers/people_controller_spec.rb index 2339897fd..fa7e4d4ef 100644 --- a/spec/controllers/people_controller_spec.rb +++ b/spec/controllers/people_controller_spec.rb @@ -57,7 +57,7 @@ describe PeopleController do get :index, :q => "eugene@example.org" assigns[:people].should =~ [eugene2] end - + it "does not redirect to person page if there is exactly one match" do get :index, :q => "Korth" response.should_not redirect_to @korth @@ -169,6 +169,12 @@ describe PeopleController do get :show, :id => @user.person.id response.should be_success end + + it 'passes through the includes option for json requests' do + json = @user.person.as_json + Person.any_instance.should_receive(:as_json).with(:includes => "horses").and_return(json) + get :show, :format => :json, :id => @user.person.id, :includes => "horses" + end end context "with no user signed in" do diff --git a/spec/models/person_spec.rb b/spec/models/person_spec.rb index af2907cb0..94cce73f4 100644 --- a/spec/models/person_spec.rb +++ b/spec/models/person_spec.rb @@ -364,7 +364,18 @@ describe Person do end describe '#as_json' do - it 'returns a hash representation of a person' - it 'return tags if asked' + it 'returns a hash representation of a person' do + @person.as_json.should == { + :id => @person.id, + :name => @person.name, + :avatar => @person.profile.image_url(:thumb_medium), + :handle => @person.diaspora_handle, + :url => "/people/#{@person.id}", + } + end + it 'return tags if asked' do + @person.as_json(:includes => :tags). + should == @person.as_json.merge(:tags => @person.profile.tags.map{|t| "##{t.name}"}) + end end end