Skip to content

Commit

Permalink
Added attr accessible and protected examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
jnunemaker committed Aug 29, 2010
1 parent ad83986 commit 5226a79
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
22 changes: 22 additions & 0 deletions examples/attr_accessible.rb
@@ -0,0 +1,22 @@
$LOAD_PATH.unshift(File.expand_path('../../lib', __FILE__))
require 'mongo_mapper'

MongoMapper.database = 'testing'

class User
include MongoMapper::Document
key :email, String
key :admin, Boolean, :default => false

# Only accessible or protected can be used, they cannot be used together
attr_accessible :email
end

# only accessible are set on new/create/etc.
user = User.create(:email => 'IDontLowerCaseThings@gmail.com', :admin => true)
puts user.admin # false

# can be set using accessor
user.admin = true
user.save
puts user.admin # true
22 changes: 22 additions & 0 deletions examples/attr_protected.rb
@@ -0,0 +1,22 @@
$LOAD_PATH.unshift(File.expand_path('../../lib', __FILE__))
require 'mongo_mapper'

MongoMapper.database = 'testing'

class User
include MongoMapper::Document
key :email, String
key :admin, Boolean, :default => false

# Only accessible or protected can be used, they cannot be used together
attr_protected :admin
end

# protected are ignored on new/create/etc.
user = User.create(:email => 'IDontLowerCaseThings@gmail.com', :admin => true)
puts user.admin # false

# can be set using accessor
user.admin = true
user.save
puts user.admin # true

0 comments on commit 5226a79

Please sign in to comment.