<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: Heavysixer's Code Snippets</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Thu, 07 Aug 2008 05:48:02 GMT</pubDate>
    <description>DZone Snippets: Heavysixer's Code Snippets</description>
    <item>
      <title>Ruby Speaks</title>
      <link>http://snippets.dzone.com/posts/show/3350</link>
      <description>// if you are using a mac this will allow your Ruby script to speak using the macs native text to speech functionality.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;system "osascript -e 'say \"the rain in Spain falls mainly on the planes\"'"&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 23 Jan 2007 22:49:56 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3350</guid>
      <author>heavysixer ()</author>
    </item>
    <item>
      <title>Colorize Function</title>
      <link>http://snippets.dzone.com/posts/show/2461</link>
      <description>/*&lt;br /&gt;I don't use the flash colorize function much so this is an example of how to colorize a movieclip with a supplied hexnumber&lt;br /&gt;&lt;br /&gt;colorize(0xff0000,my_movieClip);&lt;br /&gt;&lt;br /&gt;//will turn my_movieClip bright red.&lt;br /&gt;*/&lt;br /&gt;&lt;code&gt;&lt;br /&gt;function colorize(c:Number,mc:MovieClip){&lt;br /&gt;  var tf:Transform = new Transform(mc);&lt;br /&gt;  var color_tf:ColorTransform = new ColorTransform();&lt;br /&gt;  color_tf.rgb = c;&lt;br /&gt;  tf.colorTransform = color_tf;&lt;br /&gt;};&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 23 Aug 2006 08:42:03 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2461</guid>
      <author>heavysixer ()</author>
    </item>
    <item>
      <title>Application version number and cool codename based on subversion number</title>
      <link>http://snippets.dzone.com/posts/show/2370</link>
      <description>// description of your code here&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#codename generated from the dictionary&lt;br /&gt;REVISION_NUMBER = `svn info`.split("\n")[4][/\d+/].to_i&lt;br /&gt;APP_CODENAME  = IO.readlines("/usr/share/dict/words")[REVISION_NUMBER]&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 05 Aug 2006 10:10:34 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2370</guid>
      <author>heavysixer ()</author>
    </item>
    <item>
      <title>Add Subversion Revsions Number to Rails Application.</title>
      <link>http://snippets.dzone.com/posts/show/2317</link>
      <description>// description of your code here&lt;br /&gt;Add this to your environment.rb file for a dead easy way to get your subversion revision into your application.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;APP_VERSION  = IO.popen("svn info").readlines[4]&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 20 Jul 2006 04:30:56 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2317</guid>
      <author>heavysixer ()</author>
    </item>
    <item>
      <title>how to escape from AJAX URL</title>
      <link>http://snippets.dzone.com/posts/show/1705</link>
      <description>From Dylan Stamat's post on the Ruby on Rails list 	&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Within my "login.rjs" template that get's invoked after my login process, it returns to the same page with an error message on error, or... if the login was successful, it needs to "redirect" to another controller... which is pretty much impossible to do otherwise.  so, my " login.rjs" looks like:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;if @logged_in_client&lt;br /&gt;  page.replace_html "message", :partial =&gt; 'shared/bad_login'&lt;br /&gt;else&lt;br /&gt;  page.redirect_to "whatever you want here"&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;------ End Message--------&lt;br /&gt;Here is my two cents:&lt;br /&gt;Technically, you can do this without a RJS template at all, because it is such a simple example. Use this in your controller&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;render :update do |page|&lt;br /&gt;  if @logged_in_client&lt;br /&gt;    page.replace_html "message", :partial =&gt; 'shared/bad_login'&lt;br /&gt;  else&lt;br /&gt;    page.redirect_to "whatever you want here"&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 16 Mar 2006 08:42:06 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1705</guid>
      <author>heavysixer ()</author>
    </item>
    <item>
      <title>Submitting data to two different tables with two different models  (or how to modify multiple models from one form)</title>
      <link>http://snippets.dzone.com/posts/show/1674</link>
      <description>Taken from Norman Timmler's example on the ruby on rails mailing list.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Take two text fields for example:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# view&lt;br /&gt;&lt;%= form_tag :controller =&gt; :posts, :action =&gt; create %&gt;&lt;br /&gt; &lt;%= text_field("post", "title", "size" =&gt; 20) %&gt;&lt;br /&gt; &lt;%= text_field("user", "email", "size" =&gt; 20) %&gt;&lt;br /&gt;&lt;%= end_form_tag %&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# posts_controller&lt;br /&gt;class PostsController &lt; ApplicationController&lt;br /&gt; def create&lt;br /&gt;   @post = Post.create(params[:post])&lt;br /&gt;   @user = User.create(params[:user])&lt;br /&gt;   @post.users &lt;&lt; @user&lt;br /&gt; end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;In the controller the params hash has one key for every object (user,&lt;br /&gt;post) in your form. As a values of this key you find a hash having a&lt;br /&gt;key-value pair for every object attribute (title, email).&lt;br /&gt;&lt;br /&gt;This way you can submit multiple objects via one form.</description>
      <pubDate>Thu, 09 Mar 2006 23:20:41 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1674</guid>
      <author>heavysixer ()</author>
    </item>
    <item>
      <title>Create a new instance of Magick::Image out of uploaded form data</title>
      <link>http://snippets.dzone.com/posts/show/1673</link>
      <description>Taken from Norman Timmler's example on the Ruby on Rails mailing list:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def file=(new_file)&lt;br /&gt; new_file.rewind&lt;br /&gt; image = Magick::Image::from_blob(new_file.read).first&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 09 Mar 2006 22:41:14 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1673</guid>
      <author>heavysixer ()</author>
    </item>
    <item>
      <title>rake start_fresh</title>
      <link>http://snippets.dzone.com/posts/show/1666</link>
      <description>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&lt;br /&gt;&lt;br /&gt;clear the logs&lt;br /&gt;recreate the test and development databases&lt;br /&gt;migrate to the most recent schema&lt;br /&gt;load all your test fixtures into your development database&lt;br /&gt;clone your development structure to the test database&lt;br /&gt;&lt;br /&gt;to use it call "rake start_fresh"&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;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"&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;task :start_fresh =&gt; :environment do&lt;br /&gt;     Rake::Task[:clear_logs].invoke&lt;br /&gt;     puts 'clearing logs                           ...done'&lt;br /&gt;     &lt;br /&gt;     Rake::Task[:recreate_database].invoke&lt;br /&gt;     puts 'recreate test database                  ...done'&lt;br /&gt;     puts 'recreate development database           ...done'&lt;br /&gt;     &lt;br /&gt;     ActiveRecord::Base.establish_connection(:development)&lt;br /&gt;     Rake::Task[:migrate].invoke&lt;br /&gt;     puts 'migrate the development database        ...done'&lt;br /&gt;     &lt;br /&gt;     Rake::Task[:load_fixtures_to_development].invoke&lt;br /&gt;     puts 'load fixtures into development database ...done'&lt;br /&gt;     &lt;br /&gt;     ActiveRecord::Base.establish_connection(:test)&lt;br /&gt;     Rake::Task[:prepare_test_database].invoke&lt;br /&gt;     puts 'clone the development structure to test ...done'&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;desc "Load fixtures data into the test database"&lt;br /&gt;task :load_fixtures_to_development =&gt; :environment do&lt;br /&gt;  require 'active_record/fixtures'&lt;br /&gt;  ActiveRecord::Base.establish_connection(:development)&lt;br /&gt;  Dir.glob(File.join(RAILS_ROOT, 'test', 'fixtures', '*.{yml,csv}')).each do |fixture_file|&lt;br /&gt;    Fixtures.create_fixtures('test/fixtures', File.basename(fixture_file, '.*'))&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;task :recreate_database =&gt; :environment do&lt;br /&gt;  abcs = ActiveRecord::Base.configurations&lt;br /&gt;	tdb = Array.new&lt;br /&gt;  tdb &lt;&lt; 'test'&lt;br /&gt;  tdb &lt;&lt; 'development'&lt;br /&gt;&lt;br /&gt; 	for db in tdb&lt;br /&gt;	  case abcs[db]["adapter"]&lt;br /&gt;	    when "mysql"&lt;br /&gt;	      ActiveRecord::Base.establish_connection(db.to_sym)&lt;br /&gt;	      ActiveRecord::Base.connection.recreate_database(abcs[db]["database"])&lt;br /&gt;	    when "postgresql"&lt;br /&gt;	      ENV['PGHOST']     = abcs[db]["host"] if abcs[db]["host"]&lt;br /&gt;	      ENV['PGPORT']     = abcs[db]["port"].to_s if abcs[db]["port"]&lt;br /&gt;	      ENV['PGPASSWORD'] = abcs[db]["password"].to_s if abcs[db]["password"]&lt;br /&gt;	      enc_option = "-E #{abcs[db]["encoding"]}" if abcs[db]["encoding"]&lt;br /&gt;	      `dropdb -U "#{abcs[db]["username"]}" #{abcs[db]["database"]}`&lt;br /&gt;	      `createdb #{enc_option} -U "#{abcs[db]["username"]}" #{abcs[db]["database"]}`&lt;br /&gt;	    when "sqlite","sqlite3"&lt;br /&gt;	      dbfile = abcs[db]["database"] || abcs[db]["dbfile"]&lt;br /&gt;	      File.delete(dbfile) if File.exist?(dbfile)&lt;br /&gt;	    when "sqlserver"&lt;br /&gt;	      dropfkscript = "#{abcs[db]["host"]}.#{abcs[db]["database"]}.DP1".gsub(/\\/,'-')&lt;br /&gt;	      `osql -E -S #{abcs[db]["host"]} -d #{abcs[db]["database"]} -i db\\#{dropfkscript}`&lt;br /&gt;	      `osql -E -S #{abcs[db]["host"]} -d #{abcs[db]["database"]} -i db\\#{RAILS_ENV}_structure.sql`&lt;br /&gt;	    when "oci"&lt;br /&gt;	      ActiveRecord::Base.establish_connection(db.to_sym)&lt;br /&gt;	      ActiveRecord::Base.connection.structure_drop.split(";\n\n").each do |ddl|&lt;br /&gt;	        ActiveRecord::Base.connection.execute(ddl)&lt;br /&gt;	      end&lt;br /&gt;	    else&lt;br /&gt;	      raise "Task not supported by '#{abcs[db]["adapter"]}'"&lt;br /&gt;	  end&lt;br /&gt;	end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;desc "Start Application"&lt;br /&gt;task :start do&lt;br /&gt;  system "ruby script/server"&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 09 Mar 2006 11:47:26 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1666</guid>
      <author>heavysixer ()</author>
    </item>
    <item>
      <title>Unit Test Helper for Ruby on Rails</title>
      <link>http://snippets.dzone.com/posts/show/1665</link>
      <description>This is a helpful file to include along with the standard test_helper.rb file to allow you to test the most basic functionality typically employed by the basic ActiveRecord Model.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;ENV["RAILS_ENV"] = "test"&lt;br /&gt;require File.expand_path(File.dirname(__FILE__) + "/../config/environment")&lt;br /&gt;require 'test_help'&lt;br /&gt;class Test::Unit::TestCase&lt;br /&gt;&lt;br /&gt;  def unit_create(model,options)&lt;br /&gt;    pre_count = model.count&lt;br /&gt;    @o = model.new(options)&lt;br /&gt;    assert @o.save&lt;br /&gt;    assert_equal pre_count + 1, model.count&lt;br /&gt;    assert_kind_of model, @o&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  def unit_destroy(model,id)&lt;br /&gt;    pre_count = model.count&lt;br /&gt;    @o = model.find(id)&lt;br /&gt;    assert @o.destroy&lt;br /&gt;	assert_equal pre_count - 1, model.count&lt;br /&gt;	&lt;br /&gt;    assert_raise(ActiveRecord::RecordNotFound) { model.find(id) }&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  def unit_update(model,id,value_hash)&lt;br /&gt;    assert @o = model.find(id), "cannot test update because record was not found"&lt;br /&gt;    value_hash.each do |key,value|&lt;br /&gt;      @o[key] = value&lt;br /&gt;    end&lt;br /&gt;    assert @o.save, "could not update object, failed to save"&lt;br /&gt;    @o.reload&lt;br /&gt;    value_hash.each do |key,value|&lt;br /&gt;      assert_equal @o[key], value, "after update the database does not contain the new values."&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  def unit_protect_update_against_duplicate_records(original_record,record_to_update,*attributes)&lt;br /&gt;    attributes.each do |attribute|&lt;br /&gt;      record_to_update[attribute] = original_record [attribute]&lt;br /&gt;    end&lt;br /&gt;    assert !record_to_update.save, "the application updated the group even though it contained duplicate information on unique fields"&lt;br /&gt;    attributes.each do |attribute|&lt;br /&gt;      assert record_to_update.errors.on( attribute), "the application should have contained an error on field '#{attribute}'"&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;----------------------&lt;br /&gt;Usage&lt;br /&gt;&lt;br /&gt;require File.dirname(__FILE__) + '/../test_helper'&lt;br /&gt;require File.dirname(__FILE__) + '/../test_unit_helper'&lt;br /&gt;class AssetTest &lt; Test::Unit::TestCase&lt;br /&gt;  fixtures :assets,  :projects&lt;br /&gt;&lt;br /&gt;  test_required_attributes Asset,'cannot be blank', :name&lt;br /&gt;  &lt;br /&gt;  def setup&lt;br /&gt;    @model = Asset&lt;br /&gt;    @asset_one = assets(:asset_2)&lt;br /&gt;	@asset_two = assets(:asset_3)&lt;br /&gt;    &lt;br /&gt;	@new_obj = {&lt;br /&gt;		:status =&gt; 1,&lt;br /&gt;  		:iteration_id =&gt; 1,&lt;br /&gt;  		:name =&gt; 'Stella',&lt;br /&gt;		:description =&gt; 'shot of the amazing 6502'&lt;br /&gt;      }&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  def test_create_asset&lt;br /&gt;    unit_create @model,@new_obj &lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  def test_destroy_asset&lt;br /&gt;    unit_destroy @model, @model.find(:first).id&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  def test_update_asset&lt;br /&gt;    @id = @model.find(:first).id&lt;br /&gt;    @new_values = {&lt;br /&gt;     	:status=&gt; 1,&lt;br /&gt; 		:name=&gt; 'orb.jpg'&lt;br /&gt;    }&lt;br /&gt;    unit_update @model, @id, @new_values&lt;br /&gt;  end&lt;br /&gt; &lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 09 Mar 2006 11:37:53 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1665</guid>
      <author>heavysixer ()</author>
    </item>
    <item>
      <title>functional test helper to automate basic CRUD functions</title>
      <link>http://snippets.dzone.com/posts/show/1664</link>
      <description>This is a funcitonal test helper, which can be used to create methods on the fly to iterate through your controller methods and test for validations, url parameters and expected responses. It keeps the tests from getting as cluttered. &lt;br /&gt;&lt;code&gt;&lt;br /&gt;#expects a format like this:&lt;br /&gt;	#test_required_attributes_in_controller User,&lt;br /&gt;  	#					{:params=&gt;{:id=&gt;1,:user=&gt;{}},:session=&gt;{:user=&gt;{}}},&lt;br /&gt;	#					[:create, :update],&lt;br /&gt;	#					:first_name, :last_name, :username, :email_address, :password&lt;br /&gt;&lt;br /&gt;    def test_required_attributes_in_controller( model, options, actions,*required_fields )&lt;br /&gt;    	&lt;br /&gt;		sym = model.to_s.downcase.to_sym&lt;br /&gt;		for field in required_fields&lt;br /&gt;      		options[:params][sym][field] = ''&lt;br /&gt;    	end&lt;br /&gt;		&lt;br /&gt;      	actions.each do |action|&lt;br /&gt;        	self.class_eval do&lt;br /&gt;          		define_method("test_#{action}_method_requires_fields") do&lt;br /&gt;            		assert_nothing_raised do&lt;br /&gt;						post action, options[:params],options[:session]&lt;br /&gt;					end&lt;br /&gt;            		for field in required_fields&lt;br /&gt;      					assert !assigns(sym).errors[field].empty? unless options[:params][sym][field] == ''&lt;br /&gt;    				end&lt;br /&gt;    				assert !assigns.empty?,"This method requires specific parameters, yet no errors were created with them missing"&lt;br /&gt;          		end&lt;br /&gt;        	end&lt;br /&gt;      	end&lt;br /&gt;	end&lt;br /&gt;	&lt;br /&gt;	  #Expects format like this: &lt;br /&gt;	  #   test_required_unique_attributes_in_controller User,&lt;br /&gt;	  #							{:params=&gt;{:id=&gt;1,:user=&gt;{}},:session=&gt;{:user=&gt;{}}},&lt;br /&gt;	  #							[:create, :update],&lt;br /&gt;	  #							:username, :email_address&lt;br /&gt;	  #&lt;br /&gt;		&lt;br /&gt;    def test_required_unique_attributes_in_controller( model, options, actions,*required_fields )&lt;br /&gt;    	sym = model.to_s.downcase.to_sym&lt;br /&gt;		for field in required_fields&lt;br /&gt;      		options[:params][sym][field] = ''&lt;br /&gt;    	end&lt;br /&gt;		&lt;br /&gt;      	actions.each do |action|&lt;br /&gt;        	self.class_eval do&lt;br /&gt;          		define_method("test_#{action}_method_requires_unique_fields") do&lt;br /&gt;            		assert_nothing_raised do&lt;br /&gt;						post action, options[:params],options[:session]&lt;br /&gt;					end&lt;br /&gt;            		for field in required_fields&lt;br /&gt;      					assert !assigns(sym).errors[field].empty? unless options[:params][sym][field] == ''&lt;br /&gt;    				end&lt;br /&gt;    				assert !assigns.empty?,"This method fails without unique parameters, yet no errors were created"&lt;br /&gt;          		end&lt;br /&gt;        	end&lt;br /&gt;      	end&lt;br /&gt;    end&lt;br /&gt;	&lt;br /&gt;	#Expects format like this:&lt;br /&gt;	# test_required_url_parameters_in_controller &lt;br /&gt;	#                                           User,&lt;br /&gt;	#                                           {:params=&gt;{},:session=&gt;{:user=&gt;{}}},&lt;br /&gt;	#                                           :edit, :destroy, :show, :update	&lt;br /&gt;	&lt;br /&gt;    def test_required_url_parameters_in_controller(model, options, *actions)&lt;br /&gt;      sym = model.to_s.downcase.to_sym&lt;br /&gt;      actions.each do |action|&lt;br /&gt;        self.class_eval do&lt;br /&gt;          define_method("test_#{action}_method_requires_url_parameters") do&lt;br /&gt;            post action, options[:params],options[:session]&lt;br /&gt;            assert_response :redirect&lt;br /&gt;            assert_nil assigns(sym),"This method requires a url parameter, yet the controller still created an object."&lt;br /&gt;          end&lt;br /&gt;        end&lt;br /&gt;      end&lt;br /&gt;    end&lt;br /&gt;	&lt;br /&gt;  end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 09 Mar 2006 11:30:31 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1664</guid>
      <author>heavysixer ()</author>
    </item>
  </channel>
</rss>
