ubuntu, ror, jQuery, css, website memo
太嫩, 沒啥心得, 用來紀錄每天學的
2009年8月20日 星期四
Rails Rake
Ruby on Rails Rake Tutorial (aka. How rake turned me into an alcoholic)
How do I write Rake tasks for my Rails application?
#可以透過RAILS_ROOT 拿到application 的path
namespace :utils do
desc "Create blank directories if they don’t already exist"
task(:create_directories) do
1. The folders I need to create
shared_folders = ["icons","images","groups"]
for folder in shared_folders
1. Check to see if it exists
if File.exists?("#{RAILS_ROOT}/public/#{folder}")
puts "#{RAILS_ROOT}/public/#{folder} exists"
else
puts "#{RAILS_ROOT}/public/#{folder} doesn’t exist so we’re creating"
Dir.mkdir "#{RAILS_ROOT}/public/#{folder}"
end
end
end
end
Can I access my Rails Models inside a Task?
#重點就是在 :enviroment
#To run this task on my development db I would run “rake utils:send_expire_soon_emails”. If I wanted to run this on my production database, I would run “rake RAILS_ENV=production utils:send_expire_soon_emails”.
#0 0 * * * cd /var/www/apps/rails_app/ && /usr/local/bin/rake RAILS_ENV=production utils:send_expire_soon_emails
namespace :utils do
desc "Finds soon to expire subscriptions and emails users"
task(:send_expire_soon_emails => :environment) do
Find users to email
for user in User.members_soon_to_expire
puts “Emailing #{user.name}”
UserNotifier.deliver_expire_soon_notification(user)
end
end
end
#rake a excute task b, task c first
rask(:a => [:b, :c] ) do
# do some thing
end
#===========================
#rake dev:build
namespace :dev do
desc "Rebuild system"
task :build => ["tmp:clear","db:drop", "db:create", "db:migrate", :setup, :fake]
desc "Setup system data"
task :setup => :environment do
puts "Attempts to create system user"
u = User.new( :login => "root", :password => ‘password’ )
u.save!
end
desc "Create fake data"
task :fake => :environment do
Event.create!( :title => 'osdc 2008' )
Event.create!( :title => 'osdc 2009' )
Event.create!( :title => 'osdc 2010' )
...
...
end
end
沒有留言:
張貼留言
較新的文章
較舊的文章
首頁
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言