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

Introduce NotificationSerializer

And remove the note_html property from the model.
parent a3f208c3
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
......@@ -52,13 +52,16 @@ class NotificationsController < ApplicationController
format.html
format.xml { render :xml => @notifications.to_xml }
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
render json: @notifications, each_serializer: NotificationSerializer
}
end
end
def default_serializer_options
{
context: self,
root: false
}
end
def read_all
......
......@@ -8,16 +8,10 @@ class Notification < ActiveRecord::Base
has_many :actors, class_name: "Person", through: :notification_actors, source: :person
belongs_to :target, polymorphic: true
attr_accessor :note_html
def self.for(recipient, opts={})
where(opts.merge!(recipient_id: recipient.id)).order("updated_at DESC")
end
def as_json(opts={})
super(opts.merge(methods: :note_html))
end
def email_the_user(target, actor)
recipient.mail(mail_job, recipient_id, actor.id, target.id)
end
......
class NotificationSerializer < ActiveModel::Serializer
attributes :id,
:target_type,
:target_id,
:recipient_id,
:unread,
:created_at,
:updated_at,
:note_html
def note_html
context.render_to_string(partial: "notifications/notification", locals: {note: object, no_aspect_dropdown: true})
end
def initialize(*_)
super
self.polymorphic = true
self.root = false
end
end
require "spec_helper"
describe NotificationSerializer do
let(:notifications_controller) { NotificationsController.new }
before do
allow(notifications_controller).to receive(:current_user).and_return(notification.recipient)
notifications_controller.request = ActionController::TestRequest.new(host: AppConfig.pod_uri)
end
let(:notification) { FactoryGirl.create(:notification) }
let(:json_output) {
NotificationSerializer.new(notification, context: notifications_controller).to_json
}
subject(:parsed_hash) {
JSON.parse json_output
}
it { is_expected.to have_key(notification.type.demodulize.underscore.to_s) }
context "typed object" do
let(:object) {
parsed_hash[notification.type.demodulize.underscore]
}
it "have all properties set" do
%w(id target_id recipient_id unread target_type).each do |key|
expect(object[key]).to eq(notification.send(key))
end
%w(created_at updated_at).each do |key|
expect(object[key]).to eq(notification.send(key).strftime("%FT%T.%LZ"))
end
expect(object).to have_key("note_html")
end
context "note_html" do
before do
# Nokogiri issue, see https://github.com/sparklemotion/nokogiri/issues/800
# TODO: remove when the above issue is fixed
expect_any_instance_of(ApplicationHelper).to receive(:timeago).and_return("")
end
let(:note_html) { object["note_html"] }
it "contains valid html" do
expect(Nokogiri::HTML(note_html).errors).to eq([])
end
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