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

Matt Scilipoti

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

Rails: 301 Permanent Redirect

First there was:
 headers["Status"] = "301 Moved Permanently"  
 redirect_to "http://www.newdomain.com"  

Then there was:
head :moved_permanently, :location => book_url(@book)


As of [7820] on 2007/10/09, there's a new redirector in town.
  # Examples: 
  #   redirect_to post_url(@post), :status=>:found 
  #   redirect_to :action=>'atom', :status=>:moved_permanently 
  #   redirect_to post_url(@post), :status=>301 
  #   redirect_to :action=>'atom', :status=>302

sweet.

see: http://dev.rubyonrails.org/changeset/7820

Code Rails, Drink Beer, Be Happy

This was an interesting use of CSS.
Snipped from: http://rubyonrails.com.au/

  <div id="beer">
    Code Rails,<br/>
    Drink Beer,<br/>
    Be Happy.<br/>
  </div>


And the CSS...
#beer {main.css (line 124)
background:transparent url(../images/beer.png) no-repeat scroll left top;
color:#666666;
font-family:"Times New Roman",serif;
font-size:175%;
margin-top:36px;
padding:6px 0pt 12px 60px;
}

Simple mock: override_method

Note: ALL code samples in this post were hijacked from: http://www.karmiccoding.com/articles/2006/03/11/under-the-hood-with-ruby-partial-mock-objects-for-unit-testing
# Overrides the method +method_name+ in +obj+ with the passed block
def override_method(obj, method_name, &block)
  # Get the singleton class/eigenclass for 'obj'
  klass = class <<obj; self; end 

  # Undefine the old method (using 'send' since 'undef_method' is protected)
  klass.send(:undef_method, method_name)

  # Create the new method
  klass.send(:define_method, method_name, block)
end

# Just an example class
class Foo 
  def do_stuff
    "I'm okay!" 
  end
end

# Test code
list = []
5.times { list.push(Foo.new) }

# We override the method here!
override_method(list.first, :do_stuff) { "I'm NOT okay!" }

list.each_with_index { |f, i| puts "(#{i}) #{f.do_stuff}" }


Outputs:
(0) I'm NOT okay!
(1) I'm okay!
(2) I'm okay!
(3) I'm okay!
(4) I'm okay!

Howto: setup the rails environment in a .rb script

Stolen from: http://www.railsonwave.com/railsonwave/2007/2/14/howto-setup-the-rails-environment-in-a-rb-script
I quote:

It can happen that you need to perform some operations on your rails application from a script, for example you may need to have a cron job that do something on your models. To archieve this goal you need to set up a rails environment in your script; it’s really easy, you’ve just to put these lines on the top of your .rb file.
RAILS_ROOT = RELATIVEPATHTOYOURAPPFOLDER
require RAILS_ROOT + "/config/environment" 
Dependencies.load_file("application.rb")

I really don’t know why but you have to manually load application.rb, if you don’t rails translate the ‘ApplicationController’ constant into ‘application_controller’ and look for a file named ‘application_controller.rb’ that doesn’t exist.

Sandro.

From the comments:
edbond says:
for script in your RAILS_ROOT folder
ENV['RAILS_ENV'] = 'development' require File.expand_path(File.dirname(__FILE__) + "/config/environment")


// insert code here..

SSL Cert on the cheap.

Stolen from: http://groups.google.com/group/rubyonrails-talk/browse_frm/thread/148ada4b0d33cfac?hl=en

If cost is an issue, you may wish to investigate Reverse Proxying with a
single external certificate and multiple internal certificates (either
self-signed or issued from an internal CA).

Reflector as VS External tool

Tuesday, February 20, 2007 12:51:57 PM (Pacific Standard Time, UTC-08:00)
I'll add one additional must do for reflector - add it to the tools menu in VS. Add it as a tools - External tools. Here's what I put for Arguments:

/fontsize:14 /fontfamily:Arial $(TargetPath)


Automatically pulls up the current assembly you are working on.
Jon Flanders

Windows Start|Run magic

Stolen from: http://www.hanselman.com/blog/Reflector5ReleasedWorldDominationAssured.aspx

Tuesday, February 20, 2007 1:18:12 PM (Pacific Standard Time, UTC-08:00)
The PATH might make sense if you have a single folder with a bunch of utilities. Otherwise, you can safely leave your DOS tricks behind and just add an entry for reflector.exe to:
HKLM\Software\Microsoft\Windows\CurrentVersion\App Paths

Set the (Default) string value to the full path to the executable. Start|Run glory, the Windows way.
Joshua Flanagan

Windows Path

Stolen From: http://www.hanselman.com/blog/Reflector5ReleasedWorldDominationAssured.aspx
Tuesday, February 20, 2007 4:54:50 PM (Pacific Standard Time, UTC-08:00)

You mention that reflector should be in your path. That gives me an opportunity to exploit a (ruby) gem I created a few weeks ago which makes managing your path a breeze. Instead of going to My Computer | Properties | Blah blah and adding the path entry using that terrible dialog, just follow these steps:
gem install patheditor
path_editor --add c:\path\to\reflectore

Adding items to the Windows Path is so painful, I created this utility to make it a little easier for everyone.

It uses a WSH object to update your USER path setting, and the change will be permanent - current and future command prompts will see the new path. Unfortunately, slickrun does not respect path updates so you'll have to kill it and restart if you launch prompts from there ...
Justin

Also:
Thursday, February 22, 2007 5:25:22 PM (Pacific Standard Time, UTC-08:00)
@Jon

Updating path that way (Set PATH=) only affects your current command prompt. My script uses WSH objects to propagate the change to all running processes (that respect the update), and future prompts will include the new path.

The script will also not add duplicate entries, and it can even clean duplicate/non-existent directories out of your current path.
Justin

Safely join file parts

Simple way to safely join file parts.

File.join("C:", "osql", "foo.rb")

Recover Master Boot Record

From http://www.trajano.net/2006/07/freedos-to-rescue.html:

http://www.freedos.org/freedos/files/
If you ever bust up your dual boot installation because you wanted to remove Linux on your laptop with no floppy drive. Like me a few minutes ago. Go grab the FreeDOS ISO image and burn it to your CD-RW media and boot from it.
Press Enter
Type 2 (for safe mode)
cd FREEDOS
FDISK /MBR
« Newer Snippets
Older Snippets »
Showing 1-10 of 46 total  RSS