attr_accessible on Post

This commit is contained in:
Raphael 2010-10-28 15:50:19 -07:00
parent f7d2665950
commit 512f40eac2
8 changed files with 31 additions and 26 deletions

View file

@ -16,9 +16,7 @@ class AlbumsController < ApplicationController
def create
aspect = params[:album][:to]
data = clean_hash(params[:album])
@album = current_user.post(:album, data)
@album = current_user.post(:album, params[:album])
flash[:notice] = I18n.t 'albums.create.success', :name => @album.name
redirect_to :action => :show, :id => @album.id, :aspect => aspect
end
@ -53,9 +51,7 @@ class AlbumsController < ApplicationController
def update
@album = current_user.find_visible_post_by_id params[:id]
data = clean_hash(params[:album])
if current_user.update_post( @album, data )
if current_user.update_post( @album, params[:album] )
flash[:notice] = I18n.t 'albums.update.success', :name => @album.name
respond_with @album
else
@ -63,12 +59,4 @@ class AlbumsController < ApplicationController
render :action => :edit
end
end
private
def clean_hash(params)
return {
:name => params[:name],
:to => params[:to]
}
end
end

View file

@ -39,7 +39,7 @@ class DevUtilitiesController < ApplicationController
def set_profile_photo
render :nothing => true
album = Album.create(:person => current_user.person, :name => "Profile Photos")
album = current_user.post(:album, :name => "Profile Photos", :to => current_user.aspects.first.id)
current_user.raw_visible_posts << album
current_user.save

View file

@ -16,6 +16,8 @@ class Album < Post
before_destroy :destroy_photos
attr_accessible :name
def self.mine_or_friends(friend_param, current_user)
friend_param ? Album.find_all_by_person_id(current_user.friend_ids) : current_user.person.albums
end

View file

@ -5,7 +5,7 @@
class PhotoAlbumValidator < ActiveModel::Validator
def validate(document)
unless document.album.person_id == document.person_id
document.errors[:base] << "You post photos to that album"
document.errors[:base] << "You can't post photos to that album"
end
end
end

View file

@ -33,7 +33,11 @@ class Post
after_destroy :destroy_comments
def self.instantiate params
self.create params.to_hash
new_post = self.new params.to_hash
new_post.person = params[:person]
new_post.public = params[:public]
new_post.save
new_post
end
def as_json(opts={})

View file

@ -13,11 +13,6 @@ describe AlbumsController do
sign_in :user, @user
end
it "should update the name of an album" do
put :update, :id => @album.id, :album => { :name => "new_name"}
@album.reload.name.should eql("new_name")
end
describe '#create' do
it 'all aspects' do
params = {"album" => {"name" => "Sunsets","to" => "all"}}
@ -28,4 +23,19 @@ describe AlbumsController do
post :create, params
end
end
describe "#update" do
it "should update the name of an album" do
put :update, :id => @album.id, :album => { :name => "new_name"}
@album.reload.name.should eql("new_name")
end
it "doesn't overwrite random attributes" do
new_user = Factory.create :user
params = {:name => "Bruisers", :person_id => new_user.person.id}
put('update', :id => @album.id, "album" => params)
@album.reload.person_id.should == @user.person.id
@album.name.should == 'Bruisers'
end
end
end

View file

@ -8,7 +8,7 @@ describe DevUtilitiesController do
render_views
before do
@tom = Factory.create(:user, :email => "tom@tom.joindiaspora.org")
@tom = Factory.create(:user_with_aspect, :email => "tom@tom.joindiaspora.org")
sign_in :user, @tom
end

View file

@ -39,13 +39,14 @@ describe Photo do
end
it 'must have an album' do
photo = Photo.new(:person => @user.person)
photo = Photo.new()
photo.person = @user.person
photo.image = File.open(@fixture_name)
photo.save
photo.valid?.should be false
photo.album = Album.create(:name => "foo", :person => @user.person)
photo.album = @album
photo.save
Photo.first.album.name.should == 'foo'
photo.reload.album.name.should == 'foo'
end
it 'should have a caption' do