Skip to content

Instantly share code, notes, and snippets.

@tony1223
Created July 13, 2010 10:18
Show Gist options
  • Save tony1223/473700 to your computer and use it in GitHub Desktop.
Save tony1223/473700 to your computer and use it in GitHub Desktop.
rails enviroment
# Just for Rails...
if rails_env = ENV['RAILS_ENV']
load File.dirname(__FILE__) + "/.railsrc"
rails_root = File.basename(Dir.pwd)
IRB.conf[:PROMPT] ||= {}
IRB.conf[:PROMPT][:RAILS] = {
:PROMPT_I => "#{rails_root}> ",
:PROMPT_S => "#{rails_root}* ",
:PROMPT_C => "#{rails_root}? ",
:RETURN => "=> %s\n"
}
IRB.conf[:PROMPT_MODE] = :RAILS
# Called after the irb session is initialized and Rails has
# been loaded (props: Mike Clark).
IRB.conf[:IRB_RC] = Proc.new do
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Base.instance_eval { alias :[] :find }
define_model_find_shortcuts
end
end
#for some base shortcut
def execute(query)
ActiveRecord::Base.connection.select_all(query)
end
def define_model_find_shortcuts
model_files = Dir.glob("app/models/**/*.rb")
table_names = model_files.map { |f| File.basename(f).split(".")[0..-2].join}
table_names.each do |table_name|
Object.instance_eval do
define_method(table_name) do |*args|
table_name.camelize.constantize.send(:find, *args)
end
end
end
end
@tony1223
Copy link
Author

.railsrc 沒有 .rb 副檔名,加上去只是為了讓程式碼有變色而已 -.-

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