From 05d19e69ee1836f387ef26213484fd19076822aa Mon Sep 17 00:00:00 2001 From: Steffen van Bergerem Date: Sat, 12 Nov 2016 03:11:58 +0100 Subject: [PATCH] Move ExtremePostPresenter methods into PostPresenter, add #with_initial_interactions method --- app/controllers/reshares_controller.rb | 2 +- app/presenters/extreme_post_presenter.rb | 14 ---- app/presenters/post_presenter.rb | 14 ++++ spec/presenters/post_presenter_spec.rb | 97 ++++++++++++++++++------ 4 files changed, 88 insertions(+), 39 deletions(-) delete mode 100644 app/presenters/extreme_post_presenter.rb diff --git a/app/controllers/reshares_controller.rb b/app/controllers/reshares_controller.rb index 846091398..d38f49f6a 100644 --- a/app/controllers/reshares_controller.rb +++ b/app/controllers/reshares_controller.rb @@ -7,7 +7,7 @@ class ResharesController < ApplicationController rescue ActiveRecord::RecordNotFound, ActiveRecord::RecordInvalid render plain: I18n.t("reshares.create.error"), status: 422 else - render json: ExtremePostPresenter.new(reshare, current_user), status: 201 + render json: PostPresenter.new(reshare, current_user).with_interactions, status: 201 end def index diff --git a/app/presenters/extreme_post_presenter.rb b/app/presenters/extreme_post_presenter.rb deleted file mode 100644 index c35b54088..000000000 --- a/app/presenters/extreme_post_presenter.rb +++ /dev/null @@ -1,14 +0,0 @@ -#this file should go away, hence the name that is so full of lulz -#post interactions should probably be a decorator, and used in very few places... maybe? -class ExtremePostPresenter - def initialize(post, current_user) - @post = post - @current_user = current_user - end - - def as_json(options={}) - post = PostPresenter.new(@post, @current_user) - interactions = PostInteractionPresenter.new(@post, @current_user) - post.as_json.merge!(:interactions => interactions.as_json) - end -end \ No newline at end of file diff --git a/app/presenters/post_presenter.rb b/app/presenters/post_presenter.rb index 86064fbf9..88a94d07a 100644 --- a/app/presenters/post_presenter.rb +++ b/app/presenters/post_presenter.rb @@ -14,6 +14,20 @@ class PostPresenter < BasePresenter .merge(non_directly_retrieved_attributes) end + def with_interactions + interactions = PostInteractionPresenter.new(@post, current_user) + as_json.merge!(interactions: interactions.as_json) + end + + def with_initial_interactions + as_json.tap do |post| + post[:interactions].merge!( + likes: LikeService.new(current_user).find_for_post(@post.id).as_api_response(:backbone), + reshares: ReshareService.new(current_user).find_for_post(@post.id).as_api_response(:backbone) + ) + end + end + def metas_attributes { keywords: {name: "keywords", content: comma_separated_tags}, diff --git a/spec/presenters/post_presenter_spec.rb b/spec/presenters/post_presenter_spec.rb index 952da0a2c..4110cedeb 100644 --- a/spec/presenters/post_presenter_spec.rb +++ b/spec/presenters/post_presenter_spec.rb @@ -1,44 +1,93 @@ describe PostPresenter do - before do - @sm = FactoryGirl.create(:status_message, public: true) - @sm_with_poll = FactoryGirl.create(:status_message_with_poll, public: true) - @presenter = PostPresenter.new(@sm, bob) - @unauthenticated_presenter = PostPresenter.new(@sm) - end + let(:status_message) { FactoryGirl.create(:status_message, public: true) } + let(:status_message_with_poll) { FactoryGirl.create(:status_message_with_poll, public: true) } + let(:presenter) { PostPresenter.new(status_message, bob) } + let(:unauthenticated_presenter) { PostPresenter.new(status_message) } it "takes a post and an optional user" do - expect(@presenter).not_to be_nil + expect(presenter).not_to be_nil end describe "#as_json" do it "works with a user" do - expect(@presenter.as_json).to be_a Hash + expect(presenter.as_json).to be_a Hash end it "works without a user" do - expect(@unauthenticated_presenter.as_json).to be_a Hash + expect(unauthenticated_presenter.as_json).to be_a Hash + end + end + + context "post with interactions" do + before do + bob.like!(status_message) + bob.reshare!(status_message) + end + + describe "#with_interactions" do + it "works with a user" do + post_hash = presenter.with_interactions + expect(post_hash).to be_a Hash + expect(post_hash[:interactions]).to eq PostInteractionPresenter.new(status_message, bob).as_json + end + + it "works without a user" do + post_hash = unauthenticated_presenter.with_interactions + expect(post_hash).to be_a Hash + expect(post_hash[:interactions]).to eq PostInteractionPresenter.new(status_message, nil).as_json + end + end + + describe "#with_initial_interactions" do + it "works with a user" do + post_hash = presenter.with_initial_interactions + expect(post_hash).to be_a Hash + expect(post_hash[:interactions][:likes]).to eq( + LikeService.new(bob).find_for_post(status_message.id).as_api_response(:backbone) + ) + expect(post_hash[:interactions][:reshares]).to eq( + ReshareService.new(bob).find_for_post(status_message.id).as_api_response(:backbone) + ) + end + + it "works without a user" do + post_hash = unauthenticated_presenter.with_initial_interactions + expect(post_hash).to be_a Hash + expect(post_hash[:interactions][:likes]).to eq( + LikeService.new.find_for_post(status_message.id).as_api_response(:backbone) + ) + expect(post_hash[:interactions][:reshares]).to eq( + ReshareService.new.find_for_post(status_message.id).as_api_response(:backbone) + ) + end end end describe "#user_like" do + before do + bob.like!(status_message) + end + it "includes the users like" do - bob.like!(@sm) - expect(@presenter.send(:user_like)).to be_present + expect(presenter.send(:user_like)).to be_present end it "is nil if the user is not authenticated" do - expect(@unauthenticated_presenter.send(:user_like)).to be_nil + expect(unauthenticated_presenter.send(:user_like)).to be_nil end end describe "#user_reshare" do + before do + bob.reshare!(status_message) + end + it "includes the users reshare" do - bob.reshare!(@sm) - expect(@presenter.send(:user_reshare)).to be_present + expect(presenter.send(:user_reshare)).to be_present end it "is nil if the user is not authenticated" do - expect(@unauthenticated_presenter.send(:user_reshare)).to be_nil + expect(unauthenticated_presenter.send(:user_reshare)).to be_nil end end @@ -67,23 +116,23 @@ describe PostPresenter do it "delegates to message.title" do message = double(present?: true) expect(message).to receive(:title) - @presenter.post = double(message: message) - @presenter.send(:title) + presenter.post = double(message: message) + presenter.send(:title) end end context "with posts without text" do it "displays a messaage with the post class" do - @sm = double(message: double(present?: false), author: bob.person, author_name: bob.person.name) - @presenter.post = @sm - expect(@presenter.send(:title)).to eq("A post from #{@sm.author.name}") + sm = double(message: double(present?: false), author: bob.person, author_name: bob.person.name) + presenter.post = sm + expect(presenter.send(:title)).to eq("A post from #{sm.author.name}") end end end describe "#poll" do it "works without a user" do - presenter = PostPresenter.new(@sm_with_poll) + presenter = PostPresenter.new(status_message_with_poll) expect(presenter.as_json).to be_a(Hash) end end @@ -134,15 +183,15 @@ describe PostPresenter do describe "#build_open_graph_cache" do it "returns a dummy og cache if the og cache is missing" do - expect(@presenter.build_open_graph_cache.image).to be_nil + expect(presenter.build_open_graph_cache.image).to be_nil end context "with an open graph cache" do it "delegates to as_api_response" do og_cache = double("open_graph_cache") expect(og_cache).to receive(:as_api_response).with(:backbone) - @presenter.post = double(open_graph_cache: og_cache) - @presenter.send(:build_open_graph_cache) + presenter.post = double(open_graph_cache: og_cache) + presenter.send(:build_open_graph_cache) end it "returns the open graph cache data" do