DG IZ; update_or_repost
This commit is contained in:
parent
432b7d532d
commit
6dd7911c8c
4 changed files with 35 additions and 2 deletions
|
|
@ -52,7 +52,7 @@ class AlbumsController < ApplicationController
|
||||||
|
|
||||||
data = clean_hash(params[:album])
|
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."
|
flash[:notice] = "Album #{@album.name} successfully edited."
|
||||||
respond_with @album
|
respond_with @album
|
||||||
else
|
else
|
||||||
|
|
|
||||||
|
|
@ -88,7 +88,7 @@ class PhotosController < ApplicationController
|
||||||
|
|
||||||
data = clean_hash(params)
|
data = clean_hash(params)
|
||||||
|
|
||||||
if @photo.update_attributes data[:photo]
|
if current_user.update_or_repost( @photo, data[:photo] )
|
||||||
flash[:notice] = "Photo successfully updated."
|
flash[:notice] = "Photo successfully updated."
|
||||||
respond_with @photo
|
respond_with @photo
|
||||||
else
|
else
|
||||||
|
|
|
||||||
|
|
@ -122,6 +122,16 @@ class User
|
||||||
post
|
post
|
||||||
end
|
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)
|
def validate_aspect_permissions(aspect_ids)
|
||||||
aspect_ids = [aspect_ids.to_s] if aspect_ids.is_a? BSON::ObjectId
|
aspect_ids = [aspect_ids.to_s] if aspect_ids.is_a? BSON::ObjectId
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -64,6 +64,29 @@ describe User do
|
||||||
aspect1.posts.count.should be 1
|
aspect1.posts.count.should be 1
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|
||||||
context 'dispatching' do
|
context 'dispatching' do
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue