diff --git a/app/models/user.rb b/app/models/user.rb index 4e2e642e30922810ec39c257ab097d24597dd5c3..91d84340d530a7313d24068599a7da61bf9c8661 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -106,11 +106,7 @@ class User aspect_ids = options.delete(:to) end - aspect_ids = [aspect_ids.to_s] if aspect_ids.is_a? BSON::ObjectId - - raise ArgumentError.new("You must post to someone.") if aspect_ids.nil? || aspect_ids.empty? - aspect_ids.each{ |aspect_id| - raise ArgumentError.new("Cannot post to an aspect you do not own.") unless aspect_id == "all" || self.aspects.find(aspect_id) } + aspect_ids = validate_aspect_permissions(aspect_ids) post = build_post(class_name, options) @@ -120,6 +116,28 @@ class User post end + def repost( post, options = {} ) + aspect_ids = validate_aspect_permissions(options[:to]) + push_to_aspects(post, aspect_ids) + post + end + + def validate_aspect_permissions(aspect_ids) + aspect_ids = [aspect_ids.to_s] if aspect_ids.is_a? BSON::ObjectId + + if aspect_ids.nil? || aspect_ids.empty? + raise ArgumentError.new("You must post to someone.") + end + + aspect_ids.each do |aspect_id| + unless aspect_id == "all" || self.aspects.find(aspect_id) + raise ArgumentError.new("Cannot post to an aspect you do not own.") + end + end + + aspect_ids + end + def build_post( class_name, options = {}) options[:person] = self.person model_class = class_name.to_s.camelize.constantize diff --git a/spec/models/user/posting_spec.rb b/spec/models/user/posting_spec.rb index 1f24f3fed239581cf2116dde6e8558bd3c28e9ce..da52ed8491133178fec9a3ccf737af40501d2341 100644 --- a/spec/models/user/posting_spec.rb +++ b/spec/models/user/posting_spec.rb @@ -26,15 +26,22 @@ describe User do end context 'posting' do - describe '#post' do + + describe '#validate_aspect_permissions' do it 'should not be able to post without a aspect' do - proc {user.post(:status_message, :message => "heyheyhey")}.should raise_error /You must post to someone/ + proc { + user.validate_aspect_permissions([]) + }.should raise_error /You must post to someone/ end it 'should not be able to post to someone elses aspect' do - proc {user.post(:status_message, :message => "heyheyhey", :to => aspect2.id)}.should raise_error /Cannot post to an aspect you do not own./ + proc { + user.validate_aspect_permissions(aspect2.id) + }.should raise_error /Cannot post to an aspect you do not own./ end - + end + + describe '#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 @@ -47,6 +54,16 @@ describe User do aspect.posts.should include album end end + + describe '#repost' do + let!(:status_message) { user.post(:status_message, :message => "hello", :to => aspect.id) } + + it 'should make the post visible in another aspect' do + user.repost( status_message, :to => aspect1.id ) + aspect1.reload + aspect1.posts.count.should be 1 + end + end end context 'dispatching' do