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

View file

@ -39,7 +39,7 @@ class DevUtilitiesController < ApplicationController
def set_profile_photo def set_profile_photo
render :nothing => true 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.raw_visible_posts << album
current_user.save current_user.save

View file

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

View file

@ -5,7 +5,7 @@
class PhotoAlbumValidator < ActiveModel::Validator class PhotoAlbumValidator < ActiveModel::Validator
def validate(document) def validate(document)
unless document.album.person_id == document.person_id 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 end
end end

View file

@ -33,7 +33,11 @@ class Post
after_destroy :destroy_comments after_destroy :destroy_comments
def self.instantiate params 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 end
def as_json(opts={}) def as_json(opts={})

View file

@ -13,11 +13,6 @@ describe AlbumsController do
sign_in :user, @user sign_in :user, @user
end 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 describe '#create' do
it 'all aspects' do it 'all aspects' do
params = {"album" => {"name" => "Sunsets","to" => "all"}} params = {"album" => {"name" => "Sunsets","to" => "all"}}
@ -28,4 +23,19 @@ describe AlbumsController do
post :create, params post :create, params
end end
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 end

View file

@ -8,7 +8,7 @@ describe DevUtilitiesController do
render_views render_views
before do 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 sign_in :user, @tom
end end

View file

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