From f978a75b8cbe3c08e984dd984660df05000ad490 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabi=C3=A1n=20Rodr=C3=ADguez?= Date: Sun, 23 Dec 2012 22:25:09 -0200 Subject: [PATCH] bootstrap my_aspects data --- app/helpers/layout_helper.rb | 3 ++- app/presenters/user_presenter.rb | 13 +++++++++---- spec/presenters/user_presenter_spec.rb | 4 ++-- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/app/helpers/layout_helper.rb b/app/helpers/layout_helper.rb index 781fa5679..c226aff74 100644 --- a/app/helpers/layout_helper.rb +++ b/app/helpers/layout_helper.rb @@ -39,7 +39,8 @@ module LayoutHelper def set_current_user_in_javascript return unless user_signed_in? - user = UserPresenter.new(current_user).to_json + a_ids = session[:a_ids] || [] + user = UserPresenter.new(current_user, a_ids).to_json content_tag(:script) do <<-JS.html_safe window.current_user_attributes = #{user} diff --git a/app/presenters/user_presenter.rb b/app/presenters/user_presenter.rb index 018b57187..1874570c4 100644 --- a/app/presenters/user_presenter.rb +++ b/app/presenters/user_presenter.rb @@ -1,8 +1,9 @@ class UserPresenter - attr_accessor :user + attr_accessor :user, :aspects_ids - def initialize(user) - self.user = user + def initialize(user, aspects_ids) + self.user = user + self.aspects_ids = aspects_ids end def to_json(options = {}) @@ -27,7 +28,11 @@ class UserPresenter end def aspects - AspectPresenter.as_collection(user.aspects) + @aspects ||= begin + aspects = AspectPresenter.as_collection(user.aspects) + no_aspects = self.aspects_ids.empty? + aspects.each{ |a| a[:selected] = no_aspects || self.aspects_ids.include?(a[:id].to_s) } + end end def notifications_count diff --git a/spec/presenters/user_presenter_spec.rb b/spec/presenters/user_presenter_spec.rb index 2f322b497..38720dcb3 100644 --- a/spec/presenters/user_presenter_spec.rb +++ b/spec/presenters/user_presenter_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' describe UserPresenter do before do - @presenter = UserPresenter.new(bob) + @presenter = UserPresenter.new(bob, []) end describe '#to_json' do @@ -34,4 +34,4 @@ describe UserPresenter do @presenter.configured_services.should include("fakebook") end end -end \ No newline at end of file +end