Move ExtremePostPresenter methods into PostPresenter, add #with_initial_interactions method
This commit is contained in:
parent
339dd27651
commit
05d19e69ee
4 changed files with 88 additions and 39 deletions
|
|
@ -7,7 +7,7 @@ class ResharesController < ApplicationController
|
||||||
rescue ActiveRecord::RecordNotFound, ActiveRecord::RecordInvalid
|
rescue ActiveRecord::RecordNotFound, ActiveRecord::RecordInvalid
|
||||||
render plain: I18n.t("reshares.create.error"), status: 422
|
render plain: I18n.t("reshares.create.error"), status: 422
|
||||||
else
|
else
|
||||||
render json: ExtremePostPresenter.new(reshare, current_user), status: 201
|
render json: PostPresenter.new(reshare, current_user).with_interactions, status: 201
|
||||||
end
|
end
|
||||||
|
|
||||||
def index
|
def index
|
||||||
|
|
|
||||||
|
|
@ -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
|
|
||||||
|
|
@ -14,6 +14,20 @@ class PostPresenter < BasePresenter
|
||||||
.merge(non_directly_retrieved_attributes)
|
.merge(non_directly_retrieved_attributes)
|
||||||
end
|
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
|
def metas_attributes
|
||||||
{
|
{
|
||||||
keywords: {name: "keywords", content: comma_separated_tags},
|
keywords: {name: "keywords", content: comma_separated_tags},
|
||||||
|
|
|
||||||
|
|
@ -1,44 +1,93 @@
|
||||||
describe PostPresenter do
|
describe PostPresenter do
|
||||||
before do
|
let(:status_message) { FactoryGirl.create(:status_message, public: true) }
|
||||||
@sm = FactoryGirl.create(:status_message, public: true)
|
let(:status_message_with_poll) { FactoryGirl.create(:status_message_with_poll, public: true) }
|
||||||
@sm_with_poll = FactoryGirl.create(:status_message_with_poll, public: true)
|
let(:presenter) { PostPresenter.new(status_message, bob) }
|
||||||
@presenter = PostPresenter.new(@sm, bob)
|
let(:unauthenticated_presenter) { PostPresenter.new(status_message) }
|
||||||
@unauthenticated_presenter = PostPresenter.new(@sm)
|
|
||||||
end
|
|
||||||
|
|
||||||
it "takes a post and an optional user" do
|
it "takes a post and an optional user" do
|
||||||
expect(@presenter).not_to be_nil
|
expect(presenter).not_to be_nil
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "#as_json" do
|
describe "#as_json" do
|
||||||
it "works with a user" do
|
it "works with a user" do
|
||||||
expect(@presenter.as_json).to be_a Hash
|
expect(presenter.as_json).to be_a Hash
|
||||||
end
|
end
|
||||||
|
|
||||||
it "works without a user" do
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "#user_like" do
|
describe "#user_like" do
|
||||||
|
before do
|
||||||
|
bob.like!(status_message)
|
||||||
|
end
|
||||||
|
|
||||||
it "includes the users like" do
|
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
|
end
|
||||||
|
|
||||||
it "is nil if the user is not authenticated" do
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "#user_reshare" do
|
describe "#user_reshare" do
|
||||||
|
before do
|
||||||
|
bob.reshare!(status_message)
|
||||||
|
end
|
||||||
|
|
||||||
it "includes the users reshare" do
|
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
|
end
|
||||||
|
|
||||||
it "is nil if the user is not authenticated" do
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -67,23 +116,23 @@ describe PostPresenter do
|
||||||
it "delegates to message.title" do
|
it "delegates to message.title" do
|
||||||
message = double(present?: true)
|
message = double(present?: true)
|
||||||
expect(message).to receive(:title)
|
expect(message).to receive(:title)
|
||||||
@presenter.post = double(message: message)
|
presenter.post = double(message: message)
|
||||||
@presenter.send(:title)
|
presenter.send(:title)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context "with posts without text" do
|
context "with posts without text" do
|
||||||
it "displays a messaage with the post class" do
|
it "displays a messaage with the post class" do
|
||||||
@sm = double(message: double(present?: false), author: bob.person, author_name: bob.person.name)
|
sm = double(message: double(present?: false), author: bob.person, author_name: bob.person.name)
|
||||||
@presenter.post = @sm
|
presenter.post = sm
|
||||||
expect(@presenter.send(:title)).to eq("A post from #{@sm.author.name}")
|
expect(presenter.send(:title)).to eq("A post from #{sm.author.name}")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "#poll" do
|
describe "#poll" do
|
||||||
it "works without a user" 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)
|
expect(presenter.as_json).to be_a(Hash)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
@ -134,15 +183,15 @@ describe PostPresenter do
|
||||||
|
|
||||||
describe "#build_open_graph_cache" do
|
describe "#build_open_graph_cache" do
|
||||||
it "returns a dummy og cache if the og cache is missing" 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
|
end
|
||||||
|
|
||||||
context "with an open graph cache" do
|
context "with an open graph cache" do
|
||||||
it "delegates to as_api_response" do
|
it "delegates to as_api_response" do
|
||||||
og_cache = double("open_graph_cache")
|
og_cache = double("open_graph_cache")
|
||||||
expect(og_cache).to receive(:as_api_response).with(:backbone)
|
expect(og_cache).to receive(:as_api_response).with(:backbone)
|
||||||
@presenter.post = double(open_graph_cache: og_cache)
|
presenter.post = double(open_graph_cache: og_cache)
|
||||||
@presenter.send(:build_open_graph_cache)
|
presenter.send(:build_open_graph_cache)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "returns the open graph cache data" do
|
it "returns the open graph cache data" do
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue