Commit 9bca03d5 authored by Steffen van Bergerem's avatar Steffen van Bergerem Committed by Benjamin Neff
Browse files

Refactor ShareVisibilitesController

closes #7196
parent 75f95fae
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@
* Force jasmine fails on syntax errors [#7185](https://github.com/diaspora/diaspora/pull/7185)
* Don't display mail-related view content if it is disabled in the pod's config [#7190](https://github.com/diaspora/diaspora/pull/7190)
* Use typeahead.js from rails-assets.org [#7192](https://github.com/diaspora/diaspora/pull/7192)
* Refactor ShareVisibilitesController to use PostService [#7196](https://github.com/diaspora/diaspora/pull/7196)

## Bug fixes
* Fix fetching comments after fetching likes [#7167](https://github.com/diaspora/diaspora/pull/7167)
+5 −8
Original line number Diff line number Diff line
@@ -7,17 +7,14 @@ class ShareVisibilitiesController < ApplicationController
  before_action :authenticate_user!

  def update
    #note :id references a postvisibility
    params[:shareable_id] ||= params[:post_id]
    params[:shareable_type] ||= 'Post'

    vis = current_user.toggle_hidden_shareable(accessible_post)
    render :nothing => true, :status => 200
    post = post_service.find!(params[:post_id])
    current_user.toggle_hidden_shareable(post)
    head :ok
  end

  private

  def accessible_post
    @post ||= params[:shareable_type].constantize.where(:id => params[:post_id]).select("id, guid, author_id, created_at").first
  def post_service
    @post_service ||= PostService.new(current_user)
  end
end
+23 −13
Original line number Diff line number Diff line
@@ -7,32 +7,42 @@ require 'spec_helper'
describe ShareVisibilitiesController, :type => :controller do
  before do
    @status = alice.post(:status_message, :text => "hello", :to => alice.aspects.first)
    sign_in(bob, scope: :user)
  end

  describe '#update' do
    context "on a post you can see" do
      before do
        sign_in(bob, scope: :user)
      end

      it 'succeeds' do
        put :update, :format => :js, :id => 42, :post_id => @status.id
        expect(response).to be_success
      end

      it 'it calls toggle_hidden_shareable' do
        expect(@controller.current_user).to receive(:toggle_hidden_shareable).with(an_instance_of(Post))
        expect(@controller.current_user).to receive(:toggle_hidden_shareable).with(an_instance_of(StatusMessage))
        put :update, :format => :js, :id => 42, :post_id => @status.id
      end
    end

    context "on a post you can't see" do
      before do
        sign_in(eve, scope: :user)
      end

  describe "#accessible_post" do
    it "memoizes a query for a post given a post_id param" do
      id = 1
      @controller.params[:post_id] = id
      @controller.params[:shareable_type] = 'Post'
      it "raises an error" do
        expect {
          put :update, format: :js, id: 42, post_id: @status.id
        }.to raise_error ActiveRecord::RecordNotFound
      end

      expect(Post).to receive(:where).with(hash_including(:id => id)).once.and_return(double.as_null_object)
      2.times do |n|
        @controller.send(:accessible_post)
      it "it doesn't call toggle_hidden_shareable" do
        expect(@controller.current_user).not_to receive(:toggle_hidden_shareable).with(an_instance_of(StatusMessage))
        begin
          put :update, format: :js, id: 42, post_id: @status.id
        rescue ActiveRecord::RecordNotFound
        end
      end
    end
  end