DG IZ; update_or_repost

This commit is contained in:
danielvincent 2010-09-22 10:52:29 -07:00
parent 432b7d532d
commit 6dd7911c8c
4 changed files with 35 additions and 2 deletions

View file

@ -52,7 +52,7 @@ class AlbumsController < ApplicationController
data = clean_hash(params[:album])
if @album.update_attributes data
if current_user.update_or_repost( @album, data )
flash[:notice] = "Album #{@album.name} successfully edited."
respond_with @album
else

View file

@ -88,7 +88,7 @@ class PhotosController < ApplicationController
data = clean_hash(params)
if @photo.update_attributes data[:photo]
if current_user.update_or_repost( @photo, data[:photo] )
flash[:notice] = "Photo successfully updated."
respond_with @photo
else

View file

@ -122,6 +122,16 @@ class User
post
end
def update_or_repost( post, post_hash = {} )
if self.owns? post
if post_hash[:aspect_ids]
repost(post, post_hash[:aspect_ids]) if validate_aspect_permissions post_hash[:aspect_ids]
else
post.update_attributes!(post_hash)
end
end
end
def validate_aspect_permissions(aspect_ids)
aspect_ids = [aspect_ids.to_s] if aspect_ids.is_a? BSON::ObjectId

View file

@ -64,6 +64,29 @@ describe User do
aspect1.posts.count.should be 1
end
end
describe '#update_or_repost' do
let!(:album) { user.post(:album, :name => "Profile Photos", :to => aspect.id) }
it 'should repost' do
update_hash = { :aspect_ids => aspect1.id }
user.should_receive(:repost).with(album, update_hash[:aspect_ids]).and_return album
user.update_or_repost( album, update_hash )
end
it 'should update fields' do
update_hash = { :name => "Other Photos" }
user.update_or_repost( album, update_hash )
album.name.should == "Other Photos"
end
it 'should reject posting to an external aspect' do
update_hash = { :aspect_ids => [aspect3.id] }
proc{
user.update_or_repost( album, update_hash )
}.should raise_error /Cannot post to an aspect you do not own./
end
end
end
context 'dispatching' do