Created
July 13, 2010 10:18
-
-
Save tony1223/473700 to your computer and use it in GitHub Desktop.
rails enviroment
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
# 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 |
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
#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 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
.railsrc 沒有 .rb 副檔名,加上去只是為了讓程式碼有變色而已 -.-