Never been to CodeSnippets 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 (or not, you can keep them private!)

Failed generation of resource route for action

// description of your code here

These methods test that the routes for resources defined in routes.rb are working as expected. Call them from your functional (controller) tests.

Add the following 3 methods to test/test_helper.rb (updated for Rails 1.2.5 which no longer uses semicolons as a separator for the edit action):

# Test for routes generated by map.resource (singular).
def assert_routing_for_resource(controller, skip=[], nesting=[])
  routes = [
    ["new",'/new',{},:get], ["create",'',{},:post],
    ["show",'',{},:get], ["edit",'/edit',{},:get],
    ["update",'',{},:put], ["destroy",'',{},:delete]
    ]
  check_resource_routing(controller, routes, skip, nesting)
end
# Test for routes generated by map.resources (plural).
def assert_routing_for_resources(controller, skip=[], nesting=[])
  routes = [
    ["index",'',{},:get], ["new",'/new',{},:get], ["create",'',{},:post],
    ["show",'/1',{:id=>'1'},:get], ["edit",'/1/edit',{:id=>'1'},:get],
    ["update",'/1',{:id=>'1'},:put], ["destroy",'/1',{:id=>'1'},:delete]
    ]
  check_resource_routing(controller, routes, skip, nesting)
end

# Check that the expected paths will be generated by a resource, and that
# the expected params will be generated by paths defined by a resource.
# routes is array of [action, url string after controller, extra params].
def check_resource_routing(controller, routes, skip=[], nesting=[])
  # set a prefix for nested resources
  prefix = nesting.join('s/1/')
  unless prefix.blank?
    prefix += "s/1/"
  end
  # Add params for nested resources.
  # For each 'nest', include a ":nest_id=>'1'" param.
  params = {}
  nesting.each do |param|
    params["#{param}_id".to_sym] = '1'
  end
  # Test each of the standard resource routes.
  routes.each do |pair|
    unless skip.include? pair[0]
      assert_generates("/#{prefix}#{controller}#{pair[1]}",
        {:controller=>controller,
        :action=>pair[0]}.merge(pair[2]).merge(params), {}, {},
        "Failed generation of resource route for action #{pair[0]} /#{prefix}#{controller}#{pair[1]}")
      assert_recognizes(
        {:controller=>controller,
          :action=>pair[0]}.merge(pair[2]).merge(params),
        {:path=>"/#{prefix}#{controller}#{pair[1]}", :method=>pair[3]},
        {}, "Failed to recognize resource route for path #{pair[3]}:/#{prefix}#{controller}#{pair[1]}")
    end
  end
end





Altın Fiyatları
Revizyon ile Organize Matbaacılık Brnckvvtmllttrhaberi





function to increase numbers in vim

// from: http://rayninfo.co.uk/vimtips.html

<code format="Viml">
1 " -----------------8<-----------------------
2 " from: http://rayninfo.co.uk/vimtips.html
3 " advanced incrementing (really useful)
4 " put following in _vimrc
5 let g:I=0
6 function! INC(increment)
7 let g:I =g:I + a:increment
8 return g:I
9 endfunction
10
11 " usage
12 " :%g/pattern/s/$/\=INC(1)/g
13 " -----------------8<-----------------------

vim reference to register

How use registers in subst in vim?
let @a="new text"

:%s/old text/\=@a/g

.vimrc mappings

My .vimrc key mappings

" comment lines out with #
map cl <ESC>:'<,'>s/^/#/g<CR>
" uncomment lines commented with #
map cu <ESC>:'<,'>s/^#//g<CR>
" toggle line numbers
map <F12> :set nu!<CR>
" open new tab
map <F9> :tabnew<CR>
" tab navigation
map <S-h> gT
map <S-l> gt
" close all tabs without saving
map <S-q> :tabdo q<CR>
" close all tabs with saving
map <S-w> :tabdo wq<CR>

vim backreference on search and replace

// description of your code here

Use \(\) to denote an atom.
Use \1 to specify the first atom.

<this is your search and replacement terms>

:%s'\(<stuff_to_find>\)'\1<new_stuff>'gc

vim search and replace escape forward slash

Don't escape it, just use a different delimiter, such as single quote

:%s'replacethis'withthis'gc

vi find/replaces for adding a table prefix to a SQL dump

Was importing a DB but wanted to prefix the tables, but the file was too large for TextMate. Here's all the vim substitutions

%s/DROP TABLE IF EXISTS `/DROP TABLE IF EXISTS `prefix_/g
%s/CREATE TABLE `/CREATE TABLE `prefix_/g
%s/INSERT INTO `/INSERT INTO `prefix_/g
%s/LOCK TABLES `/LOCK TABLES `prefix_/g
%s/ALTER TABLE `/ALTER TABLE `prefix_/g

Open existing vim in iTerm

// open existing vim in iTerm
//
// experimenting with something like this
// as a modification to RunVim
//
// http://www.fastnlight.com/runvim/

tell application "iTerm"
	activate
	set _term_list to every terminal
	repeat with _term in _term_list
		tell _term
			try
				select session "Vim"
				select _term
				set _vim_tty to tty of session "Vim"
				try --check to see if vim is even running
					do shell script "ps -t " & _vim_tty & " | grep vim"
					
					try --check to see if vim is foreground
						do shell script "ps -t " & _vim_tty & " -o command,state | grep vim.*+"
					on error --vim is suspended or something
						--check for vim as job %+
						tell i term application "System Events"
							key code 6 using control down
							delay 0.1
							key code 14 using control down
							key code 32 using control down
						end tell
						tell session "Vim"
							write text "jobs | grep ]+.*vim"
						end tell
						delay 0.1
						set fulltext to text of session "Vim"
						set fulltext to quoted form of fulltext
						set lastline to do shell script "echo " & fulltext & " | tail -n 2 | head -n 1"
						set lastline to quoted form of lastline
						try --check %+
							do shell script "echo " & lastline & " | grep -v jobs"
							tell session "Vim"
								write text "fg %+"
							end tell
						on error --vim is not %+ job
							
							--check to see if vim is %- job
							tell session "Vim"
								write text "jobs | grep ]-.*vim"
							end tell
							delay 0.1
							set fulltext to text of session "Vim"
							set fulltext to quoted form of fulltext
							set lastline to do shell script "echo " & fulltext & " | tail -n 2 | head -n 1"
							set lastline to quoted form of lastline
							try --check %-
								do shell script "echo " & lastline & " | grep -v jobs"
								tell session "Vim"
									write text "fg %-"
								end tell
							on error --vim is not %- job; just pick the first one in jobs list
								
								tell session "Vim"
									write text "jobs | grep -m 1 vim"
								end tell
								delay 0.1
								set fulltext to text of session "Vim"
								set fulltext to quoted form of fulltext
								set lastline to do shell script "echo " & fulltext & " | tail -n 2 | head -n 1"
								set lastline to quoted form of lastline
								set jobnumber to do shell script "echo " & lastline & " | sed 's/[^0-9]//g'"
								tell session "Vim"
									write text "fg " & jobnumber
								end tell
								
							end try --%-
						end try --%+
						
					end try --vim foreground
					-- escape in case Vim was not in command mode
					tell i term application "System Events"
						key code 53
					end tell
					tell session "Vim"
						write text ":enew"
					end tell
				on error -- vim not running
					tell i term application "System Events"
						key code 6 using control down
						delay 0.1
						key code 14 using control down
						key code 32 using control down
					end tell
					tell session "Vim"
						write text "vim; exit"
					end tell
				end try
				exit repeat
			end try
		end tell
	end repeat
	tell _term
		try
			select session "Vim"
		on error --no existing Vim session
			launch session "Default Session"
			set name of the last session to "Vim"
			tell the last session
				write text "vim; exit"
			end tell
		end try
	end tell
end tell

How to count particular words in vim

From one of the comments in http://www.vim.org/tips/tip.php?tip_id=689

:%s/word/&/g


The above substitutes "word" for itself, and tells you the number of substitutions made.

VIM Tips

Quick Tips

* To quickly get out of command-line, press ctrl+c, it's faster than hitting ESC multiple times.
* ~ changes case of current letter.
* zb, zt, and zz scrolls the screen to make the current line at the top, bottom, or middle.
*
* to auto-indent a piece of code, highlight it in visual mode, and press =, to auto-indent the current line, press ==.
* Use gq to wrap the highlighted peice of text.
* Use :set wrap and :set wrap! to toggle long line wrapping.
* To visually check the difference between two files, use vimdiff file1 file2, it provides a nice color-highlighted view with code folding, much better than plain diff.
*
* Type :help to access Vim's help, and :help cmd for information on the command cmd.
* vimtutor is Vim newcomer's friend, it provides a course on Vim usage.


// insert code here..