Created
March 2, 2012 03:03
-
-
Save JonKernPA/1955242 to your computer and use it in GitHub Desktop.
Demonstrating opening a mongomapper class and extending
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Run this in mmconsole | |
class User | |
include MongoMapper::Document | |
key :name | |
key :role | |
def admin? | |
role.index(/admin/i) | |
end | |
def to_s | |
"#{name} #{role} #{admin? ? '#' : ''}" | |
end | |
end | |
admin = User.create(:name => "George Washington", :role => "admin") | |
regular = User.create(:name => "Ben Franklin", :role => "inventor") | |
puts admin | |
#George Washington admin # | |
puts regular | |
#Ben Franklin inventor | |
# Extend core User | |
class User | |
#devise :timeoutable uses this sort of method call | |
def timeout_in | |
if self.admin? | |
puts "timeout for ADMIN" | |
120.minutes | |
else | |
puts "configatron timeout" | |
# This needs to come from a dynamic table lookup | |
20.minutes | |
end | |
end | |
end | |
[admin, regular].each {|u| puts "#{u} will timeout in: #{u.timeout_in}"} | |
=begin | |
timeout for ADMIN | |
George Washington admin # will timeout in: 7200 | |
configatron timeout | |
Ben Franklin inventor will timeout in: 1200 | |
=end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment