Skip to content
Extraits de code Groupes Projets
Non vérifiée Valider a3f208c3 rédigé par cmrd Senya's avatar cmrd Senya
Parcourir les fichiers

Notifications and search page backend updates

Updates introduce support for preloading contacts to Gon in order
to support client-side rendering of aspect membership dropdown box.
parent 82ac6113
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -38,9 +38,6 @@ class NotificationsController < ApplicationController ...@@ -38,9 +38,6 @@ class NotificationsController < ApplicationController
pager.replace(result) pager.replace(result)
end end
@notifications.each do |note|
note.note_html = render_to_string( :partial => 'notification', :locals => { :note => note } )
end
@group_days = @notifications.group_by{|note| note.created_at.strftime('%Y-%m-%d')} @group_days = @notifications.group_by{|note| note.created_at.strftime('%Y-%m-%d')}
@unread_notification_count = current_user.unread_notifications.count @unread_notification_count = current_user.unread_notifications.count
...@@ -54,7 +51,12 @@ class NotificationsController < ApplicationController ...@@ -54,7 +51,12 @@ class NotificationsController < ApplicationController
respond_to do |format| respond_to do |format|
format.html format.html
format.xml { render :xml => @notifications.to_xml } format.xml { render :xml => @notifications.to_xml }
format.json { render :json => @notifications.to_json } format.json {
@notifications.each do |note|
note.note_html = render_to_string(partial: "notification", locals: {note: note, no_aspect_dropdown: true})
end
render json: @notifications.to_json
}
end end
end end
......
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
# the COPYRIGHT file. # the COPYRIGHT file.
class PeopleController < ApplicationController class PeopleController < ApplicationController
include GonHelper
before_action :authenticate_user!, except: %i(show stream hovercard) before_action :authenticate_user!, except: %i(show stream hovercard)
before_action :find_person, only: %i(show stream hovercard) before_action :find_person, only: %i(show stream hovercard)
...@@ -168,16 +170,14 @@ class PeopleController < ApplicationController ...@@ -168,16 +170,14 @@ class PeopleController < ApplicationController
end end
def hashes_for_people(people, aspects) def hashes_for_people(people, aspects)
ids = people.map{|p| p.id} people.map {|person|
contacts = {} {
Contact.unscoped.where(:user_id => current_user.id, :person_id => ids).each do |contact| person: person,
contacts[contact.person_id] = contact contact: current_user.contact_for(person) || Contact.new(person: person),
end aspects: aspects
}.tap {|hash|
people.map{|p| gon_load_contact(hash[:contact])
{:person => p, }
:contact => contacts[p.id],
:aspects => aspects}
} }
end end
......
...@@ -3,25 +3,6 @@ ...@@ -3,25 +3,6 @@
# the COPYRIGHT file. # the COPYRIGHT file.
module AspectGlobalHelper module AspectGlobalHelper
def aspect_membership_dropdown(contact, person, hang)
aspect_membership_ids = {}
selected_aspects = all_aspects.select{|aspect| contact.in_aspect?(aspect)}
selected_aspects.each do |a|
record = a.aspect_memberships.find { |am| am.contact_id == contact.id }
aspect_membership_ids[a.id] = record.id
end
button_class = selected_aspects.size > 0 ? "btn-success" : "btn-default"
render "aspect_memberships/aspect_membership_dropdown",
:selected_aspects => selected_aspects,
:aspect_membership_ids => aspect_membership_ids,
:person => person,
:hang => hang,
:button_class => button_class
end
def aspect_options_for_select(aspects) def aspect_options_for_select(aspects)
options = {} options = {}
aspects.each do |aspect| aspects.each do |aspect|
......
module GonHelper
def gon_load_contact(contact)
Gon.preloads[:contacts] ||= []
if Gon.preloads[:contacts].none? {|stored_contact| stored_contact[:person][:id] == contact.person_id }
Gon.preloads[:contacts] << ContactPresenter.new(contact, current_user).full_hash_with_person
end
end
end
...@@ -12,5 +12,9 @@ module Notifications ...@@ -12,5 +12,9 @@ module Notifications
sender = contact.person sender = contact.person
create_notification(contact.user_id, sender, sender).email_the_user(sender, sender) create_notification(contact.user_id, sender, sender).email_the_user(sender, sender)
end end
def contact
recipient.contact_for(target)
end
end end
end end
.media.stream_element{:data=>{:guid => note.id, :type => (Notification.types.key(note.type) || '') }, :class => (note.unread ? 'unread' : 'read')} .media.stream_element{:data=>{:guid => note.id, :type => (Notification.types.key(note.type) || '') }, :class => (note.unread ? 'unread' : 'read')}
.unread-toggle.pull-right .unread-toggle.pull-right
%i.entypo-eye{title: (note.unread ? t("notifications.index.mark_read") : t("notifications.index.mark_unread"))} %i.entypo-eye{title: (note.unread ? t("notifications.index.mark_read") : t("notifications.index.mark_unread"))}
- if note.type == "Notifications::StartedSharing" && contact = current_user.contact_for(note.target) - if note.type == "Notifications::StartedSharing" && (!defined?(no_aspect_dropdown) || !no_aspect_dropdown)
.pull-right - if note.target.present?
= aspect_membership_dropdown(contact, note.target, "right") - gon_load_contact(note.contact)
.pull-right
.aspect_membership_dropdown.placeholder{data: {person_id: note.target.id}}
.media-object.pull-left .media-object.pull-left
= person_image_link note.actors.first, :size => :thumb_small, :class => 'hovercardable' = person_image_link note.actors.first, :size => :thumb_small, :class => 'hovercardable'
......
.media.stream_element{id: person.id} .media.stream_element{id: person.id}
.pull-right .pull-right
= render partial: 'people/relationship_action', = render partial: 'people/relationship_action',
locals: { person: person, contact: contact, locals: { person: person,
:current_user => current_user } :current_user => current_user }
.media-object.pull-left .media-object.pull-left
= person_image_link(person, size: :thumb_small) = person_image_link(person, size: :thumb_small)
......
- unless person == current_user.person - unless person == current_user.person
- contact = current_user.contacts.find_by_person_id(person.id) .aspect_membership_dropdown.placeholder{data: {person_id: person.id}}
- contact ||= Contact.new(:person => person)
.aspect_membership_dropdown
-else -else
%span.thats_you %span.thats_you
= t('people.person.thats_you') = t("people.person.thats_you")
...@@ -111,7 +111,7 @@ describe NotificationsController, :type => :controller do ...@@ -111,7 +111,7 @@ describe NotificationsController, :type => :controller do
eve.share_with(alice.person, eve.aspects.first) eve.share_with(alice.person, eve.aspects.first)
get :index, "per_page" => 5 get :index, "per_page" => 5
expect(Nokogiri(response.body).css(".aspect_membership")).not_to be_empty expect(Nokogiri(response.body).css(".aspect_membership_dropdown")).not_to be_empty
end end
it 'succeeds on mobile' do it 'succeeds on mobile' do
......
require "spec_helper"
describe GonHelper, type: :helper do
include_context :gon
describe "#gon_load_contact" do
let(:contact) { FactoryGirl.build(:contact) }
let(:current_user) { contact.user }
let(:another_contact) { FactoryGirl.build(:contact, user: current_user) }
before do
RequestStore.store[:gon] = Gon::Request.new(controller.request.env)
Gon.preloads = {}
end
it "calls appropriate presenter" do
expect_any_instance_of(ContactPresenter).to receive(:full_hash_with_person)
gon_load_contact(contact)
end
shared_examples "contacts loading" do
it "loads contacts to gon" do
gon_load_contact(contact)
gon_load_contact(another_contact)
expect(Gon.preloads[:contacts].count).to eq(2)
end
it "avoids duplicates" do
gon_load_contact(contact)
gon_load_contact(contact)
expect(Gon.preloads[:contacts].count).to eq(1)
end
end
context "with non-persisted contacts" do
include_examples "contacts loading"
end
context "with persisted contacts" do
before do
contact.save!
another_contact.save!
end
include_examples "contacts loading"
end
end
end
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter