Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-10 of 34 total  RSS 

Rake task to set all S3 files public_read

// If you ever need to make sure all your Amazon S3 files are set to public_read, here's a rake task

   1  
   2  
   3  namespace :s3 do
   4    desc "Make all objects in S3 public_read"
   5    task :make_public_readable do
   6      require 'aws/s3'
   7      # you might have this setup as env vars, doesn't work for me as i have more than one AWS account
   8      AWS::S3::Base.establish_connection!(:access_key_id => '',:secret_access_key => '')    
   9      
  10      marker = ""
  11      
  12      loop do
  13        objects = AWS::S3::Bucket.objects('your_bucket', :marker=>marker, :max_keys=>100)
  14        puts "found #{objects.size} objects"
  15      
  16        break if objects.size == 0
  17      
  18        marker = objects.last.key
  19        puts "new marker is \"#{marker}\""
  20      
  21        public_grant = AWS::S3::ACL::Grant.grant :public_read
  22    
  23        objects.each do |o|
  24          if not o.acl.grants.include? public_grant
  25            puts "\"#{o.key}\" does not include public_read"
  26            o.acl.grants << public_grant
  27            o.acl(o.acl)
  28          end
  29        end
  30      end
  31    end
  32    
  33  end

rake task for restart rails application runed on mod_rails


   1  
   2  amespace :passenger do
   3    desc "Restart Application"
   4    task :restart do
   5      puts `touch tmp/restart.txt`
   6    end
   7  end

Easier path construction using overloaded /


Especially when writing rake tasks where a lot of path construction code is written the following overloading of the / operator makes the code more readable.

   1  
   2  
   3  class String
   4    def /(other)
   5      File.join(self,other)
   6    end
   7  end
   8  
   9  # and you can write
  10  #   path = all/along/the/watch/tower
  11  # instead of 
  12  #   path = "#{all}/#{along}/#{the}/#{watch}/#{tower}"
  13  
  14  

rake task to rename views to rails 2.0 format

// This task will rename your files to the rails 2.0 format, using git mv. Add it to /lib/tasks/rails.rake

   1  
   2  namespace 'views' do
   3    desc 'Renames all .rhtml views to .html.erb, .rjs to .js.rjs, .rxml to .xml.builder, and .haml to .html.haml'
   4    task 'rename' do
   5      Dir.glob('app/views/**/[^_]*.rhtml').each do |file|
   6        puts `git mv #{file} #{file.gsub(/\.rhtml$/, '.html.erb')}`
   7      end
   8  
   9      Dir.glob('app/views/**/[^_]*.rxml').each do |file|
  10        puts `git mv #{file} #{file.gsub(/\.rxml$/, '.xml.builder')}`
  11      end
  12  
  13      Dir.glob('app/views/**/[^_]*.rjs').each do |file|
  14        puts `git mv #{file} #{file.gsub(/\.rjs$/, '.js.rjs')}`
  15      end
  16      Dir.glob('app/views/**/[^_]*.haml').each do |file|
  17        puts `git mv #{file} #{file.gsub(/\.haml$/, '.html.haml')}`
  18      end
  19    end
  20  end

create new rails rake task

Create new task for /unit/helpers/*_test.rb

Rakefile
   1  
   2  namespace :test do
   3    Rake::TestTask.new(:helpers) do |t|
   4      t.libs << "test"
   5      t.pattern = 'test/unit/helper/*_test.rb'
   6      t.verbose = true
   7    end
   8  
   9    Rake::Task['test:helpers'].invoke
  10  end

Active Record YAML Backup Solution - Drop in rake task and config file to backup db and directorys of your choice (user uploaded files)

EZ drop in backup rake task for your rails projects - works well - 5 - 10 min set up, one rake file and one config file
backs up db and any directory's to any number of servers with rsync


BSD License or whatever, but it would be cool if you told me your using it
2007 ISS http://industrialstrengthinc.com

this is a sample config file and a rake task you can drop into any rails project to do backups, easy to automate.
Backs up your specified environment db to activerecord yml files (one per table) and zips them up in a human readable timestamped file
syncs that and any other set of arbitrary directory's to any number of arbitrary servers with rsync
be sure to set ssh key based logins
to run: rake backup
also: rake backup:db, rake backup:restoredb (promts for a file created by backup:db), rake backup:push (to push out what u got)
note: this uses something like 30 lines of code from some blog site I got it from that I cant recall, yay for that guy, thanks

   1  
   2  ## example crontab entry:
   3  
   4  3 * * * * cd /rails_deployment_dir/current && nice rake backup RAILS_ENV=production >> /rails_deployment_dir/production_backup_system.log
   5  
   6  ## config file - goes in config/backup.yml
   7  
   8  production: 
   9    dirs: 
  10     - db/backups
  11     - public/uploaded_images
  12    servers: 
  13      -  name: backup server number 1
  14         host: gridserver.com
  15         port: 22
  16         user: blah@whatever.com
  17         dir: /home/blah/backups
  18      -  name: backup server two
  19         host: kradradio.com
  20         port: 22
  21         user: kraduser
  22         dir: /home/kraduser/backups_from_my_rails_proj
  23  
  24  
  25  development: 
  26    dirs: 
  27     - db/backups
  28     - public/uploaded_images
  29    servers: 
  30      -  name: local self
  31         host: localhost
  32         port: 5222
  33         user: oneman
  34         dir: /home/oneman/Documents/development_backup
  35  
  36  
  37  ## rake file lib/tasks/backup.rake
  38  
  39  desc "Backup Everything Specified in config/backup.yml"
  40  task :backup => [ "backup:db",  "backup:push"]
  41  
  42  namespace :backup do
  43   
  44      RAILS_APPDIR = RAILS_ROOT.sub("/config/..","")
  45      
  46     def interesting_tables
  47       ActiveRecord::Base.connection.tables.sort.reject! do |tbl|
  48         ['schema_info', 'sessions', 'public_exceptions'].include?(tbl)
  49       end
  50     end
  51    
  52     desc "Push backup to remote server"
  53     task :push  => [:environment] do 
  54        FileUtils.chdir(RAILS_APPDIR)
  55        backup_config = YAML::load( File.open( 'config/backup.yml' ) )[RAILS_ENV]
  56        for server in backup_config["servers"]
  57         puts "Backing up #{RAILS_ENV} directorys #{backup_config['dirs'].join(', ')} to #{server['name']}"
  58         puts "Time is " + Time.now.rfc2822 + "\n\n"
  59           for dir in backup_config["dirs"]
  60            local_dir = RAILS_APPDIR + "/" + dir + "/"
  61            remote_dir = server['dir'] + "/" + dir.split("/").last + "/"
  62            puts "Syncing #{local_dir} to #{server['host']}#{remote_dir}"
  63            sh "/usr/bin/rsync -avz -e 'ssh -p#{server['port']} ' #{local_dir} #{server['user']}@#{server['host']}:#{remote_dir}"
  64           end
  65         puts "Completed backup to #{server['name']}\n\n"
  66        end
  67     end
  68  
  69      task :storedb => :environment do 
  70  
  71        backupdir = RAILS_APPDIR + '/db/backup'
  72        FileUtils.mkdir_p(backupdir)
  73        FileUtils.chdir(backupdir)
  74        puts "Dumping database to activerecord yaml files in #{backupdir}"
  75        interesting_tables.each do |tbl|
  76  
  77          klass = tbl.classify.constantize
  78          puts "Writing #{tbl}..."
  79          File.open("#{tbl}.yml", 'w+') { |f| YAML.dump klass.find(:all).collect(&:attributes), f }      
  80        end
  81        puts "Database Dumped.\n\n"
  82      end
  83  
  84      desc "Dump Current Environment Db to file"    
  85      task :db => [:environment, :storedb ] do
  86        backupdir = RAILS_APPDIR + '/db/backup'
  87        archivedir = RAILS_APPDIR + '/db/backups'
  88        backup_filename = "#{RAILS_ENV}_db_backup_#{Time.now.strftime("%B.%d.%Y_at_%I.%M.%S%p_%Z")}.tar.bz2"
  89        FileUtils.mkdir_p(archivedir)
  90        puts "Archiving #{backupdir} yaml files to #{backup_filename}\n\n"
  91        `tar -C #{backupdir} -cjf #{backup_filename} *`
  92        `mv #{backup_filename} #{archivedir}`
  93      end
  94  
  95      desc "Restore Current Environment Db from a file"    
  96      task :restoredb => [:environment] do 
  97          backupdir = RAILS_APPDIR + '/db/backup'
  98          archivedir = RAILS_APPDIR + '/db/backups'
  99          print "Input a file to load into the db: #{archivedir}/"
 100          backup_filename = STDIN.gets.chomp
 101          puts "Loading backup file: #{backup_filename}"
 102          FileUtils.chdir(archivedir)
 103          `tar -xjf #{backup_filename}`
 104          `mv *.yml #{backupdir}`
 105          FileUtils.mkdir_p(backupdir)
 106          FileUtils.chdir(backupdir)
 107      
 108          interesting_tables.each do |tbl|
 109          puts "Clearing #{tbl} table.."
 110          ActiveRecord::Base.connection.execute "TRUNCATE #{tbl}"
 111          puts "Loading #{tbl} backup file..."
 112          table = YAML.load_file("#{tbl}.yml")        
 113  
 114          if table.length > 0 && table.first.key?("id")
 115              highestid = 0
 116              table.each do |fixture|
 117               if fixture["id"] > highestid
 118                  highestid = fixture["id"]
 119               end
 120              end
 121  
 122              ActiveRecord::Base.connection.execute "SELECT setval('#{tbl}_id_seq',#{highestid})"
 123              puts "Setting #{tbl}_id sequence to #{highestid}"
 124          end
 125           
 126          #klass = tbl.classify.constantize
 127          ActiveRecord::Base.transaction do 
 128          
 129            puts "Inserting #{table.length} values into #{tbl}"
 130            table.each do |fixture|
 131              ActiveRecord::Base.connection.execute "INSERT INTO #{tbl} (#{fixture.keys.join(",")}) VALUES (#{fixture.values.collect { |value| ActiveRecord::Base.connection.quote(value) }.join(",")})", 'Fixture Insert'
 132            end        
 133            puts "#{tbl} table restored.\n\n"
 134          end
 135         end
 136      end
 137  
 138   
 139  end

Save db data to fixture files

rake db:fixtures:dump_all
он сохранит все данные из ваших таблиц development базы в ямлы для тестов rake

db:fixtures:dump_references
["areas","countries"]
сохранит только те таблицы которы епрописаны у него в конфиге, в данном случае это

   1  
   2  namespace :db do
   3    namespace :fixtures do
   4  
   5      desc 'Create YAML test fixtures from data in an existing database.
   6  Defaults to development database. Set RAILS_ENV to override.'
   7      task :dump_all => :environment do
   8        sql = "SELECT * FROM %s"
   9        skip_tables = ["schema_info"]
  10        ActiveRecord::Base.establish_connection(:development)
  11        (ActiveRecord::Base.connection.tables - skip_tables).each do |table_name|
  12          i = "000"
  13          File.open("#{RAILS_ROOT}/test/fixtures/#{table_name}.yml", 'w') do |file|
  14            data = ActiveRecord::Base.connection.select_all(sql % table_name)
  15            file.write data.inject({}) { |hash, record|
  16              hash["#{table_name}_#{i.succ!}"] = record
  17              hash
  18            }.to_yaml
  19          end
  20        end
  21      end
  22    end
  23    
  24    namespace :fixtures do
  25      desc 'Create YAML test fixtures for references. Defaults to development database. 
  26      Set RAILS_ENV to override.'
  27      task :dump_references => :environment do
  28        sql = "SELECT * FROM %s"
  29        dump_tables = ["areas","countries"]
  30        ActiveRecord::Base.establish_connection(:development)
  31        dump_tables.each do |table_name|
  32          i = "000"
  33          file_name = "#{RAILS_ROOT}/test/fixtures/#{table_name}.yml"
  34          p "Fixture save for table #{table_name} to #{file_name}"
  35          File.open(file_name, 'w') do |file|
  36            data = ActiveRecord::Base.connection.select_all(sql % table_name)
  37            file.write data.inject({}) { |hash, record|
  38              hash["#{table_name}_#{i.succ!}"] = record
  39              hash
  40            }.to_yaml
  41          end
  42        end
  43      end
  44    end
  45  end 
  46  

View all colors from a set of stylesheets

Rake task to grep out colors from a set of CSS stylesheets and display them on a web page. Default configuration works for OS X Safari and a Rails application.

   1  
   2  BROWSER = "/Applications/Safari.app/Contents/MacOS/Safari"
   3  CSS_FILES = "#{RAILS_ROOT}/public/stylesheets/**/*.css"
   4  
   5  task :colors do
   6    require "tempfile"
   7    colors = Dir[CSS_FILES].map(&File.method(:read)).join.scan(/\#[0-9a-f]{3,6}/i).map{|c| c.upcase}.uniq
   8    Tempfile.open "colors" do |f|
   9      f.write <<-EOHTML
  10      <html>
  11        <head>
  12          <style type="text/css">
  13            div { width: 50px; height: 50px; display: inline-block }
  14          </style>
  15        </head>
  16        <body>
  17          #{colors.map{|clr| 
  18            "<div style='background: #{clr}'>&nbsp;</div> #{clr} <br />"
  19          }.join}
  20        </body>
  21      </html>
  22      EOHTML
  23      system BROWSER, f.path
  24    end
  25  end

rcov rake task for rails project

This task will create a folder (doc/coverage) then run all of your tests and produce HTML with code coverage information in that folder. If you are on a mac it will even open the index.html file up for you automatically

   1  
   2  # this requires the RCOV gem to be installed on your system
   3  namespace :test do
   4    desc "Generate code coverage with rcov"
   5    task :coverage do
   6      rm_f "doc/coverage/coverage.data"
   7      rm_f "doc/coverage"
   8      mkdir "doc/coverage"
   9      rcov = %(rcov --rails --aggregate doc/coverage/coverage.data --text-summary -Ilib --html -o doc/coverage test/**/*_test.rb)
  10      system rcov
  11      system "open doc/coverage/index.html" if PLATFORM['darwin']
  12    end
  13  end

Dump models into fixtures.

This snippet of code will look in app/models for all models, then dump their contents into the corresponding fixture file found in test/fixtures. At the moment, it can not differentiate between regular models and mailer models, so just remove the extension then run the script. Same goes for models you don't want dumped. I'm working on adding only and except as well.

   1  
   2  require 'find'
   3  
   4  namespace :db do
   5    namespace :fixtures do
   6      desc 'Dumps all models into fixtures.'
   7      task :dump => :environment do
   8        models = []
   9        Find.find(RAILS_ROOT + '/app/models') do |path|
  10          unless File.directory?(path) then models << path.match(/(\w+).rb/)[1] end
  11        end
  12    
  13        puts "Found models: " + models.join(', ')
  14        
  15        models.each do |m|
  16          puts "Dumping model: " + m
  17          model = m.capitalize.constantize
  18          entries = model.find(:all, :order => 'id ASC')
  19          
  20          formatted, increment, tab = '', 1, '  '
  21          entries.each do |a|
  22            formatted += m + '_' + increment.to_s + ':' + "\n"
  23            increment += 1
  24            
  25            a.attributes.each do |column, value|
  26              formatted += tab
  27              
  28              match = value.to_s.match(/\n/)
  29              if match
  30                formatted += column + ': |' + "\n"
  31                
  32                value.to_a.each do |v|
  33                  formatted += tab + tab + v
  34                end
  35              else
  36                formatted += column + ': ' + value.to_s
  37              end
  38              
  39              formatted += "\n"
  40            end
  41                      
  42            formatted += "\n"
  43          end
  44        
  45          model_file = RAILS_ROOT + '/test/fixtures/' + m.pluralize + '.yml'
  46          
  47          File.exists?(model_file) ? File.delete(model_file) : nil
  48          File.open(model_file, 'w') {|f| f << formatted}
  49        end
  50      end
  51    end
  52  end
« Newer Snippets
Older Snippets »
Showing 1-10 of 34 total  RSS