Skip to content

Commit

Permalink
Moving to activemodel validations
Browse files Browse the repository at this point in the history
  • Loading branch information
Raphael committed Sep 24, 2010
1 parent b9bff76 commit 40120bb
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 20 deletions.
3 changes: 1 addition & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ gem 'bundler', '1.0.0'
gem 'devise', :git => 'http://github.com/BadMinus/devise.git'

#Mongo
gem 'mongo_mapper', '0.8.4', :git => 'http://github.com/jnunemaker/mongomapper.git'
gem 'jnunemaker-validatable', '1.8.4', :git => 'http://github.com/jnunemaker/validatable.git'
gem 'mongo_mapper', :branch => 'rails3', :git => 'http://github.com/jnunemaker/mongomapper.git'
gem 'bson_ext', '1.0.7'
gem 'bson', '1.0.7'

Expand Down
17 changes: 5 additions & 12 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,14 @@ GIT

GIT
remote: http://github.com/jnunemaker/mongomapper.git
revision: 931dab779011aa7acf60c1a4c7ad19e1ba838345
revision: b0eac421ef5b3bee782e15d391eca372a189d414
branch: rails3
specs:
mongo_mapper (0.8.4)
activesupport (>= 2.3.4)
jnunemaker-validatable (~> 1.8.4)
activemodel (~> 3.0.0)
activesupport (~> 3.0.0)
plucky (~> 0.3.5)

GIT
remote: http://github.com/jnunemaker/validatable.git
revision: 8d7c3ce14133760e748a0e34b99dfe6ec4d69153
specs:
jnunemaker-validatable (1.8.4)
activesupport (>= 2.3.4)

GEM
remote: http://rubygems.org/
specs:
Expand Down Expand Up @@ -241,12 +235,11 @@ DEPENDENCIES
em-websocket
factory_girl_rails
haml
jnunemaker-validatable (= 1.8.4)!
json
magent!
mini_magick
mocha
mongo_mapper (= 0.8.4)!
mongo_mapper!
nifty-generators
pubsubhubbub
rails (= 3.0.0)
Expand Down
16 changes: 10 additions & 6 deletions app/models/photo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@
# licensed under the Affero General Public License version 3. See
# the COPYRIGHT file.


class PhotoAlbumValidator < ActiveModel::Validator
def validate(document)
unless document.album.person_id == document.person_id
document.errors[:base] << "You post photos to that album"
end
end
end
class Photo < Post
require 'carrierwave/orm/mongomapper'
include MongoMapper::Document
Expand All @@ -22,7 +28,7 @@ class Photo < Post
timestamps!

validates_presence_of :album
validates_true_for :album_id, :logic => lambda {self.validate_album_person}
validates_with PhotoAlbumValidator

before_destroy :ensure_user_picture

Expand All @@ -36,10 +42,6 @@ def self.instantiate(params = {})
photo
end

def validate_album_person
album.person_id == person_id
end

def remote_photo
image.url.nil? ? (remote_photo_path + '/' + remote_photo_name) : image.url
end
Expand Down Expand Up @@ -70,3 +72,5 @@ def thumb_hash
{:thumb_url => url(:thumb_medium), :id => id, :album_id => album_id}
end
end


0 comments on commit 40120bb

Please sign in to comment.