From f60a748c21d48fc59503c45b599ba6a020583a51 Mon Sep 17 00:00:00 2001 From: Raphael Sofaer Date: Fri, 15 Jul 2011 14:23:44 -0400 Subject: [PATCH] Ajax in the contacts for mentioning after page load --- app/controllers/aspects_controller.rb | 2 +- app/controllers/contacts_controller.rb | 5 +++++ app/views/shared/_publisher.html.haml | 4 ++-- public/javascripts/publisher.js | 15 ++++++++------- spec/controllers/contacts_controller_spec.rb | 6 ++++++ 5 files changed, 22 insertions(+), 10 deletions(-) diff --git a/app/controllers/aspects_controller.rb b/app/controllers/aspects_controller.rb index 57f67a52d..ad4958ae3 100644 --- a/app/controllers/aspects_controller.rb +++ b/app/controllers/aspects_controller.rb @@ -39,7 +39,7 @@ class AspectsController < ApplicationController all_selected_people = Person.joins(:contacts => :aspect_memberships). where(:contacts => {:user_id => current_user.id}, :aspect_memberships => {:aspect_id => aspect_ids}) - @selected_people = all_selected_people.select("DISTINCT people.*").includes(:profile) + @selected_people = all_selected_people.select("DISTINCT people.*").limit(20).includes(:profile) end @aspect_ids = @aspects.map { |a| a.id } diff --git a/app/controllers/contacts_controller.rb b/app/controllers/contacts_controller.rb index 667dda4c4..221afa722 100644 --- a/app/controllers/contacts_controller.rb +++ b/app/controllers/contacts_controller.rb @@ -11,6 +11,11 @@ class ContactsController < ApplicationController if params[:a_id] @aspect_ = current_user.aspects.find(params["a_id"]) @contacts = @aspect_.contacts.includes(:aspects, :person => :profile).order('profiles.last_name ASC').paginate(:page => params[:page], :per_page => 25) + elsif params[:aspect_ids] && request.format == "json" + @people = Person.joins(:contacts => :aspect_memberships). + where(:contacts => {:user_id => current_user.id}, + :aspect_memberships => {:aspect_id => params[:aspect_ids]}) + render :json => @people.to_json elsif params[:set] == "only_sharing" @contacts = current_user.contacts.only_sharing.includes(:aspects, :person => :profile).order('profiles.last_name ASC').paginate(:page => params[:page], :per_page => 25) elsif params[:set] != "all" diff --git a/app/views/shared/_publisher.html.haml b/app/views/shared/_publisher.html.haml index 75346a456..da22fe96b 100644 --- a/app/views/shared/_publisher.html.haml +++ b/app/views/shared/_publisher.html.haml @@ -12,8 +12,6 @@ #publisher.closed{:class => ((aspect == :profile)? 'mention_popup' : nil )} .content_creation = form_for(StatusMessage.new, :remote => true, :html => {"data-type" => "json"}) do |status| - - if @selected_people - = hidden_field_tag :contact_json, @selected_people.to_json = status.error_messages %p %params @@ -52,6 +50,8 @@ .facebox_content #question_mark_pane = render 'shared/public_explain' + = link_to '', contacts_path(:aspect_ids => aspect_ids), :class => 'selected_contacts_link hidden' + #publisher_photo_upload = render 'photos/new_photo', :aspect_ids => aspect_ids.join(',') diff --git a/public/javascripts/publisher.js b/public/javascripts/publisher.js index d533ca4ad..e86e6aea5 100644 --- a/public/javascripts/publisher.js +++ b/public/javascripts/publisher.js @@ -254,14 +254,15 @@ var Publisher = { return ''; } }, - contactsJSON: function(){ - return $.parseJSON($('#contact_json').val()); - }, initialize: function(){ - Publisher.input().autocomplete(Publisher.autocompletion.contactsJSON(), - Publisher.autocompletion.options()); - Publisher.input().result(Publisher.autocompletion.selectItemCallback); - Publisher.oldInputContent = Publisher.input().val(); + $.getJSON($("#publisher .selected_contacts_link").attr("href"), undefined , + function(data){ + Publisher.input().autocomplete(data, + Publisher.autocompletion.options()); + Publisher.input().result(Publisher.autocompletion.selectItemCallback); + Publisher.oldInputContent = Publisher.input().val(); + } + ); } }, determineSubmitAvailability: function(){ diff --git a/spec/controllers/contacts_controller_spec.rb b/spec/controllers/contacts_controller_spec.rb index 5720a6fe2..d362f135e 100644 --- a/spec/controllers/contacts_controller_spec.rb +++ b/spec/controllers/contacts_controller_spec.rb @@ -62,6 +62,12 @@ describe ContactsController do contacts.to_set.should == bob.contacts.to_set end + it 'will return the contacts for multiple aspects' do + get :index, :aspect_ids => bob.aspect_ids, :format => 'json' + assigns[:people].should == bob.contacts.map(&:person) + response.should be_success + end + it "generates a jasmine fixture", :fixture => true do get :index save_fixture(html_for("body"), "aspects_manage")