Last active
December 19, 2015 07:59
-
-
Save elgalu/5922818 to your computer and use it in GitHub Desktop.
Add helper methods to your Rails 3 console
[from blog](http://opensoul.org/blog/archives/2012/11/08/add-helper-methods-to-your-rails-console)
This file contains 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
# config/application.rb | |
module SpeakerDeck | |
class Application < Rails::Application | |
# … | |
console do | |
require 'speaker_deck/console' | |
Rails::ConsoleMethods.send :include, SpeakerDeck::Console | |
end | |
end | |
end |
This file contains 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
# lib/speaker_deck/console.rb | |
module SpeakerDeck | |
# These methods are included into the Rails console | |
module Console | |
# Find a user by username | |
def u(username) | |
User.find_by_username!(username) | |
end | |
# Find a talk by url, e.g. "holman/how-to-be-sexy-like-me" | |
def t(param) | |
username, slug = param.split('/') | |
Talk.find_by_username_and_slug!(username, slug) | |
end | |
end | |
end |
This file contains 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
>> user = u 'bkeepers' | |
=> #<User id:…> | |
>> talk = t 'sachag/side-projects' | |
=> #<Talk id:…> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment