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

About this user

« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS 

Ruby Speaks

// if you are using a mac this will allow your Ruby script to speak using the macs native text to speech functionality.

   1  
   2  system "osascript -e 'say \"the rain in Spain falls mainly on the planes\"'"

Application version number and cool codename based on subversion number

// description of your code here

   1  
   2  #codename generated from the dictionary
   3  REVISION_NUMBER = `svn info`.split("\n")[4][/\d+/].to_i
   4  APP_CODENAME  = IO.readlines("/usr/share/dict/words")[REVISION_NUMBER]
   5  

Add Subversion Revsions Number to Rails Application.

// description of your code here
Add this to your environment.rb file for a dead easy way to get your subversion revision into your application.

   1  
   2  APP_VERSION  = IO.popen("svn info").readlines[4]

Submitting data to two different tables with two different models (or how to modify multiple models from one form)

Taken from Norman Timmler's example on the ruby on rails mailing list.


Take two text fields for example:
   1  
   2  # view
   3  <%= form_tag :controller => :posts, :action => create %>
   4   <%= text_field("post", "title", "size" => 20) %>
   5   <%= text_field("user", "email", "size" => 20) %>
   6  <%= end_form_tag %>

   1  
   2  # posts_controller
   3  class PostsController < ApplicationController
   4   def create
   5     @post = Post.create(params[:post])
   6     @user = User.create(params[:user])
   7     @post.users << @user
   8   end
   9  end


In the controller the params hash has one key for every object (user,
post) in your form. As a values of this key you find a hash having a
key-value pair for every object attribute (title, email).

This way you can submit multiple objects via one form.

rake start_fresh

Sometimes you don't want to fool with migrating up or down a version of your development server and you just want to start fresh. This task will

clear the logs
recreate the test and development databases
migrate to the most recent schema
load all your test fixtures into your development database
clone your development structure to the test database

to use it call "rake start_fresh"


there is also a short "rake start" command, which I tossed in as a little something something, you can use it instead of typing "ruby script/server"


   1  
   2  task :start_fresh => :environment do
   3       Rake::Task[:clear_logs].invoke
   4       puts 'clearing logs                           ...done'
   5       
   6       Rake::Task[:recreate_database].invoke
   7       puts 'recreate test database                  ...done'
   8       puts 'recreate development database           ...done'
   9       
  10       ActiveRecord::Base.establish_connection(:development)
  11       Rake::Task[:migrate].invoke
  12       puts 'migrate the development database        ...done'
  13       
  14       Rake::Task[:load_fixtures_to_development].invoke
  15       puts 'load fixtures into development database ...done'
  16       
  17       ActiveRecord::Base.establish_connection(:test)
  18       Rake::Task[:prepare_test_database].invoke
  19       puts 'clone the development structure to test ...done'
  20  end
  21  
  22  desc "Load fixtures data into the test database"
  23  task :load_fixtures_to_development => :environment do
  24    require 'active_record/fixtures'
  25    ActiveRecord::Base.establish_connection(:development)
  26    Dir.glob(File.join(RAILS_ROOT, 'test', 'fixtures', '*.{yml,csv}')).each do |fixture_file|
  27      Fixtures.create_fixtures('test/fixtures', File.basename(fixture_file, '.*'))
  28    end
  29  end
  30  
  31  task :recreate_database => :environment do
  32    abcs = ActiveRecord::Base.configurations
  33  	tdb = Array.new
  34    tdb << 'test'
  35    tdb << 'development'
  36  
  37   	for db in tdb
  38  	  case abcs[db]["adapter"]
  39  	    when "mysql"
  40  	      ActiveRecord::Base.establish_connection(db.to_sym)
  41  	      ActiveRecord::Base.connection.recreate_database(abcs[db]["database"])
  42  	    when "postgresql"
  43  	      ENV['PGHOST']     = abcs[db]["host"] if abcs[db]["host"]
  44  	      ENV['PGPORT']     = abcs[db]["port"].to_s if abcs[db]["port"]
  45  	      ENV['PGPASSWORD'] = abcs[db]["password"].to_s if abcs[db]["password"]
  46  	      enc_option = "-E #{abcs[db]["encoding"]}" if abcs[db]["encoding"]
  47  	      `dropdb -U "#{abcs[db]["username"]}" #{abcs[db]["database"]}`
  48  	      `createdb #{enc_option} -U "#{abcs[db]["username"]}" #{abcs[db]["database"]}`
  49  	    when "sqlite","sqlite3"
  50  	      dbfile = abcs[db]["database"] || abcs[db]["dbfile"]
  51  	      File.delete(dbfile) if File.exist?(dbfile)
  52  	    when "sqlserver"
  53  	      dropfkscript = "#{abcs[db]["host"]}.#{abcs[db]["database"]}.DP1".gsub(/\\/,'-')
  54  	      `osql -E -S #{abcs[db]["host"]} -d #{abcs[db]["database"]} -i db\\#{dropfkscript}`
  55  	      `osql -E -S #{abcs[db]["host"]} -d #{abcs[db]["database"]} -i db\\#{RAILS_ENV}_structure.sql`
  56  	    when "oci"
  57  	      ActiveRecord::Base.establish_connection(db.to_sym)
  58  	      ActiveRecord::Base.connection.structure_drop.split(";\n\n").each do |ddl|
  59  	        ActiveRecord::Base.connection.execute(ddl)
  60  	      end
  61  	    else
  62  	      raise "Task not supported by '#{abcs[db]["adapter"]}'"
  63  	  end
  64  	end
  65  end
  66  
  67  desc "Start Application"
  68  task :start do
  69    system "ruby script/server"
  70  end
« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS