From 060d8f9c7bb6b240d6f8b1fc9939e1932f86db5a Mon Sep 17 00:00:00 2001 From: danielvincent Date: Mon, 25 Oct 2010 19:24:48 -0700 Subject: [PATCH] DG MS; fixed some bugs with posting publicly. --- app/controllers/status_messages_controller.rb | 12 ++++----- app/models/user.rb | 26 +++++++------------ app/views/shared/_publisher.haml | 2 +- .../status_message_controller_spec.rb | 16 +++++------- spec/models/user/posting_spec.rb | 11 ++++---- 5 files changed, 30 insertions(+), 37 deletions(-) diff --git a/app/controllers/status_messages_controller.rb b/app/controllers/status_messages_controller.rb index 89cf59f32..cf6625839 100644 --- a/app/controllers/status_messages_controller.rb +++ b/app/controllers/status_messages_controller.rb @@ -9,9 +9,13 @@ class StatusMessagesController < ApplicationController respond_to :json, :only => :show def create + public_flag = params[:status_message][:public] + public_flag.match(/(true)/) ? public_flag = true : public_flag = false + params[:status_message][:public] = public_flag + data = clean_hash params[:status_message] message = params[:status_message][:message] - @status_message = current_user.post(:status_message, data) + status_message = current_user.post(:status_message, data) render :nothing => true end @@ -23,11 +27,7 @@ class StatusMessagesController < ApplicationController def show @status_message = current_user.find_visible_post_by_id params[:id] - unless @status_message - render :status => 404 - else - respond_with @status_message - end + respond_with @status_message end private diff --git a/app/models/user.rb b/app/models/user.rb index 8c179c239..6a184b156 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -155,31 +155,25 @@ class User post.socket_to_uid(id, :aspect_ids => aspect_ids) if post.respond_to?(:socket_to_uid) push_to_aspects(post, aspect_ids) - if options[:public] + if options[:public] == true self.services.each do |service| - self.send("post_to_#{service.provider}".to_sym) + self.send("post_to_#{service.provider}".to_sym, service, post.message) end end post end - def post_to_facebook(message) - facebook = self.services.find_by_provider("facebook") - if facebook - Rails.logger.info("Sending a message: #{message} to Facebook") - EventMachine::HttpRequest.new("https://graph.facebook.com/me/feed?message=#{message}&access_token=#{facebook.access_token}").post - end + def post_to_facebook(service, message) + Rails.logger.info("Sending a message: #{message} to Facebook") + EventMachine::HttpRequest.new("https://graph.facebook.com/me/feed?message=#{message}&access_token=#{service.access_token}").post end - def post_to_twitter(message) - twitter = self.services.find_by_provider("twitter") - if twitter - oauth = Twitter::OAuth.new(SERVICES['twitter']['consumer_token'], SERVICES['twitter']['consumer_secret']) - oauth.authorize_from_access(twitter.access_token, twitter.access_secret) - client = Twitter::Base.new(oauth) - client.update(message) - end + def post_to_twitter(service, message) + oauth = Twitter::OAuth.new(SERVICES['twitter']['consumer_token'], SERVICES['twitter']['consumer_secret']) + oauth.authorize_from_access(service.access_token, service.access_secret) + client = Twitter::Base.new(oauth) + client.update(message) end def update_post(post, post_hash = {}) diff --git a/app/views/shared/_publisher.haml b/app/views/shared/_publisher.haml index 5e3be8f2a..4ac7c5392 100644 --- a/app/views/shared/_publisher.haml +++ b/app/views/shared/_publisher.haml @@ -35,7 +35,7 @@ - if aspect == :all .public_toggle - = status.check_box( :public, :value => false ) + = status.check_box( :public, {}, true, false ) make public = link_to '(?)', "#question_mark_pane", :class => 'question_mark' diff --git a/spec/controllers/status_message_controller_spec.rb b/spec/controllers/status_message_controller_spec.rb index 536db40d4..0869972b3 100644 --- a/spec/controllers/status_message_controller_spec.rb +++ b/spec/controllers/status_message_controller_spec.rb @@ -16,35 +16,33 @@ describe StatusMessagesController do end describe '#create' do - let(:status_message_hash) {{"status_message"=>{"public"=>"1", "message"=>"facebook, is that you?", "to" =>"#{aspect.id}"}}} - + let(:status_message_hash) {{"status_message"=>{"public"=>"true", "message"=>"facebook, is that you?", "to" =>"#{aspect.id}"}}} context "posting out to facebook" do - before do - @controller.stub!(:logged_into_fb?).and_return(true) - end + let!(:service2) { s = Factory(:service, :provider => 'facebook'); user.services << s; s } it 'should post to facebook when public is set' do user.should_receive(:post_to_facebook) post :create, status_message_hash end - it 'should not post to facebook when public in not set' do - status_message_hash['status_message']['public'] = '0' + it 'should not post to facebook when public is not set' do + status_message_hash['status_message']['public'] = 'false' user.should_not_receive(:post_to_facebook) post :create, status_message_hash end end - context "posting to twitter" do + let!(:service1) { s = Factory(:service, :provider => 'twitter'); user.services << s; s } + it 'should post to twitter if public is set' do user.should_receive(:post_to_twitter).and_return(true) post :create, status_message_hash end it 'should not post to twitter when public in not set' do - status_message_hash['status_message']['public'] = '0' + status_message_hash['status_message']['public'] = 'false' user.should_not_receive(:post_to_twitter) post :create, status_message_hash end diff --git a/spec/models/user/posting_spec.rb b/spec/models/user/posting_spec.rb index f21bad8a7..5e1b115ae 100644 --- a/spec/models/user/posting_spec.rb +++ b/spec/models/user/posting_spec.rb @@ -13,8 +13,8 @@ describe User do let!(:aspect1) { user.aspect(:name => 'other') } let!(:aspect2) { user2.aspect(:name => 'losers') } - let!(:service1) { user.services << Factory(:service, :provider => 'twitter') } - let!(:service2) { user.services << Factory(:service, :provider => 'facebook') } + let!(:service1) { s = Factory(:service, :provider => 'twitter'); user.services << s; s } + let!(:service2) { s = Factory(:service, :provider => 'facebook'); user.services << s; s } describe '#validate_aspect_permissions' do @@ -56,9 +56,10 @@ describe User do end it "posts to services if post is public" do - user.should_receive(:post_to_twitter).exactly(1).times - user.should_receive(:post_to_facebook).exactly(1).times - user.post :status_message, :message => "hi", :to => "all", :public => true + message = "hello, world!" + user.should_receive(:post_to_twitter).with(service1, message).exactly(1).times + user.should_receive(:post_to_facebook).with(service2, message).exactly(1).times + user.post :status_message, :message => message, :to => "all", :public => true end it "does not post to services if post is not public" do