Skip to content

Instantly share code, notes, and snippets.

@martinciu
Created July 23, 2011 19:46
Show Gist options
  • Save martinciu/1101803 to your computer and use it in GitHub Desktop.
Save martinciu/1101803 to your computer and use it in GitHub Desktop.
me.rb - My entry to the CodeBrawl.com "Terminal admin" contes

me.rb

In almost every my project there is a model that refers to myself. It can be an instance of User, Staff, Admin, Dude, etc. I use it for logging in and testing staff. I use it many times in rails console as well. Because typing User.where(:email => 'my@email.com').first can by annoying so I've put together little snippet that work in Mongoid and AcitveRecord as well.

$ rails console
Loading development environment (Rails 3.1.0.rc4)
>> load 'me.rb'
=> nil
>> User.me
=> #<User _id: 4c14ba297826c943b2000002, encrypted_password: "16336dd440d4bbe843dc39....", .... >

I hope you find it useful!

module ActiveRecord
class Base
class << self
def me
where(:email => `git config user.email`.chomp).first
end
end
end
end if defined? ActiveRecord
module Mongoid
module Document
module ClassMethods
def me
where(:email => `git config user.email`.chomp).first
end
end
end
end if defined? Mongoid
@jeffkreeftmeijer
Copy link

Great entry! Fetching the user's email address from the git config is very clever. :)

@asaaki
Copy link

asaaki commented Aug 6, 2011

Love this! And will use it in current and future project.

(I let it load automatically and instanciate it in a global var for very quick'n'dirty access. ;o)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment