Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added rails_routes to rails helper function r() script.

  • Loading branch information...
commit af7d8a2ec54a2a0ad8b922124737492d6f4ce1ac 1 parent 6e60c70
@wayneeseguin wayneeseguin authored
Showing with 158 additions and 44 deletions.
  1. +0 −44 contrib/r
  2. +158 −0 contrib/rails
View
44 contrib/r
@@ -1,44 +0,0 @@
-#!/usr/bin/env bash
-
-#
-# r - Rails shortcut command.
-#
-r() {
- local action args
-
- action="$1" ; shift
-
- args="$@"
-
- case "$action" in
-
- c) action=console ;;
-
- db) action=dbconsole ;;
-
- g) action=generate ;;
-
- h|help) action="-h" ;;
-
- esac
-
- if [[ -s config.ru ]] ; then
-
- ruby ./script/rails $args
-
- else
-
- if [[ -s ./script/$action ]] ; then
-
- ruby ./script/$action $args
-
- else
-
- printf "ERROR: Missing script ./script/$action !!!"
-
- fi
-
- fi
-
- return 0
-}
View
158 contrib/rails
@@ -0,0 +1,158 @@
+#!/usr/bin/env bash
+
+# Copyright (c) 2011 Wayne E. Seguin <wayneeseguin@gmail.com>
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+#
+# To use the functions provided below source this script in your start up
+# profiles. For example, if RVM is installed to $HOME/.rvm:
+#
+# [[ -s "$HOME/.rvm/contrib/rails" ]] && . "$HOME/.rvm/contrib/rails"
+#
+rails_routes()
+{
+ # Usage:
+ #
+ # rails_routes [shell glob or search string]
+ #
+ # Examples:
+ #
+ # rails_routes admin
+ # /admin/reports/agent/:id(.:format) {:controller=>"reports", :action=>"agent"}
+ #
+ # rails_routes "P@(OS|U)T"
+ # POST /current(.:format) {:action=>"create", :controller=>"current"}
+ # PUT /current/:id(.:format) {:action=>"update", :controller=>"current"}
+ #
+ if [[ "$1" = "--trace" ]]; then
+ set -o xtrace
+ export PS4='+[${BASH_SOURCE}] : ${LINENO} : ${FUNCNAME[0]:+${FUNCNAME[0]}() $ }'
+ shift
+ fi
+
+ if [[ -n "${ZSH_VERSION:-}" ]] ; then
+ setopt extendedglob
+ else
+ if [[ -n "${BASH_VERSION:-}" ]] ; then
+ shopt -s extglob # Extended globs
+ else
+ printf "%s\n" "What the heck kind of shell are you running here???"
+ fi
+ fi
+
+ local md5_current md5_cached cache_file routes search_string
+
+ search_string="${1:-}" ; shift
+ search_string="*${search_string//\|/*|*}*"
+
+ cache_file="tmp/.rake_routes"
+
+ if ! command -v rake >/dev/null 2>&1 ; then
+ printf "ERROR: rake not found.\n" >&2
+ return 1
+ fi
+
+ case $(uname) in
+ Linux) md5="md5sum" ;;
+ Darwin) md5="/sbin/md5 -q" ;;
+ *) printf "ERROR: Unknown OS Type.\n" >&2 ; return 1 ;;
+ esac
+
+ md5_current=$($md5 config/routes.rb)
+ md5_current=${md5%% *}
+
+ if [[ -s "$cache_file" && -s "$cache_file.md5" ]]; then
+ md5_cached=$(cat -v "${cache_file}.md5")
+ md5_cached=${md5%% *}
+ else
+ md5_cached=""
+ fi
+
+ if [[ -s "$cache_file" && $md5_current = $md5_cached ]]; then
+ routes=$(cat -v "$cache_file")
+ else
+ routes=$(rake routes 2>/dev/null)
+ if [[ $? -gt 0 ]]; then
+ printf "ERROR: There was an error running 'rake routes', does your application load console properly?" >&2
+ return 1
+ else
+ printf "$md5_current" > "${cache_file}.md5"
+ printf "$routes" > "$cache_file"
+ fi
+ fi
+ routes="${routes#\|\(in *\)}"
+
+ (IFS="
+"
+ for route in ${routes} ; do
+ eval "case '$route' in (${search_string}) printf \"\$route\\n\" ;; esac"
+ done
+ )
+}
+
+#
+# r - Rails scripts helper function.
+#
+r() {
+ local action args
+
+ action="$1" ; shift
+
+ args="$@"
+
+ case "$action" in
+
+ c)
+ action=console
+ ;;
+
+ db)
+ action=dbconsole
+ ;;
+
+ g)
+ action=generate
+ ;;
+
+ routes|r)
+ rails_routes $*
+ return $?
+ ;;
+
+ h|help)
+ action="-h"
+ ;;
+
+ esac
+
+ if [[ -s ./script/rails ]] ; then
+
+ ruby ./script/rails $args
+
+ else
+
+ if [[ -s ./script/$action ]] ; then
+
+ ruby ./script/$action $args
+
+ else
+
+ printf "ERROR: Missing script ./script/$action !!!"
+
+ fi
+
+ fi
+
+ return 0
+}
Please sign in to comment.
Something went wrong with that request. Please try again.