From 1c67211ebca8d1da2d40e3ed497efe38930e5f50 Mon Sep 17 00:00:00 2001 From: Raphael Date: Tue, 2 Nov 2010 18:51:34 -0700 Subject: [PATCH 1/3] Move post dispatching to dispatch_post --- app/models/user.rb | 47 ++++++++++++++++---------------- spec/lib/websocket_spec.rb | 1 + spec/models/user/posting_spec.rb | 15 ++++++++-- 3 files changed, 38 insertions(+), 25 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index 48e2a05f2..0ec7ed50b 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -150,19 +150,34 @@ class User post = build_post(class_name, options) - if post.persisted? - Rails.logger.info("Pushing: #{post.inspect} out to aspects") - push_to_aspects(post, aspect_ids) - post.socket_to_uid(id, :aspect_ids => aspect_ids) if post.respond_to?(:socket_to_uid) - if options[:public] == true - self.services.each do |service| - self.send("post_to_#{service.provider}".to_sym, service, post.message) - end - end + if post.save + raise 'MongoMapper failed to catch a failed save' unless post.id + dispatch_post(post, :to => aspect_ids) end post end + def build_post(class_name, options = {}) + options[:person] = self.person + options[:diaspora_handle] = self.person.diaspora_handle + + model_class = class_name.to_s.camelize.constantize + model_class.instantiate(options) + end + + def dispatch_post(post, opts = {}) + self.raw_visible_posts << post + self.save + Rails.logger.info("Pushing: #{post.inspect} out to aspects") + push_to_aspects(post, opts[:to]) + post.socket_to_uid(id, :aspect_ids => opts[:to]) if post.respond_to?(:socket_to_uid) + if post.public + self.services.each do |service| + self.send("post_to_#{service.provider}".to_sym, service, post.message) + end + end + 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 @@ -203,20 +218,6 @@ class User aspect_ids end - def build_post(class_name, options = {}) - options[:person] = self.person - options[:diaspora_handle] = self.person.diaspora_handle - - model_class = class_name.to_s.camelize.constantize - post = model_class.instantiate(options) - if post.save - raise 'MongoMapper failed to catch a failed save' unless post.id - self.raw_visible_posts << post - self.save - end - post - end - def push_to_aspects(post, aspect_ids) if aspect_ids == :all || aspect_ids == "all" aspects = self.aspects diff --git a/spec/lib/websocket_spec.rb b/spec/lib/websocket_spec.rb index e3917008a..2615ac82d 100644 --- a/spec/lib/websocket_spec.rb +++ b/spec/lib/websocket_spec.rb @@ -9,6 +9,7 @@ describe Diaspora::WebSocket do @user = make_user @aspect = @user.aspects.create(:name => "losers") @post = @user.build_post(:status_message, :message => "hey", :to => @aspect.id) + @post.save end it 'should queue a job' do diff --git a/spec/models/user/posting_spec.rb b/spec/models/user/posting_spec.rb index 734c187fc..79233cb4c 100644 --- a/spec/models/user/posting_spec.rb +++ b/spec/models/user/posting_spec.rb @@ -36,7 +36,19 @@ describe User do end end - describe '#post' do + describe '#build_post' do + it 'does not save a status_message' do + post = user.build_post(:status_message, :message => "hey", :to => aspect.id) + post.persisted?.should be_false + end + + it 'does not save an album' do + post = user.build_post(:album, :name => "hey", :to => aspect.id) + post.persisted?.should be_false + end + end + + describe '#dispatch_post' do it 'should put the post in the aspect post array' do post = user.post(:status_message, :message => "hey", :to => aspect.id) aspect.reload @@ -67,7 +79,6 @@ describe User do user.should_receive(:post_to_facebook).exactly(0).times user.post :status_message, :message => "hi", :to => "all" end - end describe '#update_post' do From d5a4de58b3a70f6b25e9ce2cacad954125540981 Mon Sep 17 00:00:00 2001 From: Raphael Date: Tue, 2 Nov 2010 19:15:52 -0700 Subject: [PATCH 2/3] User#post is now like a future controller, Photo#instantiate no longer saves --- app/models/photo.rb | 8 ++------ app/models/user.rb | 35 +++++++++++++++++------------------ spec/models/photo_spec.rb | 17 ++++++++++------- 3 files changed, 29 insertions(+), 31 deletions(-) diff --git a/app/models/photo.rb b/app/models/photo.rb index 6c96e425c..1eb0aa10e 100644 --- a/app/models/photo.rb +++ b/app/models/photo.rb @@ -34,15 +34,11 @@ class Photo < Post before_destroy :ensure_user_picture def self.instantiate(params = {}) + photo = super(params) image_file = params.delete(:user_file) - person = params.delete(:person) - - photo = Photo.new(params) - photo.diaspora_handle = params[:diaspora_handle] + photo.album_id = params[:album_id] photo.image.store! image_file - photo.person = person - photo.save photo end diff --git a/app/models/user.rb b/app/models/user.rb index 0ec7ed50b..9784a480f 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -138,39 +138,38 @@ class User end ######## Posting ######## - def post(class_name, options = {}) - if class_name == :photo && !options[:album_id].to_s.empty? - aspect_ids = aspects_with_post(options[:album_id]) - aspect_ids.map! { |aspect| aspect.id } - else - aspect_ids = options.delete(:to) - end - - aspect_ids = validate_aspect_permissions(aspect_ids) - - post = build_post(class_name, options) + def post(class_name, opts = {}) + post = build_post(class_name, opts) if post.save raise 'MongoMapper failed to catch a failed save' unless post.id - dispatch_post(post, :to => aspect_ids) + dispatch_post(post, :to => opts[:to]) end post end - def build_post(class_name, options = {}) - options[:person] = self.person - options[:diaspora_handle] = self.person.diaspora_handle + def build_post(class_name, opts = {}) + opts[:person] = self.person + opts[:diaspora_handle] = self.person.diaspora_handle model_class = class_name.to_s.camelize.constantize - model_class.instantiate(options) + model_class.instantiate(opts) end def dispatch_post(post, opts = {}) + if post.is_a?(Photo) && post.album_id + aspect_ids = aspects_with_post(post.album_id) + aspect_ids.map! { |aspect| aspect.id } + else + aspect_ids = opts.delete(:to) + end + + aspect_ids = validate_aspect_permissions(aspect_ids) self.raw_visible_posts << post self.save Rails.logger.info("Pushing: #{post.inspect} out to aspects") - push_to_aspects(post, opts[:to]) - post.socket_to_uid(id, :aspect_ids => opts[:to]) if post.respond_to?(:socket_to_uid) + push_to_aspects(post, aspect_ids) + post.socket_to_uid(id, :aspect_ids => aspect_ids) if post.respond_to?(:socket_to_uid) if post.public self.services.each do |service| self.send("post_to_#{service.provider}".to_sym, service, post.message) diff --git a/spec/models/photo_spec.rb b/spec/models/photo_spec.rb index f0e38fa42..0e2513e1d 100644 --- a/spec/models/photo_spec.rb +++ b/spec/models/photo_spec.rb @@ -42,15 +42,18 @@ describe Photo do it 'sets the persons diaspora handle' do @photo2.diaspora_handle.should == @user.person.diaspora_handle end + it 'has a constructor' do + image = File.open(@fixture_name) + photo = Photo.instantiate( + :person => @user.person, :album => @album, :user_file => image) + photo.created_at.nil?.should be true + photo.image.read.nil?.should be false + photo.album.should == @album + end + end - it 'has a constructor' do - image = File.open(@fixture_name) - photo = Photo.instantiate( - :person => @user.person, :album => @album, :user_file => image) - photo.created_at.nil?.should be false - photo.image.read.nil?.should be false - end + it 'should save a photo' do @photo.image.store! File.open(@fixture_name) From 034d56656448c4fd9148376f72a5de3520a4ce22 Mon Sep 17 00:00:00 2001 From: Sarah Mei Date: Tue, 2 Nov 2010 21:49:24 -0700 Subject: [PATCH 3/3] CI has decided it wants a proper shell to launch the virtual display. So FINE. It can have one. --- lib/tasks/cruise.rake | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/tasks/cruise.rake b/lib/tasks/cruise.rake index e3a7a65a1..91c3f31d8 100644 --- a/lib/tasks/cruise.rake +++ b/lib/tasks/cruise.rake @@ -1,10 +1,13 @@ namespace :cruise do desc "Run all specs and features" task :cruise => :environment do - system('/etc/init.d/xvfb start') + puts "Starting virtual display..." + `sh -e /etc/init.d/xvfb start` + puts "Starting specs..." system('export DISPLAY=:99.0 && export SELENIUM_SERVER_PORT=53809 && bundle exec rake') exit_status = $?.exitstatus - system('/etc/init.d/xvfb stop') + puts "Stopping virtual display..." + `sh -e /etc/init.d/xvfb stop` raise "tests failed!" unless exit_status == 0 end end