Custom extensions for Rails console
There are short code snippets I often use during my Rails script/console
sessions, like e.g.
>> app.get 'admin/login'
>> app.response.body
# look up 'authenticity_token' in the login form's H开发者_如何学JAVATML
>> login_data = { "authenticity_token" => "token_value",
"username" => "admin",
"password" => "admin_password" }
>> app.post 'admin/login', login_data
I'd like to make a helper method / extension for the console so I'd just use
>> app.admin_logon
What are the possible solutions to accomplish this?
You can define helper functions in the file ~/.irbrc. This will affect all of your irb sessions, not just rails console sessions, so you may have to conditionally execute some helpers:
if defined? Rails
[helper code here...]
end
Simply add a module in your project, in a folder that's loaded.
For example, add console_extensions.rb
to your lib
folder:
module ConsoleExtensions
def admin_logon
# custom code here
end
end
精彩评论