Skip to content

Instantly share code, notes, and snippets.

@ariera
Created July 25, 2011 09:34
Show Gist options
  • Save ariera/1103826 to your computer and use it in GitHub Desktop.
Save ariera/1103826 to your computer and use it in GitHub Desktop.
Codebrawl::TerminalAdmin

short_arel

What is this about?

Do you use ActiveRecord? Tired of writing in console all those verbose Arel methods? Well short_arel is for you!

The idea is to reduce to the minimum the numbers of characters you have to type to perform ActiveRecord queries. short arel just aliases long-named methods with its short version, so instead of having to write:

Post.where(:public => true).includes(:comments).order('edited_at ASC')

you could write

Post.w(:public => true).i(:comments).o('edited_at ASC')

Why?

This is the reason: http://codebrawl.com/contests/terminal-admin

Usage:

Put this file in your rails directory. Load console (rails c) and require the file

require './short_arel.rb'
class ActiveRecord::Base
class << self
short_aliases = {
:f => :find,
:f1 => :first,
:f1! => :first!,
:l => :last,
:l! => :last!,
:al => :all,
:e? => :exists?,
:a? => :any?,
:m? => :many?,
:d => :destroy,
:da => :destroy_all,
:dl => :delete,
:dla => :delete_all,
:u => :update,
:ual => :update_all,
:fe => :find_each,
:fib => :find_in_batches,
:s => :select,
:g => :group,
:o => :order,
:e => :except,
:r => :reorder,
:li => :limit,
:o => :offset,
:j => :joins,
:w => :where,
:p => :preload,
:e => :eager_load,
:i => :includes,
:h => :having,
:c => :count
}
short_aliases.each do |short_method_name, long_method_name|
eval "alias #{short_method_name} #{long_method_name}"
end
end
end
@ariera
Copy link
Author

ariera commented Jul 26, 2011

joshk: I know it is not great, but still I think the concept is interesting. Still everybody is welcomed to take this concept and do something great (as opposed to this lousy script XDD)

jeffkreeftmeijer: I know what you mean, when you take a look at that list it feels overwhelming but the ideas is not having to remember them but knowing how to build them. Except for a few cases the short alias corresponds to the first letter of the method name (or the first letter of each word in the method name)

In any case, ideas al welcomed, I just wanted to enter in this week codebrawl contest and didn't had much time to do something fancier ; )

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